[audio] Fix: a track box belongs to the trak it is in (the hdlr of udta/meta overwrote the chapter track's)

This commit is contained in:
2026-11-17 21:57:12 +01:00
parent 7aa13d6c0a
commit 1d6693a195
4 changed files with 15 additions and 8 deletions
+6 -1
View File
@@ -141,6 +141,8 @@ struct Walk<'a> {
chapters: Vec<Chapter>,
title: Option<String>,
tracks: Vec<Track>,
/// The `trak` being walked: track boxes elsewhere (the `hdlr` of `udta/meta`) belong to no track.
cur: Option<usize>,
_b: std::marker::PhantomData<&'a ()>,
}
@@ -181,6 +183,7 @@ fn mp4(b: &[u8], caps: Caps) -> Result<Info, AudioError> {
chapters: vec![],
title: None,
tracks: vec![],
cur: None,
_b: Default::default(),
};
walk(&mut w, b, 0, b.len(), 0)?;
@@ -310,14 +313,16 @@ fn walk(w: &mut Walk, b: &[u8], from: usize, to: usize, depth: usize) -> Result<
if body > end {
return Err(AudioError::Damaged("box size"));
}
let cur = w.tracks.len().checked_sub(1);
let cur = w.cur;
match &kind {
b"trak" => {
if w.tracks.len() >= MAX_TRACKS {
return Err(AudioError::Damaged("too many tracks"));
}
w.tracks.push(Track::default());
let outer = w.cur.replace(w.tracks.len() - 1);
walk(w, b, body, end, depth + 1)?;
w.cur = outer;
}
b"tkhd" => {
let v = *b.get(body).ok_or(AudioError::Damaged("tkhd"))?;
+3 -1
View File
@@ -295,7 +295,9 @@ fn m4b_with_chapter_track(titles: &[&str], starts_ms: &[u32]) -> Vec<u8> {
.concat(),
);
let mut f = ftyp(b"M4B ");
f.extend(bx(b"moov", &[mvhd(1000, 600_000), audio, text].concat()));
// iTunes metadata after the tracks, with its own `hdlr` (`mdir`): it belongs to no track.
let udta = bx(b"udta", &bx(b"meta", &[full(0), hdlr(b"mdir")].concat()));
f.extend(bx(b"moov", &[mvhd(1000, 600_000), audio, text, udta].concat()));
f
};
let head_len = build(0).len() as u32;