Internet Radio to Podcast with Shell Tools

Combine several stand-alone programs with some shell “glue” and record your favorite Internet radio show while you sleep.
Why Not Just Do It in Perl?

It may seem strange that I'm calling a scripting language from another scripting language. The point is that I'm using each to do the things it's best at. Bash is designed to execute commands, and it's really easy to start a background process, find out its process ID and kill it again. On the other hand, trying to add an XML entry in Bash using the more basic string-handling tools, such as sed and grep, would have been, well, exactly the kind of thing that drove Larry Wall to write Perl in the first place.

Now that we have a script, we make the file executable and run it:

chmod +x catchthewolf
./catchthewolf

which results in a properly tagged MP3 file and a new entry in the wolfrss.xml RSS feed. When testing, you can uncomment the 30-second test line to make sure everything's working properly, but be sure to comment it back out before trying to catch a show.

Now all that's left is to get our computer to run this thing at 5AM on Saturday. That's done by using the system's cron utility—invoke crontab -e— and adding an entry like this:

MAILTO=phil # Testing: mail script output to me

# Catch hour of the wolf 5AM Saturdays
59 4 * * sat /home/phil/catchthewolf

crontab's editor is most likely to be set to vi-style commands, so you have to use i to start typing and <Esc>:wq to save-and-exit. When you're done, you should see this message:

crontab: installing new crontab

which says you're all set. Check man 5 crontab for more information on how to make jobs repeat every day, once a month or whatever. You also want to make sure your user name is in the file /etc/cron.allow—the list of who can run jobs on the system's scheduler. If you're running on a remote system, verify with the administrators that you're allowed to run cron jobs.

To see the resulting podcast, point your RSS-aware software at the XML file the script creates. In Firefox, use Bookmarks→Manage Bookmarks→Add Live Bookmark, and remember to enter the URL starting with file:// and not the filename itself.

That's a Wrap

By taking two programs already on the hard drive, downloading two Perl modules and writing a few lines of shell script, we have assembled a homebrew Webcast recording system that saves our favorite programs for us to listen to whenever we choose. It also lets us know what it has done by popping up live bookmarks in Firefox and automatically transfers the recordings to our MP3 player. Some scripts for capturing other Internet radio shows will be available on the Linux Journal FTP site (see the on-line Resources). Now I just have to remember to delete the older files before my hard drive fills up with leftover Webcasts.

Thanks to Anne Troop, Jen Hamilton and Chris Riley for their many shell-scripting hints over the years; to Anne's friend Janeen Pisciotta for finding “Hour of the Wolf” for us in the first place; and to LJ Editor in Chief Don Marti for the cool podcast idea.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

This is helpful - but doesn't work... possibly out of date?

ejoso's picture

Does anyone follow this thread anymore? The most recent update being 2007, it's fast become a long tail article...

I've tried this and tinkered with it and the XML::RSS modules kick up pattern match errors for me and I've not been able to resolve them. It would be awesome if any of you fine folks who got this to work in the past could help me out with it...

