// ZOOM AT POINTS in a per-clip build (lane clip-kit, slice F): a clip of a short or a desktop clip pushes in on a // part of its picture for a moment, eased in and out -- the operator: "we may want to zoom in at points". // // clip.zooms [{ at, hold?, scale?, focus?: [x, y], box?: [x, y, w, h], ramp? }] 1 to 8, none overlapping // at the source second the push starts (inside the clip's window; gap-aware) // hold seconds held at full zoom (0.3–10; 1.5) // scale how far in (1.05–3; 1.3) -- or `box`, the part of the SOURCE picture to fill the frame with // focus the point that stays where it is on screen, fractions of the source picture ([0.5, 0.5]; a box's // centre) // ramp the ease in, and out again (0.2–1 s; 0.4; smoothstep) // render.zooms { soften?: 1–1.3 } how much softer than the layout's resting picture a zoom may play (absent: 1) // // IN SOURCE SPACE, BEFORE THE LAYOUT. The picture is pushed in about `focus` and the layout then cuts it as it always // does: a short's 9:16 crop (`vertical.crop`) is the same box, the focus keeps its place on screen and the picture // grows around it; a desktop clip's full frame likewise. Captions, the source line, the code and the promo card are // drawn after it and never zoom. // // SHARP. A layout already shows its source at some enlargement, `rest` (a 1080 window fitted into the 1080x608 box // of a framed short: 0.56; cropped 9:16 to fill 1080x1920: 1.78; a desktop clip: 1.0; from a 720 window 1.5× that). // A zoom multiplies it: the picture plays at rest × scale its source's pixels. THE REFERENCE is the coarser of the // source's own pixels and the layout's resting picture, max(1, rest): a zoom may play its source at no more than // that -- the sharp limit is the scale that reaches it, max(1, 1 / rest), so where the layout already enlarges (a // full 9:16 crop, a desktop clip from a 720 window) a strict cut cannot zoom. Past it is refused, by name -- unless // the project allows a SOFT zoom, `render.zooms.soften` (1–1.3): the picture may then play up to `soften` times the // reference (rest × scale ≤ soften × max(1, rest)), and the build says how much softer than its source it plays. // NOT a cap on the total enlargement past the source's pixels: a full 9:16 crop already plays its 1080 source at // 1.78×, and capping that total at 1.3 would leave a full short (and a desktop clip from 720 windows) no zoom at all. // // PURE: no fs, no ffmpeg run. The build (build-video.mjs) draws it with ffmpeg's perspective (zoomFilters). import { offsetIn } from "./gaps.mjs"; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const num = (v) => typeof v === "number" && Number.isFinite(v); export const ZOOM_KEYS = Object.freeze(["at", "hold", "scale", "focus", "box", "ramp"]); export const ZOOM_LIMITS = Object.freeze({ hold: Object.freeze({ min: 0.3, max: 10, default: 1.5 }), scale: Object.freeze({ min: 1.05, max: 3, default: 1.3 }), ramp: Object.freeze({ min: 0.2, max: 1, default: 0.4 }), }); export const ZOOMS_MAX = 8; /** `render.zooms.soften`: how far past the reference, max(1, rest), a zoom may play its source (1: strict). */ export const ZOOM_SOFT = Object.freeze({ min: 1, max: 1.3, default: 1 }); /** A zoom's span on a clock: from `at`, eased in over `ramp`, held, eased out over `ramp`. */ export const zoomEnd = (z) => z.at + 2 * (z.ramp ?? ZOOM_LIMITS.ramp.default) + (z.hold ?? ZOOM_LIMITS.hold.default); /** * Each thing wrong with a clip's `zooms` ([] when fine, or absent). `entry` its window (start, end), when known; * `cut` (the writer): against its cut (cutStart–cutEnd, else its window), a zoom it does not play refused. */ export function zoomsErrors(zooms, where = "clip.zooms", entry = null, { cut = false } = {}) { if (zooms === undefined) return []; if (!Array.isArray(zooms) || !zooms.length || zooms.length > ZOOMS_MAX) return [`${where} is a list of 1 to ${ZOOMS_MAX} zooms [{at, hold?, scale?, focus?, box?, ramp?}]`]; const errors = []; zooms.forEach((z, i) => { const w = `${where}[${i}]`; if (!isObj(z)) return errors.push(`${w} is {at, hold?, scale?, focus?, box?, ramp?}`); for (const k of Object.keys(z)) if (!ZOOM_KEYS.includes(k)) errors.push(`${w}.${k} is not a key (${ZOOM_KEYS.join(", ")})`); if (!num(z.at) || z.at < 0) errors.push(`${w}.at is a source second`); else if (entry && num(entry.start) && num(entry.end)) { const a = cut && num(entry.cutStart) ? entry.cutStart : entry.start; const b = cut && num(entry.cutEnd) ? entry.cutEnd : entry.end; const span = cut && (num(entry.cutStart) || num(entry.cutEnd)) ? "cut" : "window"; if (z.at < a - 0.02 || zoomEnd(z) > b + 0.02) errors.push(`${w} (${z.at}–${Math.round(zoomEnd(z) * 100) / 100} s) runs outside the clip's ${span} (${a}–${b})`); } for (const [k, b] of Object.entries(ZOOM_LIMITS)) if (z[k] !== undefined && !(num(z[k]) && z[k] >= b.min && z[k] <= b.max)) errors.push(`${w}.${k} is ${b.min}–${b.max}`); if (z.focus !== undefined && !(Array.isArray(z.focus) && z.focus.length === 2 && z.focus.every((n) => num(n) && n >= 0 && n <= 1))) errors.push(`${w}.focus is [x, y], fractions of the picture`); if (z.box !== undefined) { const b = z.box; if (!(Array.isArray(b) && b.length === 4 && b.every(num) && b[2] > 0.02 && b[3] > 0.02 && b[0] >= 0 && b[1] >= 0 && b[0] + b[2] <= 1.001 && b[1] + b[3] <= 1.001)) errors.push(`${w}.box is [x, y, w, h], fractions of the picture inside it`); if (z.scale !== undefined || z.focus !== undefined) errors.push(`${w}: a box says its own scale and focus -- give box, or scale and focus`); } }); const ok = zooms.filter((z) => isObj(z) && num(z.at)).sort((a, b) => a.at - b.at); for (let i = 1; i < ok.length; i += 1) { if (ok[i].at < zoomEnd(ok[i - 1]) - 1e-6) errors.push(`${where}: the zoom at ${ok[i].at} s starts before the one at ${ok[i - 1].at} s is out again (${Math.round(zoomEnd(ok[i - 1]) * 100) / 100} s)`); } return errors; } /** Each thing wrong with `render.zooms` ([] when fine, or absent). */ export function renderZoomsErrors(v, where = "render.zooms") { if (v === undefined || v === null) return []; if (!isObj(v)) return [`${where} is {soften}`]; const errors = []; for (const k of Object.keys(v)) if (k !== "soften") errors.push(`${where}.${k} is not a key (soften)`); if (v.soften !== undefined && !(num(v.soften) && v.soften >= ZOOM_SOFT.min && v.soften <= ZOOM_SOFT.max)) { errors.push(`${where}.soften is ${ZOOM_SOFT.min}–${ZOOM_SOFT.max}: how much softer than the layout's resting picture (or the source's own pixels, if sharper) a zoom may play (1: never)`); } return errors; } /** Every reason a manifest's zooms cannot be drawn. */ export function validateZooms(manifest) { const errors = renderZoomsErrors(manifest?.render?.zooms); for (const e of manifest?.timeline ?? []) { if (e?.zooms === undefined) continue; if (e.type !== "clip") errors.push(`timeline ${e.id ?? "?"}: zooms are a clip's`); else errors.push(...zoomsErrors(e.zooms, `timeline ${e.id}.zooms`, e)); } return errors; } /** * The enlargement a layout shows its source at, before any zoom: the frame's pixels over the source's for the part * of the source it shows. `region` that part ({w, h}, px of the source); `box` the frame's box it fills ({w, h}). * A fitted picture is the smaller ratio. */ export const restEnlargement = (region, box) => Math.min(box.w / region.w, box.h / region.h); /** The sharp limit: the zoom at which the picture reaches its source's own pixels (never under 1). */ export const sharpLimit = (rest) => Math.max(1, 1 / rest); /** * Each zoom resolved for a segment (PURE): its clock in the segment's seconds (`from` the source second it starts at, * `gaps` the cut's), its scale (a box's derived from the region the layout shows) and focus, and how sharp it plays. * `region` px of the source the layout shows ({x, y, w, h}); `rest` its enlargement; `soften` the soft limit. * Returns `{zooms, errors, notes}`: a zoom past its limit is an error naming it, one past the sharp limit (soft, * allowed) a note. */ export function resolveZooms(entry, { from, gaps = [], srcW, srcH, region, rest, soften = 1, seconds = Infinity, id = entry?.id ?? "clip" }) { const zooms = []; const errors = []; const notes = []; const sharp = sharpLimit(rest); const ceiling = sharp * Math.max(1, Math.min(ZOOM_SOFT.max, soften)); const round = (n) => Math.round(n * 100) / 100; for (const z of Array.isArray(entry?.zooms) ? entry.zooms : []) { const ramp = z.ramp ?? ZOOM_LIMITS.ramp.default; const hold = z.hold ?? ZOOM_LIMITS.hold.default; let scale = z.scale ?? ZOOM_LIMITS.scale.default; let focus = Array.isArray(z.focus) ? z.focus : null; if (Array.isArray(z.box)) { const [bx, by, bw, bh] = z.box; scale = Math.min(region.w / (bw * srcW), region.h / (bh * srcH)); focus = [bx + bw / 2, by + bh / 2]; } focus ??= [(region.x + region.w / 2) / srcW, (region.y + region.h / 2) / srcH]; const at = offsetIn(z.at, from, gaps); const what = `${id}: the zoom at ${z.at} s (×${round(scale)})`; // A push outside what this segment plays (a cut tighter than its window, a reel's part): not drawn, said. if (at < -0.05 || at + 2 * ramp + hold > seconds + 0.05) { notes.push(`${what} is outside what plays here (${round(from)} s on, ${round(seconds)} s) -- left out`); continue; } if (scale <= 1.001) { notes.push(`${what} shows no more than the layout already does -- left out`); continue; } if (scale > ceiling + 1e-6) { const ways = ["a smaller zoom", "a sharper source (render.tallWindows)", ...(soften < ZOOM_SOFT.max ? [`a soft limit (umtool look

