简体   繁体   中英

High performance video edit in Python

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?

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM