Dirk Gently Airs, Convert RealAudio to MP3

BBC Radio 4 is finally airing the second Dirk Gently series: “The Long Dark Tea-Time of the Soul”.  Unfortunately it is only available as a RealAudio stream (who still uses RealPlayer?) so you’ll have to cope with some mplayer magic to convert it to something an iPod will play:

[code lang="perl"]
#!/usr/bin/perl -w
use strict;
use Getopt::Long;

my $encode_mp3 = 1;
my $encode_wav = 1;
GetOptions('mp3!' => \$encode_mp3,
	   'wav!' => \$encode_wav);

my $WGET = '/usr/bin/wget';
my $MPLAYER = '/usr/bin/mplayer -nojoystick -nolirc';
my $LAME = '/usr/bin/lame';
my $TMP = '/tmp';

my $ramurl = shift;
my @ram = `$WGET -q -O - $ramurl`;
my ($rtspurl) = grep(/^rtsp:/, @ram);
$rtspurl =~ s/\&.*$//;
$rtspurl =~ /\/([^\/]*)\.r[ma]/ or die("Can't parse rtsp URL: \"$rtspurl\"");
my $filebase = $1;
print("Opening stream at " . localtime() . "\n");
system("$MPLAYER -dumpstream -dumpfile $TMP/$filebase.dump $rtspurl");
my $wavfile = $TMP;
$wavfile = '.' unless $encode_mp3 && !$encode_wav;
$wavfile .= "/$filebase.wav";
system("$MPLAYER -ao pcm:file=$wavfile $TMP/$filebase.dump");
if ($encode_mp3)
    system("$LAME $wavfile $filebase.mp3");# or die("Couldn't convert to mp3");
unlink("$wavfile") unless $encode_wav;

Pass the script the RAM URL as a command line parameter, and if all goes well it should dump the stream to a WAV, then convert it into an MP3 file. You’ll need mplayer and lame installed.

(And if all else fails, the CD should be released in a few months…)

About this entry