Execute on song change
Rmpc provides on_song_change property in the config file which can be used to run a command whenever the song changes. This can be used for various purposes like showing a desktop notification, automatic lyrics download and more.
All song metadata are available to the script as environment variables. For example $TITLE
, $FILE
, $DURATION
, etc.
Additional environment variables available to the script are:
$VERSION
- rmpc’s version$PID
- intended for use with theremote
command and allows it to target specific rmpc instance$LRC_FILE
- contains the path where rmpc expects to find the lrc file$HAS_LRC
- indicates whether any lyrics were found for the current song.
Desktop notification on song change
Assuming you have a notification daemon with support for images like dunst running.
-
Create a script and place it somewhere. For example
~/.config/rmpc/notify
. Below is an example of such script. Edit it to your needs or create a new one.~/.config/rmpc/notify #!/usr/bin/env sh# Directory where to store temporary dataTMP_DIR="/tmp/rmpc"# Ensure the directory is createdmkdir -p "$TMP_DIR"# Where to temporarily store the album art received from rmpcALBUM_ART_PATH="$TMP_DIR/notification_cover"# Path to fallback album art if no album art is found by rmpc/mpd# Change this to your needsDEFAULT_ALBUM_ART_PATH="$TMP_DIR/default_album_art.jpg"# Save album art of the currently playing song to a fileif ! rmpc albumart --output "$ALBUM_ART_PATH"; then# Use default album art if rmpc returns non-zero exit codeALBUM_ART_PATH="${DEFAULT_ALBUM_ART_PATH}"fi# Send the notificationnotify-send -i "${ALBUM_ART_PATH}" "Now Playing" "$ARTIST - $TITLE" -
Make the script executable
Terminal window chmod +x ~/.config/rmpc/notify -
In your rmpc’s
config.ron
specify on_song_change property and point it at location of the script from step 1.config.ron #![enable(implicit_some)]#![enable(unwrap_newtypes)]#. Please note that rmpc has to be running for the script to execute and increment the play count.
-
Configure sticker_file in your
mpd.conf
-
Create a script and place it somewhere. For example
~/.config/rmpc/playcount
and make the script executable. Or append it to an existingon_song_change
script. -
Increment playCount sticker on the song inside the script. (requires jq)
#!/usr/bin/env shsticker=$(rmpc sticker get "$FILE" "playCount" | jq -r '.value')if [ -z "$sticker" ]; thenrmpc sticker set "$FILE" "playCount" "1"elsermpc sticker set "$FILE" "playCount" "$((sticker + 1))"fi -
Change your
config.ron
to execute the script you created on song changeconfig.ron #![enable(implicit_some)]#![enable(unwrap_newtypes)]#]#![enable(unwrap_newtypes)]#,(prop: (kind: Sticker("playCount"), default: (kind: Text("0"))),width: "9",alignment: Right,label: "Playcount"),(...),],...
Automatically fetch lyrics
Provided by wvffle on github. This script allows you to automatically
download lrc files from lrclib.net. Status message is also displayed in rmpc when the operation
finishes. The script assumes you have your lyrics_dir
configured. The script is just an example and you are encouraged
to create your own!
#!/bin/env sh
LRCLIB_INSTANCE="https://lrclib.net"
if [ "$HAS_LRC" = "false" ]; then mkdir -p "$(dirname "$LRC_FILE")"
LYRICS="$(curl -X GET -sG \ -H "Lrclib-Client: rmpc-$VERSION" \ --data-urlencode "artist_name=$ARTIST" \ --data-urlencode "track_name=$TITLE" \ --data-urlencode "album_name=$ALBUM" \ "$LRCLIB_INSTANCE/api/get" | jq -r '.syncedLyrics')"
if [ -z "$LYRICS" ]; then rmpc remote --pid "$PID" status "Failed to download lyrics for $ARTIST - $TITLE" --level error exit fi
if [ "$LYRICS" = "null" ]; then rmpc remote --pid "$PID" status "Lyrics for $ARTIST - $TITLE not found" --level warn exit fi
echo "[ar:$ARTIST]" >"$LRC_FILE" { echo "[al:$ALBUM]" echo "[ti:$TITLE]" } >>"$LRC_FILE" echo "$LYRICS" | sed -E '/^\[(ar|al|ti):/d' >>"$LRC_FILE"
rmpc remote --pid "$PID" indexlrc --path "$LRC_FILE" rmpc remote --pid "$PID" status "Downloaded lyrics for $ARTIST - $TITLE" --level infofi