-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautovidedit.py
205 lines (176 loc) · 6.29 KB
/
autovidedit.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python
import argparse
import commands
import csv
import logging
import moviepy.video.fx.all as vfx
import os
import sys
import time
from moviepy.editor import *
MOVIEPATH = "/Users/tbroch/Movies/personal"
# TODOS
#
# - opencv coolness
# - find duration by utilizing transitions I insert into videos as
# - some sort of significant delta
# - find player by number and insert slowmo / overlay to
# identify them. Replay, zoom-ins also cool.
# - add parse arg for raw-only (no fx) to just produce the subclips or
# perhaps a whole separate script to merge the pieces
def parse_args():
parser = argparse.ArgumentParser(description='Auto Cut Video with MoviePy')
# required
parser.add_argument('-p', '--player')
parser.add_argument('-c', '--csv',)
# deprecated : only if you're testing w/o csv
parser.add_argument('-i', '--infile',)
parser.add_argument('-s', '--start',)
parser.add_argument('-d', '--duration',)
parser.add_argument('-t', '--text',)
args = vars(parser.parse_args())
return args
def time2secs(time):
"""Converts hrs:mins:secs to secs."""
alist = list(int(x) for x in time.split(':'))
return alist[0] * 3600 + alist[1] * 60 + alist[2]
def csvdate2filedate(date):
"""Converts mm/dd/yyyy -> yyyy-mm-dd."""
try:
alist = list(int(x) for x in date.split('/'))
rv = "%04d-%02d-%02d" % (alist[2], alist[0], alist[1])
except ValueError,IndexError:
rv = "NODATE"
return rv
def find_file(division, opponent, date):
if division.lower().startswith('j'):
path = "%s/2015_gbjpw/%s_gbjpw_vs_%s.mp4" % \
(MOVIEPATH,
csvdate2filedate(date),
opponent)
else:
path = "%s/2015_gbpw/%s_gbpw_vs_%s.mp4" % \
(MOVIEPATH,
csvdate2filedate(date),
opponent)
logging.debug("path:%s", path)
if not os.path.exists(path):
return None
return path
# 0 - division
# 1 - opponent
# 2 - date
# 3 - youtube
# 4 - yards
# 5 - description
# 6 - player number
# 7 - Offense or Defense
# 8 - start time hr:min:sec
# 9 - duration
# 10 - url link1
# 11 - url link2
def parse_csv(infile, player_num):
rv_list = []
with open(infile, 'r') as fd:
rd_list = csv.reader(fd, delimiter=',')
base_len = 0
for i,row in enumerate(rd_list):
if i == 0:
# header
base_len = len(row)
continue
if len(row) != base_len:
continue
# valid row
logging.debug('%d: %s', i, ','.join(row))
if player_num != row[6]:
continue
# build description text
txt = '#' + row[6]
if row[7].lower().startswith('o'):
txt += ':offense'
elif row[7].lower().startswith('s'):
txt += ':steams'
else:
txt += ':defense'
if row[5] != '':
txt += ":%s" % row[5]
if row[8] == '' or row[2] == '':
logging.warn('Skipping %s line ... date/time missing', row)
continue
time_idx = time.mktime(
time.strptime("%s %s" % (csvdate2filedate(row[2]),
row[8]), "%Y-%m-%d %H:%M:%S"))
movie = find_file(row[0], row[1], row[2])
logging.debug("time_idx:%d movie:%s", time_idx, movie)
if movie is None:
logging.warn('Unable to find movie for division:%s vs '
'%s on %s', row[0], row[1], row[2])
break
# movie, descrip, time, duration
rv_list.append([time_idx, movie, txt, row[8], row[9]])
return rv_list
def main():
args = parse_args()
logging.basicConfig(level=logging.DEBUG)
if args['csv']:
csv_list = parse_csv(args['csv'], args['player'])
fname_root = os.path.splitext(args['csv'])[0]
else:
# expect the full movie too
if not os.path.exists(args['infile']):
logging.fatal('Need valid -i <infile> w/ -s,-d,-t params')
sys.exit(-1)
csv_list = [0, args['infile'],
args['text'],
args['start'],
args['duration']]
fname_root = "test"
file_dict = dict()
for i, (time_idx, movie, txt, start, dur) in enumerate(csv_list):
logging.debug('movie:%s start:%s dur:%s descrip:%s',
movie, start, dur, txt)
start_secs = time2secs(start)
dur_secs = int(dur)
#dur_secs = 5
text_dur_secs = 3 if (dur_secs - 3) >= 0 else dur_secs
end_secs = start_secs + dur_secs
logging.debug('start:%d end:%d', start_secs, end_secs)
video = VideoFileClip(movie).subclip(start_secs, end_secs)
video = (video.fx( vfx.freeze, freeze_duration=1 )
#.fx( vfx.freeze, t=dur_secs, freeze_duration=1)
)
video = (video.fx( vfx.fadeout, 0.75)
)
txt_clip1 = (TextClip(txt, fontsize=54, color='white', kerning=4)
.set_position('bottom')
.set_duration(text_dur_secs))
txt_clip2 = (TextClip(txt, fontsize=60, color='black')
.set_position('bottom')
.set_duration(text_dur_secs))
clips = [video, txt_clip2, txt_clip1]
fname = "%s_%d.mp4" % (fname_root, time_idx)
if not os.path.exists(fname):
result = CompositeVideoClip(clips)
result.write_videofile(fname,fps=25, audio=False)
file_dict[time_idx] = 'file %s' % fname
#break
merge = True
if merge:
flist_name = '%s_highlight.list' % fname_root
with open(flist_name, 'w') as fd:
for time_idx in sorted(file_dict):
fd.write(file_dict[time_idx] + '\n')
final_video = '%s_highlight.mp4' % fname_root
if os.path.isfile(final_video):
os.remove(final_video)
cmd = 'ffmpeg -f concat -i %s -c copy %s_highlight.mp4' % \
(flist_name, fname_root)
logging.info(cmd)
(rv, out) = commands.getstatusoutput(cmd)
logging.info(out)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit(0);