[Python] MoviePy: CompositeVideoClip generates blank, unplayable video

Discussão em 'Python' iniciado por Stack, Abril 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to generate a video: the image at the top, the text in the center and the video at the bottom. I don't want any gap between them. The image is resized to become the first half, so the height is set to 960 (the final video is 1080 x 1920). Similarly, the video is resized and cropped. The text should be on top of the whole thing. But what it generates is a blank, unplayable video (2570 x 960: why is the width 2570?); no errors, either. I've tried many combinations over the past few days, but none worked.

    Here's the code:

    import cv2
    from moviepy.editor import VideoFileClip, concatenate_videoclips, TextClip, ImageClip, CompositeVideoClip, clips_array
    from moviepy.video.fx.crop import crop
    from moviepy.video.fx.resize import resize

    from moviepy.config import change_settings
    change_settings({"IMAGEMAGICK_BINARY": r"E:\User\ImageMagick\ImageMagick-7.1.1-Q16-HDRI\magick.exe"})

    def generate_video(image_path, video_path, output_path, text, text_options: dict = None):

    image_clip = ImageClip(image_path)
    image_clip = resize(image_clip, width=1080, height=960)

    video_clip = VideoFileClip(video_path)
    video_clip = resize(video_clip, height=960)
    video_clip = crop(video_clip, x_center=video_clip.w/2, width=1080)

    if text_options is None:
    text_options = {'fontsize': 30, 'color': 'white', 'bg_color': 'black', 'font': 'Arial'}

    text_clip = TextClip(text, text_options)
    text_clip = text_clip.set_position(("center", "center"))

    image_clip = image_clip.set_duration(video_clip.duration)
    text_clip = text_clip.set_duration(video_clip.duration)

    image_clip = image_clip.set_position((0.0, 0.0), relative=True)
    video_clip = video_clip.set_position((0.0, 0.5), relative=True)

    final_clip = CompositeVideoClip([image_clip, text_clip, video_clip])

    final_clip.write_videofile(output_path, codec="libx264", audio=False)

    image_clip.close()
    text_clip.close()
    video_clip.close()


    It's worked before (so the installation is fine; when the script was smaller), the paths are correct, and I've called the function with valid arguments. Why doesn't this code work? I'm also curious to know how the code would be if clips_array or concatenate_videoclips were used (the output should be exactly the same).

    Continue reading...

Compartilhe esta Página