radio.pl killed the video star

A little while ago, I wrote about a small perlscript for playing internet radio stations. Since then, a few more features have crept in, such as resume support (‘radio start’ will play the last station you listened to).
Another idea I had been toying with was adding scrobbling support, so I could submit the tracks to last.fm. Their API isn’t extremely advanced but it did require some information that wasn’t readily available, such as the unix timestamp for when a track started. But thanks to an ugly hack (piping mplayer output to `perl -nle ‘BEGIN { open STDOUT, “>”, $lfmfile or die $!; } print time, “:”, $_;’`, someone please give me a better solution), that was solvable. Another issue I haven’t figured a not-completely-horrible solution to is calculating the length of a track, since last.fm doesn’t want tracks that are shorter than 30 secs (those are usually ads anyway).
But in conclusion radio.pl now supports submitting your tracks to last.fm, if you provide your credentials. Basically, just create a file ~/.radio.cfg with the following content:

lastfm_user = arkanoid
lastfm_passwd = md5:117a520adbd19eff51100215aa7a7fbf

and it should work (provided the radio stream actually contains the proper information in ICY, which at least SomaFM, SKY Radio and Digitally-Imported streams seem to do). You can also add your password in plaintext (without the md5: prefix), but that’s just silly.
The current support is quite ugly so expect breakages. If you want to know what’s going on with the scrobbler, just add –lfmfile /tmp/lfmlog when you start the script and tail that file. Please do that as well, if you submit a bugreport.

Download the radio script
A sample .radiostations file

Please note that compared to the old script which only spawned mplayer, you will now have three processes spawned (mplayer, a perl process for adding timestamps to mplayer output and a fork of the radio script for handling last.fm submission) if you enable last.fm submission (by adding your credentials). This version also leaves the radio.state file behind when you stop it, since it’s used for the resume feature.

Go ahead and break it!

Adding scrobbling support took less than a day, thanks to last.fm’s really simple and quite well-documented submission protocol and their quick response when I asked for a client ID. The only snag I hit, was with the length of a track, “l[0]” in the postdata. According to the documentation, this is optional for radio scrobbling, but in fact you need to pass it along, although you can just set it to zero. I guess my interpretation of ‘optional’ differs from their.

radio.pl on last.fm screenshot

It is actually doable!

See DR, it’s not impossible to give people access to your media archives in an easy way. Please get your act together and start working on something like that instead of that lame web-based player and your sucky “Bonanza”-imitation.

Since I’m forced to pay a fee to you because of my internet connection, please give me (and everybody else, not just us “opensource freaks”) content for my money’s worth.

radio on the command-line

I got bored of remembering paths to playlists for streaming radio all the time, so I wrote a short script to do the job for me.
Rather than explaining what it can and can’t do, here’s some of the output from –help:

The following commands are recognised:
radio tag
plays the station associated with tag

radio info tag
shows information for tag

radio stop
stops playback

radio status
prints status

radio random
start playing a random station from your list

radio np
prints the currently playing track, if the stream supports it

radio
prints a list of stations. Use -v for detailed information

The script will fork, exec mplayer and then kill the parent so you won’t need to have the script running all the time. State is saved so you can get status info at any time (if you want now playing support in your IRC client of choice, for example), stop playback or switch station (if radio is already playing a station when you ask it to start playback, it will kill the old one first) and so forth.

The script needs mplayer, perl and the YAML perl-module. YAML is available from cpan and probably also from your distribution of choice.

The .radiostations file is a YAML-file with a simple layout:

tag:
   name: Name of the station
   desc: Description of the stations
   path: Path to the station's playlist (a .pls file, usually)
   stream: Misc. stream information.

The tag is what the script uses to identify a station, when you ask it to play one. It must be unique or a later station with the same tag will override the first one.

The radio script
A sample .radiostations file
The .radiostations file contain all SomaFM, DI.fm and SKY.fm stations and a few stations from DR (p1, p2, p3, boogie, barometer and electronica).

Note that the “Now Playing” feature is somewhat suboptimal. It reads the info from ICY, but not all streams use that and not all streams keep it updated. There is no general solution though, and for most sane stations (SomaFM and some DI and SKY.fm streams at least) it should work just fine. The DR streams won’t have np-support since DR sucks big time…

As usual, no guarantees or anything else. Use it if you want to :-)

There’s a similar shellscript [danish] written by Jesper Nyerup if you want to avoid perl and the YAML-module.