zooms='{"soften":${ZOOM_SOFT.max}}')`] : [])]; errors.push(`${what} plays its source at ${round(rest * scale)}× its pixels; the limit here is ×${round(ceiling)} (${soften > 1 ? `render.zooms.soften ${soften} times ` : ""}the sharp ×${round(sharp)}) -- ${ways.join(", ")}`); continue; } if (rest * scale > 1.001) notes.push(`${what} plays softer than its source: ${round(rest * scale)}× its pixels`); zooms.push({ at: Math.max(0, at), ramp, hold, scale, focus }); } return { zooms, errors, notes }; } const f = (n) => Number(n).toFixed(4); /** The eased zoom of one push at `t` (an ffmpeg expression): 0 outside it, smoothstep up, held, smoothstep down. */ function pushExpr(z, t) { const a = z.at; const b = a + z.ramp; const c = b + z.hold; const d = c + z.ramp; const up = `((${t}-${f(a)})/${f(z.ramp)})`; const down = `((${f(d)}-${t})/${f(z.ramp)})`; const ease = (u) => `(${u}*${u}*(3-2*${u}))`; return `if(between(${t},${f(a)},${f(d)}),${f(z.scale - 1)}*if(lt(${t},${f(b)}),${ease(up)},if(lt(${t},${f(c)}),1,${ease(down)})),0)`; } /** * The zoom as ONE ffmpeg filter chain on the source picture (PURE): its frames at `fps`, then the part of the picture * the zoom shows mapped onto the whole frame by `perspective` -- its corners in SUB-PIXEL source coordinates, * interpolated (cubic), re-evaluated each frame on the frame count -- and only over the pushes (`enable`): between * them the frame passes untouched. Not zoompan, which rounds its crop's x, y and size to whole pixels each frame, so * a slow ease steps back and forth (a wobble). The focus stays where it is on screen: the shown part starts at * focus × (1 − 1/zoom) of the picture. Empty when there are no zooms. */ export function zoomFilters(zooms, { srcW, srcH, fps }) { if (!zooms?.length) return []; // perspective's `in` counts frames from 1 (enable's `n` from 0): frame n is at (in − 1) / fps. const t = `((in-1)/${fps})`; const Z = `(1+${zooms.map((z) => pushExpr(z, t)).join("+")})`; // The focus of the push under way (a nested choice by span); outside every push the filter is off. const pick = (k) => zooms.reduceRight((acc, z) => `if(between(${t},${f(z.at)},${f(zoomEnd(z))}),${f(z.focus[k])},${acc})`, "0.5"); const x = `${pick(0)}*W*(1-1/${Z})`; const y = `${pick(1)}*H*(1-1/${Z})`; const r = `${x}+W/${Z}`; const b = `${y}+H/${Z}`; const on = zooms.map((z) => `between(n/${fps},${f(z.at)},${f(zoomEnd(z))})`).join("+"); void srcW; void srcH; return [`fps=${fps}`, `perspective=x0='${x}':y0='${y}':x1='${r}':y1='${y}':x2='${x}':y2='${b}':x3='${r}':y3='${b}':interpolation=cubic:sense=source:eval=frame:enable='${on}'`]; }