#๐Ÿ”’ MoviePy write_videofile is extremely slow

84 messages ยท Page 1 of 1 (latest)

celest cosmos
#

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 minute 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 or just Python in general not suited for something like this?

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

glass havenBOT
#

@celest cosmos

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

vocal mango
#

Show code, and timing info to show which 'step' is slow

meager crown
#

Note that specifying a bitrate requires ffmpeg to parse the video and reencode it to get the desired compression.

If you're only joining things together with the same framerate etc you could just "copy" the video with -c:v. That's very fast.

#

Example from a "remux" script I've got here:

ffmpeg -i demo.mp4 -map 0 -map -0:3 -c copy -fflags genpts demo2.mp4

Ignore the -fflags genpts and look at the map and copy options.

celest cosmos
meager crown
#

Then my suggestion will not help you.

celest cosmos
# vocal mango Show code, and timing info to show which 'step' is slow
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'])
celest cosmos
celest cosmos
tiny turret
celest cosmos
tiny turret
celest cosmos
tiny turret
celest cosmos
tiny turret
celest cosmos
tiny turret
#

and how long is it taking in total to finish?

celest cosmos
#

and 44mb

tiny turret
#

have you tried enabling hardware acceleration? Although I don't think this is necessary

tiny turret
celest cosmos
#

like this ffmpeg_params=['-b:v','10000k', '-c:v', 'h264_nvenc]?

celest cosmos
# tiny turret yeah
 b'[h264_nvenc @ 0000019609bdedc0] [Eval @ 000000217e5fdef0] Undefined constant or missing \'(\' in \'ultrafast\'\r\n[h264_nvenc @ 0000019609bdedc0] Unable to parse option value "ultrafast"\r\n[h264_nvenc @ 0000019609bdedc0] Error setting option preset to value ultrafast.\r\nError initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height\r\n'
tiny turret
#

instead of ultrafast

celest cosmos
#

there we go

#

now im at 7it/s

#

still pretty slow though

tiny turret
#

bits or megabits

celest cosmos
#

and still took a minute

celest cosmos
tiny turret
#

oh

#

hmm

celest cosmos
#

i think its the amount of frames

tiny turret
#

your write file doesn't specify

celest cosmos
tiny turret
celest cosmos
#

433

tiny turret
#

oh

celest cosmos
#

frames

#

60 fps is 1082 frames ๐Ÿ˜†

tiny turret
#

oh lmaoo

#

I thought you meant fps

#

uhh

#

are you using the built in ffmpeg installation for moviepy?

celest cosmos
tiny turret
tiny turret
#
from moviepy.config import change_settings

change_settings({"FFMPEG_BINARY":"path_to_ffmpeg"})

I think this should work

tiny turret
#

damn

#

I have no idea then

#

sorry

#

maybe try restarting lmao

celest cosmos
#

yeah i did that too

#

Do you know of any libraries like this that are much faster?

tiny turret
#

nope, sorry

#

oh

#

one last thing that might boost your speed a tiny bit

#

change -b:v, 10000k to -crf, 23(You can tweak this number)

celest cosmos
#

whats the number for?

tiny turret
#

lower is higher quality

#

crf is constant rate factor

#

just try with 23

celest cosmos
#

still no

#

darn

tiny turret
#

I don't know then

#

sorry

celest cosmos
#

thanks so much

#

I appreciate you trying

tiny turret
#

of course, I hope someone is able to help you

celest cosmos
#

have a good one!

tiny turret
#

you too

glass havenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.