Video editing with Python
Move concatenate_videoclips from his own file to CompositeVideoClip file, as its just a tools to create a composite video clip, just like clip_array
osaajani692af8d90e14
diff --git a/moviepy/__init__.py b/moviepy/__init__.pyindex 164960a6..6b8f9ae0 100644--- a/moviepy/__init__.py+++ b/moviepy/__init__.py@@ -21,8 +21,7 @@ from moviepy.tools import convert_to_secondsfrom moviepy.version import __version__from moviepy.video import fx as vfx, tools as videotoolsfrom moviepy.video.compositing import transitions as transfx-from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip, clips_array-from moviepy.video.compositing.concatenate import concatenate_videoclips+from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip, clips_array, concatenate_videoclipsfrom moviepy.video.io import ffmpeg_toolsfrom moviepy.video.io.downloader import download_webfilefrom moviepy.video.io.ImageSequenceClip import ImageSequenceClipdiff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.pyindex b79ba372..f648e143 100644--- a/moviepy/video/VideoClip.py+++ b/moviepy/video/VideoClip.py@@ -543,7 +543,7 @@ class VideoClip(Clip):clips = [clip for clip in [left, center, right] if clip is not None]# beurk, have to find other solution- from moviepy.video.compositing.concatenate import concatenate_videoclips+ from moviepy.video.compositing.CompositeVideoClip import concatenate_videoclipsreturn concatenate_videoclips(clips).with_start(self.start)@@ -855,7 +855,7 @@ class VideoClip(Clip):def __add__(self, other):if isinstance(other, VideoClip):- from moviepy.video.compositing.concatenate import concatenate_videoclips+ from moviepy.video.compositing.CompositeVideoClip import concatenate_videoclipsmethod = "chain" if self.size == other.size else "compose"return concatenate_videoclips([self, other], method=method)@@ -964,6 +964,7 @@ class UpdatedVideoClip(VideoClip):self.world = worlddef make_frame(t):+ print(t)while self.world.clip_t < t:world.update()return world.to_frame()diff --git a/moviepy/video/compositing/CompositeVideoClip.py b/moviepy/video/compositing/CompositeVideoClip.pyindex 14e032a0..0110bb16 100644--- a/moviepy/video/compositing/CompositeVideoClip.py+++ b/moviepy/video/compositing/CompositeVideoClip.py@@ -2,6 +2,7 @@import numpy as npfrom PIL import Image+from functools import reducefrom moviepy.audio.AudioClip import CompositeAudioClipfrom moviepy.video.VideoClip import ColorClip, VideoClip@@ -214,3 +215,116 @@ def clips_array(array, rows_widths=None, cols_heights=None, bg_color=None):array[i, j] = clip.with_position((x, y))return CompositeVideoClip(array.flatten(), size=(xs[-1], ys[-1]), bg_color=bg_color)+++def concatenate_videoclips(+ clips, method="chain", transition=None, bg_color=None, is_mask=False, padding=0+):+ """Concatenates several video clips.++ Returns a video clip made by clip by concatenating several video clips.+ (Concatenated means that they will be played one after another).++ There are two methods:++ - method="chain": will produce a clip that simply outputs+ the frames of the successive clips, without any correction if they are+ not of the same size of anything. If none of the clips have masks the+ resulting clip has no mask, else the mask is a concatenation of masks+ (using completely opaque for clips that don't have masks, obviously).+ If you have clips of different size and you want to write directly the+ result of the concatenation to a file, use the method "compose" instead.++ - method="compose", if the clips do not have the same resolution, the final+ resolution will be such that no clip has to be resized.+ As a consequence the final clip has the height of the highest clip and the+ width of the widest clip of the list. All the clips with smaller dimensions+ will appear centered. The border will be transparent if mask=True, else it+ will be of the color specified by ``bg_color``.++ The clip with the highest FPS will be the FPS of the result clip.++ Parameters+ ----------+ clips+ A list of video clips which must all have their ``duration``+ attributes set.+ method+ "chain" or "compose": see above.+ transition+ A clip that will be played between each two clips of the list.++ bg_color+ Only for method='compose'. Color of the background.+ Set to None for a transparent clip++ padding+ Only for method='compose'. Duration during two consecutive clips.+ Note that for negative padding, a clip will partly play at the same+ time as the clip it follows (negative padding is cool for clips who fade+ in on one another). A non-null padding automatically sets the method to+ `compose`.++ """+ if transition is not None:+ clip_transition_pairs = [[v, transition] for v in clips[:-1]]+ clips = reduce(lambda x, y: x + y, clip_transition_pairs) + [clips[-1]]+ transition = None++ timings = np.cumsum([0] + [clip.duration for clip in clips])++ sizes = [clip.size for clip in clips]++ w = max(size[0] for size in sizes)+ h = max(size[1] for size in sizes)++ timings = np.maximum(0, timings + padding * np.arange(len(timings)))+ timings[-1] -= padding # Last element is the duration of the whole++ if method == "chain":++ def make_frame(t):+ i = max([i for i, e in enumerate(timings) if e <= t])+ return clips[i].get_frame(t - timings[i])++ def get_mask(clip):+ mask = clip.mask or ColorClip([1, 1], color=1, is_mask=True)+ if mask.duration is None:+ mask.duration = clip.duration+ return mask++ result = VideoClip(is_mask=is_mask, make_frame=make_frame)+ if any([clip.mask is not None for clip in clips]):+ masks = [get_mask(clip) for clip in clips]+ result.mask = concatenate_videoclips(masks, method="chain", is_mask=True)+ result.clips = clips+ elif method == "compose":+ result = CompositeVideoClip(+ [+ clip.with_start(t).with_position("center")+ for (clip, t) in zip(clips, timings)+ ],+ size=(w, h),+ bg_color=bg_color,+ is_mask=is_mask,+ )+ else:+ raise Exception(+ "MoviePy Error: The 'method' argument of "+ "concatenate_videoclips must be 'chain' or 'compose'"+ )++ result.timings = timings++ result.start_times = timings[:-1]+ result.start, result.duration, result.end = 0, timings[-1], timings[-1]++ audio_t = [+ (clip.audio, t) for clip, t in zip(clips, timings) if clip.audio is not None+ ]+ if audio_t:+ result.audio = CompositeAudioClip([a.with_start(t) for a, t in audio_t])++ fpss = [clip.fps for clip in clips if getattr(clip, "fps", None) is not None]+ result.fps = max(fpss) if fpss else None+ return resultdiff --git a/moviepy/video/compositing/concatenate.py b/moviepy/video/compositing/concatenate.pydeleted file mode 100644index cbdea828..00000000--- a/moviepy/video/compositing/concatenate.py+++ /dev/null@@ -1,122 +0,0 @@-"""Video clips concatenation."""--from functools import reduce--import numpy as np--from moviepy.audio.AudioClip import CompositeAudioClip-from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip-from moviepy.video.VideoClip import ColorClip, VideoClip---def concatenate_videoclips(- clips, method="chain", transition=None, bg_color=None, is_mask=False, padding=0-):- """Concatenates several video clips.-- Returns a video clip made by clip by concatenating several video clips.- (Concatenated means that they will be played one after another).-- There are two methods:-- - method="chain": will produce a clip that simply outputs- the frames of the successive clips, without any correction if they are- not of the same size of anything. If none of the clips have masks the- resulting clip has no mask, else the mask is a concatenation of masks- (using completely opaque for clips that don't have masks, obviously).- If you have clips of different size and you want to write directly the- result of the concatenation to a file, use the method "compose" instead.-- - method="compose", if the clips do not have the same resolution, the final- resolution will be such that no clip has to be resized.- As a consequence the final clip has the height of the highest clip and the- width of the widest clip of the list. All the clips with smaller dimensions- will appear centered. The border will be transparent if mask=True, else it- will be of the color specified by ``bg_color``.-- The clip with the highest FPS will be the FPS of the result clip.-- Parameters- ----------- clips- A list of video clips which must all have their ``duration``- attributes set.- method- "chain" or "compose": see above.- transition- A clip that will be played between each two clips of the list.-- bg_color- Only for method='compose'. Color of the background.- Set to None for a transparent clip-- padding- Only for method='compose'. Duration during two consecutive clips.- Note that for negative padding, a clip will partly play at the same- time as the clip it follows (negative padding is cool for clips who fade- in on one another). A non-null padding automatically sets the method to- `compose`.-- """- if transition is not None:- clip_transition_pairs = [[v, transition] for v in clips[:-1]]- clips = reduce(lambda x, y: x + y, clip_transition_pairs) + [clips[-1]]- transition = None-- timings = np.cumsum([0] + [clip.duration for clip in clips])-- sizes = [clip.size for clip in clips]-- w = max(size[0] for size in sizes)- h = max(size[1] for size in sizes)-- timings = np.maximum(0, timings + padding * np.arange(len(timings)))- timings[-1] -= padding # Last element is the duration of the whole-- if method == "chain":-- def make_frame(t):- i = max([i for i, e in enumerate(timings) if e <= t])- return clips[i].get_frame(t - timings[i])-- def get_mask(clip):- mask = clip.mask or ColorClip([1, 1], color=1, is_mask=True)- if mask.duration is None:- mask.duration = clip.duration- return mask-- result = VideoClip(is_mask=is_mask, make_frame=make_frame)- if any([clip.mask is not None for clip in clips]):- masks = [get_mask(clip) for clip in clips]- result.mask = concatenate_videoclips(masks, method="chain", is_mask=True)- result.clips = clips- elif method == "compose":- result = CompositeVideoClip(- [- clip.with_start(t).with_position("center")- for (clip, t) in zip(clips, timings)- ],- size=(w, h),- bg_color=bg_color,- is_mask=is_mask,- )- else:- raise Exception(- "MoviePy Error: The 'method' argument of "- "concatenate_videoclips must be 'chain' or 'compose'"- )-- result.timings = timings-- result.start_times = timings[:-1]- result.start, result.duration, result.end = 0, timings[-1], timings[-1]-- audio_t = [- (clip.audio, t) for clip, t in zip(clips, timings) if clip.audio is not None- ]- if audio_t:- result.audio = CompositeAudioClip([a.with_start(t) for a, t in audio_t])-- fpss = [clip.fps for clip in clips if getattr(clip, "fps", None) is not None]- result.fps = max(fpss) if fpss else None- return resultdiff --git a/moviepy/video/fx/freeze.py b/moviepy/video/fx/freeze.pyindex db39fcd2..21eae0c7 100644--- a/moviepy/video/fx/freeze.py+++ b/moviepy/video/fx/freeze.py@@ -1,5 +1,5 @@from moviepy.decorators import requires_duration-from moviepy.video.compositing.concatenate import concatenate_videoclips+from moviepy.video.compositing.CompositeVideoClip import concatenate_videoclips@requires_durationdiff --git a/tests/test_SubtitlesClip.py b/tests/test_SubtitlesClip.pyindex 55f42720..6937842e 100644--- a/tests/test_SubtitlesClip.py+++ b/tests/test_SubtitlesClip.py@@ -4,8 +4,7 @@ import osimport pytest-from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip-from moviepy.video.compositing.concatenate import concatenate_videoclips+from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip, concatenate_videoclipsfrom moviepy.video.tools.subtitles import SubtitlesClip, file_to_subtitlesfrom moviepy.video.VideoClip import ColorClip, TextClipdiff --git a/tests/test_compositing.py b/tests/test_compositing.pyindex 365b1ad4..47c40ffc 100644--- a/tests/test_compositing.py+++ b/tests/test_compositing.py@@ -6,8 +6,7 @@ import numpy as npimport pytest-from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip, clips_array-from moviepy.video.compositing.concatenate import concatenate_videoclips+from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip, clips_array, concatenate_videoclipsfrom moviepy.video.compositing.transitions import slide_in, slide_outfrom moviepy.video.fx.resize import resizefrom moviepy.video.VideoClip import BitmapClip, ColorClipdiff --git a/tests/test_ffmpeg_writer.py b/tests/test_ffmpeg_writer.pyindex 89ee48e9..6ad856b7 100644--- a/tests/test_ffmpeg_writer.py+++ b/tests/test_ffmpeg_writer.py@@ -7,7 +7,7 @@ from PIL import Imageimport pytest-from moviepy.video.compositing.concatenate import concatenate_videoclips+from moviepy.video.compositing.CompositeVideoClip import concatenate_videoclipsfrom moviepy.video.io.ffmpeg_writer import ffmpeg_write_image, ffmpeg_write_videofrom moviepy.video.io.gif_writers import write_giffrom moviepy.video.io.VideoFileClip import VideoFileClipdiff --git a/tests/test_issues.py b/tests/test_issues.pyindex cfe89e17..eb99bbc8 100644--- a/tests/test_issues.py+++ b/tests/test_issues.py@@ -5,8 +5,7 @@ import osimport pytestfrom moviepy.audio.io.AudioFileClip import AudioFileClip-from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip-from moviepy.video.compositing.concatenate import concatenate_videoclips+from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip, concatenate_videoclipsfrom moviepy.video.compositing.transitions import crossfadein, crossfadeoutfrom moviepy.video.fx.resize import resizefrom moviepy.video.io.VideoFileClip import VideoFileClipdiff --git a/tests/test_videotools.py b/tests/test_videotools.pyindex 8fd71bb4..84e40f47 100644--- a/tests/test_videotools.py+++ b/tests/test_videotools.py@@ -13,7 +13,7 @@ import pytestfrom moviepy.audio.AudioClip import AudioClip, CompositeAudioClipfrom moviepy.audio.fx.multiply_volume import multiply_volumefrom moviepy.audio.tools.cuts import find_audio_period-from moviepy.video.compositing.concatenate import concatenate_videoclips+from moviepy.video.compositing.CompositeVideoClip import concatenate_videoclipsfrom moviepy.video.fx.loop import loopfrom moviepy.video.fx.time_mirror import time_mirrorfrom moviepy.video.io.VideoFileClip import VideoFileClip