203
Posted October 23, 2009 by Spyros in Linux/Unix Programming
 
 

How to Create a Shell Script to Listen to Radio via Mplayer

net_radio
net_radio

There are quite a few media players in linux that work pretty well, but after using most of them, i tend to mostly use mplayer. While you could think that there are other players (like Totem) that have a better GUI, i personally prefer to use mplayer because i find it easier and more versatile to use it via the command line.

If you, like me, are not watching TV much but you like listening to the radio while you are on your computer, you may be visiting some websites to listen to your favorite online radio streams. In my case, i tend to listen to about 3-5 different radio stations online and i use mplayer to simplify that procedure for me, so that i will not have to open up a browser each time that i want to do so.

Using the standard mplayer commands, opening up a url stream is pretty easy. It is done via the command :


mplayer mms://sportfm.live24.gr/sportfm7712

Now, as you can see i am using a stream that i like to listen to sometimes. This could be any radio stream that you like to listen to however. Just visit the websites of your favorites radio stations and get their online URI. Doing so is easy. Just use your browser to see the corresponding source of the html page like “View->View Source”.  There should be a URI inside that is like mms://whatever. Just copy it and paste it to a file that you name after that station.

What i do is specifying a folder in my home and i name it radio. Inside this folder i create files that i name after radio stations. Therefore, i would create a file named “sportfm” and then write to it the URI mms://sportfm.live24.gr/sportfm7712. Then, i would just invoke this using (invoked from my home folder):


mplayer -playlist radio/sportm

Now, while this is good, it is somehow tiring to write this command every time you want to hear a radio station. Therefore, i created a bash script that makes our life easier. This is the script :

mplayer=`which mplayer`

if [ $# == 2 ] && [ $1 == '-d' ]
then
path="${HOME}/radio/$2"
echo "Dumping Radio Station $2"
$mplayer -playlist $path -ao pcm:file=./mystream.wav -vc null -vo null
elif [ $# == 1 ]
then
path="${HOME}/radio/$1"
$mplayer -playlist $path
else
echo "Usage : radio Station"
exit 1
fi

This program is actually very simple. First of all, we check if $# is equal to 2. $# refers to command line arguments length of the shell script. Therefore, if we create this script under the name “radio” and invoke it using “radio sportfm”, this has one command line argument and it is $0 = “sportfm” while the actual count is 1, since it is the only parameter. Therefore, $# = 1.

Now, this script also has the ability to dump a radio station’s data into a wav file. If the first parameter is “-d”, this means that we are trying to dump that station. Therefore, a command like “radio -d sportfm” would dump that station into a wav file. If the script has just one command line parameter, this means that it tries to fire up a channel station. The two commands that we execute to do that is :

path="${HOME}/radio/$1"
$mplayer -playlist $path

Firstly, we get the path to our radio station’s files. As i told you, there is a folder that has various radio station files inside and it is called “radio/”. We craft the filepath like “our home path + /radio/ + first parameter indicating the station”. This essentially points to a file at our radio directory. Then, we execute the mplayer command (we locate mplayer path using the command ‘which’) using the switch -playlist and the path that we specified before. This starts our radio listening experience.

Finally, if more or less command line arguments are specified we get a simple usage message ( that i have to make more detailed actually :P). The really final step is to save this code to a file, name it something like “radio” and then change it to an executable using :

chmod 755 radio

Now, a good idea is to move that file to your /usr/local/bin directory. This is your local binaries directory and it is included in your $PATH. Thus, after you do that, you can now run this command out of every working directory from your shell. Now, just to test it, you can write “radio yourstation” to listen to some radio or “radio -d yourstation” to dump that audio stream.

Have some nice and easy going listening experience :)


Spyros