Sunday, March 23, 2008

A Programmable Audio Recorder

You might remember that I like to listen to A Prairie Home Companion at the gym. That link shows you how to take the Real Audio stream from the PHC Archive and turn it into an mp3 or ogg file.

And that works, everywhere except with Verizon FIOS, which for some reason only lets streams in RTSP be played by a real RealPlayer client. Probably a firewall issue. I've tried tracking it down, but haven't gotten up the energy to try to talk my way up the FIOS help staff to find someone who would tell me how to fix it.

But, as someone once said, “there's more than one way to do it.” For example, I could use Realplayer to play the file through the speakers, and then record the output either by invoking SoX as rec, using gnome-sound-recorder, or using Audacity (which I can't get to work in this mode). Any of these methods, of course, ties up the speakers for two hours, not very desirable.

Alternatively, one could stumble upon the fact that Minnesota Public Radio (MPR) plays as an http (port 80) continuous stream on the web, that Prairie Home Companion airs for two hours starting at 5pm Central Time (6pm Eastern) every Saturday Night, and that FIOS allows mplayer to listen to that stream. Oh, and that MPR puts on a 15-20 second advertisement when you start the download.

With that information, we construct this little script, which you should save in a directory that the /bin/sh shell can find. We'll call it mpr_record, and we thank Penguin Pete for the hint on how to put all of this in a box:

#! /bin/bash

# Get to the correct directory

cd $HOME/audio/podcasts/prairie_home_companion

# get year month date

year=`date +%y`
month=`date +%m`
day=`date +%d`

# Print out what we want to do:

echo Recording to phc_$year$month$day.flac

# Record for 122 minutes:  One minute before the official start,
# and one minute after the official end, just in case our clocks
# are off, and to account for the initial MPR ad.

# You don't have to use the flac format,
# Using .wav will end up giving you a smaller .mp3 file
# with a loss of quality that won't show up at the gym.

# You might not need the two-step process, just save directly
# to .mp3 or .ogg, but my notes say this isn't very efficient
# in terms of final file size.

# The 1> /dev/null makes sure all of the streaming information that
# mplayer continuously spits out doesn't make it into the final
# output.

# is linked to the MPR stream.

mplayer -endpos 2:02:00 -vc null -vo null -ao pcm:fast -ao pcm:file=phc_$year$month$day.flac 1> /dev/null

# Convert to mp3
# ogg produces a smaller file, but my player doesn't do ogg.

echo Converting to phc_$year$month$day.mp3

# Note that SoX must be recompiled to support MP3s, if that's
# what you want here.  Actually, that link doesn't work on Ubuntu,
# I had to recompile from source.  Someday I'll write that up.

sox phc_$year$month$day.flac phc_$year$month$day.mp3

# Of course, ffmpeg would work just as well:

# ffmpeg -i phc_$year$month$day.wav phc_$year$month$day.mp3

# Edit id3 info.  This adds both id3v1 and id3v2 data.
# My labeling scheme is designed to overcome the limits of my
# player.  You might want to label yours differently.  See
# 'man id3v2' for all the options.

echo Labeling phc_$year$month$day.mp3

id3v2 -t $year$month$day -g 100 -y 20$year -A NPR -a "Prairie Home Companion" -T 1 phc_$year$month$day.mp3

So, if you've read all the comments, you'll see that this records from MPR for two hours and two minutes. Now we just need to make sure this starts one minute before 6pm (Eastern US), every Saturday. If you've been following along this blog, you know this is a job for cron. The crontab entry we need is

59 17 * * 6 mpr_record > $HOME/audio/podcasts/prairie_home_companion/mpr.errors 2>&1

which starts the recording at 17:59 (5:59pm) local time (Eastern, here), on the sixth day of the week, running the program mpr_record and putting all of its output into an error file, just in case we need to track things down.

Of course, what we've done is turned the machine into the Internet Audio version of a programmable VCR. This can be used for any regularly scheduled broadcast the mplayer can play.