'FFMPEG with moviepy
I'm working on something that concatenate videos and adds some titles on through moviepy.
As I saw on the web and on my on pc moviepy works on the CPU and takes a lot of time to save(render) a movie. Is there a way to improve the speed by running the writing of moviepy on GPU? Like using FFmpeg or something like this?
I didn't find an answer to that on the web, so I hope that some of you can help me.
I tried using thread=4
and thread=16
but they are still very very slow and didn't change much.
My CPU is very strong (i7 10700k), but still, rendering on moviepy takes me for a compilation with a total of 8 minutes 40 seconds, which is a lot.
Any ideas?Thanks! the code doesnt realy matter but :
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')
Solution 1:[1]
in the past I was same problem and I solved that.
You need to use this command in your code ONLY one time.
video = CompositeVideoClip([clip1, clip2, clip3])
and export the video:
video.write_videofile(path_final_video)
while the video is export, moviepy will use all your cores.
Hope I helped :)
Solution 2:[2]
My gpu type is nvida, and I got 10 times faster with this command. you can try this:
echo y|ffmpeg -r 25 -i "a.mkv" -vcodec h264_nvenc "b.mp4"
If that not work, you can try else gpu accelerators:
-vcodec [accelerator_type]
# h264_nvenc
# hevc
# hevc_nvenc
# libx265
Run call in 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)
But this way is not work on moviepy's concat function because it's not support GPU. u'd better use ffmpeg to connact clips.
# 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
Reference:
Solution 3:[3]
I was able to significantly speed things up by trying different encoders.
You can type in the following command to get a list on your system:
ffmpeg -encoders
Then you can try each codec to see which one gives you the best result:
final.write_videofile(
filename,
threads=5,
bitrate="2000k",
audio_codec="aac",
codec="h264_videotoolbox",
)
For me h264_videotoolbox
worked the best, but your system may be different. To my understanding you would have h264_nvenc
if you were on an nvidia system.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Idan Cohen |
Solution 2 | |
Solution 3 | Rob |