// THE CLIP EDITOR's pure rules (release 35 C2), shared by the page and the // server: snap points, a span's words, the proposals not in the cut, and a // clip's provenance in words. No node imports -- a client component holds the // same rules as the route. import { BLUR_CAPTION_DEFAULT } from "umtool-report-to-video/clip-blur"; /** @typedef {{ w?: string, word?: string, text?: string, start: number, end: number }} Word */ /** @typedef {{ start: number, end: number, text: string }} Cue */ /** @typedef {{ start: number, end: number, speaker: number }} Turn */ const fin = (v) => typeof v === "number" && Number.isFinite(v); const r2 = (n) => Math.round(n * 100) / 100; /** A word's text, whichever key the transcriber used. */ export const wordText = (w) => String(w?.w ?? w?.word ?? w?.text ?? "").trim(); /** A list of words from what a words file may hold: an array, or `{ words: [...] }`. Kept: timed ones, in time order. */ export function wordsOf(raw) { const list = Array.isArray(raw) ? raw : Array.isArray(raw?.words) ? raw.words : []; return list.filter((w) => w && fin(w.start) && fin(w.end) && w.end >= w.start && wordText(w)).sort((a, b) => a.start - b.start); } /** * Where an edge may snap to: the middle of every gap between two words (a * word gap), every edge of a silence between turns, and -- with no words -- * every cue boundary. Sorted, 2 dp, no repeats. * @param {{ words?: Word[] | null, turns?: Turn[] | null, cues?: Cue[] | null, minGap?: number }} o */ export function snapPoints({ words = null, turns = null, cues = null, minGap = 0.05 } = {}) { const out = new Set(); const ws = Array.isArray(words) ? words : []; for (let i = 0; i + 1 < ws.length; i++) { const a = ws[i].end; const b = ws[i + 1].start; if (fin(a) && fin(b) && b - a >= minGap) out.add(r2((a + b) / 2)); } if (ws.length) { out.add(r2(ws[0].start)); out.add(r2(ws[ws.length - 1].end)); } const ts = (Array.isArray(turns) ? turns : []).filter((t) => fin(t.start) && fin(t.end)).sort((a, b) => a.start - b.start); for (let i = 0; i + 1 < ts.length; i++) { if (ts[i + 1].start - ts[i].end >= minGap) { out.add(r2(ts[i].end)); out.add(r2(ts[i + 1].start)); } } if (!ws.length) { for (const c of Array.isArray(cues) ? cues : []) { if (fin(c.start)) out.add(r2(c.start)); if (fin(c.end)) out.add(r2(c.end)); } } return [...out].sort((a, b) => a - b); } /** The snap point nearest `t`, within `max` seconds, or null. */ export function nearestPoint(t, points, max = 1.5) { let best = null; for (const p of points) { if (Math.abs(p - t) <= max && (best === null || Math.abs(p - t) < Math.abs(best - t))) best = p; } return best; } /** * Both edges of a cut snapped to their nearest points, keeping it at least * half a second long; an edge with no point within `max` stays. * @param {{ cutStart: number, cutEnd: number }} cut * @param {number[]} points */ export function snapCut(cut, points, max = 1.5) { const a = nearestPoint(cut.cutStart, points, max) ?? cut.cutStart; const b = nearestPoint(cut.cutEnd, points, max) ?? cut.cutEnd; if (b - a < 0.5) return { cutStart: cut.cutStart, cutEnd: cut.cutEnd }; return { cutStart: r2(a), cutEnd: r2(b) }; } /** * The words spoken in [from, to]: from the word timings when there are any * (a word counts when its middle is inside), else the cues overlapping it. * @param {{ words?: Word[] | null, cues?: Cue[] | null }} o */ export function quoteBetween({ words = null, cues = null }, from, to) { if (Array.isArray(words) && words.length) { return words .filter((w) => (w.start + w.end) / 2 >= from && (w.start + w.end) / 2 <= to) .map(wordText) .join(" ") .replace(/\s+/g, " ") .trim(); } return (Array.isArray(cues) ? cues : []) .filter((c) => c.end > from + 0.05 && c.start < to - 0.05) .map((c) => String(c.text ?? "").trim()) .join(" ") .replace(/\s+/g, " ") .trim(); } /** * The clip finder's proposals NOT in the cut: no clip's `assist.proposal` * names them. (reactions.json's own `used` says what the last propose did; * the manifest says what the cut holds now, after a person brought one in or * removed one.) Ranked best first. * @param {{ proposals?: Array<{ id: string, score?: number }> } | null} reactions * @param {Array<{ assist?: { proposal?: string } }>} clips */ export function unusedProposals(reactions, clips) { const inCut = new Set(clips.map((c) => c?.assist?.proposal).filter((p) => typeof p === "string")); return (Array.isArray(reactions?.proposals) ? reactions.proposals : []) .filter((p) => p && typeof p.id === "string" && fin(p.start) && fin(p.end) && !inCut.has(p.id)) .sort((a, b) => (b.score ?? 0) - (a.score ?? 0)); } /** A speaker's role from reactions.json's speakers, or null. */ export function roleOf(speakers, index) { const s = (Array.isArray(speakers) ? speakers : []).find((x) => x && x.index === index); return s && typeof s.role === "string" ? s.role : null; } /** * A clip's provenance in words: "by the assistant · 0.83 · host laughs …", * "edited by you · 3 edits", "added by you" -- or null for a clip the clip * finder never touched. * @param {{ by?: string, proposal?: string, score?: number, why?: string, edits?: unknown[] } | null | undefined} assist */ export function provenanceWords(assist) { if (!assist || typeof assist !== "object") return null; const edits = Array.isArray(assist.edits) ? assist.edits : []; const n = edits.length; if (assist.by === "assistant") { return ["by the assistant", fin(assist.score) ? assist.score.toFixed(2) : null, assist.why || null].filter(Boolean).join(" · "); } const added = edits.some((e) => e && /** @type {{ what?: string }} */ (e).what === "add"); const head = added ? (assist.proposal ? "brought in by you" : "added by you") : "edited by you"; return [head, assist.proposal && fin(assist.score) ? `the assistant scored it ${assist.score.toFixed(2)}` : null, n ? `${n} edit${n === 1 ? "" : "s"}` : null] .filter(Boolean) .join(" · "); } /** m:ss.s, for edges. */ export function edgeClock(s) { const v = Math.max(0, Number(s) || 0); const h = Math.floor(v / 3600); const m = Math.floor((v % 3600) / 60); const sec = (v % 60).toFixed(1).padStart(4, "0"); return h ? `${h}:${String(m).padStart(2, "0")}:${sec}` : `${m}:${sec}`; } // --------------------------------------------------------------------------- // A NEW CLIP from a span of a recording (the clip editor's New clip from // selection, the timeline's Add clip): its quote, its citation, its id, and // the entry the timeline route's `insert` writes. Pure, so the page and a // test hold the same rule. // --------------------------------------------------------------------------- /** * The cues spoken in [from, to] as one quote: in time order, a cue repeated * word for word (a rolling caption) said once, and " … " where two cues are * not adjacent (more than `gap` seconds of nothing between them) -- the words * left out are marked, never run together. * @param {Cue[] | null | undefined} cues * @param {number} from * @param {number} to * @param {{ gap?: number }} [o] */ export function quoteFromCues(cues, from, to, { gap = 1 } = {}) { const inSpan = (Array.isArray(cues) ? cues : []) .filter((c) => c && fin(c.start) && fin(c.end) && c.end > from + 0.05 && c.start < to - 0.05) .sort((a, b) => a.start - b.start); let out = ""; let prev = null; for (const c of inSpan) { const text = String(c.text ?? "").replace(/\s+/g, " ").trim(); if (!text) continue; if (prev && text === prev.text) { prev = { text, end: Math.max(prev.end, c.end) }; continue; } out += prev ? (c.start - prev.end > gap ? " … " : " ") + text : text; prev = { text, end: c.end }; } return out; } /** * A span's quote: from the word timings when there are any (quoteBetween), else the cues (quoteFromCues). * @param {{ words?: Word[] | null, cues?: Cue[] | null }} o * @param {number} from * @param {number} to */ export function spanQuote({ words = null, cues = null }, from, to) { return Array.isArray(words) && words.length ? quoteBetween({ words }, from, to) : quoteFromCues(cues, from, to); } /** * A citation link moved to another second: its `t=` (query or fragment) * replaced, in the form it had (`t=140s` stays seconds-with-an-s). A link * with no `t=` is kept as it is -- it may cite a mirror on purpose. * @param {string} url * @param {number} sec */ export function citeUrlAt(url, sec) { const s = Math.max(0, Math.floor(sec)); let u; try { u = new URL(url); } catch { return url; } if (u.searchParams.has("t")) { const was = u.searchParams.get("t") ?? ""; u.searchParams.set("t", /^\d+s$/.test(was) ? `${s}s` : String(s)); return u.toString(); } if (/(^#|&)t=/.test(u.hash)) { u.hash = u.hash.replace(/(^#|&)t=[^&]*/, (_m, lead) => `${lead}t=${s}`); return u.toString(); } return url; } /** An id not yet among `ids`: `base`, else `base-2`, `base-3`, … (manifest.mjs freshEntryId's rule). */ export function nextFreeId(ids, base) { const clean = String(base).replace(/[^A-Za-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 56) || "entry"; const taken = new Set(ids); if (!taken.has(clean)) return clean; for (let n = 2; ; n += 1) if (!taken.has(`${clean}-${n}`)) return `${clean}-${n}`; } /** `/