mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
* 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
129 lines
3.9 KiB
TypeScript
129 lines
3.9 KiB
TypeScript
import { execFile } from "node:child_process";
|
|
import { promisify } from "node:util";
|
|
import { findFfBinary } from "@hyperframes/parsers/ff-binaries";
|
|
import {
|
|
decodableAudioStream,
|
|
misstatesPcmByteRate,
|
|
type FfprobePcmFields,
|
|
} from "@hyperframes/parsers/media-streams";
|
|
import type { AudioSignal } from "@hyperframes/core/audio-analysis";
|
|
|
|
const run = promisify(execFile);
|
|
|
|
export interface AudioStreamInfo {
|
|
audioStream: number;
|
|
sampleRate: number;
|
|
channels: number;
|
|
duration: number;
|
|
misstatedByteRate?: boolean;
|
|
}
|
|
|
|
function requiredBinary(name: "ffmpeg" | "ffprobe"): string {
|
|
const path = findFfBinary(name, { configuredMustExist: true });
|
|
if (!path) throw new Error(`${name} is required to analyze audio; run: npx hyperframes doctor`);
|
|
return path;
|
|
}
|
|
|
|
/** The format of the audio track ffmpeg can decode, or `null` when the file has none. */
|
|
export async function probeAudio(file: string): Promise<AudioStreamInfo | null> {
|
|
const { stdout } = await run(
|
|
requiredBinary("ffprobe"),
|
|
[
|
|
"-v",
|
|
"error",
|
|
"-select_streams",
|
|
"a",
|
|
"-show_streams",
|
|
"-show_format",
|
|
"-of",
|
|
"json",
|
|
"--",
|
|
file,
|
|
],
|
|
{ maxBuffer: 8 * 1024 * 1024, windowsHide: true },
|
|
);
|
|
const parsed = JSON.parse(stdout) as {
|
|
streams?: (FfprobePcmFields & { duration?: string })[];
|
|
format?: { duration?: string };
|
|
};
|
|
const index = decodableAudioStream(parsed.streams ?? []);
|
|
const stream = index === null ? undefined : parsed.streams?.[index];
|
|
if (index === null || !stream) return null;
|
|
return {
|
|
audioStream: index,
|
|
sampleRate: Number(stream.sample_rate),
|
|
channels: stream.channels ?? 1,
|
|
duration: Number(stream.duration ?? parsed.format?.duration ?? 0),
|
|
misstatedByteRate: misstatesPcmByteRate(stream),
|
|
};
|
|
}
|
|
|
|
export interface DecodeWindow {
|
|
/** Seconds into the file. */
|
|
from?: number;
|
|
/** Seconds of file to read; the rest of the file when absent. */
|
|
seconds?: number;
|
|
}
|
|
|
|
export interface DecodeFormat {
|
|
sampleRate?: number;
|
|
mono?: boolean;
|
|
}
|
|
|
|
export interface Decoded {
|
|
signal: AudioSignal;
|
|
stream: AudioStreamInfo;
|
|
}
|
|
|
|
/** Decode the decodable audio track at its own sample rate, channels kept apart; `null` when the file has none. */
|
|
export async function decodeAudio(
|
|
file: string,
|
|
window: DecodeWindow = {},
|
|
probed?: AudioStreamInfo,
|
|
format: DecodeFormat = {},
|
|
): Promise<Decoded | null> {
|
|
const probedInfo = probed ?? (await probeAudio(file));
|
|
if (!probedInfo) return null;
|
|
const info = {
|
|
...probedInfo,
|
|
sampleRate: format.sampleRate ?? probedInfo.sampleRate,
|
|
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",
|
|
...inputSeek,
|
|
"-i",
|
|
file,
|
|
...decodedSeek,
|
|
...length,
|
|
"-map",
|
|
`0:a:${probedInfo.audioStream}`,
|
|
...(format.mono ? ["-ac", "1"] : []),
|
|
...(format.sampleRate ? ["-ar", String(format.sampleRate)] : []),
|
|
"-f",
|
|
"f32le",
|
|
"-acodec",
|
|
"pcm_f32le",
|
|
"-",
|
|
],
|
|
{ encoding: "buffer", maxBuffer: 2 * 1024 * 1024 * 1024, windowsHide: true },
|
|
);
|
|
const bytes = stdout.byteLength - (stdout.byteLength % 4);
|
|
const interleaved = new Float32Array(
|
|
stdout.buffer.slice(stdout.byteOffset, stdout.byteOffset + bytes),
|
|
);
|
|
const frames = Math.floor(interleaved.length / info.channels);
|
|
const channels = Array.from({ length: info.channels }, (_, c) => {
|
|
const samples = new Float32Array(frames);
|
|
for (let i = 0; i < frames; i++) samples[i] = interleaved[i * info.channels + c]!;
|
|
return samples;
|
|
});
|
|
return { signal: { sampleRate: info.sampleRate, channels }, stream: info };
|
|
}
|