// Auto-scope (release 36 B1): where, in a long recording, each narrated video // is. A reaction stream plays one video, then another; the host talks over // both and between them. Release 35 cut the Nik Drop from one video at a time // with a scope read off the speaker clusters by hand; this finds the spans. // // From the speaker turns alone (diarization.json), pure, deterministic: // // the host the cluster assignRoles calls the host over the whole // recording (or the operator's), left out of the counting: // he talks all the way through, so he never marks a video // windows SEGMENT_WINDOW_S long, every SEGMENT_HOP_S: each window's // subject is the cluster with the most seconds in turns of at // least SHORT_TURN_S (a narrated video talks in long runs), // counted when it has SEGMENT_MIN_SUBJECT_S of them and // SEGMENT_DOMINANCE of the long-turn time // a span consecutive windows with the same subject; its edges are // that subject's first and last long turn inside them // joined neighbouring spans of one subject are one (a dip between // them); spans under SEGMENT_MIN_S are dropped // the tail the host-only time after a video's last line — the host's // reaction to it — folded into it, up to SEGMENT_TAIL_S and // never past the next video's first line; its end on a turn's // end. The time before a video's first line (an intro, the // host between videos past the tail) belongs to no segment. // // → `[{ id: "s1", from, to, subject, host, why }]`, chronological. Roles are // still assigned per segment (the analysis runs on the segment's turns with // the scope); this only says where the segments are. // // Plain ESM with a .d.mts beside it. import { SHORT_TURN_S, assignRoles, cleanTurns } from "./clips.mjs"; export const SEGMENT_WINDOW_S = 300; export const SEGMENT_HOP_S = 30; /** A window's subject has at least this many seconds of long turns in it… */ export const SEGMENT_MIN_SUBJECT_S = 60; /** …and at least this share of the window's long-turn time (the host's left out). */ export const SEGMENT_DOMINANCE = 0.6; /** A segment shorter than this is dropped. */ export const SEGMENT_MIN_S = 180; /** The host-only time after a video's last line folded into it: at most this. */ export const SEGMENT_TAIL_S = 300; const round = (x, d = 2) => Math.round(x * 10 ** d) / 10 ** d; const overlap = (a0, a1, b0, b1) => Math.max(0, Math.min(a1, b1) - Math.max(a0, b0)); /** * The narrated-video spans of a recording. * @param {{ turns: unknown, duration?: number, host?: number | null, operator?: Record }} o * `host`: the host's cluster when it is known (the operator's word); else assignRoles' over the whole recording * @returns {{ id: string, from: number, to: number, subject: number, host: number | null, why: string }[]} */ export function findSegments({ turns, duration = 0, host = null, operator = {} }) { const ts = cleanTurns(turns); if (!ts.length) return []; const end = Math.max(Number(duration) || 0, ...ts.map((t) => t.end)); let hostIx = Number.isInteger(host) && /** @type {number} */ (host) >= 0 ? /** @type {number} */ (host) : null; if (hostIx === null) { const roles = assignRoles({ turns: ts, duration: end, operator }); hostIx = roles.find((s) => s.role === "host")?.index ?? null; } const long = ts.filter((t) => t.speaker !== hostIx && t.end - t.start >= SHORT_TURN_S); // The windows, and each one's subject (or none). const starts = []; for (let a = 0; a + SEGMENT_WINDOW_S < end; a += SEGMENT_HOP_S) starts.push(a); const lastStart = Math.max(0, end - SEGMENT_WINDOW_S); if (!starts.length || starts[starts.length - 1] < lastStart) starts.push(lastStart); const labels = starts.map((a) => { const b = Math.min(end, a + SEGMENT_WINDOW_S); /** @type {Map} */ const by = new Map(); let total = 0; for (const t of long) { if (t.start >= b) break; const o = overlap(t.start, t.end, a, b); if (o <= 0) continue; by.set(t.speaker, (by.get(t.speaker) ?? 0) + o); total += o; } let best = null; for (const [s, sec] of by) if (!best || sec > best.sec || (sec === best.sec && s < best.s)) best = { s, sec }; if (!best || best.sec < SEGMENT_MIN_SUBJECT_S || best.sec / total < SEGMENT_DOMINANCE) return { a, b, subject: null }; return { a, b, subject: best.s }; }); // Runs of one subject, edged on that subject's long turns inside them. /** @type {{ from: number, to: number, subject: number }[]} */ let spans = []; for (let i = 0; i < labels.length; ) { const s = labels[i].subject; let j = i; while (j + 1 < labels.length && labels[j + 1].subject === s) j += 1; if (s !== null) { const a = labels[i].a; const b = labels[j].b; const own = long.filter((t) => t.speaker === s && t.end > a && t.start < b); if (own.length) spans.push({ from: own[0].start, to: Math.max(...own.map((t) => t.end)), subject: s }); } i = j + 1; } const join = (list) => { const out = []; for (const sp of list) { const last = out[out.length - 1]; if (last && last.subject === sp.subject) last.to = Math.max(last.to, sp.to); else out.push({ ...sp }); } // Interleaved edges (a window can see two videos' lines): the later one starts after the earlier one ends. for (let k = 1; k < out.length; k += 1) out[k].from = Math.max(out[k].from, out[k - 1].to); return out.filter((sp) => sp.to > sp.from); }; spans = join(spans); spans = join(spans.filter((sp) => sp.to - sp.from >= SEGMENT_MIN_S)); return spans.map((sp, k) => { const next = spans[k + 1]; const limit = Math.min(next ? next.from : end, sp.to + SEGMENT_TAIL_S); // The tail ends on the last turn that ends inside it (a turn is never cut). let to = sp.to; for (const t of ts) { if (t.start >= limit) break; if (t.end <= limit && t.end > to) to = t.end; } let subjectLong = 0; let allLong = 0; for (const t of long) { const o = overlap(t.start, t.end, sp.from, sp.to); allLong += o; if (t.speaker === sp.subject) subjectLong += o; } const pct = Math.floor((100 * subjectLong) / (allLong || 1)); const tail = round(to - sp.to, 0); const why = `subject ${sp.subject} holds ${pct}% of long-turn time` + (tail >= 1 ? `; ${tail} s after its last line kept (the host's reaction)` : ""); return { id: `s${k + 1}`, from: round(sp.from), to: round(to), subject: sp.subject, host: hostIx, why }; }); } /** * The segment a run is scoped to: the one named, else the longest (the * earliest of equals). Null when there are none; an unknown id throws. * @template {{ id: string, from: number, to: number }} S * @param {S[]} segments * @param {string | null} [id] * @returns {S | null} */ export function pickSegment(segments, id = null) { const list = Array.isArray(segments) ? segments : []; if (id !== null && id !== undefined && id !== "") { const hit = list.find((s) => s.id === id); if (!hit) throw new Error(`no segment ${id}${list.length ? ` (there are ${list.map((s) => s.id).join(", ")})` : " (no segments were found)"}`); return hit; } let best = null; for (const s of list) if (!best || s.to - s.from > best.to - best.from) best = s; return best; } /** * Whether a project's scope (`[from, to|null]`, or null for the whole * recording) covers a segment: they share at least half of the shorter one. * @param {[number, number | null] | null | undefined} scope * @param {{ from: number, to: number }} segment * @param {number} duration the recording's (a scope open at the end runs to it) */ export function scopeOverlapsSegment(scope, segment, duration) { const a = Array.isArray(scope) ? Number(scope[0]) || 0 : 0; const b = Array.isArray(scope) && scope[1] !== null && scope[1] !== undefined ? Number(scope[1]) : Math.max(Number(duration) || 0, segment.to); const o = overlap(a, b, segment.from, segment.to); return o > 0 && o >= 0.5 * Math.min(b - a, segment.to - segment.from); }