Files
hyperframes/packages/studio-server/src/helpers/peakMap.ts
T
Michael Wang fe93f9cd47 fix: refuse media paths that ffmpeg's image2 demuxer would read as a pattern (#8047)
* fix: refuse media paths that ffmpeg's image2 demuxer would read as a pattern

ffmpeg's image2 demuxer reads %d, %03d, %% and a % before a wildcard in an
input path as a pattern of other files, folders included. Every project,
private-file and received-file check approves the literal path, so a
composition could name a placeholder whose pattern reaches images outside
the project and get them back as video frames.

Every ffmpeg and ffprobe input built from a project file now goes through
assertLiteralMediaInput, which refuses such a local path before ffmpeg runs.
The engine's urlInputArgs, the parsers' mediaInputFormatArgs and the
studio-server's new mediaInputArgs all take the input path, so the CLI
snapshot and cloud offload, lint, core grade analysis and studio-server
callers are covered too. URLs and other percent signs, such as %20, are
read as before.

* build(parsers): declare the literal-media-input subpath

* build: raise the parsers packed-size budget for the literal-media-input subpath

The new subpath adds its own built entry, type file and export map rows,
about 2.7 KB packed. It is a separate module so test suites that mock
ff-binaries still run the real path check.

* test(parsers): a harmless name shaped like a pattern is refused too
2026-11-17 07:28:09 +00:00

115 lines
3.6 KiB
TypeScript

import { spawn } from "node:child_process";
import { requireFfBinary } from "@hyperframes/parsers/ff-binaries";
import { probeAudioTrack } from "./mediaMetadata.js";
import { mediaInputArgs } from "./mediaExtensions.js";
export const PEAK_BIN_SECONDS = 0.05;
const PEAK_SAMPLE_RATE = 48_000;
const PEAK_MAP_CACHE_VERSION = "peaks-v2";
/**
* Running max-abs per bin over interleaved f32le at the source's own channel
* count. Native channels, not `-ac 2`: ffmpeg's mono upmix pans at -3 dB,
* which would hide a clip that really does reach 0 dBFS.
*/
export interface PeakAccumulator {
bins: number[];
frame: number;
carry: Buffer;
framesPerBin: number;
channels: number;
}
export function createPeakAccumulator(
sampleRate = PEAK_SAMPLE_RATE,
binSeconds = PEAK_BIN_SECONDS,
channels = 2,
): PeakAccumulator {
return {
bins: [],
frame: 0,
carry: Buffer.alloc(0),
framesPerBin: Math.max(1, Math.round(sampleRate * binSeconds)),
channels: Math.max(1, channels),
};
}
function framePeak(data: Buffer, offset: number, channels: number): number {
let peak = 0;
for (let ch = 0; ch < channels; ch++) {
peak = Math.max(peak, Math.abs(data.readFloatLE(offset + ch * 4)));
}
return peak;
}
export function accumulatePeaks(acc: PeakAccumulator, chunk: Buffer): void {
const data = acc.carry.length > 0 ? Buffer.concat([acc.carry, chunk]) : chunk;
const frameBytes = 4 * acc.channels;
const whole = data.length - (data.length % frameBytes);
for (let offset = 0; offset < whole; offset += frameBytes) {
const peak = framePeak(data, offset, acc.channels);
const bin = Math.floor(acc.frame / acc.framesPerBin);
if (acc.bins.length <= bin) acc.bins.push(0);
if (peak > (acc.bins[bin] ?? 0)) acc.bins[bin] = peak;
acc.frame += 1;
}
acc.carry = Buffer.from(data.subarray(whole));
}
/** Linear sample peak per bin, NOT normalized — the level is the point. */
export function finishPeaks(acc: PeakAccumulator): number[] {
return acc.bins.map((peak) => Number(peak.toFixed(4)));
}
export async function decodePeakMap(mediaPath: string, signal?: AbortSignal): Promise<number[]> {
const track = await probeAudioTrack(mediaPath);
if (!track || track.channels < 1) throw new Error("no audio stream");
signal?.throwIfAborted();
const { stream, channels } = track;
const ffmpeg = requireFfBinary("ffmpeg");
return new Promise((resolvePromise, reject) => {
const proc = spawn(
ffmpeg,
[
"-v",
"error",
...mediaInputArgs(mediaPath),
"-i",
mediaPath,
"-map",
`0:a:${stream}`,
"-vn",
"-ar",
String(PEAK_SAMPLE_RATE),
"-f",
"f32le",
"pipe:1",
],
{ stdio: ["ignore", "pipe", "ignore"], windowsHide: true, signal },
);
const acc = createPeakAccumulator(PEAK_SAMPLE_RATE, PEAK_BIN_SECONDS, channels);
proc.stdout?.on("data", (chunk: Buffer) => accumulatePeaks(acc, chunk));
proc.on("close", (code) => {
if (code !== 0) {
reject(new Error(`ffmpeg failed decoding audio (exit ${code})`));
return;
}
if (acc.frame === 0) {
reject(new Error("ffmpeg produced no audio samples"));
return;
}
resolvePromise(finishPeaks(acc));
});
proc.on("error", reject);
});
}
/** Keyed on content (size + mtime) like the waveform cache, under its own version prefix. */
export function buildPeakMapCacheKey(
assetPath: string,
fingerprint: { size: number; mtimeMs: number },
): string {
const name = assetPath.replace(/[/\\]/g, "_");
return `${PEAK_MAP_CACHE_VERSION}_${name}_${fingerprint.size}-${Math.round(fingerprint.mtimeMs)}.json`;
}