From 97df93fb07dc0e1c66aba8eafbb10c2692dbe7db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Thu, 12 Nov 2026 19:30:02 +0000 Subject: [PATCH] fix(cli): check hears a WAV whose header misstates its byte rate, as render plays it (#7618) * fix(cli): check hears a WAV whose header misstates its byte rate, as render plays it A PCM WAV whose header byte rate is wrong (some speech generators write 2x) sent the silence decode's input seek past the file's end, so every clip past the middle was reported audio_clip_silent. Render already cuts such a file by decoded time (#7541); the byte-rate rule now lives in parsers and the CLI decoder seeks after decoding starts for those files. * test(cli): keep the misstated byte-rate case with the other single-clip checks * style(cli): format the WAV test helper * style(parsers): keep the byte-rate rule's comment within the line limit --- packages/cli/src/audio/analysis/decode.ts | 18 +++++++++++------- packages/cli/src/utils/checkAudio.test.ts | 17 ++++++++++++++--- packages/cli/src/utils/checkAudio.ts | 1 + packages/engine/src/utils/ffprobe.ts | 12 +----------- packages/parsers/src/mediaStreams.ts | 18 ++++++++++++++++++ 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/packages/cli/src/audio/analysis/decode.ts b/packages/cli/src/audio/analysis/decode.ts index 61e6a40b4d..f8ecf0f707 100644 --- a/packages/cli/src/audio/analysis/decode.ts +++ b/packages/cli/src/audio/analysis/decode.ts @@ -1,7 +1,11 @@ import { execFile } from "node:child_process"; import { promisify } from "node:util"; import { findFfBinary } from "@hyperframes/parsers/ff-binaries"; -import { decodableAudioStream, type FfprobeStreamCodec } from "@hyperframes/parsers/media-streams"; +import { + decodableAudioStream, + misstatesPcmByteRate, + type FfprobePcmFields, +} from "@hyperframes/parsers/media-streams"; import type { AudioSignal } from "@hyperframes/core/audio-analysis"; const run = promisify(execFile); @@ -11,6 +15,7 @@ export interface AudioStreamInfo { sampleRate: number; channels: number; duration: number; + misstatedByteRate?: boolean; } function requiredBinary(name: "ffmpeg" | "ffprobe"): string { @@ -38,11 +43,7 @@ export async function probeAudio(file: string): Promise { maxBuffer: 8 * 1024 * 1024, windowsHide: true }, ); const parsed = JSON.parse(stdout) as { - streams?: (FfprobeStreamCodec & { - sample_rate?: string; - channels?: number; - duration?: string; - })[]; + streams?: (FfprobePcmFields & { duration?: string })[]; format?: { duration?: string }; }; const index = decodableAudioStream(parsed.streams ?? []); @@ -53,6 +54,7 @@ export async function probeAudio(file: string): Promise sampleRate: Number(stream.sample_rate), channels: stream.channels ?? 1, duration: Number(stream.duration ?? parsed.format?.duration ?? 0), + misstatedByteRate: misstatesPcmByteRate(stream), }; } @@ -88,15 +90,17 @@ export async function decodeAudio( channels: format.mono ? 1 : probedInfo.channels, }; const seek = window.from ? ["-ss", String(window.from)] : []; + const [inputSeek, decodedSeek] = probedInfo.misstatedByteRate ? [[], seek] : [seek, []]; const length = window.seconds === undefined ? [] : ["-t", String(window.seconds)]; const { stdout } = await run( requiredBinary("ffmpeg"), [ "-v", "error", - ...seek, + ...inputSeek, "-i", file, + ...decodedSeek, ...length, "-map", `0:a:${probedInfo.audioStream}`, diff --git a/packages/cli/src/utils/checkAudio.test.ts b/packages/cli/src/utils/checkAudio.test.ts index 35c3d8868e..8b6418f528 100644 --- a/packages/cli/src/utils/checkAudio.test.ts +++ b/packages/cli/src/utils/checkAudio.test.ts @@ -11,8 +11,13 @@ const RATE = 48_000; const dir = mkdtempSync(join(tmpdir(), "hf-check-audio-")); afterAll(() => rmSync(dir, { recursive: true, force: true })); -/** A mono 16-bit WAV of `seconds`, each sample from `fill`. */ -function wav(name: string, seconds: number, fill: (t: number) => number): string { +/** A mono 16-bit WAV of `seconds`, each sample from `fill`; `byteRate` is the header's claim. */ +function wav( + name: string, + seconds: number, + fill: (t: number) => number, + byteRate = RATE * 2, +): string { const frames = Math.round(seconds * RATE); const data = Buffer.alloc(frames * 2); for (let i = 0; i < frames; i++) data.writeInt16LE(Math.round(fill(i / RATE) * 32767), i * 2); @@ -24,7 +29,7 @@ function wav(name: string, seconds: number, fill: (t: number) => number): string header.writeUInt16LE(1, 20); header.writeUInt16LE(1, 22); header.writeUInt32LE(RATE, 24); - header.writeUInt32LE(RATE * 2, 28); + header.writeUInt32LE(byteRate, 28); header.writeUInt16LE(2, 32); header.writeUInt16LE(16, 34); header.write("data", 36); @@ -72,6 +77,8 @@ describe.skipIf(!ffmpeg)("inspectAudio", () => { // Long enough to be sampled at spots first; sound only between the spots. wav("long-silent.wav", 20, silent); wav("long-gap.wav", 20, (t) => (t > 5 && t < 6 ? tone(t) : 0)); + // Its header claims twice the real byte rate, as some speech generators write it. + wav("misstated-rate.wav", 8, tone, RATE * 4); // Not audio at all: the render's audio probe rejects it and the whole mix fails. writeFileSync(join(dir, "notes.wav"), "RIFF but not really a wave file"); // A 1x1 PNG: it probes, but as a still image, so render refuses it as an