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 |
Foo Fighters - Genius List |
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.
- <?wpl version="1.0"?>
- <smil>
- <head>
- <meta name="Generator" content="Microsoft Windows Media Player -- 12.0.7600.16667"/>
- <meta name="ItemCount" content="0"/>
- <title>Test</title>
- </head>
- <body>
- <seq>
- <media src="J:\cdmp3\Foo Fighters\The Colour And The Shape\03_Hey, Johnny Park!.MP3" />
- </seq>
- </body>
- </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.
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:
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.
- $baseXml = "<?wpl version='1.0'?>
- <smil>
- <head>
- <meta name='Generator' content='Microsoft Windows Media Player -- 12.0.7600.16667'/>
- <meta name='ItemCount' content='0'/>
- <author/>
- <title>Test</title>
- </head>
- </smil>"
- $xml = new-object xml
- $xml.LoadXml($baseXml)
- $xml.smil.head.title = $args[1]
- $lines = [System.IO.File]::ReadAllLines($args[0])
- $location = 0
- $count = 0
- $body = $xml.CreateElement("body")
- $seq = $xml.CreateElement("seq")
- $xml.smil.AppendChild($body)
- $body.AppendChild($seq)
- foreach($line in $lines) {
- if( $count -eq 0 ) {
- foreach($col in $line.Split("`t")) {
- if( $col -ne "Location" ) {
- $location = $location + 1
- } else { "Location column: " + $location }
- }
- }
- else {
- $song = $line.Split("`t")[$location]
- $el = $xml.CreateElement("media")
- $el.SetAttribute("src", $song)
- $seq.AppendChild($el)
- }
- $count = $count + 1
- }
- $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($el) as 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.
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. daveandlou1@gmail.com
ReplyDelete