// `umtool promo` (lane clip-kit, slice D): the channel promo's two jobs that are not a key to type -- its portrait, // grabbed from a clip at a second (lib/report/stillGrab.mjs, into shots/), and the card drawn in both sizes to look // at before a render (report-to-video promo-card.mjs). The promo itself is look data: `umtool look

promo='{…}'`. // // umtool promo [--portrait-from @] [--box x,y,w,h] [--draw] [--take ] [--json] // // Plain ESM: the CLI runs it under bare node; the app's route (/api/report/promo-card) calls the same functions. import path from "node:path"; import { REPORTS_ROOT, resolveProject } from "../projects/core.mjs"; import { kindMakesTakes } from "../projects/kinds.mjs"; import { readManifest } from "../projects/report.mjs"; import { manifestToken, updateRender } from "./manifest.mjs"; import { StillRefused, grabStill } from "./stillGrab.mjs"; import { ensureWriteDir } from "./storage.mjs"; export const PROMO_USAGE = [ "usage: umtool promo [--portrait-from @] [--box x,y,w,h] [--draw] [--take ] [--json]", "", "The channel promo (render.promo: the streamer's name, page, code and portrait; set with `umtool look

", "promo='{\"name\":[\"…\",\"…\"],\"url\":\"https://…\"}'`). --portrait-from grabs the frame of a clip at a source second", "into shots/ and makes it the card's portrait (--box: the part the circle is cut from, fractions of the frame).", "--draw draws the card 9:16 and 16:9 into out/promo/ (the take's look with --take) and prints where.", ].join("\n"); class CliError extends Error {} const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); /** `@`, read. */ export function parsePortraitFrom(text) { const m = /^([A-Za-z0-9][A-Za-z0-9._~-]*)@(\d+(?:\.\d+)?)$/.exec(String(text ?? "").trim()); if (!m) throw new CliError(`--portrait-from ${text}: @ (e.g. w01@10650.2)`); return { clip: m[1], at: Number(m[2]) }; } /** `x,y,w,h` (fractions), read. */ export function parseBox(text) { const b = String(text ?? "").split(",").map((s) => Number(s.trim())); if (b.length !== 4 || !b.every((n) => Number.isFinite(n) && n >= 0 && n <= 1) || b[2] <= 0 || b[3] <= 0 || b[0] + b[2] > 1.001 || b[1] + b[3] > 1.001) { throw new CliError(`--box ${text}: x,y,w,h as fractions of the frame, inside it`); } return b; } /** * The portrait grabbed and written into render.promo (the promo must be set: it is the streamer's, not a guess). * Returns the promo as written. * * @param {{ dir: string, id: string }} project * @param {{ clip: string, at: number, box?: number[] | null }} o */ export async function setPromoPortrait(project, { clip, at, box = null }) { const manifest = await readManifest(project.dir); if (!manifest) throw new CliError(`${project.id} has no manifest`); const promo = manifest.render?.promo; if (!isObj(promo)) throw new CliError(`${project.id} sets no channel promo yet: umtool look ${project.id} promo='{"name":"…","url":"https://…"}' first`); let still; try { // The tallest window that holds the second, when the project fetches taller ones (render.tallWindows). still = await grabStill(project, manifest, clip, at, { preferTall: manifest.render?.tallWindows === true }); } catch (e) { if (e instanceof StillRefused) throw new CliError(e.message); throw e; } const next = { ...promo, portrait: { file: still.rel, ...(box ? { box } : {}), from: { clip, at: still.at } } }; const r = await updateRender(project.dir, { promo: next }, { token: await manifestToken(project.dir) }); return { promo: r.look.promo, still }; } /** * Both cards drawn (9:16 and 16:9) from the project's look, or a take's: `[{width, height, file, key, cached}]`. * * @param {{ dir: string, id: string }} project * @param {{ take?: string | null }} [o] */ export async function drawPromoCards(project, { take = null } = {}) { let manifest = await readManifest(project.dir); if (take) manifest = (await (await import("./takeManifest.mjs")).resolveTakeManifest(project.dir, take)).manifest; const promo = manifest?.render?.promo; if (!isObj(promo)) throw new CliError(`${project.id}${take ? ` (take ${take})` : ""} sets no channel promo (render.promo)`); const { promoErrors, renderPromoCard } = await import("umtool-report-to-video/promo-card"); const errors = promoErrors(promo); if (errors.length) throw new CliError(errors.join("; ")); // Through ensureWriteDir: a tiered project's out/ stays the link to the media root. const dir = await ensureWriteDir(path.join(/* turbopackIgnore: true */ project.dir, "out", "promo")); const out = []; for (const [width, height] of [[1080, 1920], [1920, 1080]]) { const r = await renderPromoCard({ promo: { ...promo, captions: promo.captions !== false }, render: manifest.render, width, height, dir, manifestDir: project.dir }); out.push({ width, height, file: r.file, key: r.key, cached: r.cached }); } return out; } /** * `umtool promo …` * @param {string[]} args * @param {{ reportsRoot?: string, log?: (s: string) => void, err?: (s: string) => void }} [o] */ export async function promoCommand(args, { reportsRoot = REPORTS_ROOT, log = console.log, err = console.error } = {}) { try { const flags = {}; const pos = []; for (let i = 0; i < args.length; i += 1) { const a = args[i]; if (["--portrait-from", "--box", "--take"].includes(a)) { if (i + 1 >= args.length) throw new CliError(`${a} needs a value`); flags[a] = args[++i]; } else if (["--draw", "--json", "--help"].includes(a)) flags[a] = true; else if (a.startsWith("--")) throw new CliError(`unknown flag ${a}`); else pos.push(a); } if (flags["--help"] || !pos.length) { log(PROMO_USAGE); return flags["--help"] ? 0 : 2; } const r = await resolveProject(pos[0], reportsRoot); if (r.ambiguous) throw new CliError(`"${pos[0]}" is the name of ${r.ambiguous.length} projects — pass its id`); if (!r.project) throw new CliError(`no project matches "${pos[0]}"`); if (!kindMakesTakes(r.project.kind)) throw new CliError(`${r.project.id} is a ${r.project.kind}, not a report video`); if (!flags["--portrait-from"] && !flags["--draw"]) throw new CliError("--portrait-from @ and/or --draw: what to do"); if (flags["--box"] && !flags["--portrait-from"]) throw new CliError("--box goes with --portrait-from"); const result = {}; if (flags["--portrait-from"]) { const { clip, at } = parsePortraitFrom(flags["--portrait-from"]); const box = flags["--box"] ? parseBox(flags["--box"]) : null; const set = await setPromoPortrait(r.project, { clip, at, box }); result.portrait = set.promo.portrait; if (!flags["--json"]) log(`portrait: ${set.still.rel} (${clip} at ${set.still.at} s)${box ? ` cut from [${box.join(", ")}]` : ""}`); } if (flags["--draw"]) { const take = typeof flags["--take"] === "string" ? flags["--take"] : null; result.cards = await drawPromoCards(r.project, { take }); if (!flags["--json"]) for (const c of result.cards) log(`${c.width}x${c.height}: ${c.file}${c.cached ? " (drawn before)" : ""}`); } if (flags["--json"]) log(JSON.stringify(result, null, 2)); return 0; } catch (e) { if (e instanceof CliError || e?.name === "RenderRefused") { err(`umtool promo: ${e instanceof Error ? e.message : e}`); return 2; } throw e; } }