Hack 81. Rip Streaming Audio
Streamripper lets you rip the live streaming audio station of your choice directly to MP3 for later listening.
Streaming audio has allowed a number of people to easily broadcast not only their favorite music tracks, but also other types of radio shows. Instead of being broadcast all day long, some shows are broadcast only at certain times of the day. If you aren't at your computer when the show is broadcast you'll miss outunless you have streamripper.
streamripper is a simple but powerful command-line application that allows you to record streaming audio directly to local MP3 files. To install streamripper, see if your distribution has already packaged it, otherwise download the source from the official page at http://streamripper.sourceforge.net and compile and install it according to the installation instructions.
To use streamripper, simply pass it the URL to your streaming audio station in a console window:
$ streamripper http://18.104.22.168:8072
By default, streamripper will create a directory named after the stream in the current directory and then start storing the streaming content as MP3s within an incoming directory. When a file is complete, streamripper will move it up from the incoming directory to the stream's main directory. Each file is named after the artist and track metadata that streamripper grabs from the audio stream. You can leave streamripper running as long as you wish (provided you have enough hard drive space), and it will continue to grab and store MP3s in the stream's directory. Provided you have enough band-width, you can even launch multiple instances of streamripper and simultaneously capture multiple streams.
The streamripper defaults are suitable for standard uses, but streamripper also allows you to configure everything through command-line options. For instance, the -d argument tells streamripper to rip files into the specified directory instead of the current working directory. The -s argument tells streamripper not to create a directory for each stream, and instead to save all of the MP3s into a single directory.
By default, MP3s that streamripper saves are titled after the artist and track name only. For some audio streams you might want to store the files in the order they were played on the stream. The -q option will cause streamripper to add a sequence number to the beginning of each filename starting from 001. In addition the -P prefix argument will let you add a particular string to the beginning of each file. Use the -a filename argument, and streamripper will store the entire stream to a single large file in addition to multiple files. Add the -A argument, and streamripper will only store to the single file and won't create the individual files.
4.10.1. Schedule Recordings
Due to its command-line nature, streamripper is ideal for scheduling recordings with at or cron. The -l argument lets you configure a number of seconds for streamripper to record before it automatically exits. Combine this with the -q and -P options and you can easily create an archive of your favorite radio show. For instance, one stream I like to listen to only plays between noon and 6:00 P.M. PST Thursday through Saturday. I created the following script called streams to record it:
#!/bin/sh # rips from Punk FM (http://punkfm.co.uk) # this stream is broadcast from noon to 6pm PST Thu-Sat URL='http://22.214.171.124:8072' DAY=`date +%F-` streamripper $URL -d /mnt/audio/mp3/streams -q -P $DAY -l 21720 --quiet &
This script rips the URL into my /mnt/audio/mp3/streams directory, makes sure that the tracks are named sequentially with the -q argument, prepends the current day's timestamp with the -P option, and tells streamtuner to run for six hours and two minutes with the -l option (I added the extra two minutes in case my clock is out of sync with the streaming computer's clock). The --quiet option disables all normal output so that my cron job doesn't fire off an email to me each time the script runs. I then added the following line to my user's crontab:
59 11 * * 4-6 /home/greenfly/bin/streams
This line will execute the script at 11:59 A.M. on Thursday, Friday, and Saturday. Read the crontab manpage (man 5 crontab) for more information on how to schedule programs with cron.
4.10.2. Listen to Streams as They Are Ripped
Another nice feature of streamripper is the ability to create a relay server for streams as they are being ripped. The -r option tells streamripper to create a relay server on port 8000, or you can specify a different port to use as an argument. If port 8000 isn't available, streamripper will try to use higher and higher ports until it finds one it can use. Then you can point your music player at port 8000 (or the port you configured) on that machine either from the same computer (http://localhost:8000) or over the network (http://ip_address:8000). By default streamripper will only allow a single connection to this relay server, but you can pass the -R number argument to it to allow a specified number of clients to connect. If you set the argument to zero, streamripper will allow an unlimited number of clients to connect (well, limited by your bandwidth and processor speed). So, to rip and relay a stream and allow three clients to connect to it, type:
$ streamripper URL -r -R 3
If I wanted to allow three clients to connect to the stream my bash script recorded, I would change the command to the following:
streamripper $URL -d /mnt/audio/mp3/streams -q -P $DAY -l 21720 --quiet -r - R 3 &
4.10.3. Track Detection
streamripper automatically splits tracks in the stream based on the silence it detects when tracks change. This method isn't perfect however, and depending on the stream (such as streams that use cross-fading) you might end up with tracks that contain a few seconds of the previous song at the beginning, or a few seconds of the next song at the end. streamripper provides a series of --xs arguments that let you configure this algorithm for tricky streams. For instance, if each track begins with three seconds of the previous track, the following will tell streamripper to offset the track splitting by an extra three seconds (expressed as milliseconds):
$ streamripper URL --xs_offset=3000
Alternatively, if a track has three seconds of the next track at the end, you can set the offset to a negative amount:
$ streamripper URL --xs_offset=-3000
If you notice that streamripper contains a various amount of previous or following tracks, but it isn't a constant value, you can also have it create a number of seconds of padding around each track so that you can go back later and edit the MP3 by hand. For instance, to add two seconds of padding before the split point and three seconds after each split point, type:
$ streamripper URL --xs_padding=2000:3000
You can also combine the following two options. If for instance, each track contains a number of seconds of the previous track, but it varies between two and six seconds, you can set the split offset in the middle of the variation, or four seconds, and then create a padding of two seconds before and after the split:
$ streamripper URL --xs_offset=4000 --xs_padding=2000:2000