PeppersONLY
PeppersONLY

Reputation: 1

MoviePy write_videofile is extremely slow

So I have am using MoviePy's concatenate_videoclips function to combine two video clips that have a image and some text overlayed on each of them. When I write the video file it takes around a WHOLE 2 minutes to complete. When I scaled it up with around 100 clips with the same info (a image and some text overlayed) it takes up to around 4 hours for just a 15 minute video. I am wondering if there is a way to mitigate this time. Is MoviePy not suited for something like this?

My example is this which runs at around 4it/s with both videos being at 1920x1080 and are both 10 secconds:

from moviepy.editor import *
from moviepy.config import change_settings

change_settings({"IMAGEMAGICK_BINARY": "D:\\ImageMagick-7.1.1-Q16-HDRI\\magick.exe"})

def create(vid_path: str) -> CompositeVideoClip:
    vid: VideoFileClip = VideoFileClip(vid_path)
    vid = vid.crossfadein(0.8)
    vid = vid.crossfadeout(0.8)

    name_square: ImageClip = ImageClip(".\\square.png")
    name_square = name_square.set_position(('center', 856))
    name_square = name_square.set_duration(vid.duration)
    name_square = name_square.crossfadein(1)
    name_square = name_square.crossfadeout(1)

    name_text: TextClip = TextClip(txt="Hello", fontsize=90, color="gray", font="MomCake")
    name_text = name_text.set_position(("center", 870))
    name_text = name_text.set_duration(vid.duration)
    name_text = name_text.crossfadein(1)
    name_text = name_text.crossfadeout(1)

    comp_clip: CompositeVideoClip = CompositeVideoClip([vid, name_text])
    return comp_clip

final: VideoFileClip = concatenate_videoclips([create("vid1.mp4"), create("vid2.mp4")])
final.write_videofile(f"final_vid.mp4", threads=12, preset="ultrafast", ffmpeg_params=['-b:v','10000k'])

My PC's specs are: Intel i7-9700K 32GB of memory NVIDIA GeForce RTX 2060

They seem like they should be more than enough for a job like this. It also seems that MoviePy doesn't utilize my GPU and puts all the video encoding on the CPU.

I use as many of MoviePy's ffmpeg functions as possible to speed things up but the write_videofile function is still very slow. I tried all optimizations I could find but none made a difference. With some of them I can get the iteration per seconds to go up by a significant amount but after a few seconds of processing, that number falls off a cliff and goes back to 3-5it/s. Some of the optimizations I've tried are:

threads=12 preset="ultrafast" audio=False logger=False ffmpeg_params=['-b:v','10000k']

Upvotes: 0

Views: 107

Answers (1)

VC.One
VC.One

Reputation: 15916

There might be a problem in using a function as an input parameter.
eg: Each check on the input means that the same long function must run each time.

Does it work faster if you setup part of your shown code like this?
(the logic is to create videos once, then later use them as inputs, without multiple recreates being done, for whenever a parameter like width or height is checked)

clip_1 = create("vid1.mp4")
clip_2 = create("vid2.mp4")
final: VideoFileClip = concatenate_videoclips([ clip_1, clip_2 ])
final.write_videofile(f"final_vid.mp4", threads=12, preset="ultrafast", ffmpeg_params=['-b:v','10000k'])

Upvotes: 0

Related Questions