29

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()
Community
  • 1
  • 1
xolodec
  • 723
  • 1
  • 6
  • 15
  • 2
    You mention `ffmpeg`, but you're using `avconv`. – llogan Nov 04 '14 at 18:19
  • Please see http://stackoverflow.com/questions/9477115/who-can-tell-me-the-difference-and-relation-between-ffmpeg-libav-and-avconv. They are different projects and substitution one for another. `avconv` if fork of `ffmpeg` which was done to distance themselves from `FFmpeg project`. – xolodec Nov 05 '14 at 06:16
  • If you launch ffmeg in ubuntu you will see message like this: `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.` – xolodec Nov 05 '14 at 06:18
  • Explore the moviepy library as suggested here [by Daweo](https://stackoverflow.com/a/55081534/10944913) – Nasheed Yasin Apr 19 '21 at 21:07

5 Answers5

45

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.

user1767754
  • 18,800
  • 14
  • 111
  • 133
  • Thank you very mach. But I am not new to Python and know how to accomplish this with `subprocess` or `multiprocessing` module. I was asking about possible disadvantages of this approach. Except unknown path to `ffmpeg`/`aconv` in a system (which i could easily find by myself) i know nothing of them. So i have asked this question in a hope that somebody will point me out another disadvantages of this approach. – xolodec Nov 05 '14 at 06:16
  • Honestly, if you are trying to use free available libraries, i don't think that you will find something better and easier to use then ffmpeg. – user1767754 Nov 05 '14 at 08:24
  • Do we have to specify absolute path toward `videofile` and `audiofile` in this code, if our `.py` file is located at different place? – abhi1610 Jun 09 '17 at 10:45
  • Wherever u launched the python session from is the base path. I would suggest using absolute path, at one point if u are debugging, u could copy paste the command and run it in shell. – user1767754 Jun 09 '17 at 11:16
  • nice, the answer also works for webm as source file! – Hoff Dec 21 '18 at 19:14
  • where does it save it? – Farhang Amaji Apr 15 '21 at 00:13
10

this could be better and easier to use than ffmpeg, it's called python-video converter, and can be used to extract the audio from video, https://github.com/senko/python-video-converter , it could be used in conjunction with mpg123, as follows

    from converter import Converter
    import os
    c = Converter()
    clip = 'clip.avi'
    conv = c.convert(clip, 'audio.mp3', {'format':'mp3','audio':{'codec': 'mp3','bitrate':'22050','channels':1}})
    for timecode in conv:
        pass    
    os.system("mpg123 -w audio.wav audio.mp3")

the converter module extracts the audio from the video and saves it as an mp3 file, while mpg123 converts the mp3 file to mp4,

a different solution is as follows: using moviepy module in python https://github.com/Zulko/moviepy

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

the numbers within the subclip function specify start and end of audio, in seconds. you can then use mpg123 to change the audio to any other format

programmer44
  • 461
  • 1
  • 4
  • 11
8

Audio clips can be created from an audio file or from the soundtrack of a video file

from moviepy.editor import *
audioclip = AudioFileClip("some_audiofile.mp3")
audioclip = AudioFileClip("some_video.avi")

https://zulko.github.io/moviepy/getting_started/audioclips.html

1

or example extract mp3 from

import os

VIDEOS_PATH = '/Users/****/videos'
VIDEOS_EXTENSION = '.webm'  # for example
AUDIO_EXT = 'wav'

EXTRACT_VIDEO_COMMAND = ('ffmpeg -i "{from_video_path}" '
                         '-f {audio_ext} -ab 192000 '
                         '-vn "{to_audio_path}"')

os.chdir(VIDEOS_PATH)
files = os.listdir(VIDEOS_PATH)
for f in files:
    if not f.endswith(VIDEOS_EXTENSION):
        continue

    audio_file_name = '{}.{}'.format(f, AUDIO_EXT)
    command = EXTRACT_VIDEO_COMMAND.format(
        from_video_path=f, audio_ext=AUDIO_EXT, to_audio_path=audio_file_name,
    )
    os.system(command)
Nick Korolkov
  • 365
  • 5
  • 3
-3

FFmpeg is one of the most famous multimedia frameworks wich is widely used for processing videos. In order to encode the video, certainly a video encoder must be used. for more information use this: http://machinelearninguru.com/computer_vision/video_processing/ffmpeg_extract_audio/ffmpeg_audio_extract.html