Playlists in Pi Musicbox


Pi Musicbox has no built-in functionality for creating playlists for local music files that are located on the SD card or a USB drive. Playlists (m3u files) need to be located (by default at least, this can be changed in settings.ini) in /var/lib/mopidy/playlists. Filenames must be absolute of course. I started out by creating these playlists manually, but since they are simple text files, it can be done in a few lines of bash script:

#!/bin/bash
rm -f /var/lib/mopidy/playlists/"$2".m3u
for filename in "$1"/*.mp3
do
    echo $filename >> /var/lib/mopidy/playlists/"$2.m3u"
done

Save it in the root directory as playlist.sh and make it executable via chmod a+x playlist.sh. You can then run it via ./playlist.sh directory playlistname, e.g. ./playlist.sh /music/USB/Free Free. It is important to supply the directory without trailing backslash.

This works fine for adding playlists for one or two directories, but it still quite slow when adding them for a couple. In comes the following script, that automatically does the same thing for all subdirectories in a given directory (Caution: This deletes all existing m3u files):

#!/bin/bash
rm -f /var/lib/mopidy/playlists/*.m3u
find "$1" -type d |
while read subdir
do
        for filename in "$subdir"/*.mp3
        do
          if [ -e "$filename" ]
          then
                echo $filename >> /var/lib/mopidy/playlists/"${subdir##*/}.m3u"
          fi
        done
done

It is invoked, after saving to playlists.sh and chmod a+x playlists.sh, with ./playlists.sh /music/USB, or wherever the MP3 files are located. Mopidy then needs to be restarted via /etc/init.d/mopidy restart to make the playlists visible in the web interface.

Leave a comment

Your email address will not be published.