Accessing iTunes XML metadata with Python
Tutorial – iTunes XML
Read and manipulate data from your iTunes XML in order to share music metadata between applications and across devices.
The Extensible Markup Language (XML)[1] is a widely used markup language and text file format for storing and exchanging arbitrary data. A wide variety of applications, including the Apple iTunes application, use the XML format for exchanging or storing library data. In this article, I will show you how to use Python to read and manipulate data from an iTunes [2] XML library file.
iTunes Library File
iTunes divides its content into two separate categories: media and metadata. Media (music and video) are saved in separate files from metadata. Throughout the rest of this article, I will focus on music information – artist, song title, album title, track number, genre, release year – and refer to it as metadata. The iTunes metadata files have either a .itl
or .xml
extension (Figure 1).
Why Choose XML?
If you are a music lover like me, there may be times when you want to exchange music metadata between a variety of applications on a variety of digital devices. In my case, I have imported iTunes metadata into database applications to run SQL queries. Some applications use proprietary formats to get optimum performance and efficiency but limit access with proprietary products. The iTunes .itl
format is an example. On the other hand, there are open formats that place more of an emphasis on wide availability and ease of development. XML is in the open format category.
Apple has historically used XML files to export library and playlist metadata to external applications. While the native format for the iTunes library is a binary file with a .itl
extension, users can configure iTunes to automatically save an XML copy of the library via Advanced preferences or manually via the File | Export menu. If you prefer to export a subset of the library, the File | Export menu allows you to save playlists to an XML file.
Advantages of the XML format are that it is both human-readable and machine-readable. The benefit of having a human-readable file is being able to easily inspect and diagnose the data with a simple text editor. Machine readability makes it easy for applications to access the metadata via an application procedural interface (API). Many applications that we use every day – text editors, word processors, spreadsheet editors, presentation tools, and graphics editors – either use XML as the native format or to exchange data with other applications.
Python XML Module
The Python standard library has structured markup tools that include modules for processing XML. In this article, I will describe how metadata in iTunes XML files is organized and how to use the Python xml.etree.ElementTree
[3] module to navigate and access that data. In order to extract metadata from an iTunes XML playlist or library file, it is useful to understand how the XML file is structured. Songs within a library or playlist file are organized as a list of tracks identified by XML tag <key>Tracks</key>
. Metadata for each track consists of a dictionary of key-value pairs.
The XML language allows for data types to be defined in a Document Type Definition (DTD) that can be declared either inline inside an XML document or as an external reference. Apple traditionally uses property list files for their application configuration, thus the term "property list" (plist
) in the declaration (Listing 1). Because metadata is structured as elements in an XML tree, the xml.etree.ElementTree
module, which stores tree elements hierarchically, is an appropriate way to get to those elements. Elements are defined in the external public DTD link specified in the DOCTYPE
declaration on the second line of the exported music file.
Listing 1
DTD in DOCTYPE Declaration
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
The data types defined in this DTD are: array, data, date, dict, real, integer, string, true, and false. XML elements are also declared in the DTD file. An element is a logical document component that begins with a start tag and ends with a matching end tag. Each metadata element contains a string that is either a key or a value in the music dictionary. Metadata for song tracks is organized as key-value pairs as illustrated in Listing 2, where a <key>
tag with a text string is followed by a <string>
or <integer>
tag with a text string.
Listing 2
XML Track Metadata Example
<key>Name</key> <string>Justice's Grove</string> <key>Artist</key> <string>Stanley Clarke</string> <key>Album</key> <string>East River Drive</string> <key>Genre</key><C> <string>Jazz</string> <key>Track Number</key> <integer>1</integer> <key>Year</key> <integer>1993</integer>
In Table 1, you will see Python code snippets to show you how to use the listed methods and attributes to access metadata in XML elements.
Table 1
Accessing Metadata in XML Elements
Code Snippet |
Action |
|
Sources and parses an external XML file or file object |
|
Returns the root element for the tree |
|
Returns an instance of the first specified sub-element |
|
Returns a list containing all matching elements in document order |
|
Specifies the element name as defined in the DTD |
|
Specifies the characters between the start and end tag |
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.
-
SUSE Offers CentOS 7 Support with Liberty Linux Lite
SUSE's Liberty Linux support offering now includes CentOS 7, which means businesses won't be forced to migrate those servers for some time.
-
Ubuntu's App Center Finally Supports Local Installs Again
If you regularly download .deb files and would prefer a GUI method of installing, Ubuntu has your back.