oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Unify and Synchronize Your iTunes Libraries

by Matthew Russell

Editor's note: Last June, David Miller published an article that explained how to synchronize playlists on iTunes. Today, Matthew Russell extends this idea by investigating and presenting three different ways to synchronize the actual contents of your iTunes music libraries when they are scattered across multiple machines. Then, in the second half of the article, Matthew lays the foundation for a custom Python script that you can extend across multiple platforms and in various other ways.

Although iTunes makes it easy to share media between multiple machines on a home network, you might want to keep more than one physical copy of your music library synchronized. Perhaps the most common example involves a family's home network consisting of a desktop and a laptop -- each containing subsets of what is really a single music library. When the laptop is at home, all of the music can be shared fairly transparently between the two machines; when the laptop is not at home, however, whatever media is held on the laptop is out of reach until it comes home. Add in another machine and/or a network drive used for backups and you quickly run into synchronization headaches. Let's investigate some of the ways that you can unify your fragmented iTunes libraries.

Two Problems, Three Solutions

What seems like one big problem can really be formulated as two smaller, interrelated problems. The first problem is the primary one: unifying a distributed music library to a single location so that the unified library represents the complete library. The second problem is much less of a burden: unifying each of the remaining libraries by distributing fragments of the now-complete library.

For example, suppose you have three iTunes libraries where each contains a set of songs: lib1 contains {A,B,C}, lib2 contains {A,C,D}, and lib3 contains {A,D,E}. Here's one methodical way we could solve the problem:

  • Step 1: Designate a library as the initial one to unify. We'll use lib1.
  • Step 2: Calculate the difference of lib1 from lib2 (the only items that are in lib2 but not lib1) and add the result back into lib1. In this case, song D is the only item in lib2 that's not in lib1. After this step, lib1 contains {A,B,C,D}.
  • Step 3: Calculate the difference of lib1 from lib3 (the only item that's in lib3 but not lib1) and add the result back into lib1. Song E is the only item not in lib1 since we already added song D in the previous step, so after this step, lib1 contains {A,B,C,D,E} and is unified.
  • Step 4: Calculate the difference of lib2 from lib1 (B and E) and unify lib2.
  • Step 5: Calculate the difference of lib3 from lib1 (B and C) and unify lib3.

Now, all three libraries have been unified and are in sync. Generalizing this approach to more libraries is straightforward.

Although following these simple steps certainly solves the first part of the problem, they do little to address the monotony of the task itself. Furthermore, maintaining this zen is still a difficult chore if all of your machines readily purchase music from iTunes Music Store, download podcasts, import CDs, etc. Fortunately, at least three good solutions are readily available: use a master/slave approach to managing your library, periodically run syncOtunes, or script your own customized solution. Let's look at each of these approaches in detail.

The Master/Slave Approach

This approach involves first manually unifying and synchronizing your libraries and then designating one machine (the master) as the sole authority to import new media into the library (the slaves.) Keeping track of the updates on the single master machine should be fairly simple to do, and armed with that knowledge, passing down periodic updates to slaves should be a trivial undertaking. Although this approach isn't necessarily the most flexible, its primary advantage is that it involves very little overhead once you've gotten your libraries in order.

For the first manual sync, you have a few options. Comparing the contents of your ~/Music/iTunes/iTunes Music Library.xml files or the results of a recursive directory listing (ls -R) are two good options, although the next two techniques we'll be taking a look at are also fine alternatives. If you don't yet have another machine, but might be getting one, now would be the ideal time to consider the advantages that the simplicity of the master/slave approach might offer to your situation. It involves little more than good housekeeping.

Syncing with syncOtunes

syncOtunes is an application designed explicitly to solve the task at hand. It's a native Cocoa application and it's donationware. syncOtunes provides you with a fairly intuitive interface and abstracts away the grueling work of manually computing set differences and building the directories of files that you'll need to import into iTunes. Great, right? Well, maybe. If you can live with its quirks and aren't concerned that it's an independent, closed source project for which support may or may not exist at any given time, then it might be just what you need. In any event, it's definitely worth giving a shot before putting in the effort that rolling your own solution takes (see Figure 1).

Thumbnail, click for full-size image.
Figure 1. syncOtunes readily compares iTunes music libraries, computes the differences between them, and prepares folders of music that you can use to remedy your synchronization problems. (Click for full-size image.)

One additional factor to consider with syncOtunes is the number of libraries that you're interested in maintaining. syncOtunes is explicitly designed to work with two libraries, but you could iteratively apply it to multiple libraries easily enough. Since syncOtunes is specifically designed for iTunes and depends on the iTunes Music Library.xml file that iTunes builds, however, it's probably not your best approach if you're slowly trying to migrate away from iTunes or trying to keep an iTunes library in sync with a non-iTunes library. But that's where some simple scripting and ingenuity comes in handy.

Rolling Your Own

If you need a more customized solution than manually keeping up with files that the master/slave approach offers and if syncOtunes just won't suffice, then rolling your own solution with some custom scripting is sure to get the job done. If you haven't done much scripting before, don't worry. This is a good place to cut your teeth and get some experience. We'll work up a Python script with some core functionality to bear the brunt of what syncOtunes accomplishes behind its interface, and you can further adapt this script to meet your particular needs.

As you probably already know, your ~/Music/iTunes/iTunes Music Library.xml file is in place to give other applications access to the contents of your music library. One quirk about this file, however, is that iTunes will not acknowledge any changes you make to it. Rather, whenever you make a change to your music library, iTunes updates this file itself. This circumstance explains why an application like syncOtunes requires you to manually drop the folders of music that it produces onto the iTunes interface; iTunes must explicitly bless the music for you. Apparently, the only way to gain full control of your music library outside the grip of iTunes would be to reverse engineer the file format of the ~/Music/iTunes/iTunes Library binary file and modify it--but we all know that every end-user license agreement known to humanity prohibits doing such things.

Moving along, let's work with what Apple has made available to us and automate as much of the monotony we can. Our first task is to compute the differences between two iTunes libraries using the XML files available to us. If your circumstances call for it, you may want to genuinely parse the file with PyObjC (see here) or an XML utility, but we'll get by using a simple regex to build a list. If you need a quick introduction to regexes in Python, take a look at this document.

Pages: 1, 2

Next Pagearrow