Skip to content

Video editing with Python

Add shortcut for Resize, Rotate, Crop, MultiplyVolume and MultiplySpeed to audioclip/videoclip

osaajani49a902315f25
diff --git a/moviepy/Clip.py b/moviepy/Clip.py
index a31cf3c4..8dc2008f 100644
--- a/moviepy/Clip.py
+++ b/moviepy/Clip.py
@@ -458,6 +458,21 @@ class Clip:
return new_clip.with_duration(self.duration - (end_time - start_time))
else: # pragma: no cover
return new_clip
+
+ def with_multiply_speed(self, factor: float = None, final_duration: float = None) :
+ """Returns a clip playing the current clip but at a speed multiplied by ``factor``.
+ For info on the parameters, please see ``vfx.MultiplySpeed``
+ """
+ from moviepy.video.fx.MultiplySpeed import MultiplySpeed
+ return self.with_effects([MultiplySpeed(factor=factor, final_duration=final_duration)])
+
+ def with_multiply_volume(self, factor: float, start_time = None, end_time = None) :
+ """Returns a new clip with audio volume multiplied by the value `factor`.
+ For info on the parameters, please see ``afx.MultiplyVolume``
+ """
+ from moviepy.audio.fx.MultiplyVolume import MultiplyVolume
+ return self.with_effects([MultiplyVolume(factor=factor, start_time=start_time,
+ end_time=end_time)])
@requires_duration
@use_clip_fps_by_default
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index 9a47943d..4e44e04c 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -21,6 +21,12 @@ from typing import Union, List, TYPE_CHECKING
if TYPE_CHECKING:
from moviepy.Effect import Effect
+from moviepy.video.fx.Resize import Resize
+from moviepy.video.fx.Rotate import Rotate
+from moviepy.video.fx.Crop import Crop
+from moviepy.audio.fx.MultiplyVolume import MultiplyVolume
+from moviepy.video.fx.MultiplySpeed import MultiplySpeed
+
from moviepy.Clip import Clip
from moviepy.decorators import (
add_mask_if_none,
@@ -916,6 +922,38 @@ class VideoClip(Clip):
"""
self.layer = layer
+ def resized(self, new_size=None, height=None, width=None, apply_to_mask=True):
+ """Returns a video clip that is a resized version of the clip.
+ For info on the parameters, please see ``vfx.Resize``
+ """
+ return self.with_effects([Resize(new_size=new_size, height=height,
+ width=width, apply_to_mask=apply_to_mask)])
+
+ def rotated(self, angle: float, unit: str = "deg", resample: str = "bicubic",
+ expand: bool = False, center: tuple = None, translate: tuple = None,
+ bg_color: tuple = None) :
+ """Rotates the specified clip by ``angle`` degrees (or radians) anticlockwise
+ If the angle is not a multiple of 90 (degrees) or ``center``, ``translate``,
+ and ``bg_color`` are not ``None``.
+ For info on the parameters, please see ``vfx.Rotate``
+ """
+ return self.with_effects([Rotate(angle=angle, unit=unit, resample=resample,
+ expand=expand, center=center, translate=translate,
+ bg_color=bg_color)])
+
+ def cropped(self, x1: int = None, y1: int = None, x2: int = None, y2: int = None,
+ width: int = None, height: int = None, x_center: int = None,
+ y_center: int = None) :
+ """Returns a new clip in which just a rectangular subregion of the
+ original clip is conserved. x1,y1 indicates the top left corner and
+ x2,y2 is the lower right corner of the croped region.
+ All coordinates are in pixels. Float numbers are accepted.
+ For info on the parameters, please see ``vfx.Crop``
+ """
+ return self.with_effects([Crop(x1=x1, y1=y1, x2=x2, y2=y2,
+ width=width, height=height, x_center=x_center, y_center=y_center)])
+
+
# --------------------------------------------------------------
# CONVERSIONS TO OTHER TYPES
diff --git a/moviepy/video/fx/Resize.py b/moviepy/video/fx/Resize.py
index febbb910..8d8aabf0 100644
--- a/moviepy/video/fx/Resize.py
+++ b/moviepy/video/fx/Resize.py
@@ -2,42 +2,44 @@ import numbers
from PIL import Image
import numpy as np
from moviepy.Effect import Effect
+from dataclasses import dataclass
+from typing import Union
-
+@dataclass
class Resize(Effect):
+ """Returns a video clip that is a resized version of the clip.
+
+ Parameters
+ ----------
+
+ new_size : tuple or float or function, optional
+ Can be either
+ - ``(width, height)`` in pixels or a float representing
+ - A scaling factor, like ``0.5``.
+ - A function of time returning one of these.
+
+ height : int, optional
+ Height of the new clip in pixels. The width is then computed so
+ that the width/height ratio is conserved.
+
+ width : int, optional
+ Width of the new clip in pixels. The height is then computed so
+ that the width/height ratio is conserved.
+
+ Examples
+ --------
+
+ >>> 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
+ """
+
+ new_size: Union[tuple, float, callable] = None
+ height: int = None
+ width: int = None
+ apply_to_mask: bool = True
- def __init__(self, new_size=None, height=None, width=None, apply_to_mask=True) :
- """Returns a video clip that is a resized version of the clip.
-
- Parameters
- ----------
-
- new_size : tuple or float or function, optional
- Can be either
- - ``(width, height)`` in pixels or a float representing
- - A scaling factor, like ``0.5``.
- - A function of time returning one of these.
-
- width : int, optional
- Width of the new clip in pixels. The height is then computed so
- that the width/height ratio is conserved.
-
- height : int, optional
- Height of the new clip in pixels. The width is then computed so
- that the width/height ratio is conserved.
-
- Examples
- --------
-
- >>> 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_size
- self.height = height
- self.width = width
- self.apply_to_mask = apply_to_mask
def resizer(self, pic, new_size):
new_size = list(map(int, new_size))[::-1]
diff --git a/moviepy/video/fx/Rotate.py b/moviepy/video/fx/Rotate.py
index 69980f87..3274593b 100644
--- a/moviepy/video/fx/Rotate.py
+++ b/moviepy/video/fx/Rotate.py
@@ -12,8 +12,8 @@ class Rotate(Effect) :
"""
Rotates the specified clip by ``angle`` degrees (or radians) anticlockwise
If the angle is not a multiple of 90 (degrees) or ``center``, ``translate``,
- and ``bg_color`` are not ``None``, the package ``pillow`` must be installed,
- and there will be black borders. You can make them transparent with:
+ and ``bg_color`` are not ``None``, there will be black borders.
+ You can make them transparent with:
>>> new_clip = clip.with_add_mask().rotate(72)