Question

I'm searching for a way to analyse the content of internet radios. I want to write a ruby client that can get the current track, next track, band, bpm and other meta information from a stream (e.g. a radio on shoutcast). Does anybody know how to do this? And how do I record that stream into a mp3 or aac file?

Maybe there is a library that can already do this, I haven't one so far.

regards

Was it helpful?

Solution

I'll answer both of your questions.

Metadata

What you are seeking isn't entirely possible. Information on the next track is not available (keep in mind not all stations are just playing songs from a playlist... many offer live content). Advanced metadata such as BPM is not available. All you get is something like this:

Some Band - Some Song

The format of {artist} - {song title} isn't always followed either.

With those caveats, you can get that metadata from a stream by connecting to the stream URL and requesting the metadata with the following request header:

Icy-MetaData: 1

That tells the server to send the metadata, which is interleaved into the stream. Every 8KB or so (specified by the server in a response header), you'll find a chunk of metadata to parse. I have written up a detailed answer on how to parse that here: Pulling Track Info From an Audio Stream Using PHP The prior question was language-specific, but you will find that my answer can be easily implemented in any language.

Saving Streams to Disk

Audio playing software is generally very resilient to errors. SHOUTcast servers are built on this principal, and are not knowledgeable about the data going through them. They just receive data from an encoder, and when the client requests the stream, they start sending that data at an arbitrary point.

You can use this to your advantage when saving stream data. It is possible to simply write the stream data as it comes in to a file. Most audio players will play them without problem. I have tested this with MP3 and AAC.

If you want a more conformant file, you will have to use a library or parse the stream yourself to split on the appropriate frames, and then handle bit reservoir issues in your code. This is a lot of work, and generally isn't worth doing unless you find your files have real compatibility problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top