// A frame scan → a DRAFT supercut (release 30, lane P): the events a frame // scan derived (frame-scan..json beside the video) become clips of a // `video.manifest.json` timeline — a window around each event, cut on the // transcript's sentence edges where cues exist, overlapping windows merged — // and the scan's running stats become the manifest's `frameScan` block: what // a stats overlay draws over each clip (chips, big blinds, players left, // level), read at any second by `frameTotals`. // // Plain ESM with no I/O, ledger-totals.mjs's pattern: the scaffold (`umtool // new --seed frames`) calls it with the parsed sidecar and `node --test` runs // it with literals. Every consumer gets the same arithmetic. No render here: // the clips are a draft to review in the bench. Since release 31 the events // are RANKED (`rankOf`: scoops, milestones, swings, pots, level-ups) into a // length budget (`target`), and the `stats` chrome region (stats.mjs, // chrome-stats.mjs) draws the overlay points over each clip. import { widen } from "yt-dlp-transcript-common/lib/cueWiden.mjs"; /** Events worth a clip, most important first (release 30's order; `rankOf` ranks since release 31). */ export const CUT_EVENT_KINDS = ["won", "bust", "final-table", "knockout", "double-up", "big-pot", "big-loss", "all-in", "eliminated", "players-left", "pot", "level-up"]; /** * The tiers a supercut fills its length budget from (release 31), first * first: the SCOOPS (the tournament won, a big pot, an all-in won or * doubled, a double-up, a knockout), the MILESTONES (the final table and the * players-left marks, the bust that ends it), the rest of the swings (an * all-in lost or unread, a big loss), the plain pots, and the level-ups last. * Within a tier a confirmed event before an unconfirmed one, then the * score, then the earlier. */ export const CUT_TIERS = Object.freeze(["scoop", "milestone", "swing", "pot", "level"]); const WON_ALL_IN = new Set(["doubled", "won"]); /** `{ tier, score }` for an event (lower tier first, higher score first); null when it is no clip. */ export function rankOf(e) { const ev = e.evidence ?? {}; const n = (v) => (typeof v === "number" && Number.isFinite(v) ? v : 0); switch (e.kind) { case "won": return { tier: 0, score: 1e6 }; case "all-in": return WON_ALL_IN.has(ev.outcome) ? { tier: 0, score: 5000 + (ev.outcome === "doubled" ? 1000 : 0) } : { tier: 2, score: 100 }; case "big-pot": return { tier: 0, score: 1000 + n(ev.potBB) }; case "double-up": return { tier: 0, score: 900 }; case "knockout": return { tier: 0, score: 800 }; case "bust": return { tier: 1, score: 5000 }; case "final-table": return { tier: 1, score: 4000 }; case "players-left": return { tier: 1, score: 1000 - n(ev.milestone) }; case "eliminated": return { tier: 1, score: 10 }; case "big-loss": return { tier: 2, score: 50 }; case "pot": return { tier: 3, score: n(ev.potBB) }; case "level-up": return { tier: 4, score: n(ev.bigBlind) }; default: return null; } } /** Ranked first-first: tier, confirmed, score, then the earlier. */ export function compareRank(a, b) { const ra = rankOf(a); const rb = rankOf(b); return ra.tier - rb.tier || (a.confidence === "unconfirmed") - (b.confidence === "unconfirmed") || rb.score - ra.score || a.t - b.t; } const ALL_IN_TITLE = { doubled: "All in, doubles up", won: "All in and wins", lost: "All in, loses chips", busted: "All in and out" }; /** A clip's on-screen title from the event that leads it. */ export function titleOf(e) { const ev = e.evidence ?? {}; if (e.kind === "all-in") return ALL_IN_TITLE[ev.outcome] ?? "All in"; if (e.kind === "players-left" && typeof ev.milestone === "number") { return ev.playersLeft === ev.milestone ? `Down to ${fmt(ev.milestone)}` : `Under ${fmt(ev.milestone)} left`; } return TITLES[e.kind] ?? e.kind; } const TITLES = { won: "Wins it", bust: "Busts out", eliminated: "Eliminated", "final-table": "Final table", "double-up": "Doubles up", "big-loss": "Loses half the stack", "all-in": "All in", knockout: "Knocks one out", "big-pot": "Takes a big pot", pot: "Takes a pot", "level-up": "Blinds go up", }; // `pre`/`post` frame a scoop, a milestone or a swing; `short` a plain pot // or a level-up. `target` is the length budget in seconds the clips fill // (merged, widened): ranked first-first, an event whose window would take // the cut past it is passed over for a later, smaller one. export const FRAMES_CUT_DEFAULTS = Object.freeze({ pre: 20, post: 25, short: Object.freeze({ pre: 6, post: 10 }), maxClips: 20, mergeGap: 5, target: 240 }); const fmt = (v) => (Number.isInteger(v) ? v.toLocaleString("en-US") : String(Math.round(v * 10) / 10)); const round2 = (v) => Math.round(v * 100) / 100; /** The last value of a series at or before `t` (a series is [[t, v, …], …] sorted by t). */ export function valueAt(series, t) { let out = null; for (const p of series ?? []) { if (p[0] > t) break; out = p[1]; } return out; } /** * The running stats at second `t`: the tournament the moment is in and the * last value of each of its series (null where none was read yet). The one * reducer the overlay and the clips' subtitles share. */ export function frameTotals(scan, t) { const tours = scan?.derived?.tournaments ?? []; const tour = tours.find((x) => t >= x.from && t < x.to) ?? [...tours].reverse().find((x) => x.from <= t) ?? null; if (!tour) return null; const s = tour.series ?? {}; return { tournament: tour.key, name: tour.name ?? tour.key, stack: valueAt(s.stack, t), stackBB: valueAt(s.stackBB, t), playersLeft: valueAt(s.playersLeft, t), level: valueAt(s.level, t), }; } /** "Level 4 · 60 left · 41,000 chips (137 BB)". */ export function statsLine(totals) { if (!totals) return ""; const parts = []; if (totals.level !== null) parts.push(`Level ${fmt(totals.level)}`); if (totals.playersLeft !== null) parts.push(`${fmt(totals.playersLeft)} left`); if (totals.stack !== null) parts.push(`${fmt(totals.stack)} chips${totals.stackBB !== null ? ` (${fmt(totals.stackBB)} BB)` : ""}`); else if (totals.stackBB !== null) parts.push(`${fmt(totals.stackBB)} BB`); return parts.join(" · "); } /** * The draft: `{ timeline, frameScan, skipped, seconds }`. `scan` is the * parsed sidecar; `cues` the video's cues (sorted), or none. A window is * `[t - pre, t + post]` (`short` for a plain pot or a level-up), widened to * sentence edges where cues exist, clamped to the video; windows within * `mergeGap` of each other are one clip. The events are taken ranked * (`compareRank`) while the merged clips fit the `target` seconds and * `maxClips`, and played in time order. `stats: true` (the cut draws the * stats region, which shows the running numbers) makes each subtitle the * leading event's detail; otherwise it is the stats line. */ export function framesCut(scan, { channel, video, cues = null, at = new Date().toISOString(), stats = false, ...o } = {}) { const opt = { ...FRAMES_CUT_DEFAULTS, ...o, short: { ...FRAMES_CUT_DEFAULTS.short, ...(o.short ?? {}) } }; if (!channel || !video) throw new Error("framesCut needs the channel and the video"); const duration = scan?.source?.durationSeconds ?? scan?.sampling?.to ?? Infinity; const events = (scan?.derived?.events ?? []) .filter((e) => rankOf(e) !== null) // A bust and the "eliminated" banner on the same moment are one event, // and so is one kind seen twice within a minute (the log and the series). .filter((e, _, all) => !(e.kind === "eliminated" && all.some((x) => x.kind === "bust" && Math.abs(x.t - e.t) <= 60))) .filter((e, i, all) => !all.some((x, j) => j < i && x.kind === e.kind && Math.abs(x.t - e.t) <= 60)); const windowOf = (e) => { const short = e.kind === "pot" || e.kind === "level-up"; let start = Math.max(0, e.t - (short ? opt.short.pre : opt.pre)); let end = Math.min(duration, e.t + (short ? opt.short.post : opt.post)); if (cues?.length) { // Only ever WIDER, and only as far as widen's own budgets (8 s before, // 12 s after): a cue past a silence is not this moment's sentence. const w = widen(cues, start, end, { maxLead: 8, maxTail: 12 }); if (w.start <= start && w.start >= start - 8) start = Math.max(0, w.start); if (w.end >= end && w.end <= end + 12) end = Math.min(duration, w.end); } return { start, end, events: [e] }; }; const mergeAll = (windows) => { const merged = []; for (const w of [...windows].sort((a, b) => a.start - b.start)) { const last = merged[merged.length - 1]; if (last && w.start <= last.end + opt.mergeGap) { last.end = Math.max(last.end, w.end); last.events.push(...w.events); } else merged.push({ ...w, events: [...w.events] }); } return merged; }; const lengthOf = (merged) => merged.reduce((n, w) => n + (w.end - w.start), 0); // Ranked first-first, each taken while the merged cut stays within the // budget and the clip count (the first always: a cut of one long clip is // still a cut); one that would pass either is skipped for a smaller one. const chosen = []; for (const e of [...events].sort(compareRank)) { const trial = mergeAll([...chosen, windowOf(e)]); if (chosen.length && (lengthOf(trial) > opt.target || trial.length > opt.maxClips)) continue; chosen.push(windowOf(e)); } const skipped = events.length - chosen.length; const merged = mergeAll(chosen).map((w) => ({ ...w, events: w.events.sort((a, b) => a.t - b.t) })); const timeline = merged.map((w, i) => { const lead = [...w.events].sort(compareRank)[0]; const totals = frameTotals(scan, lead.t); return { type: "clip", id: `f${String(i + 1).padStart(2, "0")}`, channel, video, start: round2(w.start), end: round2(w.end), cite: Math.floor(lead.t), quote: "", onscreen: { title: titleOf(lead), subtitle: stats ? lead.detail : statsLine(totals) || lead.detail }, // Code's draft (release 28's mark, `model: "code"`): no review row, and // an edit makes it the operator's. drafted: { onscreen: { task: "frames", model: "code", at, attempt: 1 } }, frameEvents: w.events.map((e) => ({ t: e.t, kind: e.kind, detail: e.detail, ...(e.confidence ? { confidence: e.confidence } : {}) })), }; }); // The overlay track: the running stats at each clip's start and at every // change inside it, in SOURCE seconds — what a stats region draws. const overlay = timeline.map((c) => { const changes = new Set([c.start]); const tour = frameTotals(scan, c.cite); const t = (scan?.derived?.tournaments ?? []).find((x) => x.key === tour?.tournament); for (const series of Object.values(t?.series ?? {})) for (const p of series) if (p[0] > c.start && p[0] < c.end) changes.add(p[0]); return { clip: c.id, points: [...changes].sort((a, b) => a - b).map((at) => ({ at, ...frameTotals(scan, at) })) }; }); return { timeline, frameScan: { profile: scan?.profile ?? null, file: scan?.profile ? `frame-scan.${scan.profile}.json` : null, channel, video, tournaments: (scan?.derived?.tournaments ?? []).map((t) => ({ key: t.key, name: t.name ?? t.key, from: t.from, to: t.to })), overlay, }, skipped, seconds: round2(lengthOf(merged)), }; }