// Client-safe types and constants for the per-video AUDIO INTEGRITY sidecar // (release 31, lane A): `audio-integrity.json`, written by the `audio-audit` // job beside a video's held audio (controller/audioAudit.ts) and by a refetch // of damaged held audio (ytdlp/corruptRefetch.ts). Server-only I/O lives in // audioIntegrity-server.ts. // // WHY A SIDECAR AND NOT THE DOWNLOAD OUTCOME. Only a download that went // through the audio check (CHANNEL.md `audioCheck`) is ever classified at // fetch time. Audio held from before the check existed, or from a channel // without it, has no verdict at all — its download-outcome.json (when there is // one) says `ok`. The audit's verdict is a fact about THE FILE ON DISK, not // about a download, so it is keyed to that file (name, size, mtime) and is // stale the moment the file changes. // [from, to] seconds — the shape lib/salvageAudio-server.ts `SalvageGap` has // (that module is server-only). export type IntegrityGap = [from: number, to: number]; export const AUDIO_INTEGRITY_FILENAME = "audio-integrity.json"; // The record's own format version. export const AUDIO_INTEGRITY_VERSION = 1; // A file is DAMAGED when less than this share of its container's duration // decodes, or when any gap is listed (lib/salvageAudio-server.ts drops holes // under 0.5 s, so one refused AAC frame never lists). The snapshot's // `corruptHeldAudio` bucket and the audit's verdict both read this one number. export const AUDIO_INTEGRITY_MIN_DECODED_RATIO = 0.98; // clean decodes to within the ratio, no gap listed; // damaged some of it decodes, but not all (a gap, or under the ratio); // undecodable nothing decodes (or ffmpeg stopped before a single frame). export type AudioIntegrityVerdict = "clean" | "damaged" | "undecodable"; export type AudioIntegrityRecord = { version: number; checkedAt: string; // The audited file: a name in the video dir (a real audio file — through // its tier link when it is one), its size in bytes and its mtime in epoch // milliseconds (floored). The record speaks for THIS file only: a different // size or mtime makes it stale (`integrityMatches`). file: string; size: number; mtime: number; // The container's own duration (ffprobe), or null when it does not say. durationSeconds: number | null; // Seconds of audio that decoded (the tolerant decode's frames, summed). decodedSeconds: number; // Spans of the source's timeline with no decoded audio, merged, sorted. gaps: IntegrityGap[]; verdict: AudioIntegrityVerdict; // What measured it: the scan's own version and ffmpeg's `-version` line. tool: { scan: string; ffmpeg?: string }; // Who wrote it: the audit job, the refetch that replaced the file, or the // speakers lane after its engine could not decode the file (diarizeOne). by: "audit" | "refetch" | "speakers"; jobId?: string; }; // The scan's version, recorded with each verdict: bump when the decode or the // rule changes, so an old verdict can be told apart. export const AUDIO_INTEGRITY_SCAN_VERSION = "scanDecodable/1"; // PURE: the verdict for a scan's numbers. export function integrityVerdict(scan: { decodedSeconds: number; durationSeconds: number | null; gaps: ReadonlyArray; }): AudioIntegrityVerdict { if (!(scan.decodedSeconds > 0)) return "undecodable"; if (scan.gaps.length > 0) return "damaged"; const total = scan.durationSeconds; if (total !== null && total > 0 && scan.decodedSeconds < total * AUDIO_INTEGRITY_MIN_DECODED_RATIO) { return "damaged"; } return "clean"; } // PURE: is this record's file damaged? Recomputed from the numbers rather than // read off `verdict`, so a record written before a rule change is judged by // the rule in force. export function isDamagedIntegrity(r: Pick): boolean { return integrityVerdict(r) !== "clean"; } // PURE: does the record speak for this file? Name and size always; mtime when // the caller has it (the snapshot has sizes, not mtimes — it pays no stat). export function integrityMatches( r: Pick, file: { name: string; size?: number; mtime?: number }, ): boolean { if (r.file !== file.name) return false; if (file.size !== undefined && file.size !== r.size) return false; if (file.mtime !== undefined && Math.floor(file.mtime) !== r.mtime) return false; return true; } // "38:55 of 1:40:00 decodes, 3 gaps" — the one line a page or a log shows. export function describeIntegrity(r: Pick): string { const total = r.durationSeconds; const pct = total && total > 0 ? ` (${Math.round((r.decodedSeconds / total) * 1000) / 10}%)` : ""; const gaps = r.gaps.length === 0 ? "no gaps" : `${r.gaps.length} gap${r.gaps.length === 1 ? "" : "s"}`; return `${r.verdict}: ${clock(r.decodedSeconds)} of ${total === null ? "?" : clock(total)} decodes${pct}, ${gaps}`; } function clock(sec: number): string { const s = Math.max(0, Math.round(sec)); const h = Math.floor(s / 3600); const m = Math.floor((s % 3600) / 60); const ss = s % 60; const p = (n: number) => String(n).padStart(2, "0"); return h > 0 ? `${h}:${p(m)}:${p(ss)}` : `${m}:${p(ss)}`; }