ITunes Music Library

The iTunes Music Library (or simply "iTunes Library" since it now includes things other than music as well) keeps track of all the information Apple's iTunes program knows about the songs, videos, podcasts, etc., which are part of a user's collection. This includes metadata such as artists and titles, dates of when each track was added to the collection and when it was last played, the count of how many times each track has been played, and other information. Also, all of the playlists are stored within the single music-library file, instead of having separate files. This means that iTunes puts all of its eggs in one basket, though it then proceeds to duplicate them in a second basket of a different design.

The two different library files are the binary (.itl) file, and the text-based (.xml) file. They are both found in the iTunes main directory, which in turn is generally located directly beneath the "Music" directory/folder of the particular user (a location which varies by version of Windows or MacOS). There may be other files related to the library there as well, such as automatically-generated backups saved at various times (the "Previous iTunes Libraries" subdirectory will usually have a whole bunch of them), but these two are the ones in direct immediate use. When you make a change to metadata (e.g., the title or artist of a song), the change will be immediately written to both versions of the library file, as well as to the song file itself if it is in a format that supports embedded metadata.

Binary File
The file actually used by iTunes is the binary one, named iTunes Library.itl (or iTunes Music Library.itl in early versions; it keeps the original filename from your system even when you upgrade). This is where iTunes gets the information it displays and uses, and it is updated continuously with any changes such as new tracks added or updated play counts every time a track is played. The format is proprietary and undocumented, and there is evidence that Apple intentionally obfuscates it to make it difficult for anybody else to use the file directly, but there have still been efforts to figure out the format with some success (see external links below). It's changed between iTunes versions before, and probably will again; all in all, it's better to use the XML version documented below if you wish to examine an iTunes collection or export playlists.

Since this binary version is the only one iTunes actually reads, if it is deleted or corrupted iTunes will rebuild it solely from the data present in the individual audio/video files themselves, losing all other metadata and playlists; it will not load this information from the XML file.

It appears that this file has as its 'magic bytes' (hex), which spell "hdfm" in ASCII, at the start of the file. (This is actually the data block identifier for the initial part, which contains headers.)

XML File
At the same time as the binary file above is updated, iTunes also keeps a parallel text-based XML file in sync with it, writing all changes to it as well as the binary one. This file is iTunes Library.xml or iTunes Music Library.xml (depending on how early a version of iTunes you started with; it keeps the filename the same later even if you upgrade).

This file is designed for interoperability with other programs that need to read (but not write to) the iTunes library/playlist data. It is in the Property List/XML format, with field and key names that are largely self-explanatory. The character encoding is UTF-8. Dates are given in ISO 8601 format, like 2013-01-05T21:22:50Z. Each track has a Track ID, so that playlists consist of arrays of these IDs (actually of dict structures containing the ID as their only element) to show which songs are on them. The structure has a number of nested dict and array structures. One way to see this structure is to open this file in a text editor.

Media categories
There are lots of arcane details about how to use iTunes that are scantily documented; it often requires digging around in Apple's forums, past lots of blind-leading-the-blind questions and answers from people who don't really know much, to find answers to basic details like how the iPhone figures out which of your media content turns up in each of the various different places designated for music, podcasts, movies, music videos, and home videos. The answer to that one is that it is set in a "Media Kind" field that can be reached within iTunes by right-clicking on an item (song, video, etc.) and choosing "Get Info", then going to the "Options" tab, where there is a pulldown for "Media Kind" with various choices (which can be different depending on what sort of file it is and whether it was imported manually or obtained from the iTunes store; some things such as purchased music don't let the user change this field at all). If the item is one of those which allows user-modification, this feature can be useful if an item isn't showing up where you want it.

Now, can anyone figure out how it determines which podcast series an episode is part of? Podcasts subscribed to via iTunes usually group correctly, but ones imported manually after downloading them from non-Apple sites seem to wind up grouped togethar senselessly, with unrelated podcasts treated as part of the same series. Changing "show name" fields in the info screens doesn't seem to affect it.

Documentation

 * Apple's official docs
 * Reverse-engineering of binary format
 * More discussion of the files
 * Discussion on XML format
 * Back up your iTunes library by copying it to an external drive
 * How to open an alternate iTunes Library file or create a new one

Software

 * iTunes playlist export software

Other links

 * Implementing "Play higher rated songs more often" in iTunes 11 (a how-to guide)
 * How to switch category of videos between "Movies" and "Home Videos" (a similar technique also works to switch between "Music" and "Podcasts")
 * Kill the DRM in Your Old iTunes Music Purchases