Skip to content

Commit 361fd49

Browse files
vanceingallsclaude
andcommitted
fix(engine): allowlist AAC-LC for the packet refinement, not deny HE-AAC
The previous gate was a HE-AAC DENYLIST, so every other profile still got the 1024-sample formula. ffprobe reports codec_name "aac" for all of them; the framing lives in the profile: LC 1024 samples/frame <- the only one this maths fits HE-AAC v1/v2 2048 output samples against a doubled sample_rate LD 512 ELD 480 Main/SSR/LTP 1024 nominally, unverified here xHE-AAC variable LD and ELD therefore had their already-correct container duration overwritten with a value 2x / ~2.13x too large, and an unknown or missing profile fell through — so an unrecognised HE spelling preserved the exact truncation the previous commit set out to close. Now an affirmative match on LC. Skipping the refinement is harmless: format.duration is already correct before it runs. Tests: 11 non-LC profiles (including LD, ELD, xHE-AAC, empty and unrecognised) assert the container duration is kept AND that the second probe is not launched; LC still refines, with whitespace tolerated. The pre-existing duration table asserted that an UNPROFILED "aac" stream refines — the behaviour under review — so it now states LC explicitly and adds an unprofiled row that must not refine. Also strengthened the `--` separator test while it was failing: it compared a flattened count of 3 across three spawns, which one call emitting three terminators would satisfy. Now asserts the last two argv entries per call. Reverting the allowlist fails 8. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 242a42f commit 361fd49

2 files changed

Lines changed: 125 additions & 28 deletions

File tree

‎packages/engine/src/utils/ffprobe.test.ts‎

Lines changed: 107 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -212,40 +212,81 @@ describe("ffprobe missing-binary fallback", () => {
212212
expect(calls[0]?.args.slice(0, 2)).toEqual(["-v", "error"]);
213213
});
214214

