Zulko / moviepy

Video editing with Python
https://zulko.github.io/moviepy/
MIT License
12.42k stars 1.55k forks source link

How to "animate" and concatenate static images ? #1989

Open leonardkrief opened 1 year ago

leonardkrief commented 1 year ago

I want to concatenate image files into 1 video. In order to make it more dynamic, I want to make the images "scroll". I tried to use the scrollfunction with no success. I found a way to make just 1 ImageClip move by using the set_position function. I still have 3 issues: 1) I cannot concatenate my "scrolling" ImageClips. For whatever reason, the result is very weird, like images are not in the right order, black screens, some images do not even appear... I commented out the composite.write_videofile('video.mp4') line, but you can uncomment it to look at 1 scrolled ImageClip object. 2) the scroll is jerky even though I set the fps to a big value 3) i dont know how to scroll "from a different point than the center of the image". Like for example, i would like the image to start by being slightly zoomed in and moved to its right, then it would scroll and end completely "scrolled to its left". I hope you get I mean. It would make it even more dynamic.

Is there a canonical way to achieve that with moviepy ? In most editing softwares, it is just a simple automation so I thought maybe there is a simpler way than what I am trying to do. If there is not, I would be grateful for some help to solve my 3 issues !

Here is my code, thanks for any help !

from moviepy.editor import ImageClip, concatenate_videoclips
from typing import Tuple
import random

def create_image_clips(
    image_paths: Tuple[str],
    duration: float,
    fade_duration: float = 0.35
    ) -> list:
    image_clips = []
    for i, image_path in enumerate(image_paths):
        clip = ImageClip(image_path, duration=duration)
        # clip = clip.resize(2)
        clip = clip.set_position(lambda t: (t * scroll_speed, t * scroll_speed))
        clip.fps = 120
        composite = CompositeVideoClip([clip], size=clip.size)
        # composite.write_videofile('video.mp4')
        if i != 0:
            clip = clip.crossfadein(fade_duration)
        clip = clip.crossfadeout(fade_duration)
        image_clips.append(clip)
    return image_clips

def combine_images_and_audio_to_video(
    image_paths: Tuple[str],
    image_duration=4,
    output_filename: str = "vid.mp4",
    codec: str = 'libx264') -> None:
    image_clips = create_image_clips(image_paths, image_duration)
    final_video = concatenate_videoclips(image_clips, method='compose')
    final_video.write_videofile(output_filename, codec=codec, logger=None, fps=30, verbose=False, threads=4, ffmpeg_params=None)
keikoro commented 1 year ago

Please always include your specs like we ask for in our issue templates – MoviePy version, platform used etc. – to help pinpoint what causes your problem, thanks.