【发布时间】:2014-12-31 16:41:39
【问题描述】:
相关:
How to extract audio from a video file using python?
Extract audio from video as wav
How to rip the audio from a video?
我的问题是如何从视频文件中提取 wav 音轨,比如video.avi?
我阅读了很多文章,人们到处都建议使用(来自 Python)ffmpeg 作为子进程(因为没有可靠的 python 绑定到 ffmpeg - 唯一的希望是PyFFmpeg,但我发现它现在没有维护)。我不知道这是否是正确的解决方案,我正在寻找好的解决方案。
我查看了 gstreamer,发现它很好,但无法满足我的需求——我发现从命令行完成此操作的唯一方法是
gst-launch-0.10 playbin2 uri=file://`pwd`/ex.mp4 audio-sink='identity single-segment=true ! audioconvert ! audio/x-raw-int, endianness=(int)1234, signed=(boolean)true, width=(int)16, depth=(int)16, rate=(int)16000, channels=(int)1 ! wavenc ! filesink location=foo.wav’
但这效率不高,因为我在播放视频并同时写入 wav 文件时需要等待很长时间。
ffmpeg 好多了:
avconv -i foo.mp4 -ab 160k -ac 1 -ar 16000 -vn ffaudio.wav
但我无法从 python 启动它(不是作为命令行子进程)。您能否指出从 python 启动 ffmpeg 作为命令行实用程序的优缺点? (我的意思是使用 python multiprocessing 模块或类似的东西)。
还有第二个问题。
有什么简单的方法可以将长 wav 文件切成小块,这样我就不会破坏任何单词?我的意思是 10-20 秒长度的片段,在句子/单词的暂停期间开始和结束?
我知道如何将它们分解成任意部分:
import wave
win= wave.open('ffaudio.wav', 'rb')
wout= wave.open('ffsegment.wav', 'wb')
t0, t1= 2418, 2421 # cut audio between 2413, 2422 seconds
s0, s1= int(t0*win.getframerate()), int(t1*win.getframerate())
win.readframes(s0) # discard
frames= win.readframes(s1-s0)
wout.setparams(win.getparams())
wout.writeframes(frames)
win.close()
wout.close()
【问题讨论】:
-
您提到了
ffmpeg,但您使用的是avconv。 -
请参阅stackoverflow.com/questions/9477115/…。它们是不同的项目,并且相互替代。
avconvif fork offfmpeg这样做是为了与FFmpeg project保持距离。 -
如果您在 ubuntu 中启动 ffmeg,您将看到如下消息:
The ffmpeg program is only provided for script compatibility and will be removed in a future release. It has been deprecated in the Libav project to allow for incompatible command line syntax improvements in its replacement called avconv. Please use avconv instead. -
按照这里的建议探索moviepy库by Daweo
标签: python audio video ffmpeg gstreamer