Using mplayer to extract audio from a video file

From Mike A. Leonetti

Jump to: navigation, search

Usually to study Japanese on-the-go, I take animes or Japanese dramas and just take out the audio then put it on my MP3 player. I found that mplayer is perfect for this.

Whatever you can play with mplayer, whether it be an avi, mkv, mp4, ogm, etc it can be made into an MP3 or whatever other format is necessary easily.

Method 1: Dumping the audio

Sometimes you get lucky and your encoded file has the audio encoded by MP3. Then you can just have mplayer dump the audio.

You can check and see if the video's audio encoding using file.

# file Ranma\ One-Half/Season\ 01/01.\ Here\'s\ Ranma.avi 
Ranma One-Half/Season 01/01. Here's Ranma.avi: RIFF (little-endian) data, AVI, 640 x 464, 29.97 fps,
video: DivX 3 Low-Motion, audio: MPEG-1 Layer 3 (stereo, 32000 Hz)

You can see in my example above the audio: MPEG-1 Layer 3 (stereo, 32000 Hz) portion. This means the movie's audio was encoded with MP3.

Now you can simply use

# mplayer -dumpaudio -dumpfile mp3name.mp3 moviefile.avi

To get your MP3 file.

Method 2: Dumping to ac3 then converting to mp3

Usually when trying to extract the audio off of DVDs you end up with an ac3 file. This is fine, but you'll have to convert it to an MP3 from here. This is where ffmpeg comes in handy.

First we need to dump the audio:

mplayer -dumpaudio -dumpfile audio.ac3 dvd://1

For more information on playing DVDs or to change chapters see the mplayer manual.

Note: This may take a while. It takes a least 5 minutes for me.

Now convert your ac3 file into an MP3.

ffmpeg -i audio.ac3 -ab 128 audio.mp3

Method 3: Using mplayer's PCM wav output then converting to MP3

This usually works with all files, including the silly matroska (MKV) or OGM files the never dump properly.

First we need to create a WAV with mplayer.

mplayer -vc dummy -vo null -ao pcm:file=output.wav video.mkv

If you find that mplayer is taking its sweet time in outputting the WAV you may have to append the fast option like so:

mplayer -vc dummy -vo null -ao pcm:file=output.wav,fast video.mkv

Then we create an MP3 from the WAV.

lame -h -b128 output.wav output.mp3

Again I like to make 128kbps for my bitrate.

Finally remove the temporary WAV.

rm output.wav

Or you can make a script out of the whole thing. What I did was make a script that extracts the audio out of all video files in the current working directory:

#!/bin/bash

extra_opt="-aid 0"
filetype=mkv

for file in *$filetype ; do
basename=`basename "$file" ".$filetype"`
        mplayer $extra_opt -vc dummy -vo null -ao pcm:file="$basename.wav",fast "$file"
        lame -h -b128 "$basename.wav" "$basename.mp3"
        rm -v "$basename.wav"
done

The extra_opt I use to set the audio track when a file has multiple audio streams. It helps when picking a language.

Personal tools
Google AdSense