How to save online radio stream using Linux
Mplayer is initially a movie player for Linux, but through the many available options, it is capable of doing many other things, including to save, record or dump online music stream to file.
In it’s easiest form, running the following command will dump 1.FM’s Channel X (with more channels available from www.shoutcast.com) to current.mp3
$ mplayer -dumpstream http://64.62.252.130:8070 -dumpfile current.mp3
The following bash script takes this a little bit further by keeping the latest 3 dumps, and delete the older ones.
#!/bin/bash URL=http://64.62.252.130:8070 WORKDIR=/home/shakir/temp/mplayerdump MAXDUMP=3 CURRENTDUMP=`ls $WORKDIR | wc -l` mv $WORKDIR/current.mp3 $WORKDIR/`date +%F`.mp3 if [ $CURRENTDUMP -ge $MAXDUMP ] ; then for i in `ls $WORKDIR | head -n $[($CURRENTDUMP-$MAXDUMP)+1]`; do rm $WORKDIR/$i done fi mplayer -dumpstream $URL -dumpfile $WORKDIR/current.mp3
Going a little further is to run the script through cron nightly, for daytime listening probably while being mobile and offline. Crontab entry is editable by the following command;
$ crontab -e
and add the following line (assuming the script is located at /home/shakir/mplayerdump.sh)
0 2 * * * /home/shakir/mplayerdump.sh
Do make sure the script is executable by running the following command:
$ chmod a+x /home/shakir/mplayerdump.sh