{"id":315,"date":"2006-12-17T01:43:37","date_gmt":"2006-12-17T07:43:37","guid":{"rendered":"http:\/\/blog.codedread.com\/archives\/2006\/12\/17\/setting-up-mythvideo-for-playlists\/"},"modified":"2006-12-17T01:43:37","modified_gmt":"2006-12-17T07:43:37","slug":"setting-up-mythvideo-for-playlists","status":"publish","type":"post","link":"https:\/\/www.codedread.com\/blog\/archives\/2006\/12\/17\/setting-up-mythvideo-for-playlists\/","title":{"rendered":"Setting Up MythVideo For Playlists"},"content":{"rendered":"<p>I have a bunch of cartoons and film shorts on my hard drive.  Now that I've installed Suse, I have video\/audio output to my TV\/Stereo, and I've set up <a href=\"http:\/\/www.mythtv.org\/\">Myth<\/a>, I wanted to figure out how to play a series of videos from a playlists (.pls) file.  Here's how I did it.  As a bonus, I also set up Myth with an option to randomize a playlist as well.  <!--more--><\/p>\n<p><script type=\"text\/javascript\" src=\"http:\/\/www.codedread.com\/googleads.js\"><\/script><br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\"><\/script><\/p>\n<p>MythVideo uses <a href=\"http:\/\/www.mplayerhq.hu\/\">MPlayer<\/a> to play its videos.  The command-line option for MPlayer to play a <a href=\"http:\/\/en.wikipedia.org\/wiki\/M3U\">M3U playlist<\/a> is <i>-playlist &#60;file.pls><\/i>.  The trick here is to convince MythVideo to invoke MPlayer with this option when referencing a playlist file.<\/p>\n<h3>Step By Step<\/h3>\n<ul>\n<li>Bring up the Myth front-end (run \"mythfrontend&\")<\/li>\n<li>Navigate to Setup > Videos Settings > File Types:<br \/>\n<table>\n<tr>\n<td><img decoding=\"async\" src=\"http:\/\/www.codedread.com\/images\/myth1-thumb.png\"\/><\/td>\n<\/tr>\n<tr>\n<td><img decoding=\"async\" src=\"http:\/\/www.codedread.com\/images\/myth2-thumb.png\"\/><\/td>\n<\/tr>\n<tr>\n<td><img decoding=\"async\" src=\"http:\/\/www.codedread.com\/images\/myth3-thumb.png\"\/><\/td>\n<\/tr>\n<\/table>\n<\/li>\n<li>Select the \"New\" button<br \/>\n<table>\n<tr>\n<td><img decoding=\"async\" src=\"http:\/\/www.codedread.com\/images\/myth4-thumb.png\"\/><\/td>\n<\/tr>\n<\/table>\n<\/li>\n<li>Enter \"pls\" as the new extension and select the \"Create new extension\" button<\/li>\n<table>\n<tr>\n<td><img decoding=\"async\" src=\"http:\/\/www.codedread.com\/images\/myth5-thumb.png\"\/><\/td>\n<\/tr>\n<\/table>\n<li>Navigate to the Command box and type in \"mplayer -fs -playlist %s\".  Make sure you uncheck the \"Use default player\" checkbox<br \/>\n<table>\n<tr>\n<td><img decoding=\"async\" src=\"http:\/\/www.codedread.com\/images\/myth6-thumb.png\"\/><\/td>\n<\/tr>\n<\/table>\n<\/li>\n<li>Select the \"Done\" button<\/li>\n<\/ul>\n<p>Now escape back to the Myth home menu and go to \"Videos\", select \"Video List\" and find a playlist file (this is a plain text file that has one video listed per line and ends in .pls). Once it's selected, your playlist should start playing in order.<\/p>\n<p>This thing is kind of nice if you're a movie-goer freak and, for instance, want to set up a \"movie night\" such that a cartoon and\/or comedy short is played before the \"main feature\".  It would also be worthwhile if you're into music videos or maybe you want to watch all the Lost or Twin Peaks episodes in chronological order without ever having to touch your remote\/keyboard\/mouse.  By the way, to skip ahead one video file, press the \">\" key (on my keyboard I have to press Shift and the period key to do this),<\/p>\n<p><script type=\"text\/javascript\" src=\"http:\/\/www.codedread.com\/googleads.js\"><\/script><br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\"><\/script><\/p>\n<h3>Shuffling A Video Playlist<\/h3>\n<p>Now I wanted to take this a step further and actually randomize the playlist every time it plays.<\/p>\n<p>A ways back, I wrote a quick awk script to shuffle a playlist file.  Nothing fancy or terribly efficient, but it works:<\/p>\n<div class=\"code\">\n#!\/usr\/bin\/gawk -f<br \/>\nBEGIN {<br \/>\n    srand();<br \/>\n    num_records = 0;<br \/>\n}<br \/>\n{<br \/>\n    indices[num_records] = num_records;<br \/>\n    records[num_records] = $0;<br \/>\n    ++num_records;<br \/>\n}<br \/>\nEND {<br \/>\n    for(ind = 0; ind &#60; num_records; ++ind) {<br \/>\n        swap = int(rand() * num_records)<br \/>\n        temp = indices[ind]<br \/>\n        indices[ind] = indices[swap]<br \/>\n        indices[swap] = temp<br \/>\n    }<br \/>\n    for(ind = 0; ind &#60; num_records; ++ind) {<br \/>\n        printf records[indices[ind]] \"\\n\";<br \/>\n    }<br \/>\n}\n<\/div>\n<p>Save the above file as \/usr\/bin\/shuffle and ensure it has executable permissions (\"chmod +x shuffle\").  You'll need to be root for that.<\/p>\n<p>So to make a playlist that shuffles every time before playing, I followed the same Myth directions I gave above but I created a new File type (\".spl\" for Shuffled PlayList).  This time, the Command I want MythVideo to run for a Shuffled PlayList is:<\/p>\n<div class=\"code\">cat %s &#124; shuffle > $$.spl ; mv $$.spl %s; mplayer -fs -playlist %s<\/div>\n<p>Rename your playlists to have the .spl extension and MythVideo should randomize them every time they are played.  WARNING:  Backup your playlist files and <em>use a copy<\/em> if you decide to do this, since the shuffled playlist overwrites itself with every play there's some minimal risk that a burp in the system could cause you to lose your playlist.<\/p>\n<p><i>Update 2007-02-18<\/i>: To use xine instead of MPlayer (which I now do), you have to use a slightly different command:<\/p>\n<div class=\"code\">cat %s &#124; shuffle > $$.spl ; mv $$.spl %s; xine -f --playlist %s<\/div>\n<p>On the next page, for the curious, I'll describe how the above works in more detail (for those interested in learning about the power of Unix).  <!--nextpage--><\/p>\n<h3>How It Works - A Unix\/Linux Command-Line Primer<\/h3>\n<p>For those who want to learn a little more about Linux, here's a step-by-step \"decoder ring\" of the command:<\/p>\n<div class=\"code\">cat %s &#124; shuffle > $$.spl; mv $$.spl %s; mplayer -fs -playlist %s<\/div>\n<p>First, you should understand that Unix\/Linux's command-line (like most operating system command-lines) give you a tremendous amount of power.  In fact, the shell that you run (i.e. <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bash\">bash<\/a>) has its own programming language that allows powerful programming constructs for variable storage, condition-checking, mathematical calculations and looping.  Combine this with other powerful Unix tools like sed, awk, grep, cut and you can actually do quite a lot just in the shell.  I say this both to warn you and encourage you to explore the shell and various command-line tools once you're comfortable enough with them.<\/p>\n<p><script type=\"text\/javascript\" src=\"http:\/\/www.codedread.com\/googleads.js\"><\/script><br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\"><\/script><\/p>\n<p>Now for this example, we're not doing anything terribly tricky or impressive, though all those symbols might make it look confusing to a newbie.  First off, we're just telling Myth to execute three statements in the above.  The shell determines each statement by the semicolon separators.  So, the first statement is:<\/p>\n<div class=\"code\">cat %s &#124; shuffle > $$.spl<\/div>\n<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Cat_%28Unix%29\">cat<\/a> is the standard Unix command to (con)catenate file(s), the results of which are output.  Putting only one filename as an argument to \"cat\" is the equivalent of asking your computer to print out the contents of that file.  For example, if you had a text file named <i>greeting.txt<\/i> that contained the text \"Hello, world!\" and you typed:<\/p>\n<div class=\"code\">cat greeting.txt<\/div>\n<p>on the command line, you would see \"Hello, world!\" printed out and then the next command-line prompt.  The reason that the file prints out to your terminal is because, by default, \"cat\" prints its results to the \"standard output\", which is the terminal where the command was invoked.  There are other things you can send output to (other than \"standard output\") as we shall later see.<\/p>\n<p>The next thing in our first statement is %s.  This is a special string which MythVideo recognizes.  You should think of this as a special symbol (or \"variable\") that holds the filename that you've selected in Myth.  For instance, if I selected a playlist called \"LittleRascals.pls\" on the Myth screen, then \"cat %s\" is equivalent to \"cat LittleRascals.pls\".<\/p>\n<p>The next thing in our first statement is a pipe character (&#124;).  On my keyboard, to get this symbol I hold shift and press the backslash key (\\).  This tells Unix\/Linux to <i><a href=\"http:\/\/en.wikipedia.org\/wiki\/Pipeline_%28Unix%29\">pipe<\/a> the output of the first command and feed it as input to a second command<\/i>.  This is an important concept to understand.  Think of it as telling the computer to chain commands together like in a factory assembly line.  The output of one command becomes the input of the next command.  In this example, the output of the \"cat\" command (your text file) becomes the input of the next command (which is \"shuffle\") in the above.  (Sidebar:  One of the key tenets of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Unix\">Unix<\/a> is to \"do one thing and do it well\".  Thus, we have many individual commands like cat, grep, etc that do one thing like print a text file, search for a string, etc.  The output of these commands can be chained to other commands to form a larger construct that is more powerful.  That's why the concept of a pipeline is so important).<\/p>\n<p>Anyway, I'm not going to get into the details of how the shuffle <a href=\"http:\/\/en.wikipedia.org\/wiki\/AWK_%28programming_language%29\">AWK<\/a> script works as it's a lot more involved than I have time for.  Though I will take a moment to say that AWK is one of my favourite Unixy-flavour programming languages and I'm always surprised at how much use I can get out of it.  I also appreciate its C-like syntax (not surprising, since the \"K\" in AWK stands for <a href=\"http:\/\/en.wikipedia.org\/wiki\/Brian_Kernighan\">Brian Kernighen<\/a>, one of the people that Dennis Ritchie worked with when creating the the C programming language).  Anyway, the \"shuffle\" script takes any input text, shuffles its lines and prints out the shuffled lines.  Remember:  <em>Do one thing and do it well<\/em>.<\/p>\n<p><script type=\"text\/javascript\" src=\"http:\/\/www.codedread.com\/googleads.js\"><\/script><br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\"><\/script><\/p>\n<p>Let's recap our first statement re-construction so far:  \"cat %s &#124; shuffle\".  This is the Bash way of saying \"send the playlist text file as input to the shuffle command and print out the shuffled playlist\".<\/p>\n<p>But we don't just want to print out the playlist, we want to save it as a file so we can later have MPlayer play the file.  Thus, the next thing we need to specify is that the output of the shuffle command should be sent to a file and that's what the \">\" character does.  The > character says \"write output to a filename which I specify and not to the command-line output\".  We could have done:<\/p>\n<div class=\"code\">cat %s &#124; shuffle > temp.spl<\/div>\n<p>Which would have shuffled whatever playlist was selected in Myth and wrote the shuffled playlist out to a file temp.slp.  However, the problem with doing the above is what if someone else is using Myth at the same time as you?  Or what if you already had a file named \"temp.spl\" or later wanted to create one?  In other words, specifying a filename could have later caused you problems that can easily be avoided by using another Unix\/Linux trick:  $$<\/p>\n<p>You can think of $$ as a way to get a temporary unique number in your shell scripts, though you should also try to understand that it is the process number of whatever process is running your script at this moment.  As long as a process is alive, its process number is unique to the Linux\/Unix kernel, so this is a somewhat safe way to save a temporary file and to ensure that only this current process will use it.  Now we've completed the first statement that Myth will run:<\/p>\n<div class=\"code\">cat %s &#124; shuffle > $$.spl<\/div>\n<p>Which is a bash way of saying:  Take my playlist, send it to the shuffle command and save the shuffled playlist output in a temporary file.<\/p>\n<p>The next statement is much simpler:<\/p>\n<div class=\"code\">mv $$.spl %s<\/div>\n<p>The <a href=\"http:\/\/en.wikipedia.org\/wiki\/Mv\">mv<\/a> command is used to move or rename a file.  The first argument to the \"mv\" command is the file that you want to change and the second argument to the \"mv\" command is the new location or filename.  In this case, we're renaming the temporary file we created in the first statement (the one that contains the shuffled playlist) back to our original file.  This actually overwrites our original playlist file, which is ok because it is already a shuffled playlist, so shuffling again and overwriting it is not a problem.<\/p>\n<p>Now we come to the third and final statement that we want Myth to execute:<\/p>\n<div class=\"code\">mplayer -fs -playlist %s<\/div>\n<p>mplayer is the command used to run the video player.  The options that we send to MPlayer are:<\/p>\n<ul>\n<li>-fs :  Tells mplayer to play the video fullscreen<\/li>\n<li>-playlist %s : Tells mplayer to play the playlist file that was specified in Myth (this is our shuffled playlist).<\/li>\n<\/ul>\n<p>There are a host of other options to look at for MPlayer, but those are enough to play the videos.<\/p>\n<p>So to sum up, the long string that we want MythVideo to execute when we've selected a .spl file is:<\/p>\n<div class=\"code\">cat %s &#124; shuffle > $$.spl; mv $$.spl %s; mplayer -fs -playlist %s<\/div>\n<p>Which translates to:  Take my playlist file, send it to the shuffle command, take that output and save it as a temp file.  Then rename that temp file to the original playlist file.  Finally, invoke the video player on the playlist.<\/p>\n<p>Hope this rather lengthy article helped someone understand a bit more about the Linux\/Unix command-line and some of the power at your fingertips.  Or maybe it at least convinced you that reading that command-line and understanding all its funky symbols is within your grasp.  Think about this - we've changed the way that our Media Center works because Myth allows you to use the command line.<\/p>\n<p><script type=\"text\/javascript\" src=\"http:\/\/www.codedread.com\/googleads.js\"><\/script><br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a bunch of cartoons and film shorts on my hard drive. Now that I&#8217;ve installed Suse, I have video\/audio output to my TV\/Stereo, and I&#8217;ve set up Myth, I wanted to figure out how to play a series of videos from a playlists (.pls) file. Here&#8217;s how I did it. As a bonus, [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40,25,11,14,15],"tags":[],"class_list":["post-315","post","type-post","status-publish","format-standard","hentry","category-linux","category-software","category-technology","category-tips","category-video"],"_links":{"self":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/comments?post=315"}],"version-history":[{"count":0,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}