// THE CLIP KIT's rules (lane clip-kit): the keys a clip carries for a per-clip build (a short, a desktop clip, a // batch), checked here once -- the writer (lib/report/manifest.mjs updateClip), `umtool set`, the lint and the build // all call these. This file holds the QR's (slice B); each later slice adds its keys. // // render.qr false | { scale?, quiet?, ecc?, margin?, corner?, links? } the corner code of a cut with no deck // corner "br" (the default: where it always was) | "bl" | "tr" | "tl" // links "cite" (the default: the clip's citeUrl, else its moment on the archive -- as it always was) // | "moment" (the SOURCE at the second the clip starts playing: platformSeek.mjs's rule) // | "channel" (the promo's channel page, render.promo.url) // render.vertical.qr false (the default) | true | { corner?: "tr" | "tl", size?, links? } a code on a 9:16 short // (its links default "moment": the operator asked for "a QR to the channel at the timestamp") // clip.qr false (no code on this clip) | { corner?, links? } over the cut's // // A MOMENT link is the clip's own recording at the second it plays from (the build's snapped start): // - its citeUrl, when that names this recording (it carries the clip's video id), retimed with watchUrlAt; // - else, with no citeUrl, the record's webpage_url at the second; // - else (a citeUrl that names another recording: a mirror the cut cites on purpose) the citeUrl as written. // Kick's time parameter is not confirmed (common/lib/platformSeek.mjs): watchUrlAt writes `?t=` there, and the // lint notes that a Kick (or Twitch) VOD is deleted after its retention, so the code stops opening. // // PURE: no fs, no ffmpeg. import { watchUrlAt } from "yt-dlp-transcript-common/lib/clips/assist.mjs"; import { VOD_RETENTION_DAYS, seekPlatformOfUrl } from "yt-dlp-transcript-common/lib/platformSeek.mjs"; import { DESKTOP_QR_CORNERS, DESKTOP_QR_SIZE, QR_LINKS, VERTICAL_QR_CORNERS, VERTICAL_QR_KEYS, VERTICAL_QR_SIZE, desktopKnobs, verticalQrErrors } from "yt-dlp-transcript-common/lib/report/vertical.mjs"; import { archiveMomentUrl } from "./site-links.mjs"; import { clipPromoErrors } from "./promo-card.mjs"; import { zoomsErrors } from "./clip-zoom.mjs"; import { clipCensorErrors } from "./clip-censor.mjs"; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const q = (v) => JSON.stringify(v); export const QR_CORNERS = Object.freeze(["br", "bl", "tr", "tl"]); /** The keys of `render.qr` (the first four the corner code always read). */ export const QR_KEYS = Object.freeze(["scale", "quiet", "ecc", "margin", "corner", "links"]); export { QR_LINKS, VERTICAL_QR_CORNERS, VERTICAL_QR_KEYS, VERTICAL_QR_SIZE, verticalQrErrors }; /** The keys a clip may carry for the kit (vertical-plan.mjs reads them from the project over a take's). */ export const CLIP_KIT_KEYS = Object.freeze(["qr", "name", "promo", "zooms", "coldOpen", "censor"]); /** A cold open's length (s): refused outside the hard bounds, noted outside the soft ones (what reads best). */ export const COLD_OPEN = Object.freeze({ min: 1, max: 4, softMin: 1.5, softMax: 3 }); /** A clip's pinned file name in a batch (shortsBatch.mjs): at most this many characters. */ export const CLIP_NAME_MAX = 60; /** Each thing wrong with `render.qr`'s new keys ([] when fine). The older keys are as unchecked as they were. */ export function qrSettingsErrors(qr, where = "render.qr") { if (qr === undefined || qr === null || qr === false || qr === true) return []; if (!isObj(qr)) return [`${where} is false or {${QR_KEYS.join(", ")}}`]; const errors = []; if (qr.corner !== undefined && !QR_CORNERS.includes(qr.corner)) errors.push(`${where}.corner is one of ${QR_CORNERS.join(", ")}, not ${q(qr.corner)}`); if (qr.links !== undefined && !QR_LINKS.includes(qr.links)) errors.push(`${where}.links is one of ${QR_LINKS.join(", ")}, not ${q(qr.links)}`); return errors; } /** Each thing wrong with a clip's kit keys ([] when fine), named `.`. */ export function clipKitErrors(entry, where = "clip", { writer = false } = {}) { const errors = []; if (!isObj(entry)) return errors; if (entry.qr !== undefined && entry.qr !== false) { if (!isObj(entry.qr)) errors.push(`${where}.qr is false (no code on this clip) or {corner?, links?}`); else { for (const k of Object.keys(entry.qr)) if (!["corner", "links"].includes(k)) errors.push(`${where}.qr.${k} is not a key (corner, links)`); errors.push(...qrSettingsErrors({ corner: entry.qr.corner, links: entry.qr.links }, `${where}.qr`)); } } if (entry.name !== undefined && !(typeof entry.name === "string" && entry.name.trim() && entry.name.length <= CLIP_NAME_MAX)) { errors.push(`${where}.name is a file name of 1 to ${CLIP_NAME_MAX} characters`); } // Its channel promo (promo-card.mjs): false, or its own timing. errors.push(...clipPromoErrors(entry.promo, `${where}.promo`)); // Its zooms (clip-zoom.mjs): against its cut when written (a zoom the cut does not play is refused), against its // window at a build (a cut tightened since leaves one out, said). errors.push(...zoomsErrors(entry.zooms, `${where}.zooms`, entry, { cut: writer })); // Its cold open: a span of its own cut, played first. errors.push(...coldOpenErrors(entry.coldOpen, `${where}.coldOpen`, entry)); // Its hidden regions (clip-censor.mjs). errors.push(...clipCensorErrors(entry.censor, `${where}.censor`)); if ((entry.qr !== undefined || entry.name !== undefined || entry.promo !== undefined || entry.zooms !== undefined || entry.coldOpen !== undefined || entry.censor !== undefined) && entry.type !== undefined && entry.type !== "clip") { errors.push(`${where}: qr, name, promo, zooms, coldOpen and censor are a clip's`); } return errors; } /** * Each thing wrong with a clip's `coldOpen` ([] when fine, or absent): `{from, to}`, source seconds inside the clip's * cut (its cutStart–cutEnd, else its window), 1–4 s long -- the payoff a short or a desktop clip plays FIRST, then * cuts to the clip's start. */ export function coldOpenErrors(v, where = "clip.coldOpen", entry = null) { if (v === undefined) return []; if (!isObj(v)) return [`${where} is {from, to}: source seconds of the clip's cut, played first`]; const errors = []; for (const k of Object.keys(v)) if (!["from", "to"].includes(k)) errors.push(`${where}.${k} is not a key (from, to)`); const num = (n) => typeof n === "number" && Number.isFinite(n); if (!num(v.from) || !num(v.to) || v.to <= v.from) return [...errors, `${where} is {from, to}, from before to (source seconds)`]; const len = v.to - v.from; if (len < COLD_OPEN.min - 1e-6 || len > COLD_OPEN.max + 1e-6) errors.push(`${where} is ${Math.round(len * 100) / 100} s: a cold open is ${COLD_OPEN.min}–${COLD_OPEN.max} s`); const a = num(entry?.cutStart) ? entry.cutStart : entry?.start; const b = num(entry?.cutEnd) ? entry.cutEnd : entry?.end; if (num(a) && num(b) && (v.from < a - 0.02 || v.to > b + 0.02)) errors.push(`${where} (${v.from}–${v.to}) is outside the clip's cut (${a}–${b})`); else if (num(a) && num(b) && v.from <= a + 0.02 && v.to >= b - 0.02) errors.push(`${where} (${v.from}–${v.to}) is the whole cut: a cold open plays a part of it first`); // A gap (a jump cut taken out of the clip) inside or across it: refused, never a cold open silently shorter. const clip = where.replace(/\.coldOpen$/, "").replace(/^timeline /, ""); for (const g of Array.isArray(entry?.gaps) ? entry.gaps : []) { if (!num(g?.from) || !num(g?.to)) continue; if (g.from < v.to - 0.02 && g.to > v.from + 0.02) { errors.push(`${where} (${v.from}–${v.to}) runs across ${clip}'s gap ${g.from}–${g.to} s (a jump cut taken out): move it off the gap, or take the gap out`); } } return errors; } /** A NOTE for a cold open outside what reads best (1.5–3 s), or null. */ export function coldOpenNote(entry, where) { const v = entry?.coldOpen; if (!isObj(v) || typeof v.from !== "number" || typeof v.to !== "number") return null; const len = v.to - v.from; return len < COLD_OPEN.softMin || len > COLD_OPEN.softMax ? `${where}: its cold open is ${Math.round(len * 100) / 100} s; ${COLD_OPEN.softMin}–${COLD_OPEN.softMax} s reads best` : null; } /** Every clip's kit errors in a manifest, and the render's QR settings' ([] when fine). */ export function validateClipKit(manifest) { // render.vertical.qr is checked with the vertical knobs (common vertical.mjs validateVerticalRender). const errors = [...qrSettingsErrors(manifest?.render?.qr)]; for (const e of manifest?.timeline ?? []) errors.push(...clipKitErrors(e, `timeline ${e?.id ?? "?"}`)); const wantsChannel = [manifest?.render?.qr?.links, isObj(manifest?.render?.vertical?.qr) ? manifest.render.vertical.qr.links : null, ...(manifest?.timeline ?? []).map((e) => (isObj(e?.qr) ? e.qr.links : null))].includes("channel"); if (wantsChannel && !(typeof manifest?.render?.promo?.url === "string" && /^https?:\/\//.test(manifest.render.promo.url))) { errors.push("a QR's links is \"channel\", which is the channel promo's page: set render.promo.url (an https link)"); } return errors; } /** Does `url` name the recording `video` (its id is in it)? */ export const namesRecording = (url, video) => typeof url === "string" && typeof video === "string" && video.length >= 4 && url.includes(video); /** * The source at a second: the clip's citeUrl retimed when it names this recording, else the record's page at it, * else the citeUrl as written (a mirror cited on purpose), else null. * * @param {{ citeUrl?: string, video?: string }} entry * @param {number} at the source second the clip starts playing from * @param {string | null} [webpageUrl] its record's webpage_url */ export function momentLinkOf(entry, at, webpageUrl = null) { const cite = typeof entry?.citeUrl === "string" && entry.citeUrl ? entry.citeUrl : null; if (cite && namesRecording(cite, entry.video)) return watchUrlAt(cite, at) ?? cite; if (!cite && webpageUrl) return watchUrlAt(webpageUrl, at); return cite; } /** * The code's settings a clip draws with: off (null) for `render.qr: false` or `clip.qr: false`, else * `{corner, links}` -- the clip's over the cut's, the defaults (`br`, `cite`) under both. */ export function cornerQrOf(entry, render) { if (render?.qr === false || entry?.qr === false) return null; const cut = isObj(render?.qr) ? render.qr : {}; const own = isObj(entry?.qr) ? entry.qr : {}; return { corner: own.corner ?? cut.corner ?? "br", links: own.links ?? cut.links ?? "cite" }; } /** A 9:16 short's code: null when off (the default), else `{corner, size, links}`. A clip's `qr` speaks over it. */ export function verticalQrOf(entry, render) { const v = render?.vertical?.qr; if (!v || entry?.qr === false) return null; const cut = isObj(v) ? v : {}; const own = isObj(entry?.qr) ? entry.qr : {}; const corner = own.corner && VERTICAL_QR_CORNERS.includes(own.corner) ? own.corner : cut.corner ?? "tr"; return { corner, size: cut.size ?? VERTICAL_QR_SIZE.default, links: own.links ?? cut.links ?? "moment" }; } /** A desktop clip's code (render.desktop.qr): null when off (the default), else `{corner, size, links}`; a clip's `qr` speaks over it. */ export function desktopQrOf(entry, render) { const v = desktopKnobs(render).qr; if (!v || entry?.qr === false) return null; const cut = isObj(v) ? v : {}; const own = isObj(entry?.qr) ? entry.qr : {}; const corner = DESKTOP_QR_CORNERS.includes(own.corner) ? own.corner : cut.corner ?? "br"; return { corner, size: cut.size ?? DESKTOP_QR_SIZE.default, links: own.links ?? cut.links ?? "moment" }; } /** Where a desktop clip's code stands, px: in its corner of the safe area (`safe` vertical.mjs desktopSafeArea's). */ export function desktopQrBox(qr, { W, safe }) { const size = Math.max(2, 2 * Math.round((W * qr.size) / 2)); const x = qr.corner === "bl" || qr.corner === "tl" ? safe.x0 : safe.x1 - size; const y = qr.corner === "tl" || qr.corner === "tr" ? safe.y0 : safe.y1 - size; return { x, y, size }; } /** * The link a clip's code carries. `links` "cite" is the corner code's link as it always was (the citeUrl, else the * clip's moment on the archive); "moment" the source at `at` (momentLinkOf), falling back to that; "channel" the * promo's page. Null: no link to draw (a `src` clip with no citeUrl). * * @param {Record} entry * @param {Record} provenance * @param {{ links?: string, at?: number | null, webpageUrl?: string | null, channelUrl?: string | null, local?: boolean }} [o] * `local`: the clip plays a file of its own (`src`). */ export function clipQrUrl(entry, provenance, { links = "cite", at = null, webpageUrl = null, channelUrl = null, local = false } = {}) { if (local && !entry.citeUrl) return null; const cite = () => entry.citeUrl ?? archiveMomentUrl(entry, provenance, entry.start); if (links === "channel" && channelUrl) return channelUrl; if (links === "moment") return momentLinkOf(entry, Number.isFinite(at) ? at : entry.cutStart ?? entry.start, webpageUrl) ?? cite(); return cite(); } /** * The overlay that places the corner code (ffmpeg `overlay` x/y): `br` is the one the build always drew, * byte for byte. `VW` the picture's width, `FH` the footer's height, `HH` the header's, `m` the margin. */ export function cornerOverlayXY(corner, { VW, FH = 0, HH = 0, m = 28 }) { const x = corner === "bl" || corner === "tl" ? `${m}` : `${VW}-w-${m}`; const y = corner === "tr" || corner === "tl" ? `${HH + m}` : `H-h-${FH + m}`; return `x=${x}:y=${y}`; } /** * Where a 9:16 short's code stands, px: under the top band, at the safe area's right (`tr`) or left (`tl`) edge; a * code at the left stands under the source tag. `safe` is vertical.mjs safeArea's; `tag` its geometry's tag. */ export function verticalQrBox(qr, { W, safe, tag = null }) { const size = Math.max(2, 2 * Math.round((W * qr.size) / 2)); const top = safe.y0 + Math.round(W * 0.02); const y = qr.corner === "tl" && tag ? Math.max(top, tag.y + Math.round(tag.size * 1.9)) : top; const x = qr.corner === "tl" ? safe.x0 : safe.x1 - size; return { x, y, size }; } /** * A NOTE (never a refusal) for a code that links a stream's VOD on a platform that deletes them: the code stops * opening after the retention (platformSeek.mjs VOD_RETENTION_DAYS). Null for any other link. */ export function expiringLinkNote(url, where) { const p = seekPlatformOfUrl(url); const days = p ? VOD_RETENTION_DAYS[p] : undefined; if (!days || !/\/videos?\//.test(String(url))) return null; const name = p === "kick" ? "Kick" : "Twitch"; return `${where}: its QR links a ${name} VOD (${url}); ${name} deletes VODs about ${days} days after the stream, so the code will stop opening then — link the channel instead (qr links "channel") if it must last`; }