If you're like me, your bosses actually expect you to work while on the job, and not sit listening to your favorite radio show like you might want to. In this instructable, I'll show how to record any audio stream automatically using mplayer, lame and cron to schedule the job.

Step 1: Get Ubuntu, Create Directories

For this instructable, you'll need to have Ubuntu Linux. I run 7.04, but this may work for other versions as well. If people seem interested, I'll write a seperate instructable up showing how to do this with Windows.

If you already run Ubuntu, the only things you need are freely available through apt. Use the following commands at a terminal to install Lame, Mplayer and KCron.

sudo apt-get install lame mplayer kcron

Type in your password and confirm the installs.

Use the following commands to create the folders you'll need.

sudo mkdir /scripts

mkdir /home/username/Music/NameOfShow

And this command to take ownership of the directory you just created:

sudo chown YourUserName /scripts

Step 2: Create Streamrecord Script

The steps for the script we're using are as follows:

1. Open mplayer, point to audio stream on Internet
2. Record stream to wav file in /tmp directory
3. Kill mplayer process when show ends
4. Convert /tmp/mystream.wav to mp3 file, name it with today's date and move it to more 'user-friendly' directory under user folder.
5. Delete wav file in /tmp directory.

To achieve this, you'll first need to find out the URL of the stream you're accessing. For an example, I'll use the CSPAN radio stream, and record it between the hours of noon and 2pm Monday thru Friday. Here's the text of the script:

NOW=$(date +"%b-%d-%y")
mplayer "mms://rx-wes-sea20.rbn.com/farm/pull/tx-rbn-sea34:1259/wmtencoder/cspan/cspan/wmlive/cspan4db.asf" -ao pcm:file=/tmp/mystream.wav -vc dummy -vo null ;
lame -m s /tmp/mystream.wav -o "/home/shawn/Music/CSPAN/My Show - $NOW.mp3" ;
rm /tmp/mystream.wav ;

Copy this text to a blank file using Gedit or Vi, and save it as "streamrecord". You'll need to edit the areas of the script in italics, as these wont apply to you. The text after mplayer is the URL to the stream you want to record, this will need to be replaced with your stream's URL, which can be found easily by going to the stream in Firefox, letting the mplayer plugin start, then right-click and choose "Copy URL".

Step 3: Save Script, Make Pkill Script and Make Scripts Executable

Next, we'll save the script to the /scripts directory. Once saved, go to a terminal session and type the following:

cd /scripts

chmod 700 streamrecord (This makes the script you just created into an executable file.)

Create one more file in the /scripts directory. This will be called pkill, and will be your hitman. That is, it will kill the mplayer process to allow the first script to continue with renaming and encoding the captured stream. The full text of the pkill script is exactly as follows:

pkill mplayer

Now, this breaks scripting etiquette, by not having #!/bin/sh at the top, but it works for me. Once you've saved the file pkill in the /scripts directory, use the chmod command again to make this one executable. You need to be in a terminal session, in the /scripts directory first, then type:

chmod 700 pkill

A quick "ls" will show you the files you just created, now in a lovely green instead of the standard black.

Step 4: Use KCron to Schedule Your Jobs

Cron, the wonderful but extremely confusing little text file and associated service deserves a whole series of instructubles unto itself. To make things simpler, we're going to use KCron (which we installed earlier) instead. If the install went as planned, you should see KCron under Applications --> System Tools. Alternately, the program can be launched from a terminal by typing "kcron". Use Ctrl+N to create a new job, and configure the job as shown in the picture. In the image you can see I configured the program /scripts/streamrecord to run all months, mon-fri at 12pm with 0 minutes.

The second script you made, called 'pkill' is what actually ends mplayer and allows the first script to continue. You'll need to set up another cron job to run that script as well. For my example, I end up with two jobs in KCron. (see 2nd image)

Step 5: You're in Business!

That's all there is to it, to test your handiwork you can right click the streamrecord job in Kcron, choose 'run now' and check for mystream.wav in your /tmp directory. If it's there (and growing fast), run the pkill job and you should soon see the mystream.wav disappear from your /tmp directory, and a new .mp3 file in the directory you specified when you altered the script. If you're recording a long show, keep in mind that it will take a decent amount of time to encode your .wav file. On one 3 hour show I record, it takes approximately 10 minutes to encode to mp3. If you have any problems getting this to work, feel free to drop me a line.


