Skip to content

Instantly share code, notes, and snippets.

@bonelifer
Forked from Jipok/mpd-notifications
Last active January 26, 2025 04:35
Show Gist options
  • Save bonelifer/1ca18b478fa91aa52c902583faa6581f to your computer and use it in GitHub Desktop.
Save bonelifer/1ca18b478fa91aa52c902583faa6581f to your computer and use it in GitHub Desktop.
Show music info(with cover) for mpd. Will show gray cover, when paused. defaultimage can be found here: https://williamjacoby.net/files/unknown.jpg
#!/bin/bash
expireTime="9200"
mpddir="/media/william/NewData/Music/MP3B/"
defaultimage="$mpddir/unknown.jpg"
update_cover() {
path=$mpddir$(mpc -f %file% current)
album=$(mpc -f %file% current)
ffmpeg -loglevel quiet -y -i "$path" /tmp/cover
if [[ $? -ne 0 ]] ; then
covers="$(find "$(dirname "$path")" -type d -exec find {} -maxdepth 1 -type f -iregex ".*/.*\(${album}\|cover\|folder\|artwork\|front\).*[.]\(jpe?g\)" \; )"
src="$(echo -n "$covers" | head -n1)"
if [[ -f $src ]] ; then
/bin/cp "$src" /tmp/cover
else
cp "$defaultimage" /tmp/cover
fi
fi
}
# Remove extra spaces and escape single quotes
sanitize_string() {
# Replaces single quotes with escaped ones and trims leading/trailing spaces
echo "$1" | sed "s/'/\\\'/g" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
}
while true; do
mpc -q || exit 1
[[ $(mpc idle) = "player" ]] || continue
update_cover
# Capture artist, title, and album from the current song using mpc
song_info=$(mpc current -f '%artist% * %title% * %album%')
artist=$(echo "$song_info" | cut -d '*' -f 1)
title=$(echo "$song_info" | cut -d '*' -f 2)
album=$(echo "$song_info" | cut -d '*' -f 3)
# Sanitize artist, title, and album to escape single quotes and trim spaces
artist=$(sanitize_string "$artist")
title=$(sanitize_string "$title")
album=$(sanitize_string "$album")
# Get length and status
status_output=$(mpc status)
length=$(echo "$status_output" | sed '2!D ; s/\[.* //')
status=$(echo "$status_output" | grep -o '\[.*\]' | tr -d "[]")
category="mpd"
if [[ ${status} = "paused" ]]; then
category="mpd-paused"
convert "/tmp/cover" -colorspace Gray "/tmp/cover"
fi
# Send notification without formatting
notify-send --expire-time="${expireTime}" -c ${category} -i "/tmp/cover" "MPD" \
"Title: ${title}\nArtist: ${artist}\nAlbum: ${album}\nStatus: ${length}"
done;
@bonelifer
Copy link
Author

Fixed: displaying extra spaces and escaped single quotes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment