// A CLIP'S SIDE POST, drawn (lane vertical): the post a clip carries beside it // (`clip.side`, common lib/report/vertical.mjs) as one PNG the clip's segment // lays over its picture while the side is up. Drawn by the same card page as // a post item (post-item.mjs postCardHtml), redaction and all -- the bars and // the pills of the cut's redaction are on it, its letters never in the page -- // with its attachments left off: it is read beside a clip, not played. // // Cached by a key of what it draws, beside the segment // (segments/.side.png + .side.json). import { createHash } from "node:crypto"; import { readFile, rename, writeFile, mkdir } from "node:fs/promises"; import path from "node:path"; import { pathToFileURL } from "node:url"; import { postCardHtml } from "./post-item.mjs"; import { loadChromium } from "./post-render.mjs"; // The post as a side card shows it (vertical.mjs, pure: Play's route reads it too). import { sideCardEntry } from "./vertical.mjs"; import { tmpPathFor } from "yt-dlp-transcript-common/lib/atomicFile.mjs"; export { sideCardEntry }; /** The key of a side card: what it draws, at what size, under which redaction. */ export function sideCardKey(post, { width, height, palette = null, redaction = null, avatars = {} } = {}) { return createHash("sha256").update(JSON.stringify({ v: 2, post: sideCardEntry(post), width, height, palette, redaction, avatars })).digest("hex"); } /** * The side card's PNG at `width` × `height` (its page's ground transparent * around the card), at `file`; reused when its record's key matches. */ export async function renderSideCard(post, { width, height, render = {}, avatars = {}, redaction = null, file }) { const key = sideCardKey(post, { width, height, palette: render.palette ?? null, redaction, avatars }); const rec = `${file.replace(/\.png$/, "")}.json`; const have = await readFile(rec, "utf8").then((t) => JSON.parse(t), () => null); if (have?.key === key && (await readFile(file).then(() => true, () => false))) return { file, key, cached: true }; await mkdir(path.dirname(file), { recursive: true }); const fonts = { regular: render.fontRegular ? pathToFileURL(render.fontRegular).href : "", bold: render.fontBold ? pathToFileURL(render.fontBold).href : "", }; const av = Object.fromEntries(Object.entries(avatars).map(([h, f]) => [h, pathToFileURL(f).href])); // The page is laid at least as tall as it is wide, so the card takes the whole width and its words their full // size (post-item.mjs sizes them by the page's height); the CARD alone is shot, and the clip's segment fits it // into the side's box (sideFitFilter). const vw = width; const vh = Math.max(height, Math.round(width * 0.95)); const html = postCardHtml(sideCardEntry(post), { width: vw, height: vh, palette: render.palette, fonts, phase: "read", avatars: av, redaction, transparent: true }); const chromium = await loadChromium(); const browser = await chromium.launch({ headless: true }); try { const ctx = await browser.newContext({ viewport: { width: vw, height: vh }, deviceScaleFactor: 1, offline: true, serviceWorkers: "block" }); const page = await ctx.newPage(); const htmlFile = `${file.replace(/\.png$/, "")}.html`; await writeFile(htmlFile, html, "utf8"); await page.goto(pathToFileURL(htmlFile).href, { waitUntil: "load" }); await page.waitForSelector("body[data-post-ready='1']", { timeout: 15000 }); const tmp = tmpPathFor(file, { ext: ".png" }); await page.locator("[data-post-card]").first().screenshot({ path: tmp, omitBackground: true }); await rename(tmp, file); } finally { await browser.close(); } await writeFile(rec, JSON.stringify({ version: 1, key, width, height, post: post?.id ?? null }, null, 2) + "\n"); return { file, key, cached: false }; } /** The filter that fits a side card's PNG (any size) into its box `w` × `h`, centred, its ground transparent. */ export const sideFitFilter = (w, h) => `format=rgba,scale=w=${w}:h=${h}:force_original_aspect_ratio=decrease:flags=lanczos,pad=${w}:${h}:(ow-iw)/2:(oh-ih)/2:color=0x00000000,setsar=1`;