I've got it sorted out for the ID3v2.3 tags, since there are 4 bytes, but the frame's header tag here is only a total of 3 bytes, not 4. That seems to mean that I can't use struct.unpack
to get an int
out of it.
For example, the tag I'm working with now is: TT2\x00\x00\x0c\x00Torn Within
where TT2
is the name identifier, and \x00\x00\x0c
is the size identifier. The content of the tag is \x00Torn Within
, which has a size of 12 bytes.
Here's the song's ID3 header as well. 'ID3\x02\x00\x00\x00\x04NP'
, where you can see that the encoding and flags are not set.
I've tried struct.unpack('>3b', '\x00\x00\x0c')
but that only grabs each individual byte's value.
But after that, I am stuck, because unless I prepend a \x00
to the size tag, I'm unable to continue. What do I do?
Here's the ID3 tags docs http://id3.org/id3v2-00, and the docs for the struct
module http://docs.python.org/library/struct.html#format-characters
edit found that I could do this: int(binascii.hexlify('\x00\x00\x0c'), 16)
but I don't think that's a great solution
Just prepend a null byte (\x00
) before unpacking:
>>> length = "\x00\x00\x0c"
>>> struct.unpack('>I', '\x00' + length)
(12,)
The null byte pads your length bytes out to 4 bytes without altering the meaning. The largest value the 3 size bytes can hold is 224 equals 16777216 bytes; adding the padding is not going to alter that limit in any way.