// HIDDEN REGIONS of a clip's picture (lane clip-kit, slice E): a stream layout's chat overlay, a browser window showing // other people's accounts -- the same boxes on every clip cut from ONE recording, or on one clip, each for the whole // clip or a span of it. Privacy, not TOS: they stay on in the uncensored variant (`render.blur: "off"`), as a post // attachment's censor does (post-censor.mjs, whose styles, strengths and box rules these are). // // render.censor [{ video, channel?, boxes, style?, strength?, from?, to? }] per recording (≤ 16) // video the recording's id; `channel` its channel (default: any channel's recording of that id) // clip.censor false (none, not even the recording's) | { boxes, style?, strength?, from?, to? } added to them // boxes [[x, y, w, h] | { box: [x, y, w, h], from?, to? }] 1 to 8, fractions of the SOURCE picture; // a box's own from/to (source seconds) limit it to that span // style "blur" (default) | "black" | "pixelate"; strength 1–10 (7) // from, to source seconds: every box of the entry only over that span // // WHERE: on the source picture, FIRST -- before a clip's blur, its zooms and any layout -- in every path a clip is cut // in (a cut with or without the deck, a short, a desktop clip, Make a short), so a box is the same part of the // recording whatever frames it. Nothing else is touched: the sound, the captions, the header, the deck. // // PURE: no fs, no ffmpeg run. import { blurSigma, boxOk, pixelGrid } from "./post-censor.mjs"; 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 CENSOR_STYLES = Object.freeze(["blur", "black", "pixelate"]); export const CENSOR_DEFAULTS = Object.freeze({ style: "blur", strength: 7 }); export const CENSOR_BOXES_MAX = 8; export const CENSOR_RECORDINGS_MAX = 16; const ENTRY_KEYS = ["boxes", "style", "strength", "from", "to"]; function spanErrors(o, where) { const errors = []; for (const k of ["from", "to"]) if (o[k] !== undefined && !(num(o[k]) && o[k] >= 0)) errors.push(`${where}.${k} is a source second`); if (num(o.from) && num(o.to) && o.to - o.from < 0.2) errors.push(`${where}: its span ${o.from}–${o.to} is under 0.2 s`); return errors; } /** Each thing wrong with one censor entry's boxes, style, strength and span. */ function entryErrors(v, where) { const errors = []; if (!Array.isArray(v.boxes) || !v.boxes.length || v.boxes.length > CENSOR_BOXES_MAX) errors.push(`${where}.boxes is a list of 1 to ${CENSOR_BOXES_MAX} boxes: [x, y, w, h] or {box, from?, to?}`); else v.boxes.forEach((b, j) => { const w = `${where}.boxes[${j}]`; if (Array.isArray(b)) { if (!boxOk(b)) errors.push(`${w} is [x, y, w, h], fractions of the picture inside it`); } else if (isObj(b)) { for (const k of Object.keys(b)) if (!["box", "from", "to"].includes(k)) errors.push(`${w}.${k} is not a key (box, from, to)`); if (!boxOk(b.box)) errors.push(`${w}.box is [x, y, w, h], fractions of the picture inside it`); errors.push(...spanErrors(b, w)); } else errors.push(`${w} is [x, y, w, h] or {box, from?, to?}`); }); if (v.style !== undefined && !CENSOR_STYLES.includes(v.style)) errors.push(`${where}.style is one of ${CENSOR_STYLES.join(", ")}`); if (v.strength !== undefined && !(num(v.strength) && v.strength >= 1 && v.strength <= 10)) errors.push(`${where}.strength is 1–10`); errors.push(...spanErrors(v, where)); return errors; } /** Each thing wrong with a clip's `censor` ([] when fine, or absent). */ export function clipCensorErrors(v, where = "clip.censor") { if (v === undefined || v === false) return []; if (!isObj(v)) return [`${where} is false (none, not even the recording's) or {boxes, style?, strength?, from?, to?}`]; const errors = []; for (const k of Object.keys(v)) if (!ENTRY_KEYS.includes(k)) errors.push(`${where}.${k} is not a key (${ENTRY_KEYS.join(", ")})`); return [...errors, ...entryErrors(v, where)]; } /** Each thing wrong with `render.censor` ([] when fine, or absent). */ export function renderCensorErrors(v, where = "render.censor") { if (v === undefined || v === null) return []; if (!Array.isArray(v) || v.length > CENSOR_RECORDINGS_MAX) return [`${where} is a list of at most ${CENSOR_RECORDINGS_MAX} recordings' boxes [{video, channel?, boxes, …}]`]; const errors = []; v.forEach((e, i) => { const w = `${where}[${i}]`; if (!isObj(e)) return errors.push(`${w} is {video, channel?, boxes, style?, strength?, from?, to?}`); for (const k of Object.keys(e)) if (![...ENTRY_KEYS, "video", "channel"].includes(k)) errors.push(`${w}.${k} is not a key (video, channel, ${ENTRY_KEYS.join(", ")})`); if (typeof e.video !== "string" || !e.video.trim()) errors.push(`${w}.video is the recording's id`); if (e.channel !== undefined && (typeof e.channel !== "string" || !e.channel.trim())) errors.push(`${w}.channel is a channel's slug`); errors.push(...entryErrors(e, w)); }); return errors; } /** Every reason a manifest's hidden regions cannot be drawn. */ export function validateCensor(manifest) { const errors = renderCensorErrors(manifest?.render?.censor); // A montage re-cuts its pictures in its own graph, which draws no boxes: refused by name, never cut showing them. if (isObj(manifest?.render?.montage)) { for (const e of manifest?.timeline ?? []) { const n = censorRegionsOf(e, manifest.render, manifest.provenance ?? {}).length; if (n) errors.push(`timeline ${e.id}: a montage cannot hide regions yet (${n} box(es)): take render.montage off, or the boxes only if the regions may show`); } } for (const e of manifest?.timeline ?? []) { if (e?.censor === undefined) continue; if (e.type !== "clip") errors.push(`timeline ${e.id ?? "?"}: censor is a clip's`); else errors.push(...clipCensorErrors(e.censor, `timeline ${e.id}.censor`)); } return errors; } /** Does a recording entry of `render.censor` name this clip's recording? */ export function namesClip(rec, entry, provenance = {}) { if (rec?.video !== entry?.video) return false; if (rec.channel === undefined) return true; return rec.channel === (entry.channel ?? provenance?.channelSlug ?? null); } /** * The regions a clip's picture hides (PURE): its recording's (render.censor) and its own, each `{box, style, * strength, from, to}` (`from`/`to` source seconds or null: the whole clip). None when the clip says `censor: false`. */ export function censorRegionsOf(entry, render, provenance = {}) { if (entry?.type !== "clip" || entry.censor === false) return []; const sets = [ ...(Array.isArray(render?.censor) ? render.censor.filter((r) => isObj(r) && namesClip(r, entry, provenance)) : []), ...(isObj(entry.censor) ? [entry.censor] : []), ]; const out = []; for (const s of sets) { const style = CENSOR_STYLES.includes(s.style) ? s.style : CENSOR_DEFAULTS.style; const strength = num(s.strength) ? s.strength : CENSOR_DEFAULTS.strength; for (const b of Array.isArray(s.boxes) ? s.boxes : []) { const box = Array.isArray(b) ? b : b?.box; if (!boxOk(box)) continue; const from = num(b?.from) ? b.from : num(s.from) ? s.from : null; const to = num(b?.to) ? b.to : num(s.to) ? s.to : null; out.push({ box, style, strength, from, to }); } } return out; } /** * The regions `render.censor` hides on a RECORDING (its video, and channel when known), for a player of it where no * clip exists yet (the claim bench, a candidate, a cited span): its recording's boxes, none of a clip's own. */ export const recordingRegionsOf = (video, channel, render, provenance = {}) => typeof video === "string" && video ? censorRegionsOf({ type: "clip", id: "~recording", video, ...(typeof channel === "string" && channel ? { channel } : {}) }, render, provenance) : []; const f3 = (n) => Number(n).toFixed(3); /** * The filtergraph parts that hide `regions` on a picture of `width`×`height` px (`[vIn]` → `[vOut]`, labels without * brackets): each box blurred or pixelated (split, cropped, laid back over) or filled black, over its span on the * segment's clock (`from` the source second the segment starts at, `gaps` the cut's). Empty when there are none: * the graph is the one it always was. */ export function censorParts(vIn, vOut, regions, { width, height, from = 0, gaps = [], seconds = Infinity }) { if (!regions?.length) return []; const parts = []; let cur = vIn; regions.forEach((g, j) => { const x = Math.round(g.box[0] * width); const y = Math.round(g.box[1] * height); const w = Math.max(2, 2 * Math.round(Math.min(width - x, g.box[2] * width) / 2)); const h = Math.max(2, 2 * Math.round(Math.min(height - y, g.box[3] * height) / 2)); const a = g.from === null ? null : Math.max(0, offsetIn(g.from, from, gaps)); const b = g.to === null ? null : Math.min(seconds, offsetIn(g.to, from, gaps)); const enable = a === null && b === null ? "" : `:enable='between(t,${f3(a ?? 0)},${f3(b ?? 1e6)})'`; const next = j === regions.length - 1 ? vOut : `${vOut}_c${j}`; if (g.style === "black") { parts.push(`[${cur}]drawbox=x=${x}:y=${y}:w=${w}:h=${h}:color=black@1:t=fill${enable}[${next}]`); } else { const fx = g.style === "pixelate" ? (() => { const [nx, ny] = pixelGrid(g.strength, g.box[2], g.box[3], width, height); return `scale=${nx}:${ny}:flags=neighbor,scale=${w}:${h}:flags=neighbor`; })() : `gblur=sigma=${blurSigma(g.strength, width)}:steps=3`; parts.push(`[${cur}]split[${vOut}_s${j}a][${vOut}_s${j}b]`, `[${vOut}_s${j}b]crop=${w}:${h}:${x}:${y},${fx}[${vOut}_s${j}c]`, `[${vOut}_s${j}a][${vOut}_s${j}c]overlay=x=${x}:y=${y}${enable}[${next}]`); } cur = next; }); return parts; } /** What a cut record says of the regions it hid (rounded). */ export const censorRecordOf = (regions) => regions.map((g) => ({ box: g.box, style: g.style, ...(g.style !== "black" ? { strength: g.strength } : {}), ...(g.from !== null ? { from: g.from } : {}), ...(g.to !== null ? { to: g.to } : {}) })); /** * Why segments on disk cannot take this cut (`--chrome-only`, `--chrome-preview`): a clip whose recorded hidden * regions are not the ones this cut asks for -- a box added, moved or taken off since it was cut. The regions are in * the segment's picture, so a changed set needs the segment again: refused by name, never laid over uncensored. * A clip with no picture (a poster plays) records that it had nothing to hide. */ export function censorProblems({ entries, records, render, provenance = {} }) { const wrong = []; entries.forEach((e, i) => { if (e?.type !== "clip") return; const want = censorRecordOf(censorRegionsOf(e, render, provenance)); const rec = records[i] ?? null; if (rec?.censorNoPicture === true) return; const got = Array.isArray(rec?.censor) ? rec.censor : []; if (JSON.stringify(want) === JSON.stringify(got)) return; wrong.push(`${e.id} (${got.length} box(es) hidden on disk, ${want.length} asked for)`); }); if (!wrong.length) return []; return [ `${wrong.length} segment(s) hide other regions than this cut asks for: ${wrong.join(", ")}. ` + "The hidden regions are in the segment's picture -- run a normal build (with --skip-fetch it re-cuts from the cached windows), not a chrome pass.", ]; } /** * The regions a STILL of a clip at source second `at` hides (those over it), drawn on a frame `width`×`height`: * ffmpeg args (`-filter_complex`, a graph from [0:v] to [cstill], `-map`) or none. `after` filters (a blur) follow. */ export function stillCensorArgs(regions, at, { width, height }, after = []) { const over = (regions ?? []).filter((g) => (g.from === null || g.from <= at + 1e-6) && (g.to === null || at <= g.to + 1e-6)).map((g) => ({ ...g, from: null, to: null })); if (!over.length && !after.length) return []; const parts = over.length ? censorParts("0:v", after.length ? "cstill0" : "cstill", over, { width, height }) : []; if (after.length) parts.push(`[${over.length ? "cstill0" : "0:v"}]${after.join(",")}[cstill]`); return ["-filter_complex", parts.join(";"), "-map", "[cstill]"]; }