High performance video edit in Python
Asked Answered
S

2

6

I'm trying to add watermark on very short part of the mp4 video. It has to be very, very fast. Now I tried to do it with moviepy Here is my code:

import moviepy.editor as mp

video = mp.VideoFileClip("video.mp4")
part1 = video.subclip(0,10)
part2 = video.subclip(10,15)
part3 = video.subclip(15,152.56)
logo = (mp.ImageClip("logo.png")
      .set_duration(part2.duration)
      .resize(height=50) # if you need to resize...
      .margin(right=8, top=8, opacity=0) # (optional) logo-border padding
      .set_pos(("right","top")))

partSubtitles = mp.CompositeVideoClip([part2, logo])
final_clip = mp.concatenate_videoclips([part1, partSubtitles, part3])
final_clip.write_videofile("my_concatenation.mp4")

Adding a logo and merging videos works nearly instantly, but writing to disc takes 1 min for 2 min video what is significantly too long. Do you know a way of editing only a few frames and save that much faster?

Secondly, after conversion, the new file is approximately 40% larger. Why? How to fix that?

Seattle answered 29/10, 2018 at 12:58 Comment(2)
There's probably not going to be a general solution that doesn't involve re-encoding the video file, which is probably why it takes so long and gets bigger (if moviepy isn't using as efficient encoding as the video originally had). Can you do this with something high-quality and performant, like ffmpeg?Abc
Adding a watermark (or any other visible, relevant and specific change of a video stream) requires a re-encoding of the input. That not only takes long but also adds noise to your stream. I don't think you will find a fast solution.Corvese
S
1

I tried FFMPEG but performance was also to low to make a video on request.

We bought a stronger and bigger server and now we are processing files all the time - to have always ready for new watchers. It's not a perfect solution but it's much more scalable.

To increase the rate of videos/h I didn't use moviepy but FFMPEG - 30% better performance, and less decreasing quality of video / increasing size of files.

Seattle answered 29/10, 2018 at 16:36 Comment(0)
A
1

Re-encoding a video is always going to be a slow process, and I doubt moviepy defaults to using (or even can use) a high-performance encoder. The fastest general solution is probably to use FFMPEG to do the entire edit, if at all possible. For example, here's a quick demonstration of how to add watermarks using FFMPEG. Using a low-level tool like that is probably your best chance to get high-performance editing, and if you need to execute it from Python, just call the ffmpeg command using subprocess.

Abc answered 29/10, 2018 at 13:36 Comment(1)
For what it's worth, moviepy is essentially a high-level abstraction that's using (among other things) FFMPEG underneath. There are likely still be efficiencies to be had, but it's not a completely different engine.Hypsometer
S
1

I tried FFMPEG but performance was also to low to make a video on request.

We bought a stronger and bigger server and now we are processing files all the time - to have always ready for new watchers. It's not a perfect solution but it's much more scalable.

To increase the rate of videos/h I didn't use moviepy but FFMPEG - 30% better performance, and less decreasing quality of video / increasing size of files.

Seattle answered 29/10, 2018 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.