简体   繁体   English

Python中的高性能视频编辑

[英]High performance video edit in Python

I'm trying to add watermark on very short part of the mp4 video. 我正在尝试在mp4视频的非常短的部分添加水印。 It has to be very, very fast. 它必须非常非常快。 Now I tried to do it with moviepy Here is my code: 现在我尝试用moviepy做这个是我的代码:

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. 添加徽标和合并视频几乎可以立即工作,但写入光盘需要1分钟,2分钟视频时间过长。 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. 其次,转换后,新文件大约增加了40%。 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. 重新编码视频总是一个缓慢的过程,我怀疑moviepy默认使用(甚至可以使用)高性能编码器。 The fastest general solution is probably to use FFMPEG to do the entire edit, if at all possible. 如果可能的话,最快的通用解决方案可能是使用FFMPEG进行整个编辑。 For example, here's a quick demonstration of how to add watermarks using FFMPEG . 例如, 这里是一个如何使用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 . 使用这样的低级工具可能是获得高性能编辑的最佳机会,如果需要从Python执行,只需使用进程调用ffmpeg命令即可。

I tried FFMPEG but performance was also to low to make a video on request. 我尝试过FFMPEG,但性能也很低,可根据要求制作视频。

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. 为了提高视频/ h的速度,我没有使用moviepy,而是使用FFMPEG - 性能提高30%,视频质量降低,文件大小增加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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