So I’ve had an iPod for a few months now, and it’s been absolutely great. I use it all the time at work and in the car. I’ve brought it along on a couple trips up north and it was quite useful for the car rides. I also got a little use out of it on the plane to and from Nashville the other month. Oh, and I *love* album shuffle.
iTunes is also great. It’s the best software for organizing a collection of music, without a doubt. The only place it — and the iPod — seem to fall short is in compatability with other audio formats: Apple doesn’t do Real, and it certainly doesn’t do Windows Media Player.
I’ve gotten along without these abilities fine for awhile now, but I’ve been hearing more and more about podcasting lately, and I finally had the great idea that I wanted to listen to a favorite NPR radio show of mine, Car Talk, on my iPod since I’m never able to catch it on my local affiliate in Philly. More than anything else I just want to timeshift the show so I can start listening to it again, but having it on my iPod would be even better.
Unfortunately, I discovered that the free weekly stream is in RealPlayer format only. Can’t do it. Or can I? Open source to the rescue!
The idea is simple enough. In order to get Car Talk on my iPod, I need to convert the Real stream to mp3 first, then I can just treat it like a normal audio file. So I did a little googling and arrived at a solution that Works For Me © on OS X.4. I assume some knowledge of the terminal and UNIX.
First, I needed to setup the tools to get the job done. mplayer will handle capturing the stream from the network, and lame will convert that data to an mp3 on my disk. On the way, we’re going to need DarwinPorts and RealPlayer.
I tend to prefer to compile these open source apps myself whenver possible, so I compiled lame using DarwinPorts, though I’m sure a binary library is available somewhere. Install DarwinPorts and make sure to add /opt/local/bin to your environment PATH. Once DarwinPorts is installed, you’ll want to update its program and update the portfiles as well:
sudo port -v selfupdate
sudo port -v sync
Now we’re ready to compile lame:
sudo port -v install lame
Unfortunately, I wasn’t able to compile mplayer from DarwinPorts with Real support myself. I had a lot of trouble finding sources I could compile on PPC for the Helix codecs, but by way of google I found that there is an mplayerosx project out there with precompiled binaries that can make use of the codec bundles inside RealPlayer.app. We’re only interested in the most recent command line binaries from the mplayerosx SourceForge site, labeled ffmpegX. Download and copy those to /opt/local/bin. Also download and install the RealPlayer.app from the Real website.
As of this writing, this precompiled copy of mplayer is hardcoded to look in /Library/Application Support/ffmpegX/reallib/ for the Helix codecs. A little UNIX-fu will create a symbolic link from that location to the bundles inside the RealPlayer.app.
sudo mkdir -p /Library/Application Support/ffmpegX
sudo ln -s /Applications/RealPlayer.app/Contents/Frameworks/HXClientKit.framework/HelixPlugins/Codecs/ /Library/Application\ Support/ffmpegX/reallib
That’s it. Now we just need a stream and a shell script to glue everything together. My script is fairly unsophisticated at this point, but it seems to get the job done. Copy it (below) to a file and make it excutable (chmod 775 stream2mp3).
You’ll also need to find a stream. Car Talk is a little unusual because it’s a playlist consisting of several streams played in sequence. Unfortunately, when lame encounters the EOF at the end of the first stream, it quits, so I have to keep editing the playlist file to remove the topmost stream address until I have them all. For other streams, such as Princeton University’s WPRB, it seems to work fine.
#!/bin/sh
# usage: stream2mp3 infile
for file
do
# Create a named pipe
if [ -e /tmp/stream2mp3 ]
then
rm -f /tmp/stream2mp3
fi
mkfifo /tmp/stream2mp3
# Setup listener
lame -h -brief –id3v2-only –tt ” –ta “$1″ –tl `date “+%Y/%d/%m”` \
–ty `date “+%Y”` /tmp/stream2mp3 “$1″_`date “+%Y_%m_%d__%H-%M”`.mp3 &
# Stream audio to listener
mplayer -playlist “$1″ -prefer-ipv4 -vc dummy -vo null -speed 1 -ao pcm:file=/tmp/stream2mp3
done
# Cleanup the named pipe
rm -f /tmp/stream2mp3
Download your .asx or .ram (or other stream playlist) to a file in the same directory as the scrip and invoke it as an argument to the scrip. If you’re trying to convert a file and not a stream, get rid of the -playlist option for mplayer. Silence lame output with the -S switch rather than -brief. Specify higher encoding quality by replacing -h with -q x where x is between zero and two, zero being the highest quality.
Now with a little cron magic I’ll be able to timeshift some of those radio shows I keep missing and play them back on my iPod in my car at my convenience.
Rawk!