MPD jingles

I recently create a bash script in order to automatically insert jingles into the mpd playlist:

#!/bin/bash
# directory of the database containing the jingles 
JINGLE_DIR='Jingles/'
# frequence of the jingles (one jingle every x tracks)
FREQ=5

# We add a random jingle at $FREQ position after the current playing song
# if there is no jingle in the next 2*$FREQ positions 


# current played song position
CURRENT=`mpc status | grep -oP '(?<=\[playing\] #)\d+'`

# if there is no playing song, exit
if [[ -z $CURRENT ]]
then
    exit 0
fi

# identify the next jingle
NEXT_JINGLE=`mpc playlist | tail -n +$CURRENT | head -n $((2*$FREQ)) | grep -hn "$JINGLE_DIR"`
# new jingles position 
NEW_POS=$(($CURRENT + 5))
LENGTH=`mpc playlist | wc -l`

if [[ -z $NEXT_JINGLE && (( $NEW_POS < $LENGTH )) ]]
then
    # get random jingle
    JINGLE=`mpc ls "$JINGLE_DIR" |shuf -n 1`
    echo "Insert $JINGLE at position $NEW_POS"
    # insert the jingles at the end of the playlist
    # and move it at $NEW_POS
    mpc add "$JINGLE" && mpc mv $(($LENGTH + 1)) $NEW_POS
fi

exit 0

The script is called each minute using following crontab entry:

# m  h  dom mon dow   command
  *  *   *   *   *    /home/user/mpc_jingle.sh >>/tmp/mpc_jingle.log 2>&1

This post accepts webmentions. Do you have the URL to your post?

Otherwise, send your comment on my service.

Or interact from the fediverse with your username: