Video editing with Python
Continue transition of effect from function to method. Transitions are now simple effects and have been migrated from function to class + fix a missing @dataclass in FadeOut. moviepy.video.compositing.transitions as well as transfx have been removed
osaajani22ff2e2c6dde
diff --git a/moviepy/__init__.py b/moviepy/__init__.pyindex 67e65e4d..3d69df53 100644--- a/moviepy/__init__.py+++ b/moviepy/__init__.py@@ -20,7 +20,6 @@ from moviepy.audio.io.AudioFileClip import AudioFileClipfrom moviepy.tools import convert_to_secondsfrom moviepy.version import __version__from moviepy.video import fx as vfx, tools as videotools-from moviepy.video.compositing import transitions as transfxfrom moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip, clips_array, concatenate_videoclipsfrom moviepy.video.io import ffmpeg_toolsfrom moviepy.video.io.display_in_notebook import display_in_notebook@@ -44,7 +43,6 @@ from moviepy.video.VideoClip import (audio_fxs = inspect.getmembers(afx, inspect.isfunction) + [("Loop", vfx.Loop)]video_fxs = (inspect.getmembers(vfx, inspect.isfunction)- + inspect.getmembers(transfx, inspect.isfunction)+ audio_fxs)@@ -82,9 +80,9 @@ __all__ = ["CompositeAudioClip","concatenate_audioclips","AudioFileClip",+ "Effect","vfx","afx",- "transfx","videotools","ffmpeg_tools","convert_to_seconds",diff --git a/moviepy/video/compositing/transitions.py b/moviepy/video/compositing/transitions.pydeleted file mode 100644index 22fbe283..00000000--- a/moviepy/video/compositing/transitions.py+++ /dev/null@@ -1,128 +0,0 @@-"""Here is the current catalogue. These are meant to be used with ``clip.fx``-There are available as ``transfx.crossfadein`` etc.-"""--from moviepy.decorators import add_mask_if_none, requires_duration-from moviepy.video.fx.FadeIn import FadeIn-from moviepy.video.fx.FadeOut import FadeOut---__all__ = ["crossfadein", "crossfadeout", "slide_in", "slide_out"]---@requires_duration-@add_mask_if_none-def crossfadein(clip, duration):- """Makes the clip appear progressively, over ``duration`` seconds.- Only works when the clip is included in a CompositeVideoClip.- """- clip.mask.duration = clip.duration- new_clip = clip.copy()- new_clip.mask = clip.mask.with_effect(FadeIn(duration))- return new_clip---@requires_duration-@add_mask_if_none-def crossfadeout(clip, duration):- """Makes the clip disappear progressively, over ``duration`` seconds.- Only works when the clip is included in a CompositeVideoClip.- """- clip.mask.duration = clip.duration- new_clip = clip.copy()- new_clip.mask = clip.mask.with_effect(FadeOut(duration))- return new_clip---def slide_in(clip, duration, side):- """Makes the clip arrive from one side of the screen.-- Only works when the clip is included in a CompositeVideoClip,- and if the clip has the same size as the whole composition.-- Parameters- ------------ clip : moviepy.Clip.Clip- A video clip.-- duration : float- Time taken for the clip to be fully visible-- side : str- Side of the screen where the clip comes from. One of- 'top', 'bottom', 'left' or 'right'.-- Examples- ---------- >>> from moviepy import *- >>>- >>> clips = [... make a list of clips]- >>> slided_clips = [- ... CompositeVideoClip([clip.fx(transfx.slide_in, 1, "left")])- ... for clip in clips- ... ]- >>> final_clip = concatenate_videoclips(slided_clips, padding=-1)- >>>- >>> clip = ColorClip(- ... color=(255, 0, 0), duration=1, size=(300, 300)- ... ).with_fps(60)- >>> final_clip = CompositeVideoClip([transfx.slide_in(clip, 1, "right")])- """- w, h = clip.size- pos_dict = {- "left": lambda t: (min(0, w * (t / duration - 1)), "center"),- "right": lambda t: (max(0, w * (1 - t / duration)), "center"),- "top": lambda t: ("center", min(0, h * (t / duration - 1))),- "bottom": lambda t: ("center", max(0, h * (1 - t / duration))),- }-- return clip.with_position(pos_dict[side])---@requires_duration-def slide_out(clip, duration, side):- """Makes the clip go away by one side of the screen.-- Only works when the clip is included in a CompositeVideoClip,- and if the clip has the same size as the whole composition.-- Parameters- ------------ clip : moviepy.Clip.Clip- A video clip.-- duration : float- Time taken for the clip to fully disappear.-- side : str- Side of the screen where the clip goes. One of- 'top', 'bottom', 'left' or 'right'.-- Examples- ---------- >>> clips = [... make a list of clips]- >>> slided_clips = [- ... CompositeVideoClip([clip.fx(transfx.slide_out, 1, "left")])- ... for clip in clips- ... ]- >>> final_clip = concatenate_videoclips(slided_clips, padding=-1)- >>>- >>> clip = ColorClip(- ... color=(255, 0, 0), duration=1, size=(300, 300)- ... ).with_fps(60)- >>> final_clip = CompositeVideoClip([transfx.slide_out(clip, 1, "right")])- """- w, h = clip.size- ts = clip.duration - duration # start time of the effect.- pos_dict = {- "left": lambda t: (min(0, w * (-(t - ts) / duration)), "center"),- "right": lambda t: (max(0, w * ((t - ts) / duration)), "center"),- "top": lambda t: ("center", min(0, h * (-(t - ts) / duration))),- "bottom": lambda t: ("center", max(0, h * ((t - ts) / duration))),- }-- return clip.with_position(pos_dict[side])diff --git a/moviepy/video/fx/CrossFadeIn.py b/moviepy/video/fx/CrossFadeIn.pynew file mode 100644index 00000000..04a01be4--- /dev/null+++ b/moviepy/video/fx/CrossFadeIn.py@@ -0,0 +1,26 @@+from moviepy.Clip import Clip+from moviepy.Effect import Effect+from dataclasses import dataclass++from moviepy.video.fx.FadeIn import FadeIn++@dataclass+class CrossFadeIn(Effect):+ """Makes the clip appear progressively, over ``duration`` seconds.+ Only works when the clip is included in a CompositeVideoClip.+ """++ duration: float++ def apply(self, clip: Clip) -> Clip:+ if clip.duration is None:+ raise ValueError("Attribute 'duration' not set")++ if clip.mask is None:+ clip = clip.with_add_mask()++ clip.mask.duration = clip.duration+ clip.mask = clip.mask.with_effect(FadeIn(self.duration))++ return clip+diff --git a/moviepy/video/fx/CrossFadeOut.py b/moviepy/video/fx/CrossFadeOut.pynew file mode 100644index 00000000..c492f400--- /dev/null+++ b/moviepy/video/fx/CrossFadeOut.py@@ -0,0 +1,26 @@+from moviepy.Clip import Clip+from moviepy.Effect import Effect+from dataclasses import dataclass++from moviepy.video.fx.FadeOut import FadeOut++@dataclass+class CrossFadeOut(Effect):+ """Makes the clip disappear progressively, over ``duration`` seconds.+ Only works when the clip is included in a CompositeVideoClip.+ """++ duration: float++ def apply(self, clip: Clip) -> Clip:+ if clip.duration is None:+ raise ValueError("Attribute 'duration' not set")++ if clip.mask is None:+ clip = clip.with_add_mask()++ clip.mask.duration = clip.duration+ clip.mask = clip.mask.with_effect(FadeOut(self.duration))++ return clip+diff --git a/moviepy/video/fx/FadeIn.py b/moviepy/video/fx/FadeIn.pyindex 48de1ce7..f3ed251a 100644--- a/moviepy/video/fx/FadeIn.py+++ b/moviepy/video/fx/FadeIn.py@@ -12,7 +12,7 @@ class FadeIn(Effect):masks too, where the initial color must be a number between 0 and 1.For cross-fading (progressive appearance or disappearance of a clip- over another clip, see ``transfx.crossfadein``+ over another clip, see ``CrossFadeIn``"""duration: floatdiff --git a/moviepy/video/fx/FadeOut.py b/moviepy/video/fx/FadeOut.pyindex d1937fb2..b2c42a93 100644--- a/moviepy/video/fx/FadeOut.py+++ b/moviepy/video/fx/FadeOut.py@@ -4,14 +4,14 @@ from moviepy.Clip import Clipfrom moviepy.Effect import Effectfrom dataclasses import dataclass-+@dataclassclass FadeOut(Effect):"""Makes the clip progressively fade to some color (black by default),over ``duration`` seconds at the end of the clip. Can be used for masks too,where the final color must be a number between 0 and 1.For cross-fading (progressive appearance or disappearance of a clip over another- clip, see ``transfx.crossfadeout``+ clip), see ``CrossFadeOut``"""duration: floatdiff --git a/moviepy/video/fx/SlideIn.py b/moviepy/video/fx/SlideIn.pynew file mode 100644index 00000000..bc5507b0--- /dev/null+++ b/moviepy/video/fx/SlideIn.py@@ -0,0 +1,58 @@+from moviepy.Clip import Clip+from moviepy.Effect import Effect+from dataclasses import dataclass++@dataclass+class SlideIn(Effect):+ """Makes the clip arrive from one side of the screen.++ Only works when the clip is included in a CompositeVideoClip,+ and if the clip has the same size as the whole composition.++ Parameters+ ----------++ clip : moviepy.Clip.Clip+ A video clip.++ duration : float+ Time taken for the clip to be fully visible++ side : str+ Side of the screen where the clip comes from. One of+ 'top', 'bottom', 'left' or 'right'.++ Examples+ --------++ >>> from moviepy import *+ >>>+ >>> clips = [... make a list of clips]+ >>> slided_clips = [+ ... CompositeVideoClip([clip.with_effect(vfx.SlideIn(1, "left"))])+ ... for clip in clips+ ... ]+ >>> final_clip = concatenate_videoclips(slided_clips, padding=-1)+ >>>+ >>> clip = ColorClip(+ ... color=(255, 0, 0), duration=1, size=(300, 300)+ ... ).with_fps(60)+ >>> final_clip = CompositeVideoClip([clip.with_effect(vfx.SlideIn(1, "right"))])+ """++ duration: float+ side: str++ def apply(self, clip: Clip) -> Clip:+ w, h = clip.size+ pos_dict = {+ "left": lambda t: (min(0, w * (t / self.duration - 1)), "center"),+ "right": lambda t: (max(0, w * (1 - t / self.duration)), "center"),+ "top": lambda t: ("center", min(0, h * (t / self.duration - 1))),+ "bottom": lambda t: ("center", max(0, h * (1 - t / self.duration))),+ }++ return clip.with_position(pos_dict[self.side])+++diff --git a/moviepy/video/fx/SlideOut.py b/moviepy/video/fx/SlideOut.pynew file mode 100644index 00000000..9d4d8804--- /dev/null+++ b/moviepy/video/fx/SlideOut.py@@ -0,0 +1,62 @@+from moviepy.Clip import Clip+from moviepy.Effect import Effect+from dataclasses import dataclass++@dataclass+class SlideOut(Effect):+ """Makes the clip goes away by one side of the screen.++ Only works when the clip is included in a CompositeVideoClip,+ and if the clip has the same size as the whole composition.++ Parameters+ ----------++ clip : moviepy.Clip.Clip+ A video clip.++ duration : float+ Time taken for the clip to be fully visible++ side : str+ Side of the screen where the clip goes. One of+ 'top', 'bottom', 'left' or 'right'.++ Examples+ --------++ >>> from moviepy import *+ >>>+ >>> clips = [... make a list of clips]+ >>> slided_clips = [+ ... CompositeVideoClip([clip.with_effect(vfx.SlideOut(1, "left"))])+ ... for clip in clips+ ... ]+ >>> final_clip = concatenate_videoclips(slided_clips, padding=-1)+ >>>+ >>> clip = ColorClip(+ ... color=(255, 0, 0), duration=1, size=(300, 300)+ ... ).with_fps(60)+ >>> final_clip = CompositeVideoClip([clip.with_effect(vfx.SlideOut(1, "right"))])+ """++ duration: float+ side: str++ def apply(self, clip: Clip) -> Clip:+ if clip.duration is None:+ raise ValueError("Attribute 'duration' not set")++ w, h = clip.size+ ts = clip.duration - self.duration # start time of the effect.+ pos_dict = {+ "left": lambda t: (min(0, w * (-(t - ts) / self.duration)), "center"),+ "right": lambda t: (max(0, w * ((t - ts) / self.duration)), "center"),+ "top": lambda t: ("center", min(0, h * (-(t - ts) / self.duration))),+ "bottom": lambda t: ("center", max(0, h * ((t - ts) / self.duration))),+ }++ return clip.with_position(pos_dict[self.side])+++diff --git a/moviepy/video/fx/__init__.py b/moviepy/video/fx/__init__.pyindex 057d2bb8..5a0de03e 100644--- a/moviepy/video/fx/__init__.py+++ b/moviepy/video/fx/__init__.py@@ -4,6 +4,8 @@ from moviepy.video.fx.AccelDecel import AccelDecelfrom moviepy.video.fx.BlackAndWhite import BlackAndWhitefrom moviepy.video.fx.Blink import Blinkfrom moviepy.video.fx.Crop import Crop+from moviepy.video.fx.CrossFadeIn import CrossFadeIn+from moviepy.video.fx.CrossFadeOut import CrossFadeOutfrom moviepy.video.fx.EvenSize import EvenSizefrom moviepy.video.fx.FadeIn import FadeInfrom moviepy.video.fx.FadeOut import FadeOut@@ -27,6 +29,8 @@ from moviepy.video.fx.Painting import Paintingfrom moviepy.video.fx.Resize import Resizefrom moviepy.video.fx.Rotate import Rotatefrom moviepy.video.fx.Scroll import Scroll+from moviepy.video.fx.SlideIn import SlideIn+from moviepy.video.fx.SlideOut import SlideOutfrom moviepy.video.fx.SuperSample import SuperSamplefrom moviepy.video.fx.TimeMirror import TimeMirrorfrom moviepy.video.fx.TimeSymmetrize import TimeSymmetrize@@ -37,6 +41,8 @@ __all__ = ("BlackAndWhite","Blink","Crop",+ "CrossFadeIn",+ "CrossFadeOut","EvenSize","FadeIn","FadeOut",@@ -60,6 +66,8 @@ __all__ = ("Resize","Rotate","Scroll",+ "SlideIn",+ "SlideOut","SuperSample","TimeMirror","TimeSymmetrize",