Wednesday, July 23, 2008

Random, shuffled, endless playlists with mplayer and sh

It's portable and easy. There's no need to do shell / environment specific stuff, mess with arrays in shell scripts or worry about spaces in filenames: this works in any semi-POSIX compliant environment that can run mplayer.

while `true`; do find "$1" -name \*.mp3 | while read filename; do echo $filename >> /tmp/pls.$$; done; mplayer -shuffle -playlist /tmp/pls.$$; rm /tmp/pls.$$; done

I have this plopped in ~/bin/play and run play ~/mp3 which works well for me. You could also do 2>&1 >/dev/null & or >& /dev/null & (for sh/csh like shells respectively) to kill output and background the process if you want.

Have fun :)

1 comments:

PinkPanther said...

Thanks for your nice script.
I use a slightly different method using 'locate'.
Below are the contents of the script, put it in a file called 'locplay' and make it executable.
Then call it something like this:
locplay pearl.jam
or
locplay hip.*hop

#!/bin/sh
tempfile=/tmp/locplay.txt
locate -ir "$1"|egrep '(\.mp3|\.ogg)$' > $tempfile
if [[ -s $tempfile ]]; then
cat $tempfile
echo
echo 'Use Arrow Keys & PGUP/PGDWN to skip play time, «<>» = back/forw. in playlist, «[]» to change speed, «SPACE» to pause, «m» to mute'
mplayer -shuffle -playlist $tempfile
fi