Video editing with Python
Fixed critical bug in ffmpeg_reader (now rounding for the index of frames). Moved things around in VideoClip. Improved concatenate and freeze-at-start
Zulkocb8a03c3ec87
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.pyindex 86b7607c..b765e48e 100644--- a/moviepy/video/VideoClip.py+++ b/moviepy/video/VideoClip.py@@ -101,12 +101,17 @@ class VideoClip(Clip):+ # ===============================================================+ # EXPORT OPERATIONS+++def save_frame(self, filename, t=0, savemask=False):""" Save a clip's frame to an image file.Saves the frame of clip corresponding to time ``t`` in- 'filename'. If ``savemask`` is ``True`` the mask is saved in the- alpha layer of the picture.+ 'filename'. If ``savemask`` is ``True`` the mask is saved in+ the alpha layer of the picture."""im = self.get_frame(t)@@ -116,6 +121,7 @@ class VideoClip(Clip):ffmpeg_write_image(filename, im)+def to_videofile(self, filename, fps=24, codec='libx264',bitrate=None, audio=True, audio_fps=44100,audio_nbytes = 4, audio_codec= 'libvorbis',@@ -144,7 +150,8 @@ class VideoClip(Clip):of perfect quality, but possibly very huge size. 'png' isis lossless and produces smaller files than 'rawvideo'.- ``'mpeg4'`` produces nice quality, very well compressed videos.+ ``'mpeg4'`` produces nice quality, very well compressed+ videos.``'libx264'`` (default) is a little better than 'mpeg4',a little heavier.@@ -153,8 +160,8 @@ class VideoClip(Clip):Either ``True``, ``False``, or a file name.If ``True`` and the clip has an audio clip attached, thisaudio clip will be incorporated as a soundtrack in the movie.- If ``audio`` is the name of an audio file, this audio file will- be incorporated as a soundtrack in the movie.+ If ``audio`` is the name of an audio file, this audio file+ will be incorporated as a soundtrack in the movie.audiofpsframe rate to use when writing the sound.@@ -223,7 +230,9 @@ class VideoClip(Clip):if merge_audio:# make a name for the temporary video file- videofile = Clip._TEMP_FILES_PREFIX + "to_videofile_"+ filename+ videofile = (Clip._TEMP_FILES_PREFIX ++ "to_videofile_"++ filename)else:@@ -242,7 +251,9 @@ class VideoClip(Clip):audioproc = multiprocessing.Process(target=self.audio.to_audiofile,args=(temp_audiofile,audio_fps,audio_nbytes,- audio_bufsize,audio_codec, audio_bitrate,verbose))+ audio_bufsize,audio_codec,+ audio_bitrate,+ verbose))audioproc.start()@@ -285,61 +296,6 @@ class VideoClip(Clip):" for the Oscars !\n")- def blit_on(self, picture, t):- """- Returns the result of the blit of the clip's frame at time `t`- on the given `picture`, the position of the clip being given- by the clip's ``pos`` attribute. Meant for compositing.- """-- hf, wf = sizef = picture.shape[:2]-- if self.ismask and picture.max() != 0:- return np.maximum(picture, self.blit_on(np.zeros(sizef), t))-- ct = t - self.start # clip time-- # GET IMAGE AND MASK IF ANY-- img = self.get_frame(ct)- mask = (None if (self.mask is None) else self.mask.get_frame(ct))- hi, wi = img.shape[:2]-- # SET POSITION-- pos = self.pos(ct)--- # preprocess short writings of the position- if isinstance(pos,str):- pos = { 'center': ['center','center'],- 'left': ['left','center'],- 'right': ['right','center'],- 'top':['center','top'],- 'bottom':['center','bottom']}[pos]- else:- pos = list(pos)-- # is the position relative (given in % of the clip's size) ?- if self.relative_pos:- for i, dim in enumerate(wf, hf):- if not isinstance(pos[i], str):- pos[i] = dim * pos[i]-- if isinstance(pos[0], str):- D = {'left': 0, 'center': (wf - wi) / 2, 'right': wf - wi}- pos[0] = D[pos[0]]-- if isinstance(pos[1], str):-- D = {'top': 0, 'center': (hf - hi) / 2, 'bottom': hf - hi}- pos[1] = D[pos[1]]-- pos = map(int, pos)-- return blit(img, picture, pos, mask=mask, ismask=self.ismask)--def to_images_sequence(self, nameformat, fps=None, verbose=True):""" Writes the videoclip to a sequence of image files.@@ -354,9 +310,9 @@ class VideoClip(Clip):"some_folder/frame%04d.jpeg", etc.fps- Number of frames per second to consider when writing the clip.- If not specified, the clip's ``fps`` attribute will be used if- it has one.+ Number of frames per second to consider when writing the+ clip. If not specified, the clip's ``fps`` attribute will+ be used if it has one.verboseVerbose output ?@@ -398,16 +354,13 @@ class VideoClip(Clip):----def to_gif(self, filename, fps=None, program= 'ImageMagick',opt="OptimizeTransparency", fuzz=1, verbose=True,loop=0, dispose=False):- """ Write the VideoClip to a GIF file- Converts a VideoClip into an animated GIF using ImageMagick or- ffmpeg.+ """ Write the VideoClip to a GIF file.++ Converts a VideoClip into an animated GIF using ImageMagick+ or ffmpeg.Parameters@@ -430,8 +383,9 @@ class VideoClip(Clip):'optimizeplus' or 'OptimizeTransparency'.fuzz- (ImageMagick only) Compresses the GIF by considering that the- colors that are less than fuzz% different are in fact the same.+ (ImageMagick only) Compresses the GIF by considering that+ the colors that are less than fuzz% different are in fact+ the same.Notes@@ -508,9 +462,9 @@ class VideoClip(Clip):def subfx(self, fx, ta=0, tb=None, **kwargs):""" Apply a transformation to a part of the clip.- Returns a new clip in which the function ``fun`` (clip->clip) has- been applied to the subclip between times `ta` and `tb` (in- seconds).+ Returns a new clip in which the function ``fun`` (clip->clip)+ has been applied to the subclip between times `ta` and `tb`+ (in seconds).Examples---------@@ -546,7 +500,62 @@ class VideoClip(Clip):#--------------------------------------------------------------# C O M P O S I T I N G++ def blit_on(self, picture, t):+ """+ Returns the result of the blit of the clip's frame at time `t`+ on the given `picture`, the position of the clip being given+ by the clip's ``pos`` attribute. Meant for compositing.+ """++ hf, wf = sizef = picture.shape[:2]++ if self.ismask and picture.max() != 0:+ return np.maximum(picture,+ self.blit_on(np.zeros(sizef), t))++ ct = t - self.start # clip time++ # GET IMAGE AND MASK IF ANY++ img = self.get_frame(ct)+ mask = (None if (self.mask is None) else+ self.mask.get_frame(ct))+ hi, wi = img.shape[:2]++ # SET POSITION++ pos = self.pos(ct)+++ # preprocess short writings of the position+ if isinstance(pos,str):+ pos = { 'center': ['center','center'],+ 'left': ['left','center'],+ 'right': ['right','center'],+ 'top':['center','top'],+ 'bottom':['center','bottom']}[pos]+ else:+ pos = list(pos)++ # is the position relative (given in % of the clip's size) ?+ if self.relative_pos:+ for i, dim in enumerate(wf, hf):+ if not isinstance(pos[i], str):+ pos[i] = dim * pos[i]++ if isinstance(pos[0], str):+ D = {'left': 0, 'center': (wf - wi) / 2, 'right': wf - wi}+ pos[0] = D[pos[0]]++ if isinstance(pos[1], str):++ D = {'top': 0, 'center': (hf - hi) / 2, 'bottom': hf - hi}+ pos[1] = D[pos[1]]++ pos = map(int, pos)+ return blit(img, picture, pos, mask=mask, ismask=self.ismask)def add_mask(self, constant_size=True):""" Add a mask VideoClip to the VideoClip.@@ -643,6 +652,7 @@ class VideoClip(Clip):self.mask = mask+@add_mask_if_none@outplacedef set_opacity(self, op):@@ -690,14 +700,15 @@ class VideoClip(Clip):self.pos = lambda t: pos+#--------------------------------------------------------------- # CONVERSIONS+ # CONVERSIONS TO OTHER TYPESdef to_ImageClip(self,t=0):"""- Return an ImageClip made out of the clip's frame at time ``t``+ Returns an ImageClip made out of the clip's frame at time ``t``"""return ImageClip(self.get_frame(t))@@ -705,7 +716,7 @@ class VideoClip(Clip):def to_mask(self, canal=0):"""- Return a mask a video clip made from the clip.+ Returns a mask a video clip made from the clip."""if self.ismask:return self@@ -719,7 +730,7 @@ class VideoClip(Clip):def to_RGB(self):"""- Return a non-mask video clip made from the mask video clip.+ Returns a non-mask video clip made from the mask video clip."""if self.ismask:f = lambda pic: np.dstack(3 * [255 * pic]).astype('uint8')@@ -730,6 +741,38 @@ class VideoClip(Clip):return self+ @requires_duration+ def iter_frames(self, fps=None):+ """ Iterates over all the frames of the clip.++ Returns each frame of the clip as a HxWxN np.array,+ where N=1 for mask clips and N=3 for RGB clips.++ This function is not really meant for video editing.+ It provides an easy way to do frame-by-frame treatment of+ a video, for fields like science, computer vision...++ The ``fps`` (frames per second) parameter is optional if the+ clip already has a ``fps`` attribute.++ Examples+ ---------++ >>> # prints the maximum of red that is contained+ >>> # on the first line of each frame of the clip.+ >>> from moviepy.editor import VideoFileClip+ >>> myclip = VideoFileClip('myvideo.mp4')+ >>> for frame in myclip.iter_frames():+ >>> print ( frame[0,:,0].max() )++ """++ if fps is None:+ fps = self.fps++ for t in np.arange(0, self.duration, 1.0/fps):+ yield self.get_frame(t)+#----------------------------------------------------------------# Audiodiff --git a/moviepy/video/compositing/concatenate.py b/moviepy/video/compositing/concatenate.pyindex 013fc5dd..0c2956f6 100644--- a/moviepy/video/compositing/concatenate.py+++ b/moviepy/video/compositing/concatenate.py@@ -8,7 +8,7 @@ from moviepy.video.compositing.on_color import on_colordef concatenate(clipslist, method = 'chain', transition=None,bg_color=(0, 0, 0), transparent=False, ismask=False, crossover = 0):- """+ """ Concatenates several video clipsReturns a video clip made by clip by concatenating several video clips.(Concatenated means that they will be played one after another).@@ -23,19 +23,25 @@ def concatenate(clipslist, method = 'chain', transition=None,Returns a VideoClip instance if all clips have the same size andthere is no transition, else a composite clip.- :param clipslist: a list of video clips which must all have- their ``duration`` attributes set.- :param transition: a clip that will be played between each two- clips of the list.- :param bg_color: color of the background ()- :param transparent: if True, the resulting clip's mask will be the- concatenation of the masks of the clips in the list. If- the clips do not have the same resolution, the border around- the smaller clips will be transparent.+ Parameters+ -----------++ clipslist+ A list of video clips which must all have their ``duration``+ attributes set.++ transition+ A clip that will be played between each two clips of the list.++ bg_color+ Color of the background, if any.++ transparent+ If True, the resulting clip's mask will be the concatenation of+ the masks of the clips in the list. If the clips do not have the+ same resolution, the border around the smaller clips will be+ transparent.- :ivar start_times (list): ``start_times[i]`` gives the time at which- the i-th clip starts playing.-"""@@ -52,11 +58,15 @@ def concatenate(clipslist, method = 'chain', transition=None,if method == 'chain':result = VideoClip(ismask = ismask)result.size = (w,h)+def gf(t):i = max([i for i, e in enumerate(tt) if e <= t])return clipslist[i].get_frame(t - tt[i])+result.get_frame = gf- if (len(sizes)>1) and (bg_color !=None):+ if (len(set(map(tuple,sizes)))>1) and (bg_color is not None):+ # If not all clips have the same size, flatten the result+ # on some colorresult = result.fx( on_color, (w,h), bg_color, 'center')elif method == 'compose':@@ -66,6 +76,8 @@ def concatenate(clipslist, method = 'chain', transition=None,size = (w, h), bg_color=bg_color, ismask=ismask,transparent=transparent)+ result.tt = tt+ result.clipslist = clipslistresult.start_times = tt[:-1]result.start, result.duration, result.end = 0, tt[-1] , tt[-1]diff --git a/moviepy/video/fx/freeze_at_start.py b/moviepy/video/fx/freeze_at_start.pyindex 37e8b048..9d818743 100644--- a/moviepy/video/fx/freeze_at_start.py+++ b/moviepy/video/fx/freeze_at_start.py@@ -2,7 +2,6 @@ from moviepy.decorators import requires_durationfrom moviepy.video.VideoClip import ImageClipfrom moviepy.video.compositing.concatenate import concatenate-@requires_durationdef freeze_at_start(clip, freeze_duration=None, total_duration=None):""" Momentarily freeze the clip on its first frame.@@ -15,9 +14,12 @@ def freeze_at_start(clip, freeze_duration=None, total_duration=None):"""freezed_clip = ImageClip(clip.get_frame(0))+ if clip.mask:+ freezed_clip.mask = ImageClip(clip.mask.get_frame(0))+if total_duration:freeze_duration = total_duration - clip.durationif freeze_duration:freezed_clip = freezed_clip.set_duration(freeze_duration)- return concatenate([freezed_clip,clip])+ return concatenate([freezed_clip, clip])diff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.pyindex b4ec207e..1c02d82d 100644--- a/moviepy/video/io/ffmpeg_reader.py+++ b/moviepy/video/io/ffmpeg_reader.py@@ -144,7 +144,8 @@ class FFMPEG_VideoReader:elif t > self.duration:t = self.duration- pos = int(self.fps*t)+1+ pos = int(np.round(self.fps*t))+1+if pos == self.pos:return self.lastreadelse: