Saving online radio for offline listening

If you follow this blog, you’ll probably have noticed I spend a fair bit of my time in nature, in the Laurentian Mountains, about an hour North-West of Montreal. To be able to work from there, I have a satellite internet connection, but the latter has a limit of 20GB per month, which I have to monitor and spare.

Being a big fan on online radio, I end up using a fairly large chunk of my bandwidth just streaming music from the likes of Digitally Imported and SomaFM. To avoid that, I started doing what seemed the most logical: I save a few hours of a selection of radio stations during the week while on my unlimited home connection, bring them with me to the cabin, and listen to them offline, saving my precious space-bound bandwidth.

There are of course probably hundreds of freely available software to do so, but being myself, I simply spat out a few lines of code to do it for me. You can find them below, if they can be of use to you. If you don’t know how to run them, you probably want to find something else. :)

Here’s my current bash script.

if test $# -lt 3; then
    echo "Usage: $0 stream_url output_file number_of_seconds"
PID=`curl -o $FILE $URL>/dev/null 2>&1 & echo $!`
echo "$PID: saving $URL to $FILE for $SECONDS seconds"
sleep $SECONDS
kill $PID
echo "All done!"

And my initial PHP version.

if ( 4 != count( $argv ) )
    exit( "Not enough arguments.\nUsage: $argv[0] stream_url output_file number_of_seconds\n" );
list( $self, $url, $file, $seconds ) = $argv;
$pid = shell_exec( sprintf(
    'curl -o %s %s > /dev/null 2>&1 & echo $!',
echo "$pid: Saving $url to $file for $seconds seconds.\n";
sleep( $seconds );
shell_exec( sprintf( 'kill %d', $pid ) );
exit( "All done!\n");

Pro tip: to find a radio’s actual stream URL, go to their site or, find the m3u or pls playlist they use, and get the stream’s address from there.

Happy offline listening.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.