bash python将视频转换为音频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33448759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:51:12  来源:igfitidea点击:

python converting video to audio

pythonbashaudiovideoffmpeg

提问by programmer44

We are working on a project to convert video to audio, and this is the sample code:

我们正在做一个将视频转换为音频的项目,这是示例代码:

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    

However, it gives me this error

但是,它给了我这个错误

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

Of course, the other alternative is to use ffmpeg, but the problem is that the video in this case is an object instance, and as of now I am yet to find a way of passing object instances from python to bash.

当然,另一种选择是使用ffmpeg,但问题是这种情况下的视频是一个对象实例,到目前为止,我还没有找到一种将对象实例从 python 传递到 bash 的方法。

The video object could be written as a video file, but that will lead to lots of time wastage, as the conversion takes place inside a loop.

视频对象可以写为视频文件,但这会导致大量时间浪费,因为转换是在循环内进行的。

It is quite time consuming to have to write the video file time and again, so as to easily extract audio from it.

必须一次又一次地编写视频文件,以便轻松地从中提取音频,这非常耗时。

I would highly appreciate any solution that will either help me get around the above error, or any that will allow me to pass the video fileclipobject instance to bash ffmpegas a variable.

我非常感谢任何可以帮助我解决上述错误的解决方案,或者任何允许我将视频fileclip对象实例ffmpeg作为变量传递给 bash的解决方案。

回答by Zulko

Try this:

尝试这个:

import moviepy.editor as mp
clip = mp.VideoFileClip("myvideo.mp4").subclip(0,20)
clip.audio.write_audiofile("theaudio.mp3")

You can add a lot of parameters in write_audiofile (format, codec, bitrate, fps, etc.)

可以在write_audiofile中添加很多参数(格式、编解码器、比特率、fps等)

回答by Ayoub Marouan

For those who get the following error while downloading "moviepy.editor" (NeedDownloadError: Need ffmpeg exe. You can download it by calling: imageio.plugins.ffmpeg.download())

对于那些在下载“moviepy.editor”时遇到以下错误的人(NeedDownloadError: Need ffmpeg exe。您可以调用:imageio.plugins.ffmpeg.download() 下载

Just do this :

只需这样做:

import imageio
imageio.plugins.ffmpeg.download()