See my update on July 10, 2010, below (making use of the neat “-iregex” option)!
Mplayer uses a simple kind of playlist: a text file with the path and name of each file to be played. The path to the new file is relative to the location of the playlist file itself. So, you can do:
find -maxdepth 1 -type f -name \*.\* | sort > playlist
This will find all files in the current directory that have a “.” character in it, sort them, and put their paths into playlist (a text file). We have to use the “-type f” argument because we want files, not directories. If you only want flac files, then you can use \*.flac instead. If you have files named “FLAC” as well as “flac”, you can use the “-iname” option to match case-insensitively. If you want to search deeper down directory levels, just increase the maxdepth value, or leave out the maxdepth parameter altogether to search recursively.
But let’s say your music directory is a bit messy with different filetypes. You can find multiple filetypes by just appending “-o -[i]name expression“, like this:
find -maxdepth 1 -type f -iname \*.flac -o -iname \*.ogg -o -iname \*.wav | sort > playlist
This will find all flac, ogg, and wav files case-insensitively in the current directory. You might even want to make a shell alias for this command (in your ~/.zshrc, ~/.bashrc, etc.), so that you don’t forget:
alias fa='find -maxdepth 1 -type f -iname \*.mp3 -o -iname \*.flac -o -iname \*.ogg -o -iname \*.wav -o -iname \*.aac | sort > playlist'
Now you can type fa (find audio) to generate a playlist for (most, if not all) audio files in the current directory. The only risk here is that playlist already exists, but that likelihood is small enough to ignore. This is because the “>” operator replaces whatever content that was inside the “playlist” file with the output of the previous command. If you just wanted to append the results to an existing playlist file, you could just use “>>” instead of “>”.
To play the playlist, just do:
mplayer -playlist playlist
…where “playlist” is the file with all the songs in it. Be sure to include the full path to the playlist if you are currently not inside the same directory. To make the entire playlist loop forever, type:
mplayer -playlist playlist -loop 0
and it will loop the entire playlist forever. Unfortunately, putting the “loop=0″ info in my ~/.mplayer/config file makes mplayer read that paramter first, and thus, only repeat the first file in my playlist forever. There seems to be no workaroud to this, except manually appending the loop paramter after the playlist parameter, as shown above.
UPDATE November 12, 2009: I discovered a simple hack around the above mentioned problem about trying to loop the entire playlist. The solution is to remove the “loop=0″ line from your ~/.mplayer/config, and instead make use of shell aliases. I use zsh, and this is what I have in my ~/.zshrc (the second alias is the key):
alias m='mplayer -loop 0' alias mp='mplayer -loop 0 -playlist'
Now, to play any single file infinitely, simply use “m [file]“. To infinitely loop an entire playlist, simply do “mp “. You can use the “>” and “<” hotkeys to move around the playlist.
UPDATE December 31, 2009: Silently updated the post to include the sort command. Without piping things to sort, the playlist would be somewhat random in order (find probably neglects sorting in exchange for faster results). Also added some more advanced techniques.
UPDATE July 17, 2010: Instead of typing a bunch of “-o -iname” flags, a simpler way is to just use a combined regular expression:
find -maxdepth 1 -type f -iregex ".*\.\(aac\|flac\|mp3\|ogg\|wav\)$" | sort > playlist