// THE RECORDING'S END, SAID WHERE A CLIP'S EXTENT IS WRITTEN (lane window-end): // `umtool window` and `timeline insert` say when a clip's padded fetch window // runs past the end of a saved recording known whole -- the last clip of a // recording, its pad past the end. Nothing has to change: the editor cuts that // window from the saved video clamped at the end (common/lib/recordingEnd.mjs), // `umtool fetch --dry-run` says "saved-video, clamped at the recording's end", // and the render holds the clip's end there (snap.mjs `endAt`). So the clip // keeps its whole ending; shortening it to dodge a network fetch loses seconds // that are on disk. // // Cheap: the record's duration is read first, and a clip whose padded end is // more than the tolerance before it costs nothing more. Only a clip near the // end asks the disk (sources.mjs resolveLocalSource over the corpus tiers: a // window the editor already cut clamped, else the saved container's probe). // Never fetches, never writes. import { readFile } from "node:fs/promises"; import path from "node:path"; import { resolveLocalSource, ffprobeSource } from "umtool-report-to-video/sources"; import { RECORDING_END_TOLERANCE_SECONDS } from "yt-dlp-transcript-common/lib/recordingEnd.mjs"; import { resolveVideoDir } from "yt-dlp-transcript-common/lib/videoDir-server.mjs"; import { channelFor, channelsDirFor, hasShadowChannels } from "../projects/report.mjs"; /** * Where a clip's padded fetch window stands against the end of its saved * recording: null when it does not reach it (or nothing says where it is), * else the end, the window and whether the clip's own end is past it. * * @param {{ dir: string }} project * @param {Record} manifest the manifest the clip is read in (a take's, for a take) * @param {Record} entry * @param {{ probe?: Function, channelsDir?: string }} [opts] * @returns {Promise} */ export async function recordingEndOf(project, manifest, entry, opts = {}) { if (!entry || entry.type !== "clip" || entry.src || !entry.video) return null; const start = Number(entry.start); const end = Number(entry.end); if (!Number.isFinite(start) || !Number.isFinite(end)) return null; const slug = channelFor(manifest, entry); if (!slug) return null; const render = manifest?.render ?? {}; const pad = Number.isFinite(Number(render.fetchPad)) ? Number(render.fetchPad) : 3; const from = Number(Math.max(0, start - pad).toFixed(2)); const to = Number((end + pad).toFixed(2)); const channelsDir = opts.channelsDir ?? channelsDirFor(project.dir, manifest, { shadowExists: await hasShadowChannels(project.dir) }); // The record's own length first: far from the end, nothing is probed. const videoDir = await resolveVideoDir(channelsDir, slug, String(entry.video)).catch(() => null); if (videoDir) { try { const d = Number(JSON.parse(await readFile(path.join(videoDir, "metadata.info.json"), "utf8"))?.duration); if (Number.isFinite(d) && d > 0 && to < d - RECORDING_END_TOLERANCE_SECONDS) return null; } catch { // No record: the disk is asked. } } const hit = await resolveLocalSource( { video: String(entry.video), slug, from, to }, { channelsDir, probe: opts.probe ?? ffprobeSource, kinds: ["corpus-window", "saved-video"], render }, ); if (!hit?.endsRecording || !(hit.windowEnd < to)) return null; const recordingEnd = Number(hit.windowEnd.toFixed(2)); return { recordingEnd, from, to, clipEndPast: end > hit.windowEnd + 0.05, source: hit.kind }; } /** The line `umtool window` / `timeline insert` print for it. Pure. */ export function recordingEndLine(r) { if (!r) return null; return ( ` at the recording's end: its fetch window ${r.from.toFixed(2)}–${r.to.toFixed(2)} runs past the saved recording's end ` + `(${r.recordingEnd.toFixed(2)} s, known whole) — cut from the saved video, clamped there; nothing is fetched` + (r.clipEndPast ? `. Its end is past the recording's: it plays to ${r.recordingEnd.toFixed(2)}` : "") ); }