我正在做一些东西,连接视频,并通过电影添加一些标题。
正如我在网络和pc上看到的,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')发布于 2020-09-19 23:12:08
我的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参考资料:
发布于 2021-06-03 11:36:37
通过尝试不同的编码器,我能够显著地提高速度。
您可以键入以下命令以获取系统上的列表:
ffmpeg -encoders然后你可以尝试每个编解码器,看看哪一个能给你最好的结果:
final.write_videofile(
filename,
threads=5,
bitrate="2000k",
audio_codec="aac",
codec="h264_videotoolbox",
)对我来说,h264_videotoolbox运行得最好,但你的系统可能不同。据我所知,如果你使用的是nvidia系统,你会有h264_nvenc。
https://stackoverflow.com/questions/63837260
复制相似问题