<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <title>
      Self‐Playing Playlists
     | Two 〈Foo〉s Walk into a 〈Bar〉</title>
  
  <updated>2008-07-07T22:06:00-05:00</updated>
  <author>
    <name>Chris Povirk</name>
    <email>beigetangerine@gmail.com</email>
    <uri>http://twofoos.org/</uri>
  </author>
  <link rel="alternate" type="application/xhtml+xml" hreflang="en" href="http://twofoos.org/content/self-playing-playlist/"/>
  <link rel="self" type="application/atom+xml" href="http://twofoos.org/content/self-playing-playlist/feed/"/>
  <rights>Copyright © 2003–2015 Chris Povirk.  All Rights Reserved.</rights>
  <id>http://twofoos.org/</id>
  <generator uri="http://twofoos.org/content/splat/" version="1.0">SPLAT</generator>
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
<entry>
    <updated>2008-07-07T22:06:00-05:00</updated>
    <summary>
            New: Because typing mplayer -playlist instrumental is so much harder than typing instrumental.</summary>
    <title type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">Self‐Playing Playlists</div>
    </title>
    <link rel="alternate" type="application/xhtml+xml" href="http://twofoos.org/content/self-playing-playlist/#update_2008-07-07T22:06:00-05:00"/>
    <id>http://twofoos.org/content/self-playing-playlist/#update_2008-07-07T22:06:00-05:00</id>
    <category term="linux"/>
    <category term="scripts"/>
    <content type="xhtml" xml:base="http://twofoos.org/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <strong>initial posting</strong>
        <div>

    <xhtml:h3>Playlists</xhtml:h3>
    <div>
      <p>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 <code>#</code>.  Here is a sample playlist, which I've put in a file named <code>instrumental</code>:</p>
      <pre># Instrumental songs
/music/NickelCreek/Why Should the Fire Die/Scotch &amp; Chocolate.mp3
/music/John Williams/Figrin D'an and the Modal Notes/Cantina Band #1.mp3</pre>

      <p>You can play this playlist by typing <code>mplayer -playlist instrumental</code> or, if you want it to be shuffled, <code>mplayer -shuffle -playlist instrumental</code>.  But why not make it executable so that I can just type <code>instrumental</code>?  And why not default to shuffle for certain playlists if that's what I want?</p>
    </div>

    <xhtml:h3>Self‐Playing Playlists</xhtml:h3>
    <div>
      <p>By using <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)">shebang notation</a>, we can tell the <abbr>OS</abbr> how to "execute" our playlist.  Instead of picking a script interpreter like <code>/usr/bin/python</code>, we'll use <code>/usr/bin/mplayer</code>, along with a flag to tell it that the file is a playlist.  (If <code>mplayer</code> is located elsewhere on your system, running <code>which mplayer</code> should find it.)  Here is our modified playlist:</p>
      <pre>#!/usr/bin/mplayer -playlist
# Instrumental songs
/music/NickelCreek/Why Should the Fire Die/Scotch &amp; Chocolate.mp3
/music/John Williams/Figrin D'an and the Modal Notes/Cantina Band #1.mp3</pre>
      <p>Since the line we added starts with <code>#</code>, it is treated as a comment when <code>mplayer</code> reads the playlist.  If you <code>chmod +x instrumental</code> and move it to a directory in your <code>PATH</code>, you should be able to type <code>instrumental</code> to play the playlist.</p>
      <p>Note that you can always type <code>mplayer -shuffle -playlist instrumental</code> or any other command if you wish to play the playlist with a different set of command‐line flags.</p>

      <xhtml:h4>Self‐Playing, Self‐Shuffling Playlists</xhtml:h4>
      <div>
        <p>Maybe you'd like you playlist to play in shuffled order by default.  The command‐line argument to accomplish this is <code>-shuffle</code>, so you might try this:</p>
        <pre>#!/usr/bin/mplayer -shuffle -playlist
# Instrumental songs
/music/NickelCreek/Why Should the Fire Die/Scotch &amp; Chocolate.mp3
/music/John Williams/Figrin D'an and the Modal Notes/Cantina Band #1.mp3</pre>
        <p>It won't work.  When you execute the playlist, you'll get an error:</p>
        <pre>Unknown option on the command line: -shuffle -playlist</pre>
        <p>When running the script, the <abbr>OS</abbr> passes the entire string "<code>-shuffle -playlist</code>" 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:</p>
        <pre>#!/usr/bin/perl -esystem('/usr/bin/mplayer', '-shuffle', '-playlist', @ARGV)
# Instrumental songs
/music/NickelCreek/Why Should the Fire Die/Scotch &amp; Chocolate.mp3
/music/John Williams/Figrin D'an and the Modal Notes/Cantina Band #1.mp3</pre>
        <p>The <code>-e</code> tells Perl to execute the given expression.  That expression runs <code>mplayer</code> with three arguments: <code>-shuffle</code>, <code>-playlist</code>, 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.</p>
        <p>Again, you are free to run <code>mplayer -playlist instrumental</code> if you want to override the shuffle.</p>
      </div>

    </div>

  </div>
      </div>
    </content>
  </entry></feed>