P.S. I know my methods are a bit crude, as there are probably best-practices that I'm not following in some of my steps, so please don't school me on Linux 101, this is just a quick and dirty way to never miss your favorite audio stream.
<p>Okay, so I was looking high and low trying to find some elaborate solution. Cold it really be this simple? Well, apparently. I stumbled across this page after 2 days of looking around, downloading, configuring and playing with various programs. I looked at this page and had it going in 10 minutes...well, minus KCron, I used the scheduler I already had. Thanks for making me backup a few steps to KISS. :)</p>
Another way is to use the &quot;endpos&quot; option of mplayer instead of pkill script, i.e. <br/>mplayer &quot;mms://rx-wes-sea20.rbn.com/farm/pull/tx-rbn-sea34:1259/wmtencoder/cspan/cspan/wmlive/cspan4db.asf&quot; -ao pcm:file=/tmp/mystream.wav -vc dummy -vo null -endpos &quot;01:00:00&quot;;<br/><br/>This would record for 1 hour and stop. A bit cleaner than using kill, plus one less line in crontab.<br/>
How about <a href="http://t.co/UnESWr4">recording streaming audio</a> with sound recorder? Perhaps somone will guess this will be illegal. I mean yes it might be if you record streaming audio for the sole purpose of spreading the recordings everywhere on the internet. As long as you do not spread it in any illegal form, it will be a hand way to help you out. <br> <br>
Sorry it took me so long to reply to this, but this is a really great suggestion. Thanks, I'll be re-writing the instructable (eventually) to use this method over the one I was using. Thanks!!!
I tried and googled. endpos doesn't work with -dumpstream. You have to revert to kill using the pid.
I've had good luck with something like this:<br /> <br /> mplayer $STREAM -endpos $DURATION -vo null -ao pcm:waveheader:file=$FILENAME.wav<br /> <br /> Where $DURATION&nbsp;is of the form &quot;1:30:00&quot;<br /> <br /> More details:<br /> http://www.mbeckler.org/scripts.php#savestream<br />
Does mPlayer have to use the speakers when it is recording these shows? Or can it run silently as long as the computer is on? This is crucial since I intend to sneak it into a box I'm building for Grandma so I don't have to have a dedicated box for this.<br><br><br>For background, I'm thinking about setting up a an Ubuntu box just so I can do this. I would set it up to record NPR and another show I like, then save the OGG file to my Ubuntu One account so I can access it from abroad really easily (I'm going on a big trip).
I did the same thing recently before I got chance to read your text. The first tool I used was VLC which can do similar thing as mplayer. Same as mplayer, I have to create a cron job to kill the VLC in order to end the recording. <br/><br/>Now, I have changed to use the tool <a rel="nofollow" href="http://savannah.nongnu.org/projects/mimms/">mimms</a>. It works great for me. Since mimms has the option to specify the length of recording in mintue. No need to have 2 cron jobs for each recording anymore.<br/><br/>- Mac<br/>
I'm using this to record a 4.5 hour talk radio show from a radio station's internet stream. However, for some reason it's not getting the whole show. So far of 5 recordings this week, none of them recorded the entire show and they are all different file lengths.
I've found that availability of the stream will affect whether or not you can establish and maintain a connection to the server. If your connection or the stream drops or becomes overwhelmed with requests that can happen, and this script will sense that the mplayer process has stopped, and move along to the next function, which is to encode what was just captured using Lame. The fact that all the files are of a different size makes me think that's whats going on. If you get a chance try listening to the stream live and see how reliable the stream is. You may be able to contact the person running the stream and ask that your IP be added to the whitelist, which will give your requests priority over everyone not on the list. Good Luck, Shawn
Thanks for the howto. I had the same problem as frenchie, but resolved it a little bit differently. I added the -msglevel all=-1 command just after my URL. I did this mainly because did not quite understand how his fix worked and could not get it to work. That was a success, but it started another problem. Kcron starts the recording well, and it starts the pkill well, but for some reason the .wav (which is long) only gets converted to a 1 second .mp3. Any tips?<br/><br/>thanks<br/>
Hi, really useful tutorial, thanks a lot. I'm having a strange problem with it though. The script to record the stream runs fine when I run it from the command line, but if I try to run it automatically from the crontab, mplayer crashes every time! MPlayer interrupted by signal 13 in module: play_audio - MPlayer crashed. This shouldn't happen. The script is identical, permissions are the same, I don't understand!! Thanks, John.
I was able to run it without problems, are you using 'crontab -e' to edit your crontab? I'm not too familiar with that, but did have it working at one time. I did it once, went &quot;well, this is a pain&quot; and found kcron. There are native gnome apps to do the same, but kcron works great, so that's what I went with. <br/><br/>What stream are you accessing? I have no problems getting to a local radio station, that stream is encoded as an asf. How exactly are you invoking this stream from the command line? I tried this:<br/><br/>cd /scripts<br/>sh streamrecord<br/><br/>and it worked fine, then tried it in kcron using 'run now' command, and again had no problems. Please try again to manually launch the script and see if you can get it to fail. I'm more interested in launching the actual script, not just <br/><br/>mplayer <em>url</em>.<br/><br/>Let me know if this sheds any light on your problem. Thanks,<br/><br/>Shawn<br/>
Managed to track down the problem. Yes, I'm using crontab -e to edit the crontab (originally tried kcron and it didn't work there either). My crontab entry originally looked something like this:<br/><br/>03 00 * * 2-6 script URL filename<br/><br/>I edited it to redirect output/error messages like so:<br/><br/>03 00 * * 2-6 script URL filename &gt; $HOME/logfile 2&gt;&amp;1<br/><br/>Now it works just fine! By the way, there's some useful info here that I've used together with your how-to in order to record directly to ogg/mp3 without creating a temporary wav file: <a rel="nofollow" href="http://www.tomtaylor.co.uk/blog/2007/04/17/play-it-again/">http://www.tomtaylor.co.uk/blog/2007/04/17/play-it-again/</a><br/><br/>Thanks again, it's so cool to be able to record streams automatically.<br/>John.<br/>
You may have already tried this, but if your mp3 player doesn't support ogg natively, maybe this could help: <a rel="nofollow" href="http://www.rockbox.org/">Rockbox</a>. It's an open-source alternative to the factory firmware, and it supports several models of mp3 player. Anyway, the lame conversion works just fine too.<br/><br/>Looking forward to listening to the radio show tomorrow that will be recorded while I snooze tonight!<br/><br/>all the best,<br/>john.<br/>
Great, I'm glad it's working for you. Sounds like I could learn a few things about Linux from you as well. For some reason, I thought mplayer could encode natively to .ogg format. In fact I remember finding to my dismay that my mp3 player wouldn't handle .ogg files, and therefore I had to add the conversion step with lame to go to mp3. Maybe not. Thanks for the feedback, I'm glad this is logged so I can refer back to your piping-to-a-logfile tip. Happy Thanksgiving! Shawn
what release are you running? This instructable was based off of feisty, but I'm running gutsy now, so I'll see if I have similar results and get back to you. Shawn
This Recorder is better and also lets you schedule your recordings as well as record in mp3s for your ipods. *smile*<br/><a rel="nofollow" href="http://www.xaudiotools.com/">http://www.xaudiotools.com/</a><br/>
The program you mentioned may work well, but it's $29.99 and it seems like it's better suited to archiving your old vinyl than to automagically recording internet streams. Besides, I like the idea that no GUI appears, all the magic happens in the background.
Wow, this works great. I tried this once with XMMS, to no avail. The only problem is the enormous size of the wave file created. A 4 hour program is over 1.9Gbytes, at 33Kbps! Is there a way to have mplayer record into mp3 or ogg in real time?
Unfortunately, mplayer only supports recording to wav, and I believe ogg. Ogg may be your answer, but I wanted to record to mp3 so that I could easily transfer to my mp3 player (iPods don't support OGG.). Another thing you may want to try is adjusting the bitrate that lame encodes to. This wont help the 1.9GB size, but you will end up with a smaller mp3 file. I tried this once and missed my show for that day, so it's not foolproof, and will take some experimentation. I'm glad this worked for you! Shawn

About This Instructable




More by stuffman:How to Control X10 Devices With Amazon Echo or Google Home Build a Home Theater PC from a Broken Laptop and a Tivo Easily sync music and podcasts to your Blackberry Storm with Ubuntu 
Add instructable to: