Linux: Extract audio from .mp4 (video + audio) to .mp3 (audio)

I found myself in a situation where the only access I had to an audio track was having it embedded in an .mp4 file and I wanted just the audio in an .mp3 file. This probably isn’t a common occurrence, but seeing as this could help extract the audio from .mp4s downloaded from popular video sites, this might be of some use.

First of all, I realize there are probably more ways to do this than can be reasonably listed. In fact, there are probably a bunch of tools that do this exact same thing. This was a one off thing, really, so I didn’t spend time looking for a package that did everything. It was only after I did it once that I decided to turn it into a shell script to make it easy to use again in the future.

Note: You need mplayer and lame for this.

[james@fractal ~]$ lame -?
LAME 64bits version 3.98.3 (http://www.mp3dev.org/)
...
[james@fractal ~]$ mplayer -h
...
MPlayer SVN-r31628-4.4.4 (C) 2000-2010 MPlayer Team
#!/bin/bash
#
# EDWARDS RESEARCH
# www.edwards-research.com
#
# This converts the audio from .mp4 files that include video (e.g. youtube.com streams) to
# .mp3 files.
#

# If file exists, set $FILE
#   I know this is a sloppy way to handle command line arguments -- I'm ok with that.  (I
#   was going to provide for options, blah blah...)
if [[ -e ${1} ]] ; then
    FILE=${1}
fi

# Ensure input file exits
if [[ -z $FILE ]] ; then
    echo "File not found -- exiting."
    exit
fi

# Extract Filename
base=$(basename "${FILE}" .mp4)

# Dump audio from .mp4 to .wav with mplayer
#   So, it looks as if it doesn't make a difference in terms of the output (at least from
#   my small test group) whether you pick pcm:waveheader or pcm:fast. pcm:waveheader takes
#   more than twice as long to convert but pcm:fast complains.  I'm going to leave it at
#   waveheader because I'm not in a rush and I'd rather not have the warnings.  Feel free
#   to change this to pcm:fast and experiment.
#       -ao pcm:waveheader -> 59 seconds, 4625553 byte .mp3
#       -ao pcm:fast       -> 22 seconds, 4625553 byte .mp3
#
#   mplayer  -vc null -vo null -nocorrect-pts -ao pcm:fast "${FILE}"
#
mplayer -vc null -vo null -nocorrect-pts -ao pcm:waveheader "${FILE}"
RV=$?
if [[ $RV != 0 ]] ; then
    echo "mplayer completed unsuccessfully -- exiting."
    exit
fi

# Convert .wav to .mp3
lame -h -b 192 audiodump.wav "${base}.mp3" ${VERB}
RV=$?
if [[ $RV != 0 ]] ; then
    echo "lame completed unsuccessfully -- exiting."
    exit
fi

# Cleanup Temporary File
rm audiodump.wav

echo "Conversion complete."
Posted Sunday, December 12th, 2010 under linux, projects, shell scripting, tips and tricks.

Tags: , , ,

5 comments

  1. Viv Diwakar says:

    Good stuff, does the trick 100%

  2. Great ! thx so much for the share

  3. Barutan Seijin says:

    Nice. This does the trick. Thanks!

  4. Martin Sevior says:

    Awesome! Just what I wanted and works a treat :-)

  5. Thanks for this. It worked straight “out of the box” on my CentOS 5.7 system.

    I had struggled for a couple of hours with various other tools, until I found your script.

Leave a Reply