215+
// `profile` matters now: the packet refinement is an allowlist on AAC-LC,
216+
// because the 1024-sample formula is wrong for LD/ELD/HE and unverified for
217+
// the rest. An unprofiled "aac" stream deliberately keeps its container
218+
// duration rather than being refined on an assumption.
215219
it.each([
216-
{ name: "non-AAC metadata", codec: "mp3", packets: undefined, expected: 1.25, calls: 1 },
217-
{ name: "valid AAC packet count", codec: "aac", packets: "783", expected: 16.704, calls: 2 },
220+
{
221+
name: "non-AAC metadata",
222+
codec: "mp3",
223+
profile: undefined,
224+
packets: undefined,
225+
expected: 1.25,
226+
calls: 1,
227+
},
228+
{
229+
name: "unprofiled AAC",
230+
codec: "aac",
231+
profile: undefined,
232+
packets: "783",
233+
expected: 1.25,
234+
calls: 1,
235+
},
236+
{
237+
name: "valid AAC-LC packet count",
238+
codec: "aac",
239+
profile: "LC",
240+
packets: "783",
241+
expected: 16.704,
242+
calls: 2,
243+
},
218244
{
219245
name: "missing AAC packet count",
220246
codec: "aac",
247+
profile: "LC",
221248
packets: undefined,
222249
expected: 1.25,
223250
calls: 2,
224251
},
225-
{ name: "zero AAC packet count", codec: "aac", packets: "0", expected: 1.25, calls: 2 },
252+
{
253+
name: "zero AAC packet count",
254+
codec: "aac",
255+
profile: "LC",
256+
packets: "0",
257+
expected: 1.25,
258+
calls: 2,
259+
},
226260
{
227261
name: "invalid AAC packet count",
228262
codec: "aac",
263+
profile: "LC",
229264
packets: "invalid",
230265
expected: 1.25,
231266
calls: 2,
232267
},
233268
])(
234269
"derives audio duration for $name",
235-
async ({ codec, packets, expected, calls: expectedCalls }) => {
270+
async ({ codec, profile, packets, expected, calls: expectedCalls }) => {
236271
const outcomes: SpawnOutcome[] = [
237272
{
238273
kind: "exit",
239274
code: 0,
240275
stdout: JSON.stringify({
241276
streams: [
242-
{ codec_type: "audio", codec_name: codec, sample_rate: "48000", channels: 2 },
277+
{
278+
codec_type: "audio",
279+
codec_name: codec,
280+
sample_rate: "48000",
281+
channels: 2,
282+
profile,
283+
},
243284
],
244285
format: { duration: "1.25", bit_rate: "128000" },
245286
}),
246287
},
247288
];
248-
if (codec === "aac") {
289+
if (codec === "aac" && profile === "LC") {
249290
outcomes.push({
250291
kind: "exit",
251292
code: 0,
@@ -553,7 +594,17 @@ describe("ffprobe option separator", () => {
553594
kind: "exit",
554595
code: 0,
555596
stdout: JSON.stringify({
556-
streams: [{ codec_type: "audio", codec_name: "aac", sample_rate: "48000", channels: 2 }],
597+
streams: [
598+
{
599+
codec_type: "audio",
600+
codec_name: "aac",
601+
// LC, so the packet-count refinement actually runs and its
602+
// argv is covered here too.
603+
profile: "LC",
604+
sample_rate: "48000",
605+
channels: 2,
606+
},
607+
],
557608
format: { duration: "1.25" },
558609
}),
559610
},
@@ -574,8 +625,14 @@ describe("ffprobe option separator", () => {
574625
await extractAudioMetadata("/tmp/-audio.wav");
575626
await analyzeKeyframeIntervals("/tmp/-video.mp4");
576627

577-
const args = calls.flatMap((call) => [...(call.args ?? [])]);
578-
expect(args.filter((arg) => arg === "--")).toHaveLength(3);
628+
// Per call, not a flattened count. A total of 3 is satisfied by one call
629+
// emitting three `--` and two emitting none — i.e. it cannot fail for
630+
// misplacement, which is the shape of bug this exists to catch.
631+
expect(calls.map((call) => (call.args ?? []).slice(-2))).toEqual([
632+
["--", "/tmp/-audio.wav"],
633+
["--", "/tmp/-audio.wav"],
634+
["--", "/tmp/-video.mp4"],
635+
]);
579636
});
580637
});
581638

@@ -847,18 +904,47 @@ describe("AAC duration refinement must never fail or distort the call", () => {
847904

848905
// Regression: codec_name is "aac" for HE-AAC too, but its packets carry
849906
// 2048 output samples — assuming 1024 halved a 10:00 podcast to 5:00.
850-
it.each(["HE-AAC", "HE-AACv2", "he-aac"])(
851-
"does not apply the LC packet maths to profile %s",
852-
async (profile) => {
853-
const { meta, calls } = await probe(
854-
[{ kind: "exit", code: 0, stdout: aacStream(profile) }],
855-
`/tmp/heaac-${profile}.m4a`,
856-
);
857-
expect(meta.durationSeconds).toBe(600);
858-
// The second probe is not even attempted.
859-
expect(calls).toHaveLength(1);
860-
},
861-
);
907+
it.each([
908+
"HE-AAC",
909+
"HE-AACv2",
910+
"he-aac",
911+
// 512- and 480-sample framing: the 1024 multiplier overstates these by
912+
// 2x and ~2.13x, overwriting an already-correct container duration.
913+
"LD",
914+
"ELD",
915+
// Not verified for this maths, so not allowlisted.
916+
"Main",
917+
"SSR",
918+
"LTP",
919+
"xHE-AAC",
920+
// Missing or unrecognised profile must NOT fall through to the formula —
921+
// that is how an unknown HE spelling kept the truncation bug.
922+
"",
923+
"SomethingNew",
924+
])("does not apply the LC packet maths to profile %s", async (profile) => {
925+
const { meta, calls } = await probe(
926+
[{ kind: "exit", code: 0, stdout: aacStream(profile) }],
927+
`/tmp/heaac-${profile}.m4a`,
928+
);
929+
expect(meta.durationSeconds).toBe(600);
930+
// The second probe is not even attempted.
931+
expect(calls).toHaveLength(1);
932+
});
933+
934+
it.each(["LC", " lc "])("still refines AAC profile %s", async (profile) => {
935+
const { meta } = await probe(
936+
[
937+
{ kind: "exit", code: 0, stdout: aacStream(profile) },
938+
{
939+
kind: "exit",
940+
code: 0,
941+
stdout: JSON.stringify({ streams: [{ nb_read_packets: "861" }], format: {} }),
942+
},
943+
],
944+
`/tmp/aac-lc-${profile.trim()}.m4a`,
945+
);
946+
expect(meta.durationSeconds).toBeCloseTo((861 * 1024) / 44100, 5);
947+
});
862948

863949
it("still refines a plain AAC-LC stream", async () => {
864950
const { meta } = await probe(

‎packages/engine/src/utils/ffprobe.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,24 @@ export async function extractAudioMetadata(
553553
// 2. It must honour the caller's AbortSignal. Only the first probe
554554
// received it, so aborting during this one was ignored and the call
555555
// resolved with full metadata long after cancellation.
556-
// 3. It must not apply to HE-AAC. ffprobe reports codec_name "aac" for
557-
// HE-AAC v1/v2 as well — the marker is in the profile — and with SBR
558-
// each packet carries 2048 output samples against the doubled output
559-
// sample_rate, so assuming 1024 halves the duration. A 10:00 podcast
560-
// became 5:00, truncating the audio at exactly half.
561-
const isHeAac = /he-?aac|aac\s*(?:se?|v[12])\b/i.test(audioStream.profile ?? "");
562-
if (audioCodec === "aac" && !isHeAac && sampleRate > 0) {
556+
// 3. It must apply ONLY to profiles whose 1024-sample framing is
557+
// established. ffprobe reports codec_name "aac" for every AAC
558+
// variant — the framing lives in the profile:
559+
//
560+
// LC 1024 samples/frame <- the only one this maths fits
561+
// HE-AAC v1/v2 2048 output samples against a doubled sample_rate
562+
// LD 512
563+
// ELD 480
564+
// Main/SSR/LTP 1024 nominally, but not verified here
565+
// xHE-AAC (USAC) variable
566+
//
567+
// An ALLOWLIST, not a HE-AAC denylist. The denylist form let LD/ELD
568+
// through (halving to a third of the true duration), and let an
569+
// unknown or missing profile through too — so an unrecognised HE
570+
// spelling preserved the exact truncation this is meant to close.
571+
// A skipped refinement is harmless: format.duration is already correct.
572+
const isAacLc = /^\s*LC\s*$/i.test(audioStream.profile ?? "");
573+
if (audioCodec === "aac" && isAacLc && sampleRate > 0) {
563574
try {
564575
const packetStdout = await runFfprobe(
565576
filePath,

0 commit comments

Comments
 (0)