I’ve got mopidy 2.2.3 setup with the local-sqlite plugin for playing music stored on a local disk. For normal use, everything is working well!
Sometimes though, I want to play a specific file or folder of files from the command line, where I have the file/folder name and path. At that point, going to the UI and seaching for the file is a bit of pain.
Based on earlier posts here, I’ve written a script that wraps mpc to do this. The script (see below) doesn’t quite do everything, and is longer than I initially expected… Before I finish it off, I wanted to check if this was still the best way to go about doing something like this?
A few issues, most worked around:
- File names need to be relative to the root of the media_dir setting, then prefixed with “local:track:”
- File names need to be URL escaped (eg for spaces)
- “mpc add” doesn’t seem to work with directories, need to list all the files in a directory and add them individually not sure if this is the best solution
- Only files which have been seen by a “mopidy local scan” can be added/played, anything added since then will refuse to add problem unsolved
Is there a better way? Can I do the directory thing better? Can I handle un-scanned files?
The (I think) relevant bit of my mopidy config is
[local]
library = sqlite
scan_flush_threshold = 100
media_dir = /media/music/music/
And the script I’ve written (only takes a single argument at the moment, of a single file or folder to play) is
#!/bin/bash
# Plays a single file File or Directory, which is located within the
# Local Library area
# TODO Support multiple files / directories, wildcard arguments etc
# Mopidy Local media_dir directory
BASE="/media/music/music/"
# URI prefix used by Mopidy for local files
MPREFIX="local:track:"
# Zap any playlist that might exist
mpc clear
# Grab the single argument of the file or directory to play
MUSIC=$1
# Convert to an absolute path, so we're consistent on relative vs absolute
MUSIC=`readlink -f $MUSIC`
# Let the user know what we're adding, before we mangle stuff (more)
echo ""
echo "Adding music from $MUSIC"
# Converts a file to a suitable URL with prefix and escaping
function add {
RPATH=${1#"$BASE"}
echo " - $RPATH"
ESCPATH=$(python -c "import urllib, sys; print urllib.quote(sys.argv[1])" "$RPATH")
mpc add "${MPREFIX}${ESCPATH}"
}
# Directories are not supported, so find and add all files within them
if [[ -d $MUSIC ]]; then
find $MUSIC -type f -print0 | sort -z | while read -d $'\0' FILE
do
add "$FILE"
done
else
add "$MUSIC"
fi
# Off we go!
echo ""
mpc play