Dustin Boswell - Brain Dumps about Computers, Programming, and Everything Else
dustwell.com About Me Past Academic Work Photo Album Links I Use
Articles by Me Subscribe: RSS
Oil ETFs leak money Yes/No proposition bets div, span, and CSS "display:" Pair programming + screen Dear VirginAmerica.com SSH keys in 2 easy steps "An hour" vs. "A hour" How to hash passwords Snapshotting with Rsync MacBook Pro Sharp Edge Fixing Your Flaky Internet How X-over-SSH works Drinking Distilled Water Politician != Decider Understanding iTunes files My Audi A4 Gas Mileage djb-dns installation Vim Cheat Sheet
How iTunes works under the (file) covers April 2, 2009
If you're like me, you have a bunch of mp3s (that you legitmately obtained from your CD's - ahem) in a neatly organized directory tree on your hard drive. And it's a breeze to navigate, play, backup, and share.
Now you've got an iPod and/or a Mac, and you want to take the plunge and use iTunes - but your puny little file-based UNIX-loving brain can't handle the transition. Well, neither could I at first, but now I've finally unraveled the how the "file system" of iTunes works.

Here are the juicy nuggets that will help you understand:

About authorization:
1) Instead of an .mp3, purchased songs from iTunes are .m4p files. They are similar to mp3s but they are essentially encrypted with your AppleID as a key. (In fact, if you open up the .m4p file in a text editor, you can see your email address!)

2) Apple allows you to copy those .m4p files to as many computers/ipods as you like.

3) But you can't play an .m4p file unless that computer is "authorized" by your AppleID. You can have up to 5 computers authorized at one time. The "authorize" and "deauthorize" options in iTunes do the trick. (Note: it doesn't touch your library or files.)

Where is all the data?
1) When iTunes starts up, it looks at the iTunes/ directory (inside ~/Music in OSX, somewhere in your Documents & Settings in Windows). This contains a few importants files:
- iTunes Music Library.xml - this single file has all your playlists, and playcounts, and has the file paths of your actual music files.
- There is a similar file (without the .xml) which is a binary version of the .xml file. From my understanding, the xml file is for reading convenience, but this file is what iTunes really uses. The two files are kept in sync by iTunes.
- The Album Artwork/ directory is where the album image files are stored.

2) In iTunes->Preferences->Advanced there is an option for the "iTunes Music Folder". This directory contains all the .m4p and other raw music files. The song title and other text is metadata is inside the .m4p file.

Note that this directory has a Artist/Album/Song directory structure inside it. (This is what the iTunes->Preferences->Advanced->"Keep iTunes Music Folder Organized" option enforces.) You're not meant to touch this directory yourself - the iTunes program manages these subdirectories for you.

3) When you import music via iTunes->"Add to Library" (or when you purchase music, for that matter) then it:
- Adds those raw music files to your iTunes Music Folder. (If you have iTunes->Preferences->Advanced->"Copy files to iTunes Music folder when adding to library" unchecked, it will skip this step.)
- Adds the filepath of those files to the xml database mentioned above.

Implications
1) If you copy an raw music file into your iTunes Music Folder, that doesn't do anything - your iTunes library (the xml database) doesn't know about it.
2) If you move your iTunes Music Folder, your library will be broken, since that xml-database is pointing to file locations that don't exist. You'll have to move them back, or re-import those files.
3) The "Consolidate Library..." action copies all the music files pointed to by your library (where-ever they are) to your current iTunes Music Folder.

Well, I hope that helps! Let me if there are any important parts I've left out.

Oh, and if you're curious here's a writeup on how to share an iTunes library across multiple users on the same computer
blog comments powered by Disqus