Record Streaming Audio with Linux: Part I

February 28th, 2006

Update: For a better solution check out this newer post

I happen to enjoy listening to Glenn Beck. The problem is that I can’t receive his weekday shows where I live. Even if I could receive his weekday show over the air, I wouldn’t hear most of it because I am busy working all day.

The solution: cron and mplayer with a little help from sox.

Here’s a sample script:

  1. #!/bin/bash
  2. # Use mplayer to capture the stream
  3. # at $STREAM to the file $FILE
  5. DATE=`date +%d-%b-%Y`  # Save the date as DD-Mmm-YYYY
  6. YEAR=`date +%Y` # Save just the year as YYYY
  8. # Where you want the file saved.  Leave off file extension
  9. FILE=/home/shawn/PodCasts/Glenn_Beck_Show_$DATE
  11. # The following file should be a playlist file such as .asf or .asx
  12. # You can also create your own file with a URI and put it here
  13. STREAM=/home/shawn/bin/glenn_beck_show-6-9am
  14. DURATION=3.1h # enough to catch the show, plus a bit
  15. #DURATION=200s # a quick run, just for testing
  17. # For the id3v2 Tags
  18. AUTHOR="Glenn Beck"
  19. ALBUM="104.7 WPGB-FM Pittsburgh"
  20. TITLE="Glenn Beck Show - $DATE"
  22. # Capture Stream
  23. /usr/bin/mplayer -really-quiet -cache 500 \
  24.     -ao pcm:file="$FILE.wav" -vc dummy -vo null \
  25.     -playlist $STREAM &
  26. # the & turns the capture into a background job
  27. sleep $DURATION  # wait for the show to be over
  28. kill $! # kill the stream capture
  30. # remove gaps and convert to mono
  31. sox $FILE.wav -c 1 $FILE-silenced.wav \
  32.      silence 1 -0.9 2% -1 -0.9 2% ;
  33. rm $FILE.wav ; #remove original capture
  35. # Encode to .mp3, mono 32kHz 32kb/s, and tag the file
  36. lame -a -m m --tt "$TITLE" --ta "$AUTHOR" \
  37.      --tl "$ALBUM" --ty "$YEAR" --vbr-new -V 9 \
  38.      --resample 32 $FILE-silenced.wav $FILE.mp3 ;
  39. rm $FILE-silenced.wav # Remove the raw audio data file

Once all of the variables have been set, make this executable and make a cron job for it.
crontab -e

Here’s an example for starting at 6am every weekday:

  1. 0 6 * * 1-5 /home/shawn/bin/ >& /dev/null


  • The basis for this script is the one found at this Linux Journal article.
  • You need at least SoX version 12.17.9 for the silence filter to work properly.
  • MPlayer should be fairly recent. Older versions have a different syntax for pcm (wav) audio output
  • This solution still requires huge amounts of disk space (~500MB/hour). I am still experimenting with using named pipes (fifos) to do all of the file processing in RAM and only output the final encoded file to the disk.

2 Responses to “Record Streaming Audio with Linux: Part I”

  1. Orlando says:

    Hi, i just wanted to know, how would I go about using these scripts on a hosted Linux web server? For example i want to automate a recording from station then make it be stored in a specific folder, to be listen to by viewers on the site at a later date. I am assuming i can install mplayer and the other parts into the directory on the webserver and beging from there and run a cron job from there etc..

    it would be really helpful if you could help, thanks!!

  2. Shawn Dowler says:

    I have never tried to do any of this on a hosted linux environment, so it would all depend on the restrictions they have in place. If all you use MPlayer for is this one isolated case, then there may be better tools that are smaller and easier to get going. Make sure whatever you do that you start with the updated script since it uses far less disk space.

    I would look into what format the streaming audio is sent in and see if a lighter tool than MPlayer could capture that data for you. If you have any specific questions or want to provide more details about what you have available on your Linux hosting account, then just let me know and I’ll be happy to help in any way I can. Information that may be helpful: is it a shared hosting account or do you get at least a virtual dedicated server where a full-blown linux distro is available and you have root access?

Leave a Reply

Line and paragraph breaks automatic.
XHTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>