Video editing with Python
Move from with_effect to with_effects
osaajanif637b6acc4d9
diff --git a/moviepy/Clip.py b/moviepy/Clip.pyindex 4bdb1c9a..a31cf3c4 100644--- a/moviepy/Clip.py+++ b/moviepy/Clip.py@@ -9,7 +9,7 @@ from operator import addimport numpy as npimport proglog-from typing import Union, List, TYPE_CHECKING+from typing import List, TYPE_CHECKINGif TYPE_CHECKING:from moviepy.Effect import Effect@@ -185,18 +185,15 @@ class Clip:keep_duration=keep_duration,)- def with_effect(self, effects: Union['Effect', List['Effect']]):+ def with_effects(self, effects: List['Effect']):"""Return a copy of the current clip with the effects applied- >>> new_clip = clip.with_effect(vfx.Resize(0.2, method="bilinear"))+ >>> new_clip = clip.with_effects([vfx.Resize(0.2, method="bilinear")])You can also pass multiple effect as a list- >>> clip.with_effect([vfx.VolumeX(0.5), vfx.Resize(0.3), vfx.Mirrorx()])+ >>> clip.with_effects([afx.VolumeX(0.5), vfx.Resize(0.3), vfx.Mirrorx()])"""- if not isinstance(effects, list) :- effects = [effects]-new_clip = self.copy()for effect in effects :# We always copy effect before using it, see Effect.copy@@ -327,7 +324,7 @@ class Clip:if change_duration:from moviepy.video.fx.MultiplySpeed import MultiplySpeed- newclip = self.with_effect(MultiplySpeed(fps / self.fps))+ newclip = self.with_effects(MultiplySpeed(fps / self.fps))else:newclip = self.copy()@@ -659,4 +656,4 @@ class Clip:from moviepy.video.fx.Loop import Loop- return self.with_effect(Loop(n))+ return self.with_effects(Loop(n))diff --git a/moviepy/Effect.py b/moviepy/Effect.pyindex 7c9d1bab..125681cd 100644--- a/moviepy/Effect.py+++ b/moviepy/Effect.py@@ -1,6 +1,5 @@from abc import ABCMeta, abstractmethodfrom moviepy.Clip import Clip-from dataclasses import dataclassimport copy as _copyclass Effect(metaclass=ABCMeta):diff --git a/moviepy/__init__.py b/moviepy/__init__.pyindex 2edbb994..ac3f96ba 100644--- a/moviepy/__init__.py+++ b/moviepy/__init__.py@@ -2,13 +2,11 @@can be directly imported like `from moviepy import VideoFileClip`.In particular it loads all effects from the video.fx and audio.fx folders-and turns them into VideoClip and AudioClip methods, so that instead of-``clip.fx(vfx.resize, 2)`` or ``vfx.resize(clip, 2)``-you can write ``clip.resize(2)``.+and turns them into VideoClip and AudioClip methods, where method name is effect+name in snake_case preceeded by ``with_``"""import inspect-import copyfrom moviepy.audio import fx as afxfrom moviepy.audio.AudioClip import (@@ -42,23 +40,23 @@ import re# Black magic to transforms the effects into Clip methods so that# they can be called with clip.resize(width=500) instead of-# clip.with_effect(vfx.Resize(width=500))+# clip.with_effects([vfx.Resize(width=500)])# We use a lot of inspect + closure + setattr + python scope magicaudio_fxs = inspect.getmembers(afx, inspect.isclass)video_fxs = inspect.getmembers(vfx, inspect.isclass) + audio_fxsdef add_effect_as_method(to_klass, method_name, eklass) :def effect_call(clip, *args, **kwargs):- return clip.with_effect(eklass(*args, **kwargs).copy())+ return clip.with_effects([eklass(*args, **kwargs).copy()])setattr(to_klass, method_name, effect_call)for name, effect_class in video_fxs :- camel_name = re.sub('(?!^)([A-Z]+)', r'_\1', name).lower()+ camel_name = 'with_' + re.sub('(?!^)([A-Z]+)', r'_\1', name).lower()add_effect_as_method(VideoClip, camel_name, effect_class)for name, effect_class in audio_fxs :- camel_name = re.sub('(?!^)([A-Z]+)', r'_\1', name).lower()+ camel_name = 'with_' + re.sub('(?!^)([A-Z]+)', r'_\1', name).lower()add_effect_as_method(AudioClip, camel_name, effect_class)# Add display in notebook to video and audioclipdiff --git a/moviepy/audio/fx/AudioDelay.py b/moviepy/audio/fx/AudioDelay.pyindex 0cb346e8..3e9d0498 100644--- a/moviepy/audio/fx/AudioDelay.py+++ b/moviepy/audio/fx/AudioDelay.py@@ -34,16 +34,16 @@ class AudioDelay(Effect) :-------->>> from moviepy import *- >>> videoclip = AudioFileClip('myaudio.wav').with_effect(+ >>> videoclip = AudioFileClip('myaudio.wav').with_effects([... afx.AudioDelay(offset=.2, n_repeats=10, decayment=.2)- ... )+ ... ])>>> # stereo A note>>> make_frame = lambda t: np.array(... [np.sin(440 * 2 * np.pi * t), np.sin(880 * 2 * np.pi * t)]... ).T... clip = AudioClip(make_frame=make_frame, duration=0.1, fps=44100)- ... clip = clip.with_effect(afx.AudioDelay(offset=.2, n_repeats=11, decay=0))+ ... clip = clip.with_effects([afx.AudioDelay(offset=.2, n_repeats=11, decay=0)])"""offset: float = 0.2@@ -57,7 +57,7 @@ class AudioDelay(Effect) :[clip.copy(),*[- clip.with_start((rep + 1) * self.offset).with_effect(MultiplyVolume(decayments[rep + 1]))+ clip.with_start((rep + 1) * self.offset).with_effects([MultiplyVolume(decayments[rep + 1])])for rep in range(self.n_repeats)],]diff --git a/moviepy/audio/fx/AudioFadeIn.py b/moviepy/audio/fx/AudioFadeIn.pyindex ac1ef93b..1b8735c9 100644--- a/moviepy/audio/fx/AudioFadeIn.py+++ b/moviepy/audio/fx/AudioFadeIn.py@@ -22,7 +22,7 @@ class AudioFadeIn(Effect) :-------->>> clip = VideoFileClip("media/chaplin.mp4")- >>> clip.with_effect(AudioFadeIn("00:00:06"))+ >>> clip.with_effects([vfx.AudioFadeIn("00:00:06")])"""duration: floatdiff --git a/moviepy/audio/fx/AudioFadeOut.py b/moviepy/audio/fx/AudioFadeOut.pyindex 42dab8b6..325b9f87 100644--- a/moviepy/audio/fx/AudioFadeOut.py+++ b/moviepy/audio/fx/AudioFadeOut.py@@ -23,7 +23,7 @@ class AudioFadeOut(Effect) :-------->>> clip = VideoFileClip("media/chaplin.mp4")- >>> clip.with_effect(afx.AudioFadeOut("00:00:06"))+ >>> clip.with_effects([afx.AudioFadeOut("00:00:06")])"""duration: floatdiff --git a/moviepy/audio/fx/AudioLoop.py b/moviepy/audio/fx/AudioLoop.pyindex ef540d1d..f67caddb 100644--- a/moviepy/audio/fx/AudioLoop.py+++ b/moviepy/audio/fx/AudioLoop.py@@ -18,7 +18,7 @@ class AudioLoop(Effect) :>>> from moviepy import *>>> videoclip = VideoFileClip('myvideo.mp4')>>> music = AudioFileClip('music.ogg')- >>> audio = music.with_effect(afx.AudioLoop(duration=videoclip.duration))+ >>> audio = music.with_effects([afx.AudioLoop(duration=videoclip.duration)])>>> videoclip.with_audio(audio)"""diff --git a/moviepy/audio/fx/AudioNormalize.py b/moviepy/audio/fx/AudioNormalize.pyindex b78a395b..c0c190e5 100644--- a/moviepy/audio/fx/AudioNormalize.py+++ b/moviepy/audio/fx/AudioNormalize.py@@ -16,7 +16,7 @@ class AudioNormalize(Effect) :-------->>> from moviepy import *- >>> videoclip = VideoFileClip('myvideo.mp4').with_effect(afx.AudioNormalize())+ >>> videoclip = VideoFileClip('myvideo.mp4').with_effects([afx.AudioNormalize()])"""@@ -26,4 +26,4 @@ class AudioNormalize(Effect) :if max_volume == 0:return clipelse:- return clip.with_effect(MultiplyVolume(1 / max_volume))+ return clip.with_effects(MultiplyVolume(1 / max_volume))diff --git a/moviepy/audio/fx/MultiplyStereoVolume.py b/moviepy/audio/fx/MultiplyStereoVolume.pyindex fdf90d3b..c1789f16 100644--- a/moviepy/audio/fx/MultiplyStereoVolume.py+++ b/moviepy/audio/fx/MultiplyStereoVolume.py@@ -16,8 +16,8 @@ class MultiplyStereoVolume(Effect) :>>> from moviepy import AudioFileClip>>> music = AudioFileClip('music.ogg')- >>> audio_r = music.with_effect(afx.MultiplyStereoVolume(left=0, right=1)) # mute left channel/s- >>> audio_h = music.with_effect(afx.MultiplyStereoVolume(left=0.5, right=0.5))) # half audio+ >>> audio_r = music.with_effects([afx.MultiplyStereoVolume(left=0, right=1)]) # mute left channel/s+ >>> audio_h = music.with_effects([afx.MultiplyStereoVolume(left=0.5, right=0.5)]) # half audio"""left: float = 1diff --git a/moviepy/audio/fx/MultiplyVolume.py b/moviepy/audio/fx/MultiplyVolume.pyindex c327817b..419f251f 100644--- a/moviepy/audio/fx/MultiplyVolume.py+++ b/moviepy/audio/fx/MultiplyVolume.py@@ -32,11 +32,11 @@ class MultiplyVolume(Effect) :>>> from moviepy import AudioFileClip>>>>>> music = AudioFileClip('music.ogg')- >>> doubled_audio_clip = clip.with_effect(MultiplyVolume(2)) # doubles audio volume- >>> half_audio_clip = clip.with_effect(MultiplyVolume(0.5)) # half audio+ >>> doubled_audio_clip = clip.with_effects([afx.MultiplyVolume(2)]) # doubles audio volume+ >>> half_audio_clip = clip.with_effects([afx.MultiplyVolume(0.5)]) # half audio>>>>>> # silenced clip during one second at third- >>> silenced_clip = clip.with_effect(MultiplyVolume(0, start_time=2, end_time=3))+ >>> silenced_clip = clip.with_effects([afx.MultiplyVolume(0, start_time=2, end_time=3)])"""factor: floatdiff --git a/moviepy/decorators.py b/moviepy/decorators.pyindex 97447479..b3974528 100644--- a/moviepy/decorators.py+++ b/moviepy/decorators.py@@ -75,22 +75,6 @@ def audio_video_effect(func, effect, clip, *args, **kwargs):return clipelse:return func(effect, clip, *args, **kwargs)--def audio_video_fx(func, clip, *args, **kwargs):- """Use an audio function on a video/audio clip.-- This decorator tells that the function func (audioclip -> audioclip)- can be also used on a video clip, at which case it returns a- videoclip with unmodified video and modified audio.- """- if hasattr(clip, "audio"):- new_clip = clip.copy()- if clip.audio is not None:- new_clip.audio = func(clip.audio, *args, **kwargs)- return new_clip- else:- return func(clip, *args, **kwargs)def preprocess_args(fun, varnames):diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.pyindex 26f3ac96..9a47943d 100644--- a/moviepy/video/VideoClip.py+++ b/moviepy/video/VideoClip.py@@ -16,7 +16,10 @@ from PIL import Image, ImageDraw, ImageFontimport threadingfrom moviepy.video.io.ffplay_previewer import ffplay_preview_video-from typing import List+from typing import Union, List, TYPE_CHECKING++if TYPE_CHECKING:+ from moviepy.Effect import Effectfrom moviepy.Clip import Clipfrom moviepy.decorators import (@@ -618,7 +621,7 @@ class VideoClip(Clip):# -----------------------------------------------------------------# F I L T E R I N G- def subfx(self, fx, start_time=0, end_time=None, **kwargs):+ def with_sub_effect(self, effects: List['Effect'], start_time=0, end_time=None, **kwargs):"""Apply a transformation to a part of the clip.Returns a new clip in which the function ``fun`` (clip->clip)@@ -630,11 +633,11 @@ class VideoClip(Clip):>>> # The scene between times t=3s and t=6s in ``clip`` will be>>> # be played twice slower in ``new_clip``- >>> new_clip = clip.subapply(lambda c:c.multiply_speed(0.5) , 3,6)+ >>> new_clip = clip.with_sub_effect(MultiplySpeed(0.5), 3, 6)"""left = None if (start_time == 0) else self.with_subclip(0, start_time)- center = self.with_subclip(start_time, end_time).fx(fx, **kwargs)+ center = self.with_subclip(start_time, end_time).with_effects(effects, **kwargs)right = None if (end_time is None) else self.with_subclip(start_time=end_time)clips = [clip for clip in [left, center, right] if clip is not None]@@ -644,6 +647,7 @@ class VideoClip(Clip):return concatenate_videoclips(clips).with_start(self.start)+# IMAGE FILTERSdef image_transform(self, image_func, apply_to=None):@@ -993,7 +997,7 @@ class VideoClip(Clip):return NotImplementedfrom moviepy.video.fx.Rotate import Rotate- return self.with_effect(Rotate(n))+ return self.with_effects(Rotate(n))def __and__(self, mask):return self.with_mask(mask)diff --git a/moviepy/video/fx/CrossFadeIn.py b/moviepy/video/fx/CrossFadeIn.pyindex 04a01be4..2712eb94 100644--- a/moviepy/video/fx/CrossFadeIn.py+++ b/moviepy/video/fx/CrossFadeIn.py@@ -20,7 +20,7 @@ class CrossFadeIn(Effect):clip = clip.with_add_mask()clip.mask.duration = clip.duration- clip.mask = clip.mask.with_effect(FadeIn(self.duration))+ clip.mask = clip.mask.with_effects([FadeIn(self.duration)])return clipdiff --git a/moviepy/video/fx/CrossFadeOut.py b/moviepy/video/fx/CrossFadeOut.pyindex c492f400..de10ca49 100644--- a/moviepy/video/fx/CrossFadeOut.py+++ b/moviepy/video/fx/CrossFadeOut.py@@ -20,7 +20,7 @@ class CrossFadeOut(Effect):clip = clip.with_add_mask()clip.mask.duration = clip.duration- clip.mask = clip.mask.with_effect(FadeOut(self.duration))+ clip.mask = clip.mask.with_effects([FadeOut(self.duration)])return clipdiff --git a/moviepy/video/fx/FreezeRegion.py b/moviepy/video/fx/FreezeRegion.pyindex b4b0dc0c..210b2e74 100644--- a/moviepy/video/fx/FreezeRegion.py+++ b/moviepy/video/fx/FreezeRegion.py@@ -42,7 +42,7 @@ class FreezeRegion(Effect):if self.region is not None:x1, y1, x2, y2 = self.regionfreeze = (- clip.with_effect(Crop(*self.region))+ clip.with_effects(Crop(*self.region)).to_ImageClip(t=self.t).with_duration(clip.duration).with_position((x1, y1))@@ -51,7 +51,7 @@ class FreezeRegion(Effect):elif self.outside_region is not None:x1, y1, x2, y2 = self.outside_region- animated_region = clip.with_effect(Crop(*self.outside_region)).with_position((x1, y1))+ animated_region = clip.with_effects(Crop(*self.outside_region)).with_position((x1, y1))freeze = clip.to_ImageClip(t=self.t).with_duration(clip.duration)return CompositeVideoClip([freeze, animated_region])diff --git a/moviepy/video/fx/MasksAnd.py b/moviepy/video/fx/MasksAnd.pyindex 70d20bf3..878e892c 100644--- a/moviepy/video/fx/MasksAnd.py+++ b/moviepy/video/fx/MasksAnd.py@@ -22,9 +22,9 @@ class MasksAnd(Effect) :Examples--------- >>> clip = ColorClip(color=(255, 0, 0), size=(1, 1)) # red- >>> mask = ColorClip(color=(0, 255, 0), size=(1, 1)) # green- >>> masked_clip = clip.fx(mask_and, mask) # black+ >>> clip = ColorClip(color=(255, 0, 0), size=(1, 1)) # red+ >>> mask = ColorClip(color=(0, 255, 0), size=(1, 1)) # green+ >>> masked_clip = clip.with_effects([vfx.MasksAnd(mask)]) # black>>> masked_clip.get_frame(0)[[[0 0 0]]]"""diff --git a/moviepy/video/fx/MasksOr.py b/moviepy/video/fx/MasksOr.pyindex 1880d48a..3a931709 100644--- a/moviepy/video/fx/MasksOr.py+++ b/moviepy/video/fx/MasksOr.py@@ -22,9 +22,9 @@ class MasksOr(Effect) :Examples--------- >>> clip = ColorClip(color=(255, 0, 0), size=(1, 1)) # red- >>> mask = ColorClip(color=(0, 255, 0), size=(1, 1)) # green- >>> masked_clip = clip.fx(mask_or, mask) # yellow+ >>> clip = ColorClip(color=(255, 0, 0), size=(1, 1)) # red+ >>> mask = ColorClip(color=(0, 255, 0), size=(1, 1)) # green+ >>> masked_clip = clip.with_effects([vfx.MasksOr(mask)]) # yellow>>> masked_clip.get_frame(0)[[[255 255 0]]]"""diff --git a/moviepy/video/fx/Resize.py b/moviepy/video/fx/Resize.pyindex 32d24cb8..febbb910 100644--- a/moviepy/video/fx/Resize.py+++ b/moviepy/video/fx/Resize.py@@ -29,10 +29,10 @@ class Resize(Effect):Examples--------- >>> myClip.with_effect(vfx.Resize((460,720))) # New resolution: (460,720)- >>> myClip.with_effect(vfx.Resize(0.6)) # width and height multiplied by 0.6- >>> myClip.with_effect(vfx.Resize(width=800)) # height computed automatically.- >>> myClip.with_effect(vfx.Resize(lambda t : 1+0.02*t)) # slow swelling of the clip+ >>> myClip.with_effects([vfx.Resize((460,720))]) # New resolution: (460,720)+ >>> myClip.with_effects([vfx.Resize(0.6)]) # width and height multiplied by 0.6+ >>> myClip.with_effects([vfx.Resize(width=800)]) # height computed automatically.+ >>> myClip.with_effects([vfx.Resize(lambda t : 1+0.02*t)]) # slow swelling of the clip"""self.new_size = new_sizeself.height = height@@ -83,7 +83,7 @@ class Resize(Effect):filter, keep_duration=True, apply_to=(["mask"] if self.apply_to_mask else []))if self.apply_to_mask and clip.mask is not None:- newclip.mask = newclip.mask.with_effect(Resize(clip.mask, self.new_size, apply_to_mask=False))+ newclip.mask = newclip.mask.with_effects([Resize(clip.mask, self.new_size, apply_to_mask=False)])return newclip@@ -96,7 +96,7 @@ class Resize(Effect):def func(t):return 1.0 * int(self.height(t)) / h- return clip.with_effect(Resize(func))+ return clip.with_effects(Resize(func))else:new_size = [w * self.height / h, self.height]@@ -107,7 +107,7 @@ class Resize(Effect):def func(t):return 1.0 * self.width(t) / w- return clip.with_effect(Resize(func))+ return clip.with_effects(Resize(func))else:new_size = [self.width, h * self.width / w]@@ -128,6 +128,6 @@ class Resize(Effect):new_clip = clip.image_transform(image_filter)if self.apply_to_mask and clip.mask is not None:- new_clip.mask = clip.with_effect(Resize(clip.mask, self.new_size, apply_to_mask=False))+ new_clip.mask = clip.with_effects(Resize(clip.mask, self.new_size, apply_to_mask=False))return new_clipdiff --git a/moviepy/video/fx/SlideIn.py b/moviepy/video/fx/SlideIn.pyindex bc5507b0..26236eb8 100644--- a/moviepy/video/fx/SlideIn.py+++ b/moviepy/video/fx/SlideIn.py@@ -29,7 +29,7 @@ class SlideIn(Effect):>>>>>> clips = [... make a list of clips]>>> slided_clips = [- ... CompositeVideoClip([clip.with_effect(vfx.SlideIn(1, "left"))])+ ... CompositeVideoClip([clip.with_effects([vfx.SlideIn(1, "left")])])... for clip in clips... ]>>> final_clip = concatenate_videoclips(slided_clips, padding=-1)@@ -37,7 +37,7 @@ class SlideIn(Effect):>>> clip = ColorClip(... color=(255, 0, 0), duration=1, size=(300, 300)... ).with_fps(60)- >>> final_clip = CompositeVideoClip([clip.with_effect(vfx.SlideIn(1, "right"))])+ >>> final_clip = CompositeVideoClip([clip.with_effects([vfx.SlideIn(1, "right")])])"""duration: floatdiff --git a/moviepy/video/fx/SlideOut.py b/moviepy/video/fx/SlideOut.pyindex 9d4d8804..3e14f135 100644--- a/moviepy/video/fx/SlideOut.py+++ b/moviepy/video/fx/SlideOut.py@@ -29,7 +29,7 @@ class SlideOut(Effect):>>>>>> clips = [... make a list of clips]>>> slided_clips = [- ... CompositeVideoClip([clip.with_effect(vfx.SlideOut(1, "left"))])+ ... CompositeVideoClip([clip.with_effects([vfx.SlideOut(1, "left")])])... for clip in clips... ]>>> final_clip = concatenate_videoclips(slided_clips, padding=-1)@@ -37,7 +37,7 @@ class SlideOut(Effect):>>> clip = ColorClip(... color=(255, 0, 0), duration=1, size=(300, 300)... ).with_fps(60)- >>> final_clip = CompositeVideoClip([clip.with_effect(vfx.SlideOut(1, "right"))])+ >>> final_clip = CompositeVideoClip([clip.with_effects([vfx.SlideOut(1, "right")])])"""duration: floatdiff --git a/moviepy/video/tools/credits.py b/moviepy/video/tools/credits.pyindex b7d58798..56700856 100644--- a/moviepy/video/tools/credits.py+++ b/moviepy/video/tools/credits.py@@ -132,7 +132,7 @@ class CreditsClip(TextClip):)# Scale to the required size- scaled = both_columns.with_effect(Resize(width=width))+ scaled = both_columns.with_effects(Resize(width=width))# Transform the CompositeVideoClip into an ImageClip