Recording RealAudio to MP3
Recently I’ve gone back to listening to a lot of online streams, mostly DJ mixes. A lot of these are in RealAudio, which is fine for listening to on the computer, but no good for transferring to my iPod or my Squeezebox. The best thing to do is still convert everything to MP3. The preferred tool for MP3 encoding of course is LAME, and the best audio conversion tool is MPlayer. I installed both on my OS X box using the excellent MacPorts (formerly known as DarwinPorts), it should be equally trivial on any reasonable unixy system. So, I know this is something that’s been done to death on the net, but I couldn’t find one post that told me everything I needed to know to do the conversion. So, as much for my own documentation as anything, here’s my script (based on one I found here):
#! /bin/bash
#
# Converts a Real Audio file to MP3 and adds an ID3 tag.
#
# Usage: ra2mp3 infile outfile
#
if [ -z $1 ] || [ -z $2 ]
then
echo "Usage: $0 infile.rm outfile.mp3"
exit 0
fi
author=`mplayer "$1" -vo null -ss 10:00:00 | grep author | sed -e 's/.*:\\s*\\(.*\\)/\\1/'`
title=`mplayer "$1" -vo null -ss 10:00:00 | grep name | sed -e 's/.*:\\s*\\(.*\\)/\\1/'`
if [ -z $author ]
then
echo "Author:"
read author
fi
if [ -z $title ]
then
echo "Title:"
read title
fi
mkfifo /tmp/stream.wav
mplayer "$1" -ao pcm:file=/tmp/stream.wav -vc null -vo null &
lame /tmp/stream.wav "$2" --tt "$title" --ta "$author" --preset standard
rm /tmp/stream.wav
A quick run-through: the script first checks that it’s been given the correct parameters, then it attempts to read the author and title from the RealAudio file. If it can’t find these, the user is prompted. Then MPlayer is used to dump the RealPlayer file to a PCM WAV file (i.e. just raw audio) then LAME is used to create the MP3. Since these intermediate WAV files can be pretty big, we use a trick: mkfifo /tmp/stream.wav is used to create a named pipe. MPlayer is then told to write to the pipe, and will block until something starts reading from it. LAME is then configured to read from the pipe, and the upshot is that all the data will go directly from MPlayer to LAME with no intermediate file. Neat.