Search code examples
pythonhexitunesitunes-sdk

itunes persistent id - music library xml version and iTunes hex version


I'd like to read a hex PersistentID string from ItunesMusicLibrary.xml, get two ints representing the high and low 32 bits, then use those two ints in an iTunes script.

Unfortunately, the PersistentID string in ItunesMusicLibrary.xml does not seem to be the same PersistentID that's in iTunes, as accessible through various scripting interfaces

iTunes music library.xml includes a 64 bit key, PersistentID. For example,

<key>Persistent ID</key><string>0F1610959DA92DAB</string>.

You can also get the PersistentID through a script using the windows COM interface. For example,

iTunes.ITObjectPersistentIDHigh(track) -> 253104277

iTunes.ITObjectPersistentIDLow(track) -> -1649857109

If I feed those two numbers back into iTunes, I get the correct track

iTunes.LibraryPlaylist.Tracks.ItemByPersistentID(253104277,-1649857109).Name

My problem is translating the hex string from the xml library to high and low integers

For example, in python

int('0F1610959DA92DAB'[:8], 16) -> 253104277
int('0F1610959DA92DAB'[8:], 16) -> 2645110187

The first is correct, the second is not. If I feed the two values back into iTunes, it doesn't work. Using other tracks, sometimes both numbers are wrong.

Any idea what's going on and how to fix it?


Solution

  • You're interpreting the numbers as unsigned but iTunes is using signed. 2645110187 is the same as -1649857109. You might want something like this:

    struct.unpack('!i', binascii.a2b_hex('0F1610959DA92DAB'[8:]))[0]
    

    ...or get both values in one go:

    struct.unpack('!ii', binascii.a2b_hex('0F1610959DA92DAB'))
    

    ...which gives you the tuple you need:

    (253104277, -1649857109)