Self‐Playing Playlists
Playlists
Create a playlist file as normal. A playlist is just a text document with one file on each line. There's no need to escape odd characters like spaces, apostrophes, and ampersands. You can include comments by starting the line with #
. Here is a sample playlist, which I've put in a file named instrumental
:
# Instrumental songs /music/NickelCreek/Why Should the Fire Die/Scotch & Chocolate.mp3 /music/John Williams/Figrin D'an and the Modal Notes/Cantina Band #1.mp3
You can play this playlist by typing mplayer -playlist instrumental
or, if you want it to be shuffled, mplayer -shuffle -playlist instrumental
. But why not make it executable so that I can just type instrumental
? And why not default to shuffle for certain playlists if that's what I want?
Self‐Playing Playlists
By using shebang notation, we can tell the OS how to "execute" our playlist. Instead of picking a script interpreter like /usr/bin/python
, we'll use /usr/bin/mplayer
, along with a flag to tell it that the file is a playlist. (If mplayer
is located elsewhere on your system, running which mplayer
should find it.) Here is our modified playlist:
#!/usr/bin/mplayer -playlist # Instrumental songs /music/NickelCreek/Why Should the Fire Die/Scotch & Chocolate.mp3 /music/John Williams/Figrin D'an and the Modal Notes/Cantina Band #1.mp3
Since the line we added starts with #
, it is treated as a comment when mplayer
reads the playlist. If you chmod +x instrumental
and move it to a directory in your PATH
, you should be able to type instrumental
to play the playlist.
Note that you can always type mplayer -shuffle -playlist instrumental
or any other command if you wish to play the playlist with a different set of command‐line flags.
Self‐Playing, Self‐Shuffling Playlists
Maybe you'd like you playlist to play in shuffled order by default. The command‐line argument to accomplish this is -shuffle
, so you might try this:
#!/usr/bin/mplayer -shuffle -playlist # Instrumental songs /music/NickelCreek/Why Should the Fire Die/Scotch & Chocolate.mp3 /music/John Williams/Figrin D'an and the Modal Notes/Cantina Band #1.mp3
It won't work. When you execute the playlist, you'll get an error:
Unknown option on the command line: -shuffle -playlist
When running the script, the OS passes the entire string "-shuffle -playlist
" as a single argument, not the two separate arguments that we want. We need some way to split a string into two pieces. Sounds like a job for a notoriously cryptic, Turing‐complete language:
#!/usr/bin/perl -esystem('/usr/bin/mplayer', '-shuffle', '-playlist', @ARGV) # Instrumental songs /music/NickelCreek/Why Should the Fire Die/Scotch & Chocolate.mp3 /music/John Williams/Figrin D'an and the Modal Notes/Cantina Band #1.mp3
The -e
tells Perl to execute the given expression. That expression runs mplayer
with three arguments: -shuffle
, -playlist
, and the remaining command‐line arguments, i.e., the playlist. In a fun twist, our original problem, that the remainder of the line is treated as a single argument, becomes part of our solution, as we can pass an expression containing spaces to Perl without requiring any quoting.
Again, you are free to run mplayer -playlist instrumental
if you want to override the shuffle.