Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CompositeVideoClip mask [1, 1] unexpected shape - Issue #2247 #2262

Closed
wants to merge 1 commit into from

Conversation

Implosiv3
Copy link
Contributor

@Implosiv3 Implosiv3 commented Nov 27, 2024

Fixes the issue when concatenating videoclips as mask is being considered as [1, 1] so the mask shape is different from the other videos and fails.

I think [1, 1] could be a default mask to avoid divisions by zero or something similar, but here it does not make sense.

Issue here: #2247

  • I have provided code that clearly demonstrates the bug and that only works correctly when applying this fix
  • I have added suitable tests demonstrating a fixed bug or new/changed feature to the test suite in tests/
  • I have properly documented new or changed features in the documentation or in the docstrings
  • I have properly explained unusual or unexpected code in the comments around it

Fixes the issue when concatenating videoclips as mask is being considered as [1, 1] so the mask shape is different from the other videos and fails.

I think [1, 1] could be a default mask to avoid divisions by zero or something similar, but here it does not make sense.

Issue here: Zulko#2247
@Implosiv3 Implosiv3 changed the title Fix issue #2247 Fix CompositeVideoClip mask [1, 1] unexpected shape - Issue #2247 Nov 27, 2024
@Zulko
Copy link
Owner

Zulko commented Nov 27, 2024

Thank you! Would you have time to add a minimal test? Some test snippet that would fail on the last release, but succeed with that change?

@Implosiv3
Copy link
Contributor Author

Thank you! Would you have time to add a minimal test? Some test snippet that would fail on the last release, but succeed with that change?

If I try to execute this code below in which I try to concatenate one clip withou a mask and one with the default mask:

from moviepy import concatenate_videoclips, VideoFileClip

video_without_mask = VideoFileClip('prueba.mp4')
video_with_mask = VideoFileClip('prueba.mp4').with_mask()

concatenate_videoclips([video_without_mask, video_with_mask]).write_videofile('test.mp4')

This is the result I receive:

videos = concatenate_videos([
  File "<decorator-gen-68>", line 2, in write_videofile
  File "C:\Users\dania\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\decorators.py", line 53, in requires_duration      
    return func(clip, *args, **kwargs)
  File "<decorator-gen-67>", line 2, in write_videofile
  File "C:\Users\dania\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\decorators.py", line 143, in use_clip_fps_by_default
    return func(clip, *new_args, **new_kwargs)
  File "<decorator-gen-66>", line 2, in write_videofile
    return func(clip, *args, **kwargs)
  File "<decorator-gen-65>", line 2, in write_videofile
  File "C:\Users\dania\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\decorators.py", line 94, in wrapper
    return func(*new_args, **new_kwargs)
  File "C:\Users\dania\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\video\VideoClip.py", line 389, in write_videofile  
    ffmpeg_write_video(
  File "C:\Users\dania\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\video\io\ffmpeg_writer.py", line 263, in ffmpeg_write_video
    frame = np.dstack([frame, mask])
  File "C:\Users\dania\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\shape_base.py", line 715, in dstack
    return _nx.concatenate(arrs, 2)
ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 0, the array at index 
0 has size 1080 and the array at index 1 has size 1

If you apply the change, it works:

MoviePy - Done.
MoviePy - Writing video test.mp4

frame_index:  45%|█████████████████████████████████▉                                          | 111/249 [00:13<00:14,  9.21it/s, now=

As the first clip doesn't have any mask, the system applies one [1, 1] shape ColorClip default mask, which is incompatible with the [1920, 1080] of the other clip.

OsaAjani pushed a commit to OsaAjani/moviepy that referenced this pull request Jan 5, 2025
@OsaAjani
Copy link
Collaborator

OsaAjani commented Jan 5, 2025

closed by #2307, thanks for pointing it out !

@OsaAjani OsaAjani closed this Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants