@@ -280,7 +280,7 @@ def unpack_gzip(filename, write_directory=None, remove=False):
280
280
return str (write_filename )
281
281
282
282
283
- def generate_movie (images , write_filename = None , fps = 10 , ** kwargs ):
283
+ def generate_movie (images , write_filename = None , fps = 10 , duration = None , ** kwargs ):
284
284
"""
285
285
Creates a movie from a list of images or convert movie to different type
286
286
@@ -296,6 +296,9 @@ def generate_movie(images, write_filename=None, fps=10, **kwargs):
296
296
that does not exist, will create the directory path.
297
297
fps: int
298
298
Frames per second. Passed into moviepy->ImageSequenceClip() method
299
+ duration : float, int or None
300
+ Converting mpg format can have issues with reading the duration of the movie. Set
301
+ to number of seconds to override the derived value if the result is not what you expect.
299
302
**kwargs: dict
300
303
Optional keywords passed into moviepy->write_videofile() method
301
304
@@ -309,6 +312,9 @@ def generate_movie(images, write_filename=None, fps=10, **kwargs):
309
312
if not MOVIEPY_AVAILABLE :
310
313
raise ImportError ('MoviePy v2.X needs to be installed on your system to make movies.' )
311
314
315
+ if int (moviepy .__version__ .split ('.' )[0 ]) <= 1 :
316
+ raise ImportError ('MoviePy v2.X needs to be installed on your system to make movies.' )
317
+
312
318
# Set default movie name
313
319
if write_filename is None :
314
320
write_filename = Path (Path ().cwd (), 'movie.mp4' )
@@ -332,36 +338,13 @@ def generate_movie(images, write_filename=None, fps=10, **kwargs):
332
338
with VideoFileClip (images ) as clip :
333
339
# There can be an issue converting mpeg to other movie format because the
334
340
# duration parameter in the movie file is not set. So moviepy guesses and
335
- # can get the duration wrong. This will find the correct duration (correct to 0.2 seconds)
336
- # and set before writing.
337
- if Path (images ).suffix == '.mpg' :
338
- import numpy as np
339
- import warnings
340
- from collections import deque
341
-
342
- with warnings .catch_warnings ():
343
- warnings .filterwarnings ('ignore' , category = UserWarning )
344
- desired_len = 3
345
- frame_sums = deque ()
346
- duration = 0.0 # Duration of movie in seconds
347
- while True :
348
- result = clip .get_frame (duration )
349
- frame_sums .append (np .sum (result ))
350
- if len (frame_sums ) > desired_len :
351
- frame_sums .popleft ()
352
-
353
- if len (set (frame_sums )) == 1 :
354
- break
355
-
356
- duration += 0.1
357
-
358
- clip = clip .with_start (0 )
359
- clip = clip .with_duration (duration )
360
- clip = clip .with_end (duration )
361
- clip .write_videofile (str (write_filename ), ** kwargs )
362
-
363
- else :
364
- clip .write_videofile (str (write_filename ), ** kwargs )
341
+ # can get the duration wrong.
342
+ if duration is not None :
343
+ clip = clip .with_start (0 )
344
+ clip = clip .with_duration (duration )
345
+ clip = clip .with_end (duration )
346
+
347
+ clip .write_videofile (str (write_filename ), ** kwargs )
365
348
366
349
else :
367
350
clip = moviepy .video .io .ImageSequenceClip .ImageSequenceClip (images , fps = fps )
0 commit comments