-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_video_clips_from_codings.py
executable file
·92 lines (78 loc) · 3.58 KB
/
make_video_clips_from_codings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# COMMAND LINE WORKFLOW (paraphrased, not actual wording):
# 0. Run the script: python make_video_clips_from_codings.py ./path/to/codings.txt ./path/to/video.m4v
# 1. "Which tier name?" User> Hug_Type
# 2. "That tier has XX occurrences in this video."
# 3. "Which code for that tier? (or hit RETURN to get all codes)" User> Guest Over
# 4. "That tier has XX occurrences for that code."
# 5. <lists the codings you've selected>
# 6. "Would you like to make video clips of these? (y/n)" User> y
# 7. "Making videos..."
import sys
import csv
import subprocess
if __name__ == "__main__":
print '====================================================='
# Check that input file was given as command line argument
if len(sys.argv) is not 3:
print 'Use: python get_coding_times.py ./path/to/your/file.txt ./path/to/your/video.{video extension}'
print 'ERROR: wrong number of command line arguments.'
print 'Script over.'
print 'Goodbye.'
print ''
sys.exit()
# Import ELAN codings
path_input_text = sys.argv[1] # get path from command-line argument
with open(path_input_text) as f:
reader = csv.reader(f, delimiter = '\t')
codings_all = list(reader)
# Find all codings of a certain tier
tier_chosen = raw_input('Which tier? Type the name as is it is ELAN: ')
print '...'
codings_by_tier = [coding for coding in codings_all
if coding[0] == tier_chosen]
print 'Found ' + str(len(codings_by_tier)) + ' codings with tier name ' + tier_chosen
# Find all codings of a certain level
level_chosen = raw_input('Which level of that tier? Type the name as is it is ELAN, or press RETURN to get all levels: ')
print '...'
if level_chosen == '': # if they just pressed RETURN...
codings_by_level = codings_by_tier # ...give them all codings in that tier.
print 'OK, here are all the codings for that tier name.'
else:
codings_by_level = [coding for coding in codings_by_tier
if coding[-1] == level_chosen]
print 'Found ' + str(len(codings_by_level)) + ' codings with level ' + level_chosen
# Print out all the codings that have been selected.
print '---- Beginning of selected codings ----'
for coding in codings_by_level:
print [coding[i] for i in [0,2,8,-1]] # this is clunky with python lists
print '---- End of selected codings ----'
# Ask if user wants to make video clips out of these.
want_to_make_clips = raw_input('Do you want to make clips out of these codings? (y/n) ')
if want_to_make_clips != 'y':
print 'OK, I won\'t make you any clips. You have your reward.'
print 'Script over.'
print 'Goodbye.'
print ''
sys.exit()
# Call avconv
path_input_video = sys.argv[2]
for clip_number, coding in enumerate(codings_by_level):
clip_number = '00' + str(clip_number); clip_number = clip_number[-3:] # makes the clip number three digits, left-padded by zeros
if level_chosen == '':
level_chosen = 'All'
path_output_clip = './' + tier_chosen + '.' + level_chosen + '_' + clip_number + '.m4v'
command = [
'avconv',
'-i',
path_input_video,
'-ss',
coding[2],
'-t',
coding[8],
'-loglevel', 'quiet',
path_output_clip
]
print 'Creating video clip at: ', path_output_clip
print ' ... please wait --- video gremlins at work ... '
subprocess.call(command)