Getting stream info from MPlayer

Goal: To get the trackinfo from an internet radio stream onto a LCD.
The first step:
To get MPlayer to write to a file we use this command:

>mplayer -playlist http://www.chronixradio.com/chronixaggression/listen/listen.pls 2>&1|tee /var/log/mplayer.log

We can look at the file:

>cat mplayer.log|grep "ICY"
ICY Info: StreamTitle='Opeth - Blackwater Park';StreamUrl='www.opeth.com';

But we get extra info we don't want.
A PHP-script and a regular expression fixes this:

<?php
$file = "/var/log/mplayer.log";
$fh = fopen($file, 'r') or die("Can't open file");

while(!feof($fh))
{
$line=fgets($fh);
$resnr=ereg("(StreamTitle=)\'(.*)(\';StreamUrl)",$line,$res);
if ($resnr)
echo "Song $res[2]\n";
}
fclose($fh);
?>

>php mplayerinfo.php
Opeth - Blackwater Park

Another version of the scripts also writes info to a file:

<?php
$file = "/var/log/mplayer.log";
$fh = fopen($file, 'r') or die("Can't open file");

while(!feof($fh))
{
$line=fgets($fh);
$resnr=ereg("(StreamTitle=)\'(.*)(\';StreamUrl)",$line,$res);
if ($resnr)
#echo "$res[2]\n";
$np=$res[2]."\n";
echo $np;
$file2 = "/var/run/nowplaying";
$fh2 = fopen($file2, 'a') or die("Can't open file");
fwrite($fh2, $np);
fclose($fh2);
}
fclose($fh);

?>

The next step is to install perl-LCDd-0.03 from http://www.webbastards.com/projects/, a Perl library to create lcdproc-clients. From the example provided in the sources I built my own script which uses the information from var/run/nowplaying used above and puts the last row on the LCD. Nice, the goal is reached!

AttachmentSize
nowplaying.pl.txt3.77 KB