Skip to content

Video editing with Python

Fixing improper video preview and show for composite video clip with clip inside that have masks

osaajani3a2e0715adb5
diff --git a/moviepy/Clip.py b/moviepy/Clip.py
index 8dc2008f..f39a774c 100644
--- a/moviepy/Clip.py
+++ b/moviepy/Clip.py
@@ -324,7 +324,7 @@ class Clip:
if change_duration:
from moviepy.video.fx.MultiplySpeed import MultiplySpeed
- newclip = self.with_effects(MultiplySpeed(fps / self.fps))
+ newclip = self.with_effects([MultiplySpeed(fps / self.fps)])
else:
newclip = self.copy()
@@ -671,4 +671,4 @@ class Clip:
from moviepy.video.fx.Loop import Loop
- return self.with_effects(Loop(n))
+ return self.with_effects([Loop(n)])
diff --git a/moviepy/audio/fx/AudioNormalize.py b/moviepy/audio/fx/AudioNormalize.py
index c0c190e5..77039afe 100644
--- a/moviepy/audio/fx/AudioNormalize.py
+++ b/moviepy/audio/fx/AudioNormalize.py
@@ -26,4 +26,4 @@ class AudioNormalize(Effect) :
if max_volume == 0:
return clip
else:
- return clip.with_effects(MultiplyVolume(1 / max_volume))
+ return clip.with_effects([MultiplyVolume(1 / max_volume)])
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index 4ff16b8d..2dae4de9 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -542,17 +542,21 @@ class VideoClip(Clip):
>>> from moviepy import *
>>>
>>> clip = VideoFileClip("media/chaplin.mp4")
- >>> clip.show(t=4, interactive=True)
+ >>> clip.show(t=4)
"""
clip = self.copy()
- if with_mask and (self.mask is not None):
- # Hate it, but cannot figure a better way with python awful circular dependency
- from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
- clip = CompositeVideoClip([self.with_position((0, 0))])
+ # Warning : Comment to fix a bug on preview for compositevideoclip
+ # it broke compositevideoclip and it does nothing on normal clip with alpha
- img = clip.get_frame(t)
- pil_img = Image.fromarray(img)
+ # if with_mask and (self.mask is not None):
+ # # Hate it, but cannot figure a better way with python awful circular dependency
+ # from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
+
+ # clip = CompositeVideoClip([self.with_position((0, 0))])
+
+ frame = clip.get_frame(t)
+ pil_img = Image.fromarray(frame)
pil_img.show()
@@ -1035,7 +1039,7 @@ class VideoClip(Clip):
return NotImplemented
from moviepy.video.fx.Rotate import Rotate
- return self.with_effects(Rotate(n))
+ return self.with_effects([Rotate(n)])
def __and__(self, mask):
return self.with_mask(mask)
@@ -1348,7 +1352,7 @@ class TextClip(ImageClip):
the text will be wrapped automagically.
text_align
- center | left | right. Text align similar to css. Default to ``center``.
+ center | left | right. Text align similar to css. Default to ``left``.
horizontal_align
center | left | right. Define horizontal align of text bloc in image.
@@ -1522,6 +1526,10 @@ class TextClip(ImageClip):
# Trace the image
img_mode = "RGBA" if transparent else "RGB"
+
+ if bg_color is None and transparent :
+ bg_color = (0, 0, 0, 0)
+
img = Image.new(img_mode, (img_width, img_height), color=bg_color)
pil_font = ImageFont.truetype(font, font_size)
draw = ImageDraw.Draw(img)
diff --git a/moviepy/video/fx/FreezeRegion.py b/moviepy/video/fx/FreezeRegion.py
index 210b2e74..bcacb3bd 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.region
freeze = (
- clip.with_effects(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_effects(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/Resize.py b/moviepy/video/fx/Resize.py
index 8d8aabf0..a0b9b837 100644
--- a/moviepy/video/fx/Resize.py
+++ b/moviepy/video/fx/Resize.py
@@ -85,12 +85,12 @@ 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_effects([Resize(clip.mask, self.new_size, apply_to_mask=False)])
+ newclip.mask = newclip.mask.with_effects([Resize(self.new_size, apply_to_mask=False)])
return newclip
else:
- new_size = translate_new_size(new_size)
+ self.new_size = translate_new_size(self.new_size)
elif self.height is not None:
if hasattr(self.height, "__call__"):
@@ -98,10 +98,10 @@ class Resize(Effect):
def func(t):
return 1.0 * int(self.height(t)) / h
- return clip.with_effects(Resize(func))
+ return clip.with_effects([Resize(func)])
else:
- new_size = [w * self.height / h, self.height]
+ self.new_size = [w * self.height / h, self.height]
elif self.width is not None:
if hasattr(self.width, "__call__"):
@@ -109,10 +109,10 @@ class Resize(Effect):
def func(t):
return 1.0 * self.width(t) / w
- return clip.with_effects(Resize(func))
+ return clip.with_effects([Resize(func)])
else:
- new_size = [self.width, h * self.width / w]
+ self.new_size = [self.width, h * self.width / w]
else:
raise ValueError("You must provide either 'new_size' or 'height' or 'width'")
@@ -120,16 +120,16 @@ class Resize(Effect):
if clip.is_mask:
def image_filter(pic):
- return 1.0 * self.resizer((255 * pic).astype("uint8"), new_size) / 255.0
+ return 1.0 * self.resizer((255 * pic).astype("uint8"), self.new_size) / 255.0
else:
def image_filter(pic):
- return self.resizer(pic.astype("uint8"), new_size)
+ return self.resizer(pic.astype("uint8"), self.new_size)
new_clip = clip.image_transform(image_filter)
if self.apply_to_mask and clip.mask is not None:
- new_clip.mask = clip.with_effects(Resize(clip.mask, self.new_size, apply_to_mask=False))
+ new_clip.mask = clip.mask.with_effects([Resize(self.new_size, apply_to_mask=False)])
return new_clip
diff --git a/moviepy/video/io/ffplay_previewer.py b/moviepy/video/io/ffplay_previewer.py
index 28a505df..5417a93c 100644
--- a/moviepy/video/io/ffplay_previewer.py
+++ b/moviepy/video/io/ffplay_previewer.py
@@ -66,7 +66,7 @@ class FFPLAY_VideoPreviewer:
ffplay_error = ffplay_error.decode()
error = (
- f"{err}\n\nMoviePy error: FFPALY encountered the following error while "
+ f"{err}\n\nMoviePy error: FFPLAY encountered the following error while "
f"previewing clip :\n\n {ffplay_error}"
)
@@ -94,7 +94,7 @@ class FFPLAY_VideoPreviewer:
def ffplay_preview_video(
clip,
fps,
- pixel_format=None,
+ pixel_format="rgb24",
audio_flag=None,
video_flag=None
):
@@ -111,8 +111,14 @@ def ffplay_preview_video(
Number of frames per seconds in the displayed video.
pixel_format : str, optional
+ Warning: This is not used anywhere in the code and should probably
+ be remove.
+ It is believed pixel format rgb24 does not work properly for now because
+ it require applying mask on CompositeVideoClip and thoses are believed to
+ not be working.
+
Pixel format for the output video file, ``rgb24`` for normal video, ``rgba``
- if video with mask.
+ if video with mask
audio_flag : Thread.Event, optional
A thread event that video will wait for. If not provided we ignore audio
@@ -121,9 +127,6 @@ def ffplay_preview_video(
A thread event that video will set after first frame has been shown. If not
provided, we simply ignore
"""
- if not pixel_format:
- pixel_format = "rgba" if clip.mask is not None else "rgb24"
-
with FFPLAY_VideoPreviewer(
clip.size,
@@ -134,11 +137,6 @@ def ffplay_preview_video(
for t, frame in clip.iter_frames(
with_times=True, fps=fps, dtype="uint8"
):
- if clip.mask is not None:
- mask = 255 * clip.mask.get_frame(t)
- if mask.dtype != "uint8":
- mask = mask.astype("uint8")
- frame = np.dstack([frame, mask])
previewer.show_frame(frame)
diff --git a/moviepy/video/tools/credits.py b/moviepy/video/tools/credits.py
index 56700856..9c356dfb 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_effects(Resize(width=width))
+ scaled = both_columns.with_effects([Resize(width=width)])
# Transform the CompositeVideoClip into an ImageClip