Python从视频文件中提取wav [英] Python extract wav from video file

查看:1327
本文介绍了Python从视频文件中提取wav的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关:



如何使用python从视频文件中提取音频?



从视频中提取音频作为wav



如何从视频中撕下音频?



我的问题是如何从视频文件中提取wav音轨,例如 video.avi
我阅读了许多文章,人们建议使用(从Python)的每个地方,作为一个子进程(因为没有可靠的python绑定到ffmpeg),唯一的希望就在于ffmpeg PyFFmpeg ,但我发现现在是不明了的)。我不知道它是否是正确的解决方案,我正在寻找一个好的。

我看着gstreamer,发现它很好,但无法满足我的需要 - 我发现完成这一点的唯一方法命令行看起来像

  gst-launch-0.10 playbin2 uri = file://`pwd`/ex.mp4 audio-sink ='identity single-segment = true!音频转换器音频/ x-raw-int,endianness =(int)1234,signed =(boolean)true,width =(int)16,depth =(int)16,rate =(int)16000,channels =(int) wavenc! filesink location = foo.wav'

但是效率不高,因为我需要等待播放视频的时间并同时写入wav文件。



ffmpeg 更好:

  avconv -i foo.mp4 -ab 160k -ac 1 -ar 16000 -vn ffaudio.wav 

但是我无法从python(而不是命令行子进程)启动它。你能指出我从python启动ffmpeg作为命令行实用工具的优缺点吗? (我的意思是使用python 多处理模块或类似的东西)。



第二个问题。 >



什么是简单的方法将长wav文件切成块,以便我不会打破任何单词?我的意思是10-20秒的片段,在句子/单词的停顿期间开始和结束?



我知道如何打破任意的部分:

  import wave 


win = wave.open('ffaudio.wav','rb')
wout = wave.open('ffsegment.wav','wb')

t0,t1 = 2418,2421#在2413,2422秒之间剪切音频
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()


解决方案

使用 python 子过程使用 ffmpeg 是一个非常简单的任务,并且有一个原因让人们指向



这是从给定视频中提取音频的基本命令文件:


ffmpeg -i test.mp4 -ab 160k -ac 2 -ar 44100 -vn audio.wav


Python代码正在包装这个命令:

  import subprocess 

command =ffmpeg -i C :/test.mp4 -ab 160k -ac 2 -ar 44100 -vn audio.wav

subprocess.call(command,shell = True)
/ pre>

你必须确保ffmpeg是一个已知的任务,所以在系统环境变量的路径下,ffmpeg.exe的路径应该被列出,或者你可以在你的python代码中使用exe的完整路径。


Related:

How to extract audio from a video file using python?

Extract audio from video as wav

How to rip the audio from a video?

My question is how could I extract wav audio track from video file, say video.avi? I read many articles and everywhere people suggest to use (from Python) ffmpeg as a subprocess (because there are no reliable python bindings to ffmpeg - the only hope was PyFFmpeg but i found it is unmaintaned now). I don't know if it is right solution and i am looking for good one.
I looked to gstreamer and found it nice but unable to satisfy my needs -- the only way I found to accomplish this from command line looks like

 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’ 

But it is not efficient because i need to wait ages while playing video and simultaneously writing to wav file.

ffmpeg is much better:

avconv  -i foo.mp4  -ab 160k -ac 1 -ar 16000 -vn ffaudio.wav

But i am unable to launch it from python (not as a command line subprocess). Could you please point me out pros and cons of launching ffmpeg from python as a command line utility ? (I mean using python multiprocessing module or something similar).

And second question.

What is simple way to cut long wav file into pieces so that i don't break any words ? i mean pieces of 10-20 sec length with start and end during the pause in sentences/words ?

i know how to break them on arbitrary pieces:

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()

解决方案

It is a very easy Task using ffmpeg with python subprocess and there is a reason why people are pointing to this solution as a good solution.

This is the basic command extracting audio from a given video File:

ffmpeg -i test.mp4 -ab 160k -ac 2 -ar 44100 -vn audio.wav

The Python Code is just wrapping this command:

import subprocess

command = "ffmpeg -i C:/test.mp4 -ab 160k -ac 2 -ar 44100 -vn audio.wav"

subprocess.call(command, shell=True)

You have to make sure that ffmpeg is a known task, so in your system environment variables, under path, the path to ffmpeg.exe should be listed, or you can just use the full path to the exe in your python code.

这篇关于Python从视频文件中提取wav的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