Wednesday, December 29, 2010

iTunes Genius Playlists to Windows Media Center

Since cutting the cord of cable television I only use Windows Media Center to manage my media throughout my household.  Once of the nice things about WMC is its ability to broadcast content across your local network to various WMC Extenders such as the Xbox 360 so you can share media such as movies, TV tuners (for over-the-air channels) as well as music.

Being the lazy person that I am, I rarely spend the time to put together a sweet playlist, which is why I fell in love with Genius playlists in iTunes.  Genius playlists allow you to generate a playlist in your music collection based on an individual song.
Genius Playlists
It scans your entire music library to pick songs that it thinks you would enjoy based on the song that you are generating the playlist from, and in my experience it does an excellent job as well.
Foo Fighters - Genius List
Since neither Media Center or Media Player offer this type of functionality, there are few options if you are lazy for getting nice playlists generated for listening to them via WMC.  I searched the tubes of the internet looking for some sort of plugin that might enable me to integrate iTunes and WMC but to no avail.  The closest thing I found was MCE Tunes, which was not necessarily free if you wanted the cooler features of it and the only thing I found on the internet that walked you through creating a WMC playlist from a Genius playlist involved copying the physical files from your Genius playlist into a directory and creating a playlist from it.

Having not found a suitable solution, I decided to see what sort of options iTunes had with its playlist.  After generating a playlist, I chose to save it, in hopes that a file would be generated that would be easy enough for me to script something to automatically generate a playlist that would be compatible with WMC.  Sure enough, I noticed that there was an "Export..." option for the playlist.

Conveniently, iTunes will export the playlist into a tab delimited format, perfect for retrieving the information that I would need to create a playlist automatically to be used by WMC.  The next task was to figure out what sort of format WMC needed for its playlists.  To do so, I generated a new playlist of one song and chose to "Open File Location" from the context menu.  In the directory was my Test.wpl file.  Opening this revealed a simple XML document for saving playlist information.

  1. <?wpl version="1.0"?>
  2. <smil>
  3.     <head>
  4.         <meta name="Generator" content="Microsoft Windows Media Player -- 12.0.7600.16667"/>
  5.         <meta name="ItemCount" content="0"/>
  6.         <title>Test</title>
  7.     </head>
  8.     <body>
  9.         <seq>
  10.             <media src="J:\cdmp3\Foo Fighters\The Colour And The Shape\03_Hey, Johnny Park!.MP3" />
  11.         </seq>
  12.     </body>
  13. </smil>

The only thing that was left to do was to transform the tab-delimited playlist file into the XML document that WMC expects.  I opted to write a simple PowerShell script to achieve this.  PowerShell is installed by default on the Windows 7 operating system and is quite powerful.  Below is the resulting PowerShell script that will generate a WPL file based on an exported Genius playlist.

  1. $baseXml = "<?wpl version='1.0'?>
  2. <smil>
  3.    <head>
  4.        <meta name='Generator' content='Microsoft Windows Media Player -- 12.0.7600.16667'/>
  5.        <meta name='ItemCount' content='0'/>
  6.        <author/>
  7.        <title>Test</title>
  8.    </head>
  9. </smil>"
  10. $xml = new-object xml
  11. $xml.LoadXml($baseXml)
  12. $xml.smil.head.title = $args[1]
  13. $lines = [System.IO.File]::ReadAllLines($args[0])
  14. $location = 0
  15. $count = 0
  16. $body = $xml.CreateElement("body")
  17. $seq = $xml.CreateElement("seq")
  18. $xml.smil.AppendChild($body)
  19. $body.AppendChild($seq)
  20. foreach($line in $lines) {
  21.     if( $count -eq 0 ) {
  22.         foreach($col in $line.Split("`t")) {
  23.             if( $col -ne "Location" ) {
  24.                 $location = $location + 1              
  25.             } else { "Location column:  " + $location }    
  26.         }      
  27.     }
  28.     else {
  29.         $song = $line.Split("`t")[$location]
  30.         $el = $xml.CreateElement("media")
  31.         $el.SetAttribute("src", $song)
  32.         $seq.AppendChild($el)
  33.     }
  35.     $count = $count + 1
  36. }
  37. $xml.Save($home + "\\" + $args[1] + ".wpl")

The script expects two arguments, the first being the path to the tab-delimited Genius playlist file that was exported earlier and the second being the name of the new playlist that is being generated.  The beginning of the script has the base XML structure that is needed for a WPL file.  The only caveat that I ran into while writing this script was that when you load an XmlDocument object, when you try to drill down and reference an XML node that doesn't have any inner elements, PowerShell interprets the node as a string rather than an XmlElement.  What this means is that if I had a hardcoded the <body><seq /></body> in the XML, I would not be able to reference it with $xml.smil.body.seq.AppendChild($elas I would get an error stating "Method invocation failed because [System.String] doesn't contain a method named 'AppendChild'."  To get around this, the script dynamically generates the <body /> and <seq /> elements during execution.  The rest of the script was pretty easy from there:
  • Reads each line from the tab-delimited file
  • Since the first line contains header information, the script locates the column # of the "Location" column so we know where the file resides
  • For every non-header line, it appends a <media src="" /> element to the <seq /> element
  • When finished, it saves the resulting WPL file in the current user's "Home" directory
In order to run this script on Windows 7, you'll have to first set the execution policy to RemoteSigned in order to run it.  To do so, you will first have to run PowerShell as an Administrator by right clicking on PowerShell and choosing to do so.

Set-ExecutionPolicy RemoteSigned

After running the Set-ExecutionPolicy cmdlet, you are all set to go.  Simply go to where you've saved the PS1 file and run it like so:

PS C:\Users\Levi> .\geniustowmc.ps1 "C:\Users\Levi\Desktop\Hey, Johnny Park!.txt" "Foo Fighters"

When finished, there should be a "Foo Fighters.wpl" file in your Home directory on your computer.  Simply move this file into your Playlists folder (this was found when you chose to "Open File Location" in WMP above) and both WMP and WMC will automatically recognize the newly created playlist.

If you've found a better way to achieve this, please let me know.  For now, this 3-step method for creating awesome playlists to be used by WMC will suffice.

1 comment:

  1. hi. i have playlists in my old windows media center; i imported them from creative zen. now i can't figure out how to get them from creative media center into itunes. they do not show up in windows media player. thanks for any advice you have.