HOWTO: Archive Audio Streams in to mp3 Files
I am an NPR junkie, and my new place gets awful reception. And for a long time I have admired TiVo and wondered if my life might not be better if I had a DVR for NPR programs. Add to that KQED’s obnoxious plugin/popup window and the fact that I use an older version of RealAudio to avoid Spyware, which stutters and rebuffers all the time, and I have enough motivation to rig up something different.
I have rigged up a simple system to schedule rips of programs broadcast on the KQED audio stream in to easily manageable .mp3 files. If I had one of them iPod thingies I could even listen to the radio programs on the bus. I might even get around to warezing this to interested friends via BitTorrent and CSS, which would leave us another technical explanation.
Ingredients used:
- FreeBSD running on an old laptop, for automatic, hands-free operation.
- mplayer, to rip the Windows Media stream off KQED’s web site.
- lame, to convert the audio in to mp3.
- Two shell scripts, cron, and at, to automate everything.
Where is the Audio Stream?
Mplayer is mighty and awesome, but unfortunately, you can not just click on a web page and feed it to mplayer. (Well, maybe you can and I am just dumb.) So, the first task is to find something for mplayer to chew on. In my case, this means going to http://www.kqed.org/ and clicking on the “Listen” link and selecting Windows Media and copying the URL to listen manually. You can then fetch that URL and examine the contents, and find a URL that starts with mms://
. If you can play this URL in mplayer, then you are doing well.
I would share an example, but this part of KQED’s web site is throwing errors at the moment.
Ripping
I set up a simple script that launches lame and mplayer, then sits and waits for a specified time, and kills these programs off. Crude, but effective.
Scheduling
Ah, the potentially trickier part comes with scheduling. I thought this over for a couple days, and figured that a slightly clever shell script should do the trick. I wrote a script called “today” which defines a few functions, such as everday()
and weekday()
and runs through what the schedule should be, depending on the day of the week. It then schedules rips of the appropriate programs via at at command. You could also do a comparison with the date command to schedule special one-off recordings.
Anyway, the today script is run out of cron at midnight:
0-21:33 djh@yomama ~> crontab -l 0 0 * * * $HOME/bin/today
One other trick, on FreeBSD you have to give yourself permission to use at:
0-21:35 djh@yomama ~> cat /var/at/at.allow djh
Making it Better
Do check out the comments that follow for tips and tricks, especially Shawn Dowler who has gone and wrote a page about his revised versions of the scripts.