FFMPEG 与 moviepy

问题描述 投票:0回答:5

我正在研究一些可以连接视频并通过 moviepy 添加一些标题的东西。

正如我在网络和我的电脑上看到的,moviepy 在 CPU 上工作,需要花费大量时间来保存(渲染)电影。有没有办法通过在GPU上运行moviepy的写入来提高速度?喜欢使用 FFmpeg 或类似的东西?

我在网上没有找到答案,所以我希望你们中的一些人可以帮助我。 我尝试使用

thread=4
thread=16
但它们仍然非常非常慢并且没有太大变化。

我的CPU很强(i7 10700k),但是在moviepy上渲染需要我总共8分40秒的编译时间,这很长了。

有什么想法吗?谢谢! 代码并不重要,但是:

def Edit_Clips(self):

    clips = []

    time=0.0
    for i,filename in enumerate(os.listdir(self.path)):
        if filename.endswith(".mp4"):
            tempVideo=VideoFileClip(self.path + "\\" + filename)

            txt = TextClip(txt=self.arrNames[i], font='Amiri-regular',
                           color='white', fontsize=70)
            txt_col = txt.on_color(size=(tempVideo.w + txt.w, txt.h - 10),
                                   color=(0, 0, 0), pos=(6, 'center'), col_opacity=0.6)

            w, h = moviesize = tempVideo.size
            txt_mov = txt_col.set_pos(lambda t: (max(w / 30, int(w - 0.5 * w * t)),
                                                 max(5 * h / 6, int(100 * t))))

            sub=txt_mov.subclip(time,time+4)
            time = time + tempVideo.duration

            final=CompositeVideoClip([tempVideo,sub])

            clips.append(final)

    video = concatenate_videoclips(clips, method='compose')
    print("after")
    video.write_videofile(self.targetPath+"\\"+'test.mp4',threads=16,audio_fps=44100,codec = 'libx264')
python ffmpeg gpu cpu moviepy
5个回答
6
投票

通过尝试不同的编码器,我能够显着加快速度。

您可以输入以下命令来获取系统上的列表:

ffmpeg -encoders

然后您可以尝试每种编解码器,看看哪一个可以为您提供最佳结果:

final.write_videofile(
        filename,
        threads=5,
        bitrate="2000k",
        audio_codec="aac",
        codec="h264_videotoolbox",
    )

对我来说

h264_videotoolbox
效果最好,但您的系统可能会有所不同。据我了解,如果您使用的是 nvidia 系统,您将会拥有
h264_nvenc


3
投票

我在 Windows 上使用 GTX3070,并且

h264_nvenc
编解码器允许我使用我的 GPU。
clip.write_videofile(video_file, codec='h264_nvenc')


1
投票

过去我也遇到过同样的问题,我解决了。

您只需在代码中使用此命令一次。

video = CompositeVideoClip([clip1, clip2, clip3])

并导出视频:

video.write_videofile(path_final_video)

导出视频时,moviepy 将使用您的所有核心。

希望我有帮助:)


0
投票

我的 GPU 类型是 nvida,使用此命令速度提高了 10 倍。 你可以试试这个:

echo y|ffmpeg -r 25 -i "a.mkv" -vcodec h264_nvenc  "b.mp4"

如果这不起作用,您可以尝试其他 GPU 加速器:

-vcodec [accelerator_type]
# h264_nvenc
# hevc
# hevc_nvenc
# libx265

在 python 中运行调用(win 10):

input = 'a.mkv'
output = 'b.mp4'

call = "echo y|ffmpeg -r 25 -i \"%s\" -vcodec h264_nvenc  \"%s\"" % (input, output)
call

import os
os.system(call)

# subprocess.call or os.popen can get the call's return, 
# but if you want get the return at the same time,
# you should use this way:
import subprocess
pi= subprocess.Popen(call,shell=True,stdout=subprocess.PIPE)
for i in iter(pi.stdout.readline,'b'):
    print(i)

但是这种方式不适用于 moviepy 的 concat 函数,因为它不支持 GPU。 你最好使用ffmpeg来连接剪辑。

# concat_ffmpeg.bat
echo y|ffmpeg -i 1.mkv  -qscale 4 1.mpg
echo y|ffmpeg -i 2.mkv  -qscale 4 2.mpg
echo y|ffmpeg -i "concat:1.mpg|2.mpg" -c copy output.mp4

## sometimes can't use the [-c copy], u can try this and use GPU: 
# echo y|ffmpeg -i "concat:1.mpg|2.mpg" -vcodec h264_nvenc output.mp4

或者

Concatenating media files
.

参考:

How can I speed up moviepy by gpu? #923

ffmpeg 4.0 NVIDIA NVDEC-accelerated Support ? #790


0
投票

与评论相反,这对我来说是最快的

write_videofile(result_path, bitrate=bitrate, threads=64, verbose=False, preset='ultrafast', ffmpeg_params=['-loglevel', 'panic'], codec="libx264")

事情发生了。我认为我的处理器比我的显卡好得多。 render speed img

通过这种方式,我在渲染时能够达到125-130。

© www.soinside.com 2019 - 2024. All rights reserved.