// A TALL ASK OVER A LOWER CACHED WINDOW (the height tier, lib/clipWindow.ts). // // A request for more than 720 pixels finds, at best, a window lower than it // asked (the plain 720 one, say). That window is never replaced, retired or // re-fetched for its height (operator rulings, 13 Nov): a TALLER cut is made // beside it only from the video's saved copy, and only when that copy is // taller than the window — `min(cap, the saved copy's height)` is the most the // disk can give. Otherwise the lower window is what the ask gets, with the // reason. Nothing fetches a taller window from the network over a lower one: // persisting the video gives it a copy to cut from. // // Cheap first: the pointer's recorded height (`format`, else `probed`) settles // most asks with no probe; the saved container is probed (savedWindowSource) // only when the pointer cannot rule the cut out. // // SERVER-ONLY. import type { Paths } from "./paths"; import { loadSavedVideo, recordSavedVideoProbedHeight } from "./savedVideo-server"; import { savedWindowSource, type SavedWindowSource } from "./savedVideoWindow-server"; import { fittedHeight } from "./evidenceClip-server"; export type TallerFromSaved = | { kind: "cut"; source: Extract; height: number } // The window is already as tall as the saved copy gives inside the cap's // box (an ultrawide source fits a 1080 box at 810): nothing to cut, and not // "lower" in any way a cut could fix. | { kind: "best"; height: number } | { kind: "lower"; why: string }; export const NO_SAVED_COPY_WHY = "no saved copy of the recording to cut a taller one from — the lower window is used as it is; " + "persist the video (persist-videos) to give it one, or refetch it (umtool fetch --refetch) to fetch it taller"; export async function tallerFromSaved(opts: { paths: Pick; slug: string; videoDir: string; from: number; to: number; // The best cached window's height (null: unknown, counted as lowest). hitHeight: number | null; cap: number; // Injectable for the tests. source?: typeof savedWindowSource; }): Promise { const pointer = await loadSavedVideo(opts.videoDir); if (!pointer) return { kind: "lower", why: NO_SAVED_COPY_WHY }; const have = opts.hitHeight ?? 0; const recorded = pointer.format?.height ?? pointer.probed?.height; if (recorded !== undefined && Math.min(opts.cap, recorded) <= have) { return { kind: "lower", why: `the saved copy is ${recorded}p, no taller than the window` }; } const source = await (opts.source ?? savedWindowSource)({ paths: opts.paths, slug: opts.slug, videoDir: opts.videoDir, from: opts.from, to: opts.to, }); if (source.kind === "unreadable") { return { kind: "lower", why: `the saved copy cannot be read now (${source.error})` }; } if (source.kind !== "covers") { return { kind: "lower", why: "the saved copy does not hold this span" }; } const savedHeight = source.probe.height ?? recorded ?? null; if (!source.probe.hasVideo || savedHeight === null) { return { kind: "lower", why: "the saved copy's picture height cannot be read" }; } // A pointer with no height gets the probe's, so the next ask is answered // from the pointer (a failed write costs that ask a probe, nothing else). if (recorded === undefined && source.probe.height) { await recordSavedVideoProbedHeight(opts.videoDir, pointer.file, source.probe.height).catch(() => false); } // WHAT A CUT WOULD BE: the copy's picture fitted inside the cap's 16:9 box // (the cut's own arithmetic) — not min(cap, height), which an ultrawide copy // never reaches, so it would be cut again on every ask. const best = fittedHeight(source.probe.width, source.probe.height, opts.cap) ?? Math.min(opts.cap, savedHeight); if (best <= have) { // "best" only when the BOX is the limit (an ultrawide copy fits the 1080 // box at 810): the window is all the ask could ever get from this copy. // A copy that is itself short of the ask (a 720 copy, a 1080 ask) is // "lower", with why — the same answer the pointer's early exit gives. return opts.hitHeight !== null && best < Math.min(opts.cap, savedHeight) ? { kind: "best", height: best } : { kind: "lower", why: `the saved copy is ${savedHeight}p, no taller than the window` }; } return { kind: "cut", source, height: best }; }