/** * Deterministic parsers for ffmpeg filter logs (stderr text). Internal module: * the pure exports accept raw logs so agents never hand-parse ffmpeg output. */ export type Span = { start: number; end: number } /** Parse `silencedetect` stderr into silence spans. An unterminated final silence ends at `durationSeconds`. */ export function parseSilencedetect(log: string, durationSeconds: number): Span[] { const starts = [...log.matchAll(/silence_start: (-?[\d.]+)/g)].map((m) => Number(m[1])) const ends = [...log.matchAll(/silence_end: ([\d.]+)/g)].map((m) => Number(m[1])) if (ends.length < starts.length) ends.push(durationSeconds) return starts.map((start, i) => ({ start, end: ends[i] })) } export type Loudness = { integratedLufs: number; lraLu: number; truePeakDbtp: number } /** Parse the `Summary:` block of `ebur128=peak=true` stderr. */ export function parseEbur128(log: string): Loudness { const summary = log.slice(log.lastIndexOf('Summary:')) if (!summary.startsWith('Summary:')) throw new Error('ebur128 log has no Summary block') const i = summary.match(/I:\s+(-?[\d.]+) LUFS/) const lra = summary.match(/LRA:\s+(-?[\d.]+) LU/) const peak = summary.slice(summary.indexOf('True peak')).match(/Peak:\s+(-?[\d.]+|-inf) dBFS/) if (!i || !lra || !peak) throw new Error('ebur128 Summary missing I, LRA or True peak (run with peak=true)') return { integratedLufs: Number(i[1]), lraLu: Number(lra[1]), truePeakDbtp: peak[1] === '-inf' ? -Infinity : Number(peak[1]), } } /** Count `blackdetect` runs in stderr. */ export function countBlackRuns(log: string): number { return [...log.matchAll(/black_start/g)].length } /** Parse the overall `RMS level dB` from `astats=metadata=0:measure_perchannel=none` stderr (null for silence). */ export function parseAstatsRms(log: string): number | null { const m = log.match(/RMS level dB:\s*(-?[\d.]+|-inf)/) return !m || m[1] === '-inf' ? null : Number(m[1]) }