Audio/Video: February 2010 Archives

Feb
09
itunes-logo.png

iTunes automatically creates a backup copy of its library file in a XML format. Manually creating this file from the File->Library menu is also possible. By doing so, the entire iTunes Library metadata is exported to an XML format where it can be further processed. Common requirements may be to import the library into an Excel spreadsheet as a comma-separated-value (CSV) file, or to insert the metadata into a database using SQL statements.

For me, I was working on a audio/video related project and I needed some sample, yet real-life metadata to work with. So I ended up writing a simple and compressive iTunes parsing library, implemented as a single XSL that performs most of the work. This proved to be especially useful since Apple did not implement the iTunes Library as a 'real' schema, but rather a freeform key/value pair whose definition isn't published anywhere.

The iTunes Stylesheet XSL is freely available under a BSD license. Using it can greatly reduce the amount of effort required to parse and use iTunes Library metadata. The library has been developed and tested against iTunes version 9.

Now on to the code... The following example shows how easy it is to iterate through every title and output the name and artist.


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
<xsl:include href="iTunes.xsl">
    
<xsl:template match="/">
    <xsl:for-each select="plist/dict/dict/dict">

        <xsl:call-template name="name">,
        <xsl:call-template name="artist">

    </xsl:for-each>
</xsl:call-template>

Now suppose the requirement was to insert the library into a SQL database; no problem. The iTunes Stylesheet XSL has a built-in template for escaping when the output is SQL, specifically, it escapes singles quotes.


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
<xsl:include href="iTunes.xsl">
    
<xsl:template match="/">
    <xsl:for-each select="plist/dict/dict/dict"> 

        <xsl:call-template name="sql_escape">
            <xsl:with-param name="arg1">
                <xsl:call-template name="name">
            </xsl:call-template>
        </xsl:with-param>,

        <xsl:call-template name="sql_escape">
            <xsl:with-param name="arg1">
                <xsl:call-template name="artist">
            </xsl:call-template>
        </xsl:with-param>

    </xsl:for-each>
</xsl:call-template>

Download iTunes Stylesheet XSL

iTunes Stylesheet XSL contains templates to parse the following:

  • Track ID
  • Name
  • Artist
  • Composer
  • Album Artist
  • Album
  • Genre
  • Kind
  • Size
  • Total Time
  • Disc Number
  • Disc Count
  • Track Number
  • Track Count
  • Year
  • Date Modified
  • Date Added
  • Bit Rate
  • Sample Rate
  • Comments
  • Play Count
  • Play Date
  • Play Date UTC
  • Release Date
  • Artwork Count
  • Rating
  • Rating Computed
  • Album Rating
  • Series
  • Season
  • Episode
  • Episode Order
  • Content Rating
  • Persistent ID
  • Track Type
  • Purchased
  • Podcast
  • Unplayed
  • Has Video
  • HD
  • Movie
  • TV Show
  • Music Video
  • Video Width
  • Video Height
  • Location
  • File Folder Count
  • Library Folder Count

Download iTunes Stylesheet XSL

Facebook LinkedIn last.fm Hulu Twitter RSS
Adopt a Pet

Copyright © 2010 Steve Springett