Skip to content

Instantly share code, notes, and snippets.

@Jach
Last active January 21, 2017 23:37
Show Gist options
  • Save Jach/1692261 to your computer and use it in GitHub Desktop.
Save Jach/1692261 to your computer and use it in GitHub Desktop.
Shell-based jukebox with mpv and json ipc handling single commands like q for quit, p for pause. Requires socat.
#!/bin/sh
rm /tmp/fifo
mkfifo /tmp/fifo
mdir='/mnt/media/music/ /home/kevin/Desktop/some_sound/'
filter='(\.jpg|\.png|\.bmp|\.m3u|\.ini)'
function maybe_filter() {
while read -r -d $'\0' input; do
if [[ $input == *tons_of_video_game_music* ]]; then
if [ $RANDOM -lt 16384 ]; then
echo $input
fi
else
echo $input
fi
done
}
while true; do
find -L $mdir -type f -print0 | sort -R --random-source=/dev/urandom -z | maybe_filter | egrep -v -i $filter | shuf -n 1 --random-source=/dev/urandom | sed -e "s/'/'\\\''/g" | awk '{print "'\''"$0"'\''"}' | xargs mpv --audio-display=no -quiet -input-ipc-server=/tmp/fifo; done &
p=`jobs -p`
trap ctrl_c INT
function ctrl_c() {
kill $p
reset
exit 0
}
sendstart='{ "command": ['
sendend=' ] }'
paused='no'
while true; do
read -sn1 key;
test "$key" == "q" && (echo "$sendstart \"quit\" $sendend" | socat - /tmp/fifo && continue);
if [[ "$key" == "p" ]]; then
if [[ "$paused" = 'no' ]]; then
paused='yes';
else
paused='no';
fi
echo "$sendstart \"set\",\"pause\",\"$paused\" $sendend" | socat - /tmp/fifo && continue;
fi
done
@Jach
Copy link
Author

Jach commented Mar 7, 2012

Looks like it still chokes on some files with single-quotes in their name. Eh, I'll fix it later.

@Jach
Copy link
Author

Jach commented Mar 8, 2012

Problem solved. Also yay for a filter.

@Jach
Copy link
Author

Jach commented Jun 17, 2014

Posted a change I made a while back that allows for more specific filtering that may be all-or-nothing or random or whatever you like. (I have literally thousands of video game music files that tend to dominate the jukebox selection unless I make them less likely.)

@Jach
Copy link
Author

Jach commented Jan 21, 2017

For a long time this still 'worked' with mpv replacing mplayer, then mpv killed the feature for sending commands and require using json ipc.. Updated it but only supports q and p right now, seek support maybe in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment