Neat Stuff in iTunes 4 Files

by Chris Adamson

iTunes 4 has arrived, and with it, the Music Store, AAC encoding and decoding, and artwork to go with your tunes.



Very cool. So how does it work? What follows is some stuff that might be handy for devloping helper apps, or just poking around.



Legal CYA note: I'm pretty sure this is all in line with the Music Store terms of service - this isn't a derivative work since this just examines the very-public ID3 and QuickTime formats, and I'm not looking at ways to get around the security technology in iTunes 4. In fact, the Music Store doesn't work for me (it's never satisfied with my account information), so I don't have any DRM'ed files to play with anyways.



.m4a file format



As an MPEG-4 offshoot, it shouldn't surprise anyone that the AAC files now used by iTunes use the QuickTime file format, which was adopted for the MPEG-4 file format. In an OnJava article a few months ago, I examined this format and provided an all-java parser for the format.



Here's what a typical .m4a file looks like when run through the parser:


ftyp (32 bytes)
moov (73049 bytes) - 3 children
mvhd (108 bytes)
trak (54805 bytes) - 2 children
tkhd (92 bytes)
mdia (54705 bytes) - 3 children
mdhd (32 bytes)
hdlr (34 bytes) [/soun - ]
minf (54631 bytes) - 3 children
smhd (16 bytes)
dinf (36 bytes) - 1 child
dref (28 bytes)
stbl (54571 bytes) - 5 children
stsd (103 bytes)
stts (24 bytes)
stsc (40 bytes)
stsz (51908 bytes)
stco (2488 bytes)
udta (18128 bytes) - 1 child
meta (18120 bytes)
mdat (4819123 bytes)

The big picture is that there's a moov (a "moo-vee", get it?) with a single audio track. The structure here, with the stbl sample table that describes where to get chunks of from the raw mdat media data is pretty typical.



But notice that udta atom. That's "user data", a wild and wooly place to hide anything you darn well like. My parser doesn't think that meta is a container and the parsing stops there... meta isn't described in the QT file format doc, so maybe it's an MPEG-4 atom or maybe just a proprietary Apple format.



At any rate, the contents are clearly our music metadata Here's some character data from the right column of a hexdump -C:


.>.ilst...;.nam.
..3data........A
eris' Theme (Orc
hestrated Versio
n)...%.ART....da
ta........Nobuo
Uematsu...8.alb.
..0data........F
inal Fantasy VII
Reunion Tracks.
...gnre....data.
............ trk
n....data.......
.............cpi
l....data.......
......tmpo....da
ta.............2
.too...*data....
....iTunes v4.0,
QuickTime 6.2..
..----....mean..
..com.apple.iTun
es....name....iT

The use of 4 bytes of size, and 4-byte names like genr make this structure look like normal Quicktime atoms (the nam and alb are preceded by the same byte, 0xA9, so these are probably 4-byte name constants too), so maybe this is some MPEG-4 thing I don't know about (yes, I'm too cheap to buy the real MPEG-4 docs). At any rate, it looks eminently parsable.



Cover art



