Skip to content

Instantly share code, notes, and snippets.

@bonelifer
Forked from bruhgettei/mpd_notifs.sh
Created February 24, 2024 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bonelifer/73923c4e893731217ffd085ece9974b8 to your computer and use it in GitHub Desktop.
Save bonelifer/73923c4e893731217ffd085ece9974b8 to your computer and use it in GitHub Desktop.
Script for MPD notifications using gdbus and mpc
#!/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