// IS A FETCHED WINDOW'S PICTURE WHERE ITS SOUND IS? // // A clip window (lib/clipWindow.ts) fetched over the network is cut by yt-dlp // with `--download-sections "*from-to" --force-keyframes-at-cuts`. Over // Rumble's HLS — muxed renditions, served as byte ranges of a `.tar` — that // seek does not always land both tracks on the same second. Measured on the // live corpus 2026-10-23: 26 of 318 Rumble windows came out with the PICTURE // shifted against the SOUND by 1–4 s. Cross-correlated against overlapping // good windows, the audio is placed correctly and the video shows content // about (video start_time) seconds LATER than it should — a window a viewer // sees as a dub, cut into a report video as a lie about who said what when. // // THE FFPROBE SIGNATURE SEPARATES THE TWO CLEANLY, which is why this can be a // rule and not a review step: // // bad (26) video start_time 1.03–3.83 s, the audio's about the same, and // video duration − audio duration ≈ the same 1–3.8 s. The costco // window: video start 3.200 dur 93.73, audio start 3.140 dur // 90.58, for a requested length of 93.72. // good (292) video start_time ≤ 0.07 s and |video dur − audio dur| ≤ 0.03 s // — one borderline at 0.50/0.43, which this calls desynced. // // A cut taken locally from a saved whole recording (the saved-video tier, // lib/savedVideoWindow-server.ts) is exactly in sync: the remedy for a // desynced window is that tier, not a second network fetch of the same seek. // // PURE: the probe's JSON in, a verdict out, so the verdict is testable on the // measured numbers. Running ffprobe, and setting a desynced window aside, is // clipWindowSync-server.ts. // A track starting more than this far into the window is not where it was // asked to be. The good windows' worst was 0.07 s; the bad ones' best 1.03 s. export const WINDOW_SYNC_MAX_START_S = 0.25; // The two tracks' durations differing by more than this is the other half of // the same signature (good: ≤ 0.03 s; bad: 1–3.8 s, matching the start). export const WINDOW_SYNC_MAX_DURATION_GAP_S = 0.25; // One stream of `ffprobe -show_entries stream=codec_type,start_time,duration // -of json`: ffprobe prints the numbers as strings, and "N/A" (or nothing) for // one a container does not carry — a Matroska stream's duration, typically. export type ProbedWindowStream = { codec_type?: string; start_time?: string | number; duration?: string | number; disposition?: { attached_pic?: number }; }; export type ClipWindowSync = { // False only for a window this rule has JUDGED and found desynced. A window // it cannot judge (no audio track — audio-only windows exist — or no video // track) is ok, with `reason` saying so. ok: boolean; // Null where the track is absent or ffprobe printed no number for it. videoStart: number | null; audioStart: number | null; videoDuration: number | null; audioDuration: number | null; // How far apart the picture and the sound are, in seconds: the largest of // the two starts and the duration gap (2 dp). An estimate — the measured // shift sat at about the video's start_time — and the number every log line // and sidecar quotes. lagS: number; // The window's own length (`to − from`), when the caller knew it. requestedLength?: number; reason?: string; }; // The verdict as a sidecar records it: the bytes it was taken over (a window // whose size changed has to be probed again) and when. export type RecordedClipWindowSync = ClipWindowSync & { bytes: number; checkedAt: string; }; const numOrNull = (v: unknown): number | null => { const n = typeof v === "number" ? v : typeof v === "string" ? Number.parseFloat(v) : Number.NaN; return Number.isFinite(n) ? n : null; }; const round2 = (n: number) => Math.round(n * 100) / 100; export function windowSyncOf( streams: readonly ProbedWindowStream[], requestedLength?: number, ): ClipWindowSync { const video = streams.find((s) => s.codec_type === "video" && !s.disposition?.attached_pic); const audio = streams.find((s) => s.codec_type === "audio"); const videoStart = numOrNull(video?.start_time); const audioStart = numOrNull(audio?.start_time); const videoDuration = numOrNull(video?.duration); const audioDuration = numOrNull(audio?.duration); const base = { videoStart, audioStart, videoDuration, audioDuration, ...(requestedLength !== undefined && Number.isFinite(requestedLength) ? { requestedLength: round2(requestedLength) } : {}), }; // NOT JUDGED: a window with one track has nothing to be out of sync with. if (!video || !audio) { return { ok: true, ...base, lagS: 0, reason: !video && !audio ? "no tracks: not judged" : !audio ? "no audio track: not judged" : "no video track: not judged", }; } const gap = videoDuration !== null && audioDuration !== null ? Math.abs(videoDuration - audioDuration) : null; const offences: string[] = []; if (videoStart !== null && videoStart > WINDOW_SYNC_MAX_START_S) { offences.push(`the video track starts ${videoStart.toFixed(2)} s in`); } if (audioStart !== null && audioStart > WINDOW_SYNC_MAX_START_S) { offences.push(`the audio track starts ${audioStart.toFixed(2)} s in`); } if (gap !== null && gap > WINDOW_SYNC_MAX_DURATION_GAP_S) { const longer = (videoDuration as number) > (audioDuration as number) ? "video" : "audio"; offences.push( `the ${longer} runs ${gap.toFixed(2)} s longer than the ${longer === "video" ? "audio" : "video"}`, ); } // Negative starts (an encoder's priming samples) are not "in", and say // nothing about the lag. const lagS = round2(Math.max(0, videoStart ?? 0, audioStart ?? 0, gap ?? 0)); if (offences.length === 0) return { ok: true, ...base, lagS }; return { ok: false, ...base, lagS, reason: offences.join("; ") }; } // The sidecar's `sync`, read back. Tolerant like every sidecar parse here: a // hand-edited or half-written one is "no verdict", which only means one probe. export function parseRecordedClipWindowSync(raw: unknown): RecordedClipWindowSync | undefined { if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined; const r = raw as Record; if (typeof r.ok !== "boolean") return undefined; const bytes = numOrNull(r.bytes); const lagS = numOrNull(r.lagS); if (bytes === null || lagS === null || typeof r.checkedAt !== "string") return undefined; const n = (k: string) => (typeof r[k] === "number" && Number.isFinite(r[k]) ? (r[k] as number) : null); const requestedLength = n("requestedLength"); return { ok: r.ok, videoStart: n("videoStart"), audioStart: n("audioStart"), videoDuration: n("videoDuration"), audioDuration: n("audioDuration"), lagS, ...(requestedLength !== null ? { requestedLength } : {}), ...(typeof r.reason === "string" && r.reason ? { reason: r.reason } : {}), bytes, checkedAt: r.checkedAt, }; } // "the picture runs 3.2 s off the sound" export function windowSyncLagText(sync: Pick): string { return `the picture runs ${sync.lagS.toFixed(1)} s off the sound`; } // THE SENTENCE A DESYNCED FETCH FAILS WITH — the batch's log line, the single // fetch's job error. It names the remedy: the saved-video tier cuts in sync, and // a second network fetch of the same seek is likely to come out the same. export function desyncedWindowMessage( sync: Pick, setAsideAs: string, platform?: string, ): string { const cause = platform === "rumble" ? "a Rumble HLS seek" : "a seek into the source's stream"; return ( `${windowSyncLagText(sync)} (${cause}); set aside as ${setAsideAs} — save the whole ` + `recording ("Persist source video") and fetch again to cut it from the saved video` ); }