how to rip streaming audio to mp3 in Linux (bonus: silence trimming and id3 tags)

The usual readers of this blog (all three of you) may find this quite uninteresting, but I’m posting it here as Google-fodder in hopes it may be useful to someone out there.

Say there’s an internet audio stream that interests you (like, for example, a radio feed). But say you’d rather not listen to it while tied to a device with an internet connection - you need to rip it to a file on disk. And lastly, just for kicks, say the radio feed already has its advertisements kindly blanked out with silence by the stream provider, but you find those three-minute silences irritating to skip over manually.

You’ll need the following software packages installed: mplayer, sox, lame, and id3v2 (this last only if you’re ripping to mp3 and want id3 tags in your file). All of these were available pre-packaged in Ubuntu’s repositories, so installing them is no more difficult than “sudo aptitude install mplayer sox lame id3v2″. (You may also need some additional codecs packages for mplayer depending on the format of the stream you’re ripping).

And here’s the bash script I wrote to do all the work for me: getstream.sh

#!/bin/sh
# getstream.sh
# script to download a stream and convert to mp3
if [ -z ${2} ]; then
echo "Usage: ${0##*/} playlist-file outfile-base-name [id3-album]"
exit;
fi
if [ -z ${3} ]; then
album='NFL';
else
album=${3};
fi
mplayer -playlist ${1} -ao pcm:file=${2}-original.wav -vc null -vo null
sox ${2}-original.wav ${2}.wav silence 1 00:00:01 0.5% -1 00:00:05 0.5%
lame ${2}.wav ${2}.mp3
id3v2 -g 101 -A "${album}" -t "${2}" "${2}.mp3"

A quick example of how this script might be used, before we delve into the details:

getstream.sh stream-playlist.ram StreamTitle StreamAlbum

The first parameter is the playlist file for the stream, the second parameter is the title (used both for filename and id3 title tag), and the third (optional) parameter is the id3 album name.

The playlist file is a very short file (with a .ram extension if it’s a RealPlayer stream) that is usually the target of a website link to a stream. It doesn’t actually contain any audio, just links to the actual audio file(s) (which would have a .rm extension for RealPlayer). Normally you never see the playlist file, because your browser just opens up RealPlayer to handle it and RealPlayer finds the audio and starts playing the stream. To rip the stream, instead download the playlist file through your method of choice (right-click, “Save Link As” in your browser will work) and pass it as the first parameter to getstream.sh.

Now, onto the nitty-gritty of getstream.sh:

The first few lines just output a help message if the script doesn’t get at least two command line parameters (in bash scripting, ${1} is the first parameter, ${2} is the second, etc — the “-z” tests for emptiness). The next few lines set the album name to a default value if it isn’t given as the third parameter. Then we get to the interesting part:

mplayer -playlist ${1} -ao pcm:file=${2}-original.wav -vc null -vo null

MPlayer understands the format of various playlist files, the “-playlist” parameter tells it to look in this playlist file for the URLs of the actual audio. “-ao pcm:file=…” tells mplayer that rather than sending the audio to your sound card, it should just dump it to raw PCM data (by default mplayer also sticks a WAV header in front of the data, making it a WAV file). “-vc null -vo null” speeds things up by cluing mplayer into the fact that it’s an audio-only stream, so don’t worry about video.

sox ${2}-original.wav ${2}.wav silence 1 00:00:01 0.5% -1 00:00:05 0.5%

sox is a neat little audio-processing command-line utility that can do all kinds of fun effects and tricks. We’ll just use its silence-detecting and trimming function. The parameters to the “silence” function are a bit arcane, refer to the sox manpage for more detail. The first three parameters “1 00:00:01 0.5%” tell sox to not start processing audio until it finds a period of one second with non-silence (which the third parameter defines as a volume of greater than half a percent of the maximum volume - sox will also let you specify the threshold in decibels, just use a “d” instead of the “%” suffix). The second three parameters “-1 00:00:05 0.5%” tell sox to stop processing audio when it finds a period of at least five seconds of silence (i.e. under the 0.5% threshold). The fact that the fourth parameter is negative tells sox that after it stops processing because of silence, it shouldn’t just assume the end of the audio but instead should look for more non-silence to start processing again.

lame ${2}.wav ${2}.mp3

After the complexity of sox, lame (Lame Ain’t an Mp3 Encoder) is refreshingly easy to use: give it the input file and an output file and off it goes.

id3v2 -g 101 -A "${album}" -t "${2}" "${2}.mp3"

Lastly, id3v2 is a command-line utility to set ID3 tags in MP3 files. This command line sets the genre to 101 (which is Speech), the album to whatever you passed as the third parameter (or the default), and the title to the base filename.

Voila - you’ve got an mp3 file ready to go.

Comments are closed.