Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python extract wav from video file

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() 
like image 631
xolodec Avatar asked Nov 04 '14 17:11

xolodec


People also ask

How do I separate audio from video in Python?

We need to import the moviepy package or the editor class alone specifically. Then we need to create a VideoFileClip object by referencing the video file through the parameter. Once this is done, extracting the audio is as simple as accessing the audio member of the VideoFileClip object that we created.

How do I rip the audio from a YouTube video?

1) Go to YouTube to MP3 Converter. 2) Copy and paste the YouTube video link that you want to extract the audio and click the Go button. 3) Choose MP3 with the quality you want and click the Convert button. 4) Then click Download and you'll get the audio file.


1 Answers

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.

like image 181
user1767754 Avatar answered Sep 19 '22 21:09

user1767754