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