The errors are:
Use of uninitialized value in pattern match (m//) at /usr/local/share/perl/5.10.1/XML/RSS.pm line 535.
Unregistered entity: Can't access modules field in object of class XML::RSS at /usr/local/share/perl/5.10.1/XML/RSS/Private/Output/Base.pm line 1131

Any thoughts? I pounded away at this for a loong time and wasn't able to get it working as of yet.

Thanks in advance,
Eric

something is broken...

Donovan Worrell's picture

Hi, thanks for the article on this it has helped me automate some recordings I need on a daily basis. A couple of things I noticed...I included the use strict;use warnings on the script you cited and have come up with some odd errors. I have worked through most of them but cannot figure out how to "fix" this:

Unregistered entity: Can't access modules field in object of class XML::RSS at /usr/lib/perl5/site_perl/5.8.5/XML/RSS/Private/Output/Base.pm line 926

any ideas? It seems to be centered around this:


bless\$out,XML::RSS;

based on this error:

Bareword "XML::RSS" not allowed while "strict subs" in use at -e line 1.
Bareword "ARRAY" not allowed while "strict subs" in use at -e line 1.

What am I missing here?

FIXED!!!

Donovan Worrell's picture

Hey,

Much thanks to the maintainers of XML::RSS specifically Shlomi Fish for this diff that fixes this script to work with XML::RSS current.

podcast.diff
--- podcast.sh.old 2007-10-11 20:06:41.077613900 +0200
+++ podcast.sh 2007-10-11 20:11:26.685655662 +0200
@@ -108,31 +108,19 @@

+use strict; \
+use warnings; \
use XML::RSS; \

-my \$in = XMLin('$XML'); \
-my \$out = \$in; \
-bless \$out, XML::RSS; \
-my \$item = \$in->{channel}{item}; \
-
-if ( ( ref \$item ) ne ARRAY ) { \
-
- \$out->add_item(%\$item); \
-} \
-else { \
- foreach \$item ( @{\$item} ) { \
- \$out->add_item(%\$item); \
- } \
-} \
+my \$out = XML::RSS->new(version => '2.0'); \
+\$out->parsefile('$XML'); \

\$out->channel( lastBuildDate => '$XDATE', pubDate => '$XDATE' ); \

... and the ugly version

Anonymous's picture

The ugly compressed version of the perl code is the following:

/usr/bin/perl -e "use XML::RSS; use XML::Simple; \
    $i=XMLin('$XML');$o=$i;bless $o,XML::RSS; \
    $m=$i->{channel}{item};if((ref $m)ne ARRAY) \
    {$o->add_item(%$m);} else \
    {foreach $m (@{$m}) {$o->add_item(%$m);}} \
    $o->channel(lastBuildDate=>'$XDATE', \
    pubDate=>'$XDATE'); \
    $o->add_item(title=>'$XTITLE', \
    link=>$o->{'channel'}{'link'}, \
    pubDate=>'$XDATE', \
    enclosure=>{url=>'file://$FILE', \
    length=>(stat('$FILE'))[7], \
    type=>'audio/mpeg'}, mode=>'insert'); \
    pop(@{$o->{'items'}}) \
    while (@{$o->{'items'}}>$ITEMS); \
    $o->{encoding}='UTF-8'; $o->save('$XML');"

Damn...

Anonymous's picture

I copied the wrong code block; sorry! It should be:

/usr/bin/perl -e "use XML::RSS; use XML::Simple; \
    $o=XML::RSS->new(version=>'2.0');
    $o->parsefile('$XML');
    $o->channel(lastBuildDate=>'$XDATE', \
    pubDate=>'$XDATE'); \
    $o->add_item(title=>'$XTITLE', \
    link=>$o->{'channel'}{'link'}, \
    pubDate=>'$XDATE', \
    enclosure=>{url=>'file://$FILE', \
    length=>(stat('$FILE'))[7], \
    type=>'audio/mpeg'}, mode=>'insert'); \
    pop(@{$o->{'items'}}) \
    while (@{$o->{'items'}}>$ITEMS); \
    $o->{encoding}='UTF-8'; $o->save('$XML');"

What does this achieve?

TRiG's picture

I'm not enough of a geek to understand this. (I'm a trainee geek.) Nor do I yet use Linux. (I may switch over when Microsoft drops XP support. I don't like the way XP tries to organise my life for me, and I'm told Vista is worse.)

With the above code you record a predetermined section of an Internet radio station (a programme), yes? And then you produce RSS code which creates a podcast feed of that program, yes? And then, on another computer, you set up a program (iTunes or something similar) to download that podcast, yes?

Do I understand you right? On computer 1 (always on the Internet), you record the programme and produce the podcast, and then on computer 2 (and potentially many other computers) (occasionally on the internet) you subscribe to that podcast. It seems a long-winded way of going about it, but I can see some benefits.

