我们正在开发一个将视频转换为音频的项目,这是示例代码:
from converter import Converter
from moviepy.editor import *
c = Converter()
clipv = 'g.mp4'
clipc = VideoFileClip(clipv).subclip(0,20)
conv = c.convert(clipc, 'clip5.mp3', {'format':'mp3','audio':{'codec': 'mp3','bitrate':'22050','channels':1}})
for timecode in conv:
pass
Run Code Online (Sandbox Code Playgroud)
但是,它给了我这个错误
Traceback (most recent call last)
File "del.py", line 7, in <module>
for timecode in conv:
File "/usr/local/lib/python2.7/dist-packages/converter/__init__.py", line 181, in convert
if not os.path.exists(infile):
File "/usr/lib/python2.7/genericpath.py", line 18, in exists
os.stat(path)
TypeError: coercing to Unicode: need string or buffer, instance found
Run Code Online (Sandbox Code Playgroud)
当然,另一种选择是使用ffmpeg,但问题是在这种情况下视频是一个对象实例,到目前为止我还没有找到一种方法将对象实例从python传递给bash.
视频对象可以写成视频文件,但这会导致大量的时间浪费,因为转换发生在循环内.
一次又一次地编写视频文件非常耗时,以便从中轻松提取音频.
我将非常感谢任何可以帮助我解决上述错误的解决方案,或任何允许我将视频fileclip对象实例ffmpeg …
在 moviepy 上同时并行地将视频剪辑写入为视频文件,而无需等待该过程完成,
因此我将视频分成 5 秒的片段
n=0
p = 5
clip = mp.VideoFileClip(videofile).subclip(n, n+p)
Run Code Online (Sandbox Code Playgroud)
然后我给视频添加字幕,
x = 0
text2 = 'hello'+str(x)
text[x] = TextClip(text2, font='Amiri-regular',color='white',fontsize=24).set_duration(p).set_start(0)
Run Code Online (Sandbox Code Playgroud)
然后,我对前五个剪辑一次又一次地执行此操作,在第五个剪辑上,我将剪辑写入为视频文件
我想在后台继续写入时继续处理视频的其余部分,因此我使用多重处理,在编辑@Roland Smith建议的代码后,我使用:
if float.is_integer(float(x)/5.0) == True and x != 0:
text2 = concatenate(text.values())
textd = text2.on_color(size=(clip.w ,text2.h),color=(0,0,0), col_opacity=0.6).set_pos('bottom')
video3[n] = CompositeVideoClip([VideoFileClip(videofile).subclip(n,5+n), textd])
def audioclip(data):
outname = str(data)[-10:].strip('>') + '.mp4'
data.write_videofile(outname,fps=24, codec='libx264')
return outname
names = video3.values()
h = multiprocessing.Pool()
audiofiles = h.map(audioclip, names)
gc.collect()
n = n+p
x = x+1
Run Code Online (Sandbox Code Playgroud)
我已经进口了
from moviepy.editor import …Run Code Online (Sandbox Code Playgroud)