Skip to content

Commit

Permalink
Fixing improper video preview and show for composite video clip with …
Browse files Browse the repository at this point in the history
…clip inside that have masks
  • Loading branch information
osaajani committed Jul 20, 2023
1 parent 420b12f commit aa17db2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 35 deletions.
4 changes: 2 additions & 2 deletions moviepy/Clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def with_fps(self, fps, change_duration=False):
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()

Expand Down Expand Up @@ -671,4 +671,4 @@ def __mul__(self, n):

from moviepy.video.fx.Loop import Loop

return self.with_effects(Loop(n))
return self.with_effects([Loop(n)])
2 changes: 1 addition & 1 deletion moviepy/audio/fx/AudioNormalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def apply(self, clip: Clip) -> Clip:
if max_volume == 0:
return clip
else:
return clip.with_effects(MultiplyVolume(1 / max_volume))
return clip.with_effects([MultiplyVolume(1 / max_volume)])
26 changes: 17 additions & 9 deletions moviepy/video/VideoClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,17 +542,21 @@ def show(self, t=0, with_mask=True):
>>> 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()

Expand Down Expand Up @@ -1035,7 +1039,7 @@ def __matmul__(self, n):
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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -1522,6 +1526,10 @@ def find_optimum_font_size (text, font, stroke_width, align, spacing, width, hei

# 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)
Expand Down
4 changes: 2 additions & 2 deletions moviepy/video/fx/FreezeRegion.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def apply(self, clip: Clip) -> Clip:
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))
Expand All @@ -51,7 +51,7 @@ def apply(self, clip: Clip) -> Clip:

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])

Expand Down
18 changes: 9 additions & 9 deletions moviepy/video/fx/Resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,51 +85,51 @@ def filter(get_frame, t):
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__"):

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__"):

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'")

# From here, the resizing is constant (not a function of time), size=newsize
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
20 changes: 9 additions & 11 deletions moviepy/video/io/ffplay_previewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def show_frame(self, img_array):
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}"
)

Expand Down Expand Up @@ -94,7 +94,7 @@ def __exit__(self, exc_type, exc_value, traceback):
def ffplay_preview_video(
clip,
fps,
pixel_format=None,
pixel_format="rgb24",
audio_flag=None,
video_flag=None
):
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion moviepy/video/tools/credits.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def __init__(
)

# 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

Expand Down

0 comments on commit aa17db2

Please sign in to comment.