Last active
February 24, 2024 01:38
-
-
Save bruhgettei/b9837e7bb9477270553835d8418e71ba to your computer and use it in GitHub Desktop.
Script for MPD notifications using gdbus and mpc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
notify () { | |
echo "$(gdbus call --session \ | |
--dest org.freedesktop.Notifications \ | |
--object-path /org/freedesktop/Notifications \ | |
--method org.freedesktop.Notifications.Notify \ | |
MPD \ | |
$1 \ | |
"" \ | |
"Now Playing:" \ | |
"${currentsong[0]} - ${currentsong[2]}" \ | |
[] \ | |
"{\"image-path\":<string '$2'>}" \ | |
5000 | |
)" | |
} | |
getSong () { | |
songinfo="$(mpc current --format "%artist%;%album%;%title%;%file%")" | |
IFS=";" # use ; as delimiter | |
read -ra nowplaying <<< "$songinfo" # read to nowplaying array, since bash cant return one. | |
} | |
gdbus monitor --session --dest org.freedesktop.Notifications > /tmp/mpd-notif-gdbmon & # redirect out to tmp file, detach | |
getSong # update nowplaying | |
oldsong=( ${nowplaying[@]} ) # copy nowplaying | |
id=0 | |
while "true" ; do | |
getSong | |
currentsong=( ${nowplaying[@]} ) | |
if [[ ${currentsong[@]} != ${oldsong[@]} ]] ; then | |
if [ -n ${currentsong[1]} ] ; then # if no album name, use artist+song name for thumb file instead | |
preview="$HOME/.config/mpd/previews/$(echo "${currentsong[0]}${currentsong[2]}" | basenc --base64url).png" | |
else | |
preview="$HOME/.config/mpd/previews/$(echo "${currentsong[1]}" | basenc --base64url).png" | |
fi | |
# generate thumb | |
[ -e "$preview" ] || ffmpeg -y -i "$HOME/Music/${currentsong[3]}" -an -vf scale=256:256 "$preview" > /dev/null 2>&1 | |
oldsong=(${currentsong[@]}) | |
# check if notif id is invalid. | |
if [ -n "$(grep --binary-files=text "uint32 $id, uint32 1" /tmp/mpd-notif-gdbmon)" ] ; then | |
id=0 | |
fi | |
# send notification, update id. | |
id="$(notify $id $preview)" | |
id="$(echo "$id" | sed -e "s/^(uint32 //" -e "s/,)$//")" | |
fi | |
mpc idle player > /dev/null # block till player state changes. | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment