// ROOM AT A CLIP'S EDGES (release 36 lane E): a join plays over quiet picture, // never over the end of a sentence or the start of the next one. // // render.room {tail?: true, head?: true, minGap?: 0.35, maxS?: 2.5} // clip.room false (no room for this clip), or {tail?: boolean, head?: boolean} // (that edge for this clip, whatever the cut says) // // An edge is on when its key is `true` (the clip's key, else the cut's). // Absent (or null, or neither edge on) = off, and the render is byte for byte // what it was. Room needs a join: with `render.transition` 0 (or a montage, // or a short) it does nothing. // // THE RULE, in the source's seconds (D = render.transition): // // TAIL From the cut's end, the SOUND runs on to the first silence: the end // of the run of words that crosses the cut's end or starts within // `minGap` after it, the run stopping at the first gap between two // words of at least `minGap`; never more than `maxS` past the cut's // end. A 60 ms fade starts there. The PICTURE then keeps going, muted, // for D + 0.1 s, so the join's D seconds lie wholly over quiet video. // HEAD When speech starts within D of the cut's start (a word crosses it or // begins less than D after it), the picture starts D earlier, muted, // and the sound fades in over the 60 ms that end at the cut's start. // Only when something comes before the clip in the cut. // // The words: the project's words.json when the clip is from the recording // reactions.json describes, else the corpus transcript's whisper words, // else the cues spread over their words. When there are none -- or the // cues' run reaches the cap, which says the cues abut rather than that // anyone talks on -- silencedetect on the source (-35 dB, 0.3 s): the // silence the cut's end is in (the sound stops there), else the next one // to start within maxS; no silence found leaves the sound at the cut's end. // // Every extension stays inside the file the clip is cut from: a saved // whole video has room to spare, a fetched window (the extent plus the // fetch pad) clamps it -- the build says so, and the lint warns when the // room can reach past the window. // ROOM, its edges, its bounds and validateRoom / roomOn / clipRoom are the // look's rules (common lib/report/look.mjs, release 38): re-exported here, so // the writer, the build and the video assistant refuse the same things. import { ROOM, clipRoom } from "yt-dlp-transcript-common/lib/report/look.mjs"; export { ROOM, ROOM_BOUNDS, ROOM_EDGES, clipRoom, roomOn, validateRoom } from "yt-dlp-transcript-common/lib/report/look.mjs"; const num = (v) => typeof v === "number" && Number.isFinite(v); const r3 = (n) => Math.round(n * 1000) / 1000; /** Timed words, in time order (any of the transcribers' keys for the text is fine; only times are read). */ function timed(words) { return (Array.isArray(words) ? words : []) .map((w) => ({ start: Number(w?.start), end: Number(w?.end) })) .filter((w) => Number.isFinite(w.start) && Number.isFinite(w.end) && w.end >= w.start) .sort((a, b) => a.start - b.start); } /** * Where a clip's SOUND stops, from its words: the end of the run of words * that crosses `cutEnd` or starts within `minGap` after it, the run ending at * the first gap of at least `minGap`; never more than `maxS` past `cutEnd`. * `cutEnd` itself when no word is in reach. * * @returns {{ at: number, extended: boolean, capped: boolean }} */ export function tailFromWords(words, cutEnd, { minGap = ROOM.minGap, maxS = ROOM.maxS } = {}) { const ws = timed(words); const i = ws.findIndex((w) => w.end > cutEnd); if (i < 0 || ws[i].start >= cutEnd + minGap) return { at: cutEnd, extended: false, capped: false }; let end = ws[i].end; for (let j = i + 1; j < ws.length; j += 1) { if (ws[j].start - end >= minGap) break; end = Math.max(end, ws[j].end); } const cap = cutEnd + maxS; return { at: r3(Math.min(end, cap)), extended: end > cutEnd, capped: end > cap }; } /** Where speech starts at a clip's head, from its words: the first word not over before `cutStart` (Infinity: none). */ export function speechStartFromWords(words, cutStart) { const w = timed(words).find((x) => x.end > cutStart); return w ? Math.max(cutStart, w.start) : Infinity; } /** * The same two answers from silences (`[{s, e}]`, silencedetect's), when * there are no words: the sound stops at `cutEnd` when it is in a silence, * else where the next silence within `maxS` begins; no silence in reach * leaves it at `cutEnd` (nothing says where the speech ends). * * @returns {{ at: number, extended: boolean, capped: boolean, found: boolean }} */ export function tailFromSilences(silences, cutEnd, { maxS = ROOM.maxS } = {}) { const sil = (Array.isArray(silences) ? silences : []).filter((x) => num(x?.s) && num(x?.e)).sort((a, b) => a.s - b.s); if (sil.some((x) => x.s <= cutEnd && x.e > cutEnd)) return { at: cutEnd, extended: false, capped: false, found: true }; const next = sil.find((x) => x.s > cutEnd && x.s <= cutEnd + maxS); if (!next) return { at: cutEnd, extended: false, capped: false, found: false }; return { at: r3(next.s), extended: true, capped: false, found: true }; } /** Where speech starts at a clip's head, from silences: the end of the silence `cutStart` is in, else `cutStart`. */ export function speechStartFromSilences(silences, cutStart) { const hit = (Array.isArray(silences) ? silences : []).find((x) => num(x?.s) && num(x?.e) && x.s <= cutStart && x.e > cutStart); return hit ? hit.e : cutStart; } /** * The segment a clip with room plays, in the seconds of the file it is cut * from (`cutA`/`cutB`: the snapped cut; `soundEnd`: tailFromWords' or * tailFromSilences' `at`, or null with the tail off; `speechStart`: where * speech begins, or null with the head off; `duration`: the file's length, * when known). The picture is [picA, picB]; the sound is heard over * [cutA, soundB] (muted before and after, with the fades). * * @returns {{ picA: number, picB: number, soundA: number, soundB: number, head: number, tail: number, * clamped: Array<"head" | "tail"> }} head/tail: the muted seconds before cutA / after soundB */ export function roomPlan({ cutA, cutB, D, soundEnd = null, speechStart = null, duration = null }) { const len = num(duration) && duration > 0 ? duration : Infinity; const clamped = []; let picA = cutA; if (speechStart !== null && speechStart < cutA + D) { picA = cutA - D; if (picA < 0) { picA = 0; clamped.push("head"); } } let soundB = soundEnd === null ? cutB : Math.max(cutB, soundEnd); let picB = soundEnd === null ? cutB : soundB + D + ROOM.pad; if (picB > len) { picB = len; soundB = Math.min(soundB, len); clamped.push("tail"); } picA = r3(picA); picB = r3(picB); soundB = r3(soundB); return { picA, picB, soundA: cutA, soundB, head: r3(cutA - picA), tail: r3(picB - soundB), clamped }; } /** * The sound's filters for a plan, in the segment's own clock (0 = picA): a * fade in ending at the cut's start when the head is muted, a fade out from * the silence point when the tail is. "" when neither edge moved. */ export function roomAudioFilter(plan, { fade = ROOM.fade } = {}) { const parts = []; if (plan.head > 0) parts.push(`afade=t=in:st=${Math.max(0, plan.head - fade).toFixed(3)}:d=${fade}`); if (plan.picB > plan.soundB) parts.push(`afade=t=out:st=${(plan.soundB - plan.picA).toFixed(3)}:d=${fade}`); return parts.join(","); } /** * What the clip editor's strip shades for one clip (its unsnapped cut, the * neighbourhood's words or cues): the muted head [a, b] and the muted tail * [soundEnd, end], each null when that edge is off or does not move. The * build snaps the cut first, so this is the rule on the cut as drawn. * * @param {{ cutStart: number, cutEnd: number, room: { tail: boolean, head: boolean, minGap?: number, maxS?: number } | null, * D: number, words?: Array<{ start: number, end: number }> | null, cues?: Array<{ start: number, end: number }> | null }} o * @returns {{ head: number[] | null, tail: number[] | null, soundEnd: number | null }} */ export function roomShade({ cutStart, cutEnd, room, D, words = null, cues = null }) { if (!room || !(D > 0)) return { head: null, tail: null, soundEnd: null }; const ws = Array.isArray(words) && words.length ? words : Array.isArray(cues) ? cues : []; let tail = null; let soundEnd = null; if (room.tail) { soundEnd = ws.length ? tailFromWords(ws, cutEnd, room).at : cutEnd; tail = [soundEnd, r3(soundEnd + D + ROOM.pad)]; } let head = null; if (room.head && (!ws.length || speechStartFromWords(ws, cutStart) < cutStart + D)) head = [r3(Math.max(0, cutStart - D)), cutStart]; return { head, tail, soundEnd }; } /** * The lint's word on a clip's room: a fetched window (the extent plus the * fetch pad) that the room can reach past -- the build clamps it there; a * saved whole video would not. */ export function roomWindowWarnings(manifest) { const render = manifest?.render ?? {}; const D = render.transition ?? 0.5; const out = []; const timeline = manifest?.timeline ?? []; const anyRoom = timeline.some((e) => e?.type === "clip" && clipRoom(e, render)); if (!anyRoom) return out; if (!(D > 0)) { out.push("render.room: the cut has no transition (render.transition 0), so there is no join to make room for -- room does nothing"); return out; } const pad = render.fetchPad ?? 3.0; for (const e of timeline) { if (e?.type !== "clip") continue; const room = clipRoom(e, render); if (!room || !num(e.start) || !num(e.end)) continue; const cutStart = num(e.cutStart) ? e.cutStart : e.start; const cutEnd = num(e.cutEnd) ? e.cutEnd : e.end; const reach = cutEnd + room.maxS + D + ROOM.pad; if (room.tail && reach > e.end + pad + 1e-6) { out.push(`${e.id}: its room can run ${r3(reach - cutEnd)} s past the cut but a fetched window ends ${r3(e.end + pad - cutEnd)} s after it -- a fetched source clamps the tail there (a saved whole video does not)`); } if (room.head && cutStart - D < Math.max(0, e.start - pad) - 1e-6) { out.push(`${e.id}: its muted head (${D} s) starts before a fetched window does -- a fetched source clamps it`); } } return out; }