QuickTime Chapters in iDVD 5
Part 2: Atomic Theory
Okay, here's how we want to go about this. I've established that all of the changes that have to be made can be made in the chapter track and then added to the movie. This is a useful simplification because it means we can work with relatively small files. The first thing to do is to create a working QuickTime chapter track and attach it to your movie. I'm not going to get into how to create a chapter track; I figure that if you're having the problems addressed by this article, you know how to create a chapter track and add it to your QuickTime movie. The easiest way to do it might be to use Metadata Hootenanny, which has a nice "search and click" interface to create the chapter track. Once the chapter track is created, extract it from the movie using "Extract Tracks" in QuickTime Pro to create a chapter movie.
Next, optionally use Dumpster to look at the movie structure. If you don't know what you're doing, it might make things make a little more sense when you get to the hex editor.
Next, use HexEdit to look at the file. Ideally, you should obtain a chapter track extracted from an iMovie project for comparison purposes...and because with that in your toolbox you can copy the user data atoms from the iMovie chapter track and paste them into your own. Here is what a simple chapter movie looks like in HexEdit:
00 00 04 C1 6D 6F 6F 76 00 00 00 6C 6D 76 68 64 ....moov...lmvhd
00 00 00 00 BF DE C8 88 BF DE C8 B3 00 00 02 58 ...............X
00 01 FB 75 00 01 00 00 00 FF 00 00 00 00 00 00 ...u............
00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 40 00 00 00 00 00 00 00 00 00 00 00 ....@...........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 02 00 00 04 20 74 72 61 6B 00 00 00 5C ....... trak...\
74 6B 68 64 00 00 00 0E BF DE C8 88 BF DE C8 B3 tkhd............
00 00 00 01 00 00 00 00 00 01 FB 75 00 00 00 00 ...........u....
-------------------------<stuff omitted>------------------------
00 00 01 00 00 00 01 00 00 00 14 73 74 73 7A 00 ...........stsz.
00 00 00 00 00 00 0B 00 00 00 02 00 00 00 18 73 ...............s
74 63 6F 00 00 00 00 00 00 00 02 00 00 01 EE 00 tco.............
00 01 F9 00 00 00 21 75 64 74 61 00 00 00 15 6E ......!udta....n
61 6D 65 43 68 61 70 74 65 72 20 54 72 61 63 6B ameChapter Track
00 00 00 00 00 00 00 18 75 64 74 61 00 00 00 0C ........udta....
57 4C 4F 43 00 48 00 34 00 00 00 00 -- -- -- -- WLOC.H.4....----
Okay, there are a few things I want to bring your attention to here. First of all, for this project, the top line of the HexEdit display is going to become your dear friend. The first value shown there, in this case $04C1, is the length of the file being edited. All QuickTime atoms begin with the length of the atom, followed by four bytes that give the name of the atom. The main "moov" atom is no exception. The first four bytes (shown in green in the table) are the length of the moov atom, which happens to also be the length of the file. We will be changing the length of the file, but lucky for us, HexEdit keeps track of it. So I've highlighted the file length in green. After we've added the new atoms to the movie, we need to go back and change this value in the file in order to keep the file valid. My guess is that including the file length in the moov atom (which is equal to the complete file length of a QuickTime movie) is not merely a matter of consistency, but also to accommodate fast-start movies, and movies embedded in other files.
Also at the top of the display, HexEdit shows what portion of the file is selected based on the offset from the beginning of the file, and (more important for our purposes) the number of bytes selected (here indicated in red). That is going to be useful because it saves you from having to count bytes in hexadecimal in order to set the length values for the new atoms.
I've omitted the offset counters for each line of the display, because I want to emphasize an important point. This isn't like editing the .IFO file to enable proper widescreen processing on a DVD player (see elsewhere on this site) where the correct byte to change is identified by its offset position within the file. In a QuickTime file, the byte offset within the file is unimportant; you are concerned with the QuickTime file atoms, and you have to look for those by looking for the atom headers (or programmatically by parsing the file with the length bytes).
Now have a look at the structure of the movie. The chapter movie shown here has only one track, the chapter track. It starts with the moov atom, and you can see that there is a mvhd atom immediately following the moov, and the mvhd contains binary data. Then there is a trak atom for the lone text track. I've highlighted the hex code for the trak atom in yellow. There is a lot of stuff in there, and I chopped it off after the tkhd atom. But you should be able to see references to your movie, and the actual names of your chapters in the text data. The trak atom continues, containing a whole series of information, including both text and binary data. My example picks up again near the end of the trak atom, with the lines containing the headers for the stsz and stco atoms. The one we are interested in is called 'udta', and I've highlighted its bytes in blue. Yes, that's blue. I realize it looks like green, but that's because the udta atom is contained within the trak atom (highlighted in yellow). It's important to remember that because of the changes we need to make in order to add atoms to the movie without causing an atomic meltdown, rendering the movie unplayable.
Notice also that each of the highlighted atoms begins four bytes before the atomic name. Those first four bytes represent the length of the atom, and those four bytes DO count towards the total length of the atom. With that information, you should be able to locate the trak:udta atom in your chapter movie. Now, let's have a look at the trak:udta atom in an iDVD-compliant chapter movie (note: this is from a completely different chapter movie, so the offsets aren't even close to the other example):
65 43 68 61 70 74 65 72 20 54 72 61 63 6B 00 00 eChapter Track..
00 0C 63 68 61 70 63 68 61 70 00 00 00 0C 6B 67 ..chapchap....kg
69 74 63 68 61 70 00 00 00 00 00 00 00 21 75 64 itchap.......!ud
There. That's all the information needed to fix a chapter track so that iDVD will pay attention to it. Next, let's combine the original chapter track with the trak:udta atom taken from an iMovie HD movie in a bit of atomic fusion.
<-- - 1. Introduction - 2. Atomic Theory - 3. Atomic Fusion - -->