Repository navigation
Expand file tree
/
Copy pathmaker.py
More file actions
376 lines (321 loc) · 13.8 KB
/
Copy pathmaker.py
File metadata and controls
376 lines (321 loc) · 13.8 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import os
import subprocess as sp
import datetime as dt
import warnings
import subprocess as sp
import sys
#TODO major changes
#-Logging
#-Finish changing to better structure
DEFAULTOUTPUT = "output"
#Consider changing to a Dimension object?
DEFAULTRES = "800x600"
DEFAULTFADETIME = 1
DEFAULTFORMAT = ".mp4"
#The command to find the length of a video (in hh:mm:ss.ms format)
DURATIONFIND = ["ffprobe", "-v", "error", "-select_streams", "v:0", \
"-show_entries", "stream=duration", "-sexagesimal", "-of", \
"default=noprint_wrappers=1:nokey=1"]
class Maker:
"""Class to contain information about creation of video.
By creating a Maker with appropriate parameters, the properties of a
desired output video can be fully defined in advance of forming
ffmpeg commands to construct it.
"""
def __init__(self, **kwargs):
"""Create a new Maker."""
self.title = kwargs["title"] if "title" in kwargs else DEFAULTOUTPUT
self.fadetime = kwargs["fadetime"] if "fadetime" in kwargs else DEFAULTFADETIME
self.format = kwargs["format"] if "format" in kwargs else DEFAULTFORMAT
#Not yet implemented functionally speaking:
self.resolution = kwargs["res"] if "res" in kwargs else DEFAULTRES
self.viddict = {}
self.seglist = []
self.filtergraph = []
self.command = ["ffmpeg"]
self.labgen = labelGenerator()
pass
def __repr__(self):
"""String representation of the Maker."""
#TODO non-terrible version of this...
s = "Maker of " + self.title
s += "\nTotal segments = " + str(len(self.seglist))
return s
def addVideo(self, name, path):
"""Add a video object to the maker, using name to refer to path"""
if name in self.viddict:
if not os.path.join(path) == self.viddict[name].path:
#If the video name is taken for another video, error
raise ValueError("Video short name conflict.")
else:
#If the video exists already, just do nothing
#Maybe raise a warning?
pass
else:
vid = Video(self, name)
vid.path = os.path.join(path)
vid.measureVideoLength()
self.viddict[name] = vid
pass
def addVideoSegment(self, vidname, **kws):
"""Add a new video segment to this maker.
Keywords:
start: time in video to start segment
end: time in video to end segment
path: path to video file as list (used if this Maker does not
already have this video available).
"""
l = next(self.labgen)
lab = LinkLabel(l,l)
if vidname in self.viddict:
vid = self.viddict[vidname]
else:
vid = Video(self, vidname)
if "path" in kws:
vid.path = os.path.join(kws["path"])
vid.measureVideoLength()
self.viddict[vidname] = vid
self.seglist.append(VideoSegment(vid, lab, **kws))
def addTransition(self, n, **kws):
"""Add transitions to the nth video segment.
Keywords:
fadein: time to spend fading in, or None.
fadeout: time to spend fading out, or None.
To use the default fadetime, use fadein/out=''"""
seg = self.seglist[n]
seg.transition = Transition(seg, **kws)
pass
def addInputsCommand(self):
"""Adds the inputs to the command."""
for seg in self.seglist:
self.command.extend(seg.getInputCommand())
pass
def addFilterCommand(self):
"""Add the filtergraph to the command."""
self.command.append("-filter_complex")
self.command.append(','.join(self.filtergraph))
pass
def addOutputCommand(self):
"""Add the output video path to the command."""
#This should really be a path, not a title
self.command.append(self.title + self.format)
pass
def addTransitionFilter(self):
"""Add the transitions between segments to the filtergraph."""
for seg in self.seglist:
self.filtergraph.extend(seg.transition.getFilterComponents())
def addConcatFilter(self):
"""Create a command to concatenate the video segments."""
concatstring = ""
for seg in self.seglist:
label = seg.linklabel
concatstring += (label.vlabel + label.alabel)
concatstring += "concat=n={0}:v=1:a=1".format(len(self.seglist))
self.filtergraph.append(concatstring)
pass
class Video:
"""Class to contain information about a single video."""
def __init__(self, maker, name = "DEFAULT"):
self.maker = maker
self.name = name
self.path = self.name + self.maker.format
#This could be improved to proper pathbuilding?
def measureVideoLength(self):
"""Use ffprobe to determine the length of a video."""
durastr = "N/A" # giving this a reference
findcmd = DURATIONFIND + [self.path]
try:
durastr = sp.check_output(findcmd).decode().strip()
except sp.CalledProcessError:
print("Failed to measure length of video:", self.path)
#TODO better handling?
if durastr != "N/A":
self.duration = dt.datetime.strptime(durastr, "%H:%M:%S.%f").time()
else:
findcmd = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-sexagesimal', '-of', 'default=noprint_wrappers=1:nokey=1'] + [self.path]
durastr = sp.check_output(findcmd).decode().strip()
try:
self.duration = dt.datetime.strptime(durastr, "%H:%M:%S.%f").time()
except:
print("Unable to read length of video:", self.path)
class Transition:
"""Class representing transitions to/from a video segment."""
def __init__(self, videosegment, **kws):
"""Create a new Transition object.
Keywords:
fadein: duration of fade-in in seconds, or None
fadeout: duration of fade-out in seconds, or None
"""
#The segment is the video segment bracketed by this
#transition's fades.
self.videosegment = videosegment
if "fadein" in kws:
if kws["fadein"]:
self.fadein = kws["fadein"]
elif not (kws["fadein"] == None or kws["fadein"] == 0):
#If the fade time isn't explicitly 0
self.fadein = self.videosegment.parentvideo.maker.fadetime
else:
self.fadein = None
else:
self.fadein = False
if "fadeout" in kws:
if kws["fadeout"]:
self.fadeout = kws["fadeout"]
self.starttime = (self.videosegment.getDuration() \
- dt.timedelta(seconds=int(self.fadeout))).total_seconds()
elif not (kws["fadeout"] == None or kws["fadeout"] == 0):
self.fadeout = self.videosegment.parentvideo.maker.fadetime
self.starttime = (self.videosegment.getDuration() \
- dt.timedelta(seconds=int(self.fadeout))).total_seconds()
else:
self.fadeout = None
else:
self.fadeout = False
def __repr__(self):
"""Create a printable representation of this transition."""
s = str(self.videosegment) + ": "
if self.fadein:
s += "fade-in: {0} ".format(self.fadein)
if self.fadeout:
s += "fade-out: {0} ".format(self.fadeout)
if not (self.fadein or self.fadeout):
s += "no fades"
return s
def getFilterComponents(self):
"""Create the filtergraph strings for this transition."""
filtercomps = []
if self.fadein:
filtercomps.append("{0}fade=in:d={1}".\
format(self.videosegment.linklabel.vlabel,\
self.fadein))
if self.fadeout:
#If both kinds of fade are required
filtercomps[0] += ", fade=out:d={0}:start_time={1}".\
format(self.fadeout,\
self.starttime)
elif self.fadeout:
filtercomps.append("{0}fade=out:d={1}:start_time={2}".\
format(self.videosegment.linklabel.vlabel,\
self.fadeout,\
self.starttime))
if self.fadein or self.fadeout:
#If there's a fade filter, give it an output label
self.videosegment.linklabel.\
updateLabel(video=next(self.videosegment.\
parentvideo.maker.labgen))
filtercomps[-1] += self.videosegment.linklabel.vlabel
return filtercomps
class VideoSegment:
"""Class representing a single segment of a video.
Note: Can be the entire video.
"""
def __init__(self, video, linklabel, **kws):
"""Create a new VideoSegment object.
Keywords:
start: datetime time giving time in video at which to start.
end: datetime time giving time in video at which to end.
"""
self.parentvideo = video
self.linklabel = linklabel
if "start" in kws:
st = kws["start"]
if st < self.parentvideo.duration:
self.starttime = st
else:
#This is an error; there's no obvious way to proceed
raise ValueError("Start of video segment later than video length.")
else:
self.starttime = dt.time(0)
if "end" in kws:
et = kws["end"]
if et < self.parentvideo.duration:
self.endtime = et
else:
#This could happen if e.g. rounding errors make et larger than
#the video length when they're supposed to be equal.
#A warning is raised and the segment goes to the end of the video.
warnings.warn("End of video segment after end of video, entire remainder will be used.")
self.endtime = self.parentvideo.duration
else:
self.endtime = self.parentvideo.duration
if self.endtime < self.starttime:
raise ValueError("Start of video segment after end.")
#An empty transition
self.transition = Transition(self)
def __repr__(self):
"""Create a printable representation of this segment."""
return self.parentvideo.maker.title + "segment: " \
+ str(self.parentvideo.maker.seglist.index(self))
def getDuration(self):
"""Get the duration of this video segment."""
return timediff(self.endtime, self.starttime)
def getInputCommand(self):
"""Get the command portion for adding this video segment as an input."""
commandsection = []
if self.starttime > dt.time(0):
commandsection.extend(["-ss", str(self.starttime)])
if self.endtime < self.parentvideo.duration:
commandsection.extend(["-t", str(self.getDuration())])
commandsection.extend(["-i", self.parentvideo.path])
return commandsection
def setTransition(self, transition):
"""Set the transitions in and out of this video segment."""
#Create a transition referring to this segment
#transition should be given as a dictionary of arguments to the
#Transition constructor.
self.transition = Transition(self, **transition)
class LinkLabel:
"""Class to represent link labels. Has an Audio and Video label."""
def __init__(self, vlab, alab):
"""Create a new LinkLabel object."""
self.vlabel = "[{0}:v]".format(vlab)
self.alabel = "[{0}:a]".format(alab)
def updateLabel(self, **kws):
"""Update one of the labels to a new value."""
if "video" in kws.keys():
self.vlabel = "[{0}:v]".format(kws["video"])
if "audio" in kws.keys():
self.alabel = "[{0}:a]".format(kws["audio"])
def labelGenerator():
"""Generates unique short names for link labels."""
i = 0
while True:
yield str(i)
i += 1
return
def timediff(t1, t2):
"""Find the absolute difference between two datetime Times.
Returns a timedelta object.
"""
delta1 = dt.timedelta(hours=t1.hour, \
minutes=t1.minute, \
seconds=t1.second, \
microseconds=t1.microsecond)
delta2 = dt.timedelta(hours=t2.hour, \
minutes=t2.minute, \
seconds=t2.second, \
microseconds=t2.microsecond)
return abs(delta2 - delta1)
if __name__ == "__main__":
print("Running tests...")
vidpath1 = os.path.join(".", "testvideos", "testvid1.mp4")
vidpath2 = os.path.join(".", "testvideos", "testvid2.mp4")
testmaker = Maker(title="testoutput",format=".mp4")
testmaker.addVideo("vid1", vidpath1)
testmaker.addVideo("vid2", vidpath2)
testmaker.addVideoSegment("vid1", start=dt.time(0,0,3), end=dt.time(0,0,5))
testmaker.addVideoSegment("vid2", start=dt.time(0,0,4), end=dt.time(0,0,7))
testmaker.addTransition(0,fadein='')
testmaker.addTransition(1,fadein=None,fadeout=2)
testmaker.addInputsCommand()
testmaker.addTransitionFilter()
testmaker.addConcatFilter()
testmaker.addFilterCommand()
testmaker.addOutputCommand()
print("test command")
print(testmaker.command)
if len(sys.argv) > 1:
if sys.argv[1] == "testmake":
sp.check_call(testmaker.command)
print("Tests complete.")