itunes persistent id - music library xml version a

2019-08-21 01:51发布

问题:

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 Persistent ID string in ItunesMusicLibrary.xml does not seem to be the same Persistent ID that's in itunes, as accessible through various scripting interfaces

itunes music library.xml includes a 64 bit key, Persistent ID. 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?

回答1:

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)


回答2:

This works in 3.2, but there must be a simpler way

pID = '0F1610959DA92DAB'
b = bytes(pID, 'utf-8')
blo = b[8:]
b2a = binascii.a2b_hex(blo)
print(int.from_bytes(b2a, byteorder='big', signed=True))
bhi = b[:8]
b2a = binascii.a2b_hex(bhi)
print(int.from_bytes(b2a, byteorder='big', signed=True))