Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 432
R.Slaby Avatar asked Oct 29 '18 12:10

R.Slaby


People also ask

Is Python good for video editing?

It provides functions for cutting, concatenations, title insertions, video compositing, video processing, and the creation of custom effects. It can read and write common video and audio formats and be run on any platform with Python 2.7 or 3+.

Which language is best for video editing?

First of all, the editing and handling of video files, which requires very fast routines. It also needs to be able to handle memory very well. The C++ language would do very well here, although I might prefer to go even lower and use Standard C instead.

Can you automate video editing?

Can you automate video editing? Absolutely, you can! With the help of AI, you can begin automating your video editing, as much or as little as you'd like.


1 Answers

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.

like image 154
scnerd Avatar answered Sep 28 '22 03:09

scnerd