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
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.