For those of us without access to always-online computers, is there any way we can set up such podcasts? Can we, somewhere, enter the URL of a live radio station (say http://www.cbc.ca/listen/streams/r1_toronto_32.html) and some times (say 19:00-20:00 on what I think is Eastern Time), and be given a resultant podcast feed to subscribe to?

(I live in Ireland, and don't have much experience with Canadian and US time zones. If you can understand the time expressions on the Vinyl Tap page, please explain them to me.)

Re: What does this achieve?

Phil Salkie's picture

You don't need two computers - the machine which you're saving the radio shows on makes an XML file so that podcast-aware programs can pick up the new radio shows as they're recorded, and automatically put them on to your music player at recharge/sync time. Maybe this will be an excuse to install Kubuntu on a spare hard drive partition and get your feet wet with Linux!

There's no service that I'm aware of that will make a podcast for you, basically for copyright reasons - perhaps someone in a less copyright-frenzied country will do that, and make a ton of money.

For Canadian times, look at: http://www.timetemperature.com/tzca/canada_time_zone.shtml
They seem to be assuming Eastern, AT is Atlantic, NT is Newfoundland.

For a possible way to do a similar thing on your XP machine, look at:
http://streamripper.sourceforge.net/
(You'll have to configure automatic dialing to the internet on your XP system for that to work.)

RDF, not RTF!

Evan Prodromou's picture

You said, "RSS stands for RTF Site Summary." Actually, it stands for "RDF Site Summary" -- the original name from the My Netscape Network. RDF is a framework for making statements about resources (like Web sites); see http://www.w3.org/RDF/ .

RTF is the "Rich Text Format", the default word processing exchange format used in WordPad and other word processors. It has nothing to do with RSS.

Re: RDF, not RTF!

Phil Salkie's picture

You are, of course, correct. Sadly, I've been caught perpetuating one of those errors like "to gild the lily" - so many people do it that it's become almost right. So, indeed, s/RTF/RDF/G9000

use fifo as intermediate wav file

Henk Postma's picture

You may save a lot of intermediate disk space by using a fifo buffer for the wav file. I use this for my particular version of a podcast generator:

#!/bin/bash
# call with $1=url, $2=mp3 file
# to stop recording, kill the mplayer process (killall mplayer)
# create unique name based on md5 hash of stream url and output mp3 file
output=/tmp/`echo $1 $2 | md5sum | awk '{print $1}'`
# make the fifo buffer
mkfifo "$output"
# start mplayer, dump the video (if any) to /dev/null
mplayer "$1" -ao pcm:file="$output" -vo null -vc dummy &
# and start transcoding from the fifo -> mp3 file
lame -S "$output" "$2"
rm "$output"

Kudos and extending the functionality

woodside's picture

I have hacked away on this to a point that it works pretty well for me. It am still having a few minor issues on the rss document ceation when I use the same script to record a few different programs throughout the week. It hasn't risen to the level of actually digging into it again, though. All in all, it works great.

I have been looking for a way to post-process the files to add bookmarks to the files, because one of the shows I record has 10 minute music breaks while they cut to local programming. I am looking for a way to extend this script by overlaying a bookmark. For example, 27 minutes into the show I want to insert a bookmark so that when they cut to music, I hit the forward button and advance to the next bookmark, which would be just before the cut back to the show. This assumes that the show is consistent with cuts, but that doesn't seem to be a problem.

Thanks for the excellent article.

I've brushed up the script

Rick's picture

I've brushed up the script a bit so that you only need one
and everything is pretty much passed from the crontab.

See it here

Cheers,

Rick

Link -

Wong Seuol's picture

Link is not working.. Thanks.. I would like to see ur scripted...

Thanks

Dead link

Kinney's picture

The link you refer to above appears to be dead. Would love to see the revised code.

Re: I've brushed up the script

Phil Salkie's picture

Nice job! Much cleaner than my hack-and-patch approach... Thanks!

Firefox Live Bookmarks and the enclosure tag

eric.john.miller's picture

Firefox live bookmarks do not appear to support the enclosure tag. Do you have a workaround for this?

I've been thinking about putting together something like this for a while. Thanks for a great article!

Re: Firefox Live Bookmarks and the enclosure tag

Phil Salkie's picture

Sure - it breaks the "link" feature on some news aggregators, but makes the live bookmarks work again. Sigh...

Change this bit of perl:
link=>$o->{'channel'}{'link'}, \

To this:
link=>'file://$FILE', \

With this change (assuming you have some useful plugin like Plugger or MPlayer Plugin) clicking on the live bookmark starts playing the captured file. This is really how it should have been in the article - it's much more useful than having the link to the homepage, plus that link is in the title section anyway.

Re: Firefox Live Bookmarks and the enclosure tag

eric.john.miller's picture

Thanks, that did the trick!

Mplayer?

tpurl's picture

Why not use streamripper instead? As long as you're writing your content to a file, streamripper works very well and requires fewer command switches.

Mplayer?

Sean Edwards's picture

Mplayer, ecasound, sox . . . there are many command-line audio tools. I prefer ecasound myself.

Re: Mplayer?

Phil Salkie's picture

(This comment got deleted because I'd managed to cross-up my user names... Here's my second try at it.)

Streamripper was one of the programs I looked at when I first tried to do time-based capture. I thought it would be the do-everything package that I wanted, but I found that it

1) Has almost no Linux documentation available from the website
2) Has a limited number of stream types that it can access
3) Has timed duration, but not timed start
4) Can't transcode (i.e. take a RealPlayer stream and save it as MP3)
5) Has ID3V2 file tagging, but it wasn't clear if you could tag with data that didn't come from the stream itself.

That being said, it seems to be a pretty capable package, and if it saves the shows you want in the format you want, great - you're absolutely right - it saves some messing around in the script, especially if you can get the MP3 tags to do what you want. But its main purpose seems to be to capture _songs_ from internet radio streams (like shoutcast), not whole _programs_.

So, it was sufficiently not what I was looking for that I opted to take the program which I was already using as a listener tool (MPlayer) and make it be what I wanted (a VCR for Internet Radio) by using a shell script. (Plus, that seemed to be a cool enough thing to do to write an article about.)

Thanks for the article

Joel's picture

Thanks for the article. It should be a good reference, even I don't need to do exactly what you're doing.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix