#!/bin/bash # mpd (www.musicpd.org) now playing script # author: Thomas Kear (qupada@initng.org) # no warranty expressed or implied. if it breaks, you get to keep both pieces #more useful variables mpd_host="127.0.0.1" mpd_port="6600" music_dir="/var/lib/mpd/music" #ways to get info from mpd status() { echo -e 'status\nclose' | nc $mpd_host $mpd_port | grep $1 | gawk -F\:\ '{print $2}' } currentsong() { echo -e 'currentsong\nclose' | nc $mpd_host $mpd_port | grep $1 | gawk -F\:\ '{print $2}' } artist() { currentsong "Artist" } album() { currentsong "Album" } title() { currentsong "Title" } elapsed() { stoms `status "time" | gawk -F\: '{print $1}'` } length() { stoms `status "time" | gawk -F\: '{print $2}'` } #convert value in seconds to minutes:seconds stoms(){ m=$1 s=$1 ((m/=60)) ((s-=((m*60)))) if ((s<10)); then s="0$s"; fi echo "$m:$s" } #position starts at 0, not much use position() { pos=`currentsong "Pos"` ((pos+=1)) echo $pos } playlistlength() { status "playlistlength" } #really clever trick involving calculating the bit rate from filesize/length bitrate() { filename="$music_dir/`currentsong "file"`" filename=`ls -l "$filename" | gawk -F"-> " '{print $2}'` size=`status "bitrate"` #echo "\n\n\n $filename" #ls -l "$filename" #size= status "`bitrate`" #size=`ls -l "$filename" | gawk -F\ '{print $5}'` length=`status "time" | gawk -F\: '{print $2}'` #((size/=125)) #echo `ls -l "$filename"` #((size*=8)) #((size/=128)) #((size/=length)) [[ -n `echo $filename | grep 'mp3'` ]] && type="mp3" [[ -n `echo $filename | grep 'flac'` ]] && type="flac" [[ -n `echo $filename | grep 'wma'` ]] && type="wma" [[ -n `echo $filename | grep 'ogg'` ]] && type="ogg" #size = `echo -e 'status\nclose' | nc $mpd_host $mpd_port | grep bitrate | gawk -F" " '{print $2}'` echo $type":"$size"kbps" } state() { status "state" } if [[ `state` == "stop" ]]; then echo "MPD: Stopped" elif [[ `state` == "play" ]]; then echo "Now playing: "\ `artist`" - "`album`" - "`title`" |"\ `elapsed`"/"`length`" |"\ `position`"/"`playlistlength`" |"\ `bitrate` else echo "Paused: "`artist`" - "`album`" - "`title` fi