// HOME'S QUICK CLIP BOX, BEFORE THE RENDER (umtool redesign L4): a pasted // moment (momentLink.mjs) found in the corpus and read -- which channel holds // the recording (a platform link names none: the channel whose data/ holds // it; a Rumble embed id, the channel whose metadata names it), the span (the // link's own, else from the cue it lands in to the end of its sentence, at // most three cues on, as the timeline's Add clip picks), and the words around // it to cut by: the corpus's words (report-to-video/clip-words.mjs -- a words // sidecar, a whisper transcript, the en-orig captions' word starts), else its // cues. Nothing is fetched; the render is `umtool clip` (quickClip.mjs, POST // /api/report/quick-clip), which asks the editor for the window. // // Reads only. Plain ESM. import { readdir, stat } from "node:fs/promises"; import path from "node:path"; import { resolveVideoDirId } from "yt-dlp-transcript-common/lib/videoDir-server.mjs"; import { clipWords } from "umtool-report-to-video/clip-words"; import { CHANNELS_DIR } from "../paths.mjs"; import { ENDS_SENTENCE, IS_FILLER, cuePathFor, readCues } from "../projects/report.mjs"; import { parseMomentInput } from "./momentLink.mjs"; import { quickClipSpec } from "./quickClip.mjs"; /** Seconds of words shown either side of the span. */ export const STRIP_PAD_S = 12; /** A moment with no end: its sentence, at most this long. */ export const DEFAULT_SPAN_S = 20; const r2 = (x) => Math.round(Number(x) * 100) / 100; const isDir = (p) => stat(p).then((s) => s.isDirectory(), () => false); export class MomentError extends Error {} /** * The channel (and the folder id) a recording is under: the named one, else every channel whose data/ holds it * (a Rumble embed id: by the channel's metadata). Several: refused, naming them. * @param {{ channel: string | null, video: string }} m */ export async function findRecording(m, { channelsDir = CHANNELS_DIR } = {}) { const at = async (slug) => { const dir = await resolveVideoDirId(channelsDir, slug, m.video).catch(() => m.video); return (await isDir(path.join(channelsDir, slug, "data", dir))) ? { channel: slug, video: dir } : null; }; if (m.channel) { const got = await at(m.channel); if (!got) throw new MomentError(`this corpus has no ${m.channel}/${m.video}`); return got; } const slugs = (await readdir(channelsDir, { withFileTypes: true }).catch(() => [])) .filter((d) => (d.isDirectory() || d.isSymbolicLink()) && !d.name.startsWith(".")) .map((d) => d.name) .sort(); const found = []; for (const slug of slugs) { if (await isDir(path.join(channelsDir, slug, "data", m.video))) found.push({ channel: slug, video: m.video }); } // A Rumble embed id has no folder of its own: the channel whose metadata names it (resolveVideoDirId). if (!found.length && /^v[a-z0-9]{4,}$/i.test(m.video)) { for (const slug of slugs) { const got = await at(slug); if (got) found.push(got); } } if (!found.length) throw new MomentError(`no channel in this corpus holds ${m.video} — download it first, or write /${m.video}@-`); if (found.length > 1) throw new MomentError(`${found.length} channels hold ${m.video} (${found.map((f) => f.channel).join(", ")}): write /${m.video}@-`); return found[0]; } /** * The moment's span, from its cues when the link named only a second. * @param {{ start: number, end: number | null }} m * @param {Array<{ start: number, end: number, text: string }>} cues */ export function spanOf(m, cues) { if (m.end !== null && m.end > m.start) return { start: r2(m.start), end: r2(m.end) }; const list = (cues ?? []).filter((c) => Number.isFinite(c?.start) && Number.isFinite(c?.end)).sort((a, b) => a.start - b.start); const i = list.findIndex((c) => c.end > m.start); if (i < 0) return { start: r2(m.start), end: r2(m.start + DEFAULT_SPAN_S) }; const from = list[i]; const closes = (c) => ENDS_SENTENCE.test(c.text ?? "") && !IS_FILLER.test(c.text ?? ""); const close = list.slice(i).find((c, k) => closes(c) || k >= 2) ?? from; const start = Math.max(m.start, from.start); const end = Math.min(close.end, start + DEFAULT_SPAN_S); return { start: r2(start), end: r2(end > start + 0.5 ? end : start + DEFAULT_SPAN_S) }; } /** * A pasted moment, read: `{ spec, channel, video, start, end, from, to, words | null, cues | null, wordsFrom, title, duration }`. * Throws MomentError with the sentence to show. * @param {string} text */ export async function quickClipStrip(text, { channelsDir = CHANNELS_DIR } = {}) { const m = parseMomentInput(text); if (!m) throw new MomentError("not a moment: paste an archive, YouTube or Rumble link with its time, or /