// The STATS region for the deck (release 31, lane P3): a frame scan's running // numbers -- the stack, the stack in big blinds, players left, the level -- // drawn in a corner of the footage box over each clip that carries them, // step-held between the scan's points. // // render.chrome: "stats": { "position": "top-left", "fields": ["stack", "stackBB", "playersLeft", "level"], "name": true } // timeline entry: "stats": false (this clip without it) | "stats": true (this clip only, no render.chrome.stats) // manifest: "frameScan": { "overlay": [{ "clip": "f01", "points": [{ "at": , "stack", "stackBB", "playersLeft", "level", "name" }] }] } // // The points are frames-cut.mjs's (`umtool new --seed frames`): the stats at // the clip's start and at every change inside it, in SOURCE seconds. The // schedule maps them into the cut's clock (statsSchedule); chrome-stats.mjs // draws them; the build lays the frames over the cut as it lays the stamps'. // // PURE, like factcheck.mjs: validation, the defaults, the schedule. No fs, no // ffmpeg. deck.mjs imports this file -- never the other way round. import { COMPOSITION_PARAMS } from "yt-dlp-transcript-common/lib/report/fxParams.mjs"; import { offsetIn, sourceAtOffset } from "./gaps.mjs"; /** Where the stats sit in the footage box. */ export const STATS_POSITIONS = Object.freeze(["top-left", "top-right", "bottom-left", "bottom-right"]); /** The numbers the region may show, in the order it shows them. */ export const STATS_FIELDS = Object.freeze(["stack", "stackBB", "playersLeft", "level"]); /** Each field's label over its number. */ export const STATS_LABELS = Object.freeze({ stack: "Stack", stackBB: "Big blinds", playersLeft: "Left", level: "Level" }); /** * Every stats setting and its default. `name` puts the tournament's name over * the numbers. The default corner is the one the fact-check stamp (top-left) * and the popup posts (top-right) leave: bottom-left. */ export const STATS_DEFAULTS = Object.freeze({ position: "bottom-left", fields: STATS_FIELDS, name: true }); /** The stats panel's TUNABLE params (release 38; common lib/report/fxParams.mjs): what the video assistant's `fx` op may set. */ export const STATS_PARAMS = COMPOSITION_PARAMS.stats; /** How long the panel takes to come in and go out, in seconds. */ export const STATS_MOTION = Object.freeze({ in: 0.3, out: 0.25 }); const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); /** The stats settings with every default filled in (the defaults when the cut sets none). */ export function resolveStats(render) { const s = render?.chrome?.stats; if (!isObj(s)) return { ...STATS_DEFAULTS, fields: [...STATS_DEFAULTS.fields] }; return { position: s.position ?? STATS_DEFAULTS.position, fields: Array.isArray(s.fields) ? STATS_FIELDS.filter((f) => s.fields.includes(f)) : [...STATS_DEFAULTS.fields], name: typeof s.name === "boolean" ? s.name : STATS_DEFAULTS.name, }; } /** * Every reason `render.chrome.stats` cannot be built, as sentences (empty: it * can). Unknown keys are refused, as the deck's are; validateChrome calls this. * * @returns {string[]} */ export function validateStats(s, where = "render.chrome.stats") { if (s === undefined) return []; if (!isObj(s)) return [`${where} must be an object`]; const errors = []; for (const k of Object.keys(s)) { if (!["position", "fields", "name"].includes(k)) errors.push(`${where}.${k} is not a stats setting`); } if (s.position !== undefined && !STATS_POSITIONS.includes(s.position)) { errors.push(`${where}.position must be ${STATS_POSITIONS.map((p) => `"${p}"`).join(", ")}`); } if (s.fields !== undefined) { if (!Array.isArray(s.fields) || !s.fields.length || !s.fields.every((f) => STATS_FIELDS.includes(f)) || new Set(s.fields).size !== s.fields.length) { errors.push(`${where}.fields must be a list of ${STATS_FIELDS.join(", ")}, each at most once`); } } if (s.name !== undefined && typeof s.name !== "boolean") errors.push(`${where}.name must be true or false`); return errors; } /** Does the cut set the region for every clip (`render.chrome.stats`)? */ export const statsForCut = (render) => isObj(render?.chrome?.stats); /** Does this entry ask for the stats: a clip, `stats: true`, or the cut's setting and not `stats: false`. */ export function statsWanted(entry, render) { if (entry?.type !== "clip") return false; if (entry.stats === true) return true; return statsForCut(render) && entry.stats !== false; } /** The overlay points `frameScan.overlay` holds for a clip id (sorted by `at`), or []. */ export function overlayPointsOf(manifest, id) { const o = (manifest?.frameScan?.overlay ?? []).find((x) => x?.clip === id); return Array.isArray(o?.points) ? [...o.points].filter((p) => Number.isFinite(p?.at)).sort((a, b) => a.at - b.at) : []; } /** * The clips that DECLARE the region: they ask for it (statsWanted) and the * manifest's frame scan has points for them. What the schedule draws and * verify-build holds the frames to. * * @returns {string[]} */ export function statsClips(manifest) { const render = manifest?.render ?? {}; return (manifest?.timeline ?? []).filter((e) => statsWanted(e, render) && overlayPointsOf(manifest, e.id).length).map((e) => e.id); } /** * Every `stats` key in the timeline, checked: a boolean, on a clip; `true` * only where something can draw it (the deck on) and with points to draw * (the manifest's `frameScan.overlay` names the clip). The build refuses with * these before it fetches; the lint says them. * * @returns {string[]} */ export function validateStatsEntries(manifest) { const errors = []; const render = manifest?.render ?? {}; const deck = render.chrome?.engine === "hyperframes" && render.chrome?.layout === "deck"; if (statsForCut(render) && !deck) errors.push("render.chrome.stats is drawn by the deck: render.chrome needs layout \"deck\""); (manifest?.timeline ?? []).forEach((e, i) => { if (e?.stats === undefined) return; const where = `timeline[${i}] (${e.id ?? "?"}).stats`; if (typeof e.stats !== "boolean") { errors.push(`${where} must be true or false`); return; } if (e.type !== "clip") { errors.push(`${where}: only a clip carries the stats`); return; } if (e.stats === true && !deck) errors.push(`${where}: the stats are drawn by the deck, and render.chrome is not the deck`); if (e.stats === true && !overlayPointsOf(manifest, e.id).length) errors.push(`${where}: frameScan.overlay has no points for ${e.id}`); }); return errors; } /** The value of each field at source second `t`: the last point at or before it (the first point's before it). */ function valuesAt(points, t, fields) { let cur = points[0]; for (const p of points) { if (p.at > t) break; cur = p; } const out = {}; for (const f of fields) out[f] = Number.isFinite(cur?.[f]) ? cur[f] : null; return out; } const r3 = (v) => Math.round(v * 1000) / 1000; /** * Where and what the region draws, in the CUT's clock. `segments` are the * schedule's, in order, each `{ id, start, duration, sourceFrom?, points? }` * (`sourceFrom` the source second the segment's picture starts at -- its * play window's `from` -- and `points` its overlay points, only on a clip * that declares the region). * * A clip's panel is up from its start (its incoming dissolve's middle after * the first) to its end (the outgoing dissolve's middle before the last). * Its STATES are the values at the picture's first source second, then at * each point inside the clip -- a source second `t` lands at `start + (t - * sourceFrom)` -- each held until the next. (The build snaps a clip's ends to * a nearby silence, so a state can land up to `render.snapWindow` from its * frame; the scan reads a frame every few seconds.) * * @returns {{ position: string, fields: string[], name: boolean, * clips: Array<{ segment: string, in: number, out: number, name: string|null, * states: Array<{ at: number, values: Record }> }> } | null} */ export function statsSchedule({ segments, D, total, render }) { const cfg = resolveStats(render); const clips = []; segments.forEach((s, i) => { if (!s.points?.length || !Number.isFinite(s.sourceFrom)) return; const next = segments[i + 1]; const lo = s.start + (i > 0 ? D / 2 : 0); const hi = next ? next.start + D / 2 : total; if (!(hi > lo)) return; // A segment with gaps (gaps.mjs, source seconds): its clock counts only what plays. const end = sourceAtOffset(hi - s.start, s.sourceFrom, s.gaps); const first = sourceAtOffset(lo - s.start, s.sourceFrom, s.gaps); const states = [{ at: r3(lo), values: valuesAt(s.points, first, cfg.fields) }]; for (const p of s.points) { if (p.at <= first || p.at >= end) continue; const at = r3(s.start + offsetIn(p.at, s.sourceFrom, s.gaps)); const values = valuesAt(s.points, p.at, cfg.fields); const last = states[states.length - 1]; if (cfg.fields.every((f) => last.values[f] === values[f])) continue; if (at <= last.at) last.values = values; else states.push({ at, values }); } const named = s.points.find((p) => typeof p.name === "string" && p.name)?.name ?? null; clips.push({ segment: s.id, in: r3(lo), out: r3(hi), name: cfg.name ? named : null, states }); }); return clips.length ? { position: cfg.position, fields: cfg.fields, name: cfg.name, clips } : null; }