Samstag, 24. Juli 2010
Skript: mpnotify [UPDATE]
Mir fehlte irgendwie die Möglichkeit auch ohne, dass irgendein MPD-Client läuft, anzuzeigen was mein MPD gerade abgespielt wird und deshalb habe ich mich mal daran gemacht ein kleines Skript zu schreiben.
Angefangen hat’s gestern mit nem 4-zeiligen “Einzeiler”
Inzwischen habe ich das ganze noch etwas erweitert und bin jetzt der Meinung, dass es doch eigentlich ganz gut funktioniert. Mir kam auch in den Sinn, dass es vielleicht auch andere Leute gibt für die das Skript interessant sein könnte, also poste ich es mal hier, wozu hab ich denn nen Blog
Funktionsweise: Das Skript holt per netcat die Infos vom MPD und verarbeitet diese dann mit wildem rumgefuchtle von sed/awk/grep/perl und ein paar if-Schleifen. Falls in dem Ordner, in dem das gerade abgespielte Lied ist auch ein Albumcover ist wird dies auch erkannt. Zum Schluss wird das ganze dann über das notify-System von KDE ausgegeben (man kann’s sicher auch z.B. auf das notify-System von Gnome o.ä. umbiegen) KDE/Gnome ausgegeben (über notify-send).
Features:
- Internetstreams werden erkannt und soweit Infos vorhanden sind werden diese auch angezeigt (Streamtitel als Titel und eventuelle Songinfos als Benachrichtigungstext)
- Der Titel wird als Titel der Benachrichtigung gesetzt
- Im Text der Benachrichtigung stehen Artist und Album (falls vorhanden)
- Falls vorhanden wird auch das Cover in der Benachrichtigung angezeigt
So, etz zum Skript selbst: Download
#!/bin/bash
# Copyleft 2010 by Bernd Stellwag <burned@zerties.org>
# use notify-send to display information about the song, mpd is currently playing
# Changelog:
# 0.3: rename to mpnotify: mpdnotify already exists
# add a config section for easier configuration
# 0.2: add possibility to specify host and port or print some info how to use this script and check if required notify-send (libnotify-bin) is installed
# 0.1: "release"
# Configs
# set the path where your music library is
collectionpath="/var/lib/mpd/music"
# if you have your music in a subdirectory set it here
musicsubdir="musik"
# set the filename of your cover images
covername="cover.jpg"
# see if notify-send (libnotify-bin) is installed
if ! which notify-send > /dev/null ; then
echo "Please install \"libnotify-bin\" with this Command: \"sudo aptitude install libnotify-bin\" or with your favourite Packagemanager"
exit
fi
# check if user needs some help
if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "-help" ] || [ "$1" == "help" ]; then
echo "mpnotify 0.2: Copyleft 2010 by Bernd Stellwag <burned@zerties.org>\nUSAGE: mpnotify [HOST] [PORT]"
exit
fi
# check if host and port are specified
if [ "$1" == "" ]; then
host="127.0.0.1"
else
host="$1"
fi
if [ "$2" == "" ]; then
port="6600"
else
port="$2"
fi
# fetch info
currentsong="$(echo "currentsong" | nc $host $port -q1)"
# see if we have an internetstream
httpcheck="$(echo "$currentsong" | grep "file: " | grep "http://")"
# if it is an internetstream handle it
if [ "$httpcheck" != "" ]; then
# see if there is a streamtitle
streamtitle="$(echo "$currentsong" | grep "Name: " | sed 's/Name: //g')"
# if there is no streamtitle we set the url as title
if [ "$streamtitle" == "" ]; then
streamtitle="$(echo "$currentsong" | grep "file: " | sed 's/file: //g' | sed '/http:\/\///g')"
fi
# maybe there is some information about the currently playing song
songinfo="$(echo "$currentsong" | grep "Title: " | sed 's/Title: //g')"
# send info
notify-send "$streamtitle" "$songinfo"
# exit the script
exit
fi
# set title
title="$(echo "$currentsong" | grep "Title: " | awk '{for (i=2;i<=NF;i++) printf ("%s ", $i); print "" }')"
# if there is no title set the filename as title
if [ "$title" == "" ]; then
title="$(echo "$currentsong" | grep "file: " | sed 's/file:\ //g' | sed 's/musik\///g' | awk -F "/" '{for (i=1;i<=NF;i++) printf ("%s\n", $i)}' | perl -e '@line = <>; chomp(@line); print "@line[$#line]"' | sed 's/.mp3//g')"
fi
# set artist
artist="$(echo "$currentsong" | grep "Artist: " | awk '{for (i=2;i<=NF;i++) printf ("%s ", $i); print "" }')"
# set album
album="$(echo "$currentsong" | grep "Album: " | awk '{for (i=2;i<=NF;i++) printf ("%s ", $i); print "" }')"
# set "artist - album" string if possible
if ([ "$artist" != "" ] && [ "$album" != "" ]); then
artist_album="$artist - $album"
elif ([ "$artist" != "" ] && [ "$album" == "" ]); then
artist_album="by $artist"
elif ([ "$artist" == "" ] && [ "$album" != "" ]); then
artist_album="on $album"
else
artist_album=""
fi
# if there is no artist/album see what we can get out of the filepath
if [ "$artist_album" == "" ]; then
filetitle="$(echo "$currentsong" | grep "file: " | awk -F "/" '{for (i=1;i<=NF;i++) printf ("%s\n", $i)}' | perl -e '@line = <>; chomp(@line); print "@line[$#line]"')"
artist_album="$(echo "$currentsong" | grep "file: " | sed 's/file:\ //g' | sed "s#$musicsubdir\/##g" | sed 's/^[/]//g' | sed "s/\/$filetitle//g")"
fi
# set cover
cover="$(echo "$currentsong" | grep "file: " | sed "s#file: #\/$collectionpath\/#g" | awk -F "/" '{for (i=1;i<=NF;i++) printf ("%s\n", $i)}' | perl -e '@line = <>; chomp(@line); $dir=""; for($i=1;$i<$#line;$i++){ $dir = "$dir"."/"."@line[$i]" }; print "$dir"')/$covername"
# send infos
if [ -e "$cover" ]; then
notify-send "$title" "$artist_album" -i "$cover"
else
notify-send "$title" "$artist_album"
fi
Das ganze dann noch auf ein Tastenkürzel gelegt und fertig!
Kann jetzt alles bequem am Anfang vom Skript angepasst werden (dann müsst ihr ned im Skript rumsuchen
PS: damit das Skript auch bei dir funktioniert musst du wahrscheinlich ein bisschen anpassen:
Zeile 6: falls der MPD nicht auf dem PC läuft auf dem das Skript ausgeführt wird, oder falls der MPD auf nem anderen Port läuft
Zeile 71: Pfad zur Musiksammlung, falls dieser nicht der Standardpfad (/var/lib/mpd/music/)von MPD ist (dort wird nach dem Cover-Dateien gesucht)
Zeile 71: falls die Cover-Bilddateien bei dir anders heißen als cover.jpg
und jeweils in den Zeilen 42, 65 und 66: falls du dort erst einen Unterordner hast und in diesem die Musik ist (so wie bei mir z.b. …/musik/)
thx @dario für den Tipp)
Bei Fragen und Problemen ruhig melden
ich helf gern
UPDATE #1: Habe nun noch die Möglichkeit hinzugefügt den Host und Port im Skript anzugeben. Desweiteren habe ich herausgefunden, dass dieses Skript ohne weiteres so auch mit Gnome funktioniert, nicht nur mit KDE.
UPDATE #2: Skript wurde umbenannt in mpnotify, da ich herausgefunden habe, dass es so etwas ähnliches (mit dem Namen mpdnotify) bereits gibt. Die Variablen können nun direkt am Anfang konfiguriert werden … is einfacher als im Skript nach den richtigen Zeilen und in den jeweiligen Zeilen nach den richtigen Stellen zu suchen