ITunes Music Library

The iTunes Music Library 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). 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.

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. The best way to see this structure is to open this file in a text editor.

Documentation

 * Apple's official docs
 * Reverse-engineering of binary format
 * More discussion of the files
 * Discussion on XML format

Software

 * iTunes playlist export software