iTunes still uses version 2.2 of the ID3 MP3 metadata standard, as indicated by the first four bytes of a tagged file: 49 44 33 02, or ID3 followed by the version byte 0x02. According to the standard, a picture can live in a PIC frame, and if you look for it with a hex editor, that's exactly what you'll find. This will be followed by 8 bytes (three for frame size, one for text encoding, three for image format [iTunes seems to always be JPG], one for picture type [probably 0x00 for "other]), then a null-terminated description string (looks like iTunes doesn't use the String, so there's just a 0x00), and then the image data. Plenty easy to find or write once you look at the standard.

Image formats



Interesting thing I've noticed. Take a .gif file. Look at its header. The first six bytes should be GIF89a. Now use this file as your album art for a track in iTunes. Something interesting has happened.

I took Roxy Music's notorious and outrageous cover to Country Life, turned the JPEG into a GIF with GraphicConverter, and dropped it on to the 10 tracks of the Country Life album. Then I dug in to one of the MP3's and found the PIC frame. After the header, GIF89a is not there. Instead, it's 0x89 followed by PNG. Looking further, we see the chunks IHDR, PLTE, IDAT... yep, it looks like PNG format to me. I found the same thing had happened in the presumable covr atom of the AAC file - a dropped GIF has become a PNG.



Which is interesting because this suggests iTunes has quietly performed a GIF to PNG conversion when I dropped in the artwork. Conspicuously or coincidentally, Jaguar's "Preview" app includes JPEG and PNG among the formats it can convert to, but does not export to GIF. Perhaps Apple has quietly decided to burn all GIFs. If so, my hat's off to them.



Music selection



One non-technical note you'll have to oblige me, as I bust out the "when I was your age" cliches...



Some are complaining about the music selection in the iTunes Music Store, but frankly, I'm pretty impressed. When compact discs came out in the early 80's, there were virtually no pop or rock CD's available, and some people said the format was too good for anything but classical and maybe jazz. Pop CD's eventually did emerge, but some were a long time coming. In particular, the Beatles didn't hit CD until about 1987, by which time CD players were rather common in the US. Today, the Beatles aren't in the Music Store, but give it some time. Frankly, I'm more worried about the small-label artists, both the young punk bands on hole-in-the-wall labels, and the classic artists like Todd Rundgren, Elvis Costello, and Frank Zappa, whose stuff is on specialty reissue labels like Rhino and Rykodisc. Hopefully the Music Store will eventually stock their stuff too.



Any other neat stuff in iTunes files you've found? Any good ideas for ways to add value to your music collection?


12 Comments

elness1
2003-04-29 11:51:47
Issues with Apple Music Store and account information
I too have had issues trying to purchase music through the Apple Music Store and eventually gave up. Then today, I ran across this thread in MacSlash which describes a situation similar to mine:


I'm going to update my Apple Store account info as described in the thread and will try to buy music again tonight.


Crossing my fingers! :-p

anonymous2
2003-04-29 14:59:26
PNGs
The ID3 standard recommends PNGs and JPGs used for the pictures:
"The "image/png" [PNG] or "image/jpeg" [JFIF] picture format should be used when interoperability is wanted."

anonymous2
2003-04-29 15:18:14
Rhyno and Beatles
Notice that there are no Rhyno Records stuff. The Beatles catalog is own by the Queen of Pop, M. Jackson, and he seems not to want to release it. I don't know about Rhyno. Mayhaps later..
anonymous2
2003-04-29 15:46:55
Rhyno and Beatles
the Queen of Pop doesn't own the publishing rights to the Bealtes catalog anymore. McCartney owns them. Beatles are on Capitol, which is covered by EMI. They'll be there. As will all the Rhino stuff....it's only day 2, people. Relax.
invalidname
2003-04-29 17:44:26
PNGs
Excellent point - thanks for tracking down that quote. The ID3 docs are really thorough, aren't they?


One thing I've also kind of wondered. Let's say that once we all have our tunes tagged with cover art, Apple decides to add a new feature to existing or future iPods: the ability to show the cover art on the iPod screen while you're playing. If they put a GIF decoder on every iPod, they'd be on the hook for millions of license payments to Unisys. Going PNG now keeps that from happening.


--Chris (invalidname)

anonymous2
2003-04-29 19:55:29
Payments to Unisys, not after June 20!
If Apple waits a couple of months before adding GIF decoding to the iPod, then they will not owe Unisys anything. The LZW patent expires on June 20, 2003.
anonymous2
2003-04-30 00:55:38
Payments to Unisys, not after June 20!
Decoding a GIF never required a license. Just encoding.
dougadams
2003-04-30 04:21:27
AppleScript proves it
iTunes 4 has a new class, artwork. Knocking around with this (only illustrates track 1 of selected playlist, but it's consistent):


tell application "iTunes"
set mP to view of front window
set mT to track 1 of mP
copy (format of artwork 1 of mT) to x
end tell


results in a raw data class PNG. No doubts about your Ping Thing.

shack
2003-04-30 11:51:02
Issues with Apple Music Store and account information
In my case, the trick was going to the Apple Store and reviewing account info, taking the periods as seperators out of my phone numbers. Updated, returned to iTunes, and got in.
invalidname
2003-05-01 07:05:34
That's it! (was: Issues with Apple Music Store...)
Great suggestion, shack. Thanks! I went to the Apple Store and it bugged me again about asking what county I live in (for sales tax purposes), and unlike in iTunes, I had to do it twice, once for billing address and once for shipping.


Having done that, five dollars worth of the Tubes (stuff from their greatest hits CD's that weren't on real albums) are downloading to my sad little iBook.


--Chris (invalidname)

anonymous2
2003-05-22 04:33:03
Rhyno and Beatles
Apple Records, anyone?
danno
2006-02-04 05:30:13
I am having trouble converting the m4a files from my itunes into another file onto windows media player, is there any one that knows how to do that? So that I can put it onto my mp3 player?