Extract Audio from Video Files to WAV using FFmpeg

Posted by admin on December 15, 2009 under Tech Tips | 3 Comments to Read

Previously, I described how to Extract Audio from Video Files to WAV using Mplayer. Another method using FFmpeg instead of Mplayer was also pointed out in the post titled Add Stereo Audio Tracks to MKV Files, and I figured it would be useful to outline the quick one-step process in a post all by itself.

Here’s an example of extracting the audio from a video file called video.mkv and saving it to a file called audio.wav. This very well could have been an AVI, MPEG, or any other video format that FFmpeg can decode.

ffmpeg -i video.mkv -acodec pcm_s16le -ac 2 audio.wav

It should also be mentioned that your source video file may have multiple audio channels or streams. For example, you may have both English AC3 and DTS channels, but you may also have other audio streams for other languages, directors comments, etc. If you want more control over which stream you are using, first identify them all with ffmpeg.

ffmpeg -i video.mkv
[snipped for brevity]
Input #0, matroska, from 'video.mkv':
Duration: 01:30:38.78, start: 0.000000, bitrate: N/A
Stream #0.0(eng): Video: h264, yuv420p, 1280x720, PAR 1:1 DAR 16:9, 23.98 tbr, 1k tbn, 47.95 tbc
Stream #0.1(eng): Audio: ac3, 48000 Hz, 5.1, s16
Stream #0.2(eng): Subtitle: 0x0000
Stream #0.3(heb): Audio: mp3, 48000 Hz, stereo, s16
Stream #0.4(heb): Subtitle: 0x0000
Stream #0.5: Attachment: 0x0000
Stream #0.6: Attachment: 0x0000
At least one output file must be specified

From the example above, you see that Stream #0.0 is labeled as being an English video stream with h264 encoding. Stream #0.1 and #0.3 are both audio streams, but #0.1 is English AC3 5.1 and #0.3 is Hebrew MP3 stereo. Simply reference the stream id with the -map option in the following format.

ffmpeg -i video.mkv -map 0:1 -acodec pcm_s16le -ac 2 audio.wav
[snipped for brevity]
Output #0, wav, to 'audio.wav':
Stream #0.0(eng): Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s
Stream mapping:
Stream #0.1 -> #0.0
[snipped for brevity]

Now that you have a PCM WAV file, you can manipulated it however you like, e.g. encode to MP3, OGG, FLAC, etc.

lame -V0 -q0 --vbr-new audio.wav audio.mp3
oggenc -q6 audio.wav
flac audio.wav

Be Sociable, Share!

Comments

  • Purab Gujar said,

    I tried your command. But why do you suggest we use any codec at all when converting to wav? Just giving the wav format is sufficient. i.e. why use -acodec pcm_s16le instead of ffmpeg -i video.mkv -acodec -ac 2 -f wav -y audio.wav

  • sumitavo biswas said,

    Hi friends, i am facing a issue..i have a requirement to convert a .3gp file to .wav file. We have to use ffmpeg for the same, but currently the transcoded file(.wav file) do not play, it is of 0 bytes…can anyone help me how to do it? Following is the stacktrace

    $/opt/csw/bin/ffmpeg -i sound2.3gp -acodec pcm_mulaw -ar 8000 -ac 1 -f wav 3gp.wav
    FFmpeg version SVN-r21156-snapshot, Copyright (c) 2000-2010 Fabrice Bellard, et al.
    built on Feb 12 2010 01:57:40 with gcc 4.3.4
    configuration: –prefix=/opt/csw –enable-shared –disable-static –enable-ffplay –enable-runtime-cpudetect –enable-libtheora –enable-libvorbis –enable-libx264 –enable-libxvid –enable-libmp3lame –enable-gpl –enable-postproc –enable-avfilter –enable-avfilter-lavf –bindir=/opt/csw/bin/sparcv8 –libdir=/opt/csw/lib –incdir=/opt/csw/include
    libavutil 50. 7. 0 / 50. 7. 0
    libavcodec 52.47. 0 / 52.47. 0
    libavformat 52.46. 0 / 52.46. 0
    libavdevice 52. 2. 0 / 52. 2. 0
    libavfilter 1.15. 0 / 1.15. 0
    libswscale 0. 8. 0 / 0. 8. 0
    libpostproc 51. 2. 0 / 51. 2. 0
    [amr @ 442c0]max_analyze_duration reached
    [amr @ 442c0]Estimating duration from bitrate, this may be inaccurate
    Input #0, amr, from ‘sound2.3gp’:
    Duration: N/A, bitrate: N/A
    Stream #0.0: Audio: samr / 0x726D6173, 8000 Hz, 1 channels
    File ’3gp.wav’ already exists. Overwrite ? [y/N] y
    Output #0, wav, to ’3gp.wav’:
    Stream #0.0: Audio: pcm_mulaw, 8000 Hz, 1 channels, s16, 64 kb/s
    Stream mapping:
    Stream #0.0 -> #0.0
    Decoder (codec id 73728) not found for input stream #0.

  • gmendoza said,

    Hey there! You need to compile FFmpeg with opencore-amr support. FFmpeg as it’s compile on your system does not know how to decode your audio stream. The following two lines from your output can help you when troubleshooting other scenarios.

    Input #0, amr, from ‘sound2.3gp’:

    Decoder (codec id 73728) not found for input stream #0.

    See this post to enable opencore-amr support:
    http://ubuntuforums.org/showpost.php?p=7584660&postcount=409

    Hope this helps!

Add A Comment