VLC ignored my MP3 chapters, so I moved to M4B
Hi, I'm Ishaan and I built Elsewhere, an ebook reader that runs in the browser and reads books aloud. A narrated book can be exported as one file to listen to in another app, and that file needs chapters.
My export was an MP3 with chapter markers in its ID3 tag. The tag was valid and parsed with 19 chapters at the right times. VLC played the book and showed no chapter list.
Chapters in an ID3 tag
ID3v2.3 can carry chapters in CTOC and CHAP frames. I built the tag by hand, and it has one trap worth knowing.
In ID3v2.3, the tag header stores its size as a syncsafe integer, with 7 bits per byte, while every frame inside stores a plain 32-bit integer. (ID3v2.4 made frame sizes syncsafe too.) Write a 2.3 frame size of 128 bytes or more the syncsafe way and a parser reads the wrong length, which can leave a file that plays, shows no chapters and reports no error.
Mine was written correctly, and VLC still did not show the chapters. I can find no chapter handling in the MP3 reader of VLC 3.0.24. The development version of VLC has it.
Same audio, different box
I did not want to encode the audio again. An MP4 audio track can carry MPEG Layer III as it is, so the frames can go in untouched and only the container changes.
The decoder is chosen by one byte in the esds box:
// 0x6B is MPEG-1 audio, 0x69 MPEG-2
const objectType = audio.mpeg1 ? 0x6b : 0x69
The audio I was working with then was 24 kHz, which is MPEG-2. Some voices I have added since produce 44.1 kHz, which is MPEG-1, so the check matters.
The chapters go in a Nero chpl box, a flat list of start times and titles. This is the writer, trimmed, with the constants written out:
function chpl(chapters) {
const kept = chapters.slice(0, 255)
const entries = kept.map((c) => {
const title = utf8Title(c.title)
const t = c.startMs * 10_000 // 100ns units
return concat([u32(Math.floor(t / 2 ** 32)), u32(t % 2 ** 32), u8(title.length), title])
})
return box('chpl', u8(1, 0, 0, 0), u32(0), u8(kept.length), ...entries)
}
The count is a single byte, which is why a book with more than 255 chapters gets markers for the first 255 only. The title length is one byte too, so utf8Title cuts titles to 255 bytes.
The sample table wants every frame
An MP4 file lists the size of every audio frame, so each stored piece of narration is walked frame by frame once. A leading ID3v2 tag and any cut-off tail after the last whole frame are dropped, so the table and the bytes agree.
I first wrote this walk to measure how long a stored piece plays. My first attempt looked for the sync pattern and stepped a fixed distance. The same bits turn up inside frames, so it reported 1,673 frames for a file that has 913. Each step has to use the length decoded from that frame's header.
The encoder adds a delay at the start and pads the last frame, so one 21.9 second piece played 62 ms longer than the sound that went in. I measured that one piece. At about 60 ms each, a timeline of 448 pieces would be about 27 seconds out by the end of a book a bit over three hours long. Chapter start times in the export are sums of the frame durations.
Building it in the browser
The file is assembled from slices of the stored audio, and the code reads one piece at a time instead of building the whole book in memory. The moov box goes at the front so a player can start before the whole file has arrived.
That box holds the positions of the audio, which depend on its own length. None of its fields changes size when those positions change, so I build it once to measure and a second time with the real positions, and stop with an error if the two lengths differ.
I checked the result in VLC 3.0.24. It opens the file, decodes the audio, and a jump to the second chapter lands at its start. Audio that was already stored did not need to be made again.
No QuickTime chapter track, and a 4 GB ceiling
I write the chpl list and no QuickTime chapter track, so a player that reads only the other kind will show no chapters. VLC is the player I checked. The file also has a 4 GB ceiling.
Elsewhere is my project. It opens DRM-free books, so a book locked with DRM, as many Kindle store books are, will not open. The voice is an AI voice rather than a human narrator. You can listen to a sample without an account at https://listenelsewhere.com/try
Which player do you use for audiobooks, and what does it get wrong? Let me know in the comments.
Comments
No comments yet. Start the discussion.