mocp / random / enqueue

mocp / random / enqueue

  • Written by
    Walter Doekes
  • Published on

After disk failure on our company music server, I lost my enqueue-some-random-music-script.

That shan’t happen again. So here, for my own enjoyment:

enqueue_app="mocp -a"
music_path="`dirname "$0"`"

if [ "$*" = "-c" ]; then
        # Create list of all files
        find . -type f -iname "$music_glob" > "$list_path.tmp" 2>/dev/null # no lost+found
        # Create list of all dirs that have files
        cat "$list_path.tmp" | sed -e 's/\/[^\/]*$//' | sort | uniq > "$list_path"
        exit 0

args="`echo "$*" | sed -e "s/['\\\\]//g"`" # no backslashes and single quotes please
args="`echo "$args" | sed -e 's/[[:blank:]]\+/.*/g'`" # replace blanks with .*
generator="grep -i '$args' '$list_path' | shuf"

exec 3<&0 # stdin as fd 3
eval $generator | while read -r dir; do
        echo -n "Enqueue $dir [Y/n]? "
        read answer <&3 # read from stdin
        if [ -z "$answer" -o "$answer" = "y" -o "$answer" = "Y" ]; then
                find "$dir" -maxdepth 1 -type f -iname "$music_glob" | xargs -d\\n $enqueue_app
                exit 0

Run /somewhere/ -c nightly to update the file (directory) list.

Example usage:

$ ./ ltj big bud
Enqueue ./LTJ/Big Bud - Fear of Flying/CD 2 [Y/n]? n
Enqueue ./LTJ/GLRMA 001 - Big Bud - Infinity & Infinity [Y/n]?

Now mocp is populated with the files from that second path.

Noteworthy parts in the code above:

  • We duplicate stdin to fd 3 in the main shell so we can use that in the while-subshell. If we didn’t, the answer to the question would be read from the generator as well.
  • The $generator is created earlier for extensibility, you may want to add grep -v stuff (exclusions) for certain command line parameters in the future.
  • It takes care to only enqueue files, not directories. Otherwise mocp will recursively get directory contents, which is not what you want. (Especially not if you have a stray mp3 file in your root music directory.)

Back to overview Newer post: openswan klips install / modules Older post: asterisk dialplan peculiarities / regex with eqtilde