// A CENSORED ATTACHMENT (operator, 3 Nov: "'it jiggle' will need to be // censored but I do want to include it"): a post item's picture or video // played BLURRED -- the whole frame, or only some regions of it -- with a // small label saying so. umtool/docs/knobs.md, "A post item". // // media[i].blur = { on: true, strength?: 1–10 (7), boxes?: [[x, y, w, h], …], label?: "…" | false, // band?: { below?|above?|between?: [a, b], left?|right? }, style?: "blur"|"black"|"pixelate" } // // `boxes` are fractions of the picture AS SHOWN (after its crop), held still // over the clip; none blurs the whole frame. `label` is the caption drawn on // it ("Censored" by default; false: none). A post's channel markup may censor // one of its attachments for every project (posts-markup.json's `censor`, // post-redact.mjs); the item's own `blur` wins. // // A BAND (operator, 4 Nov: "I'd also like to be able to censor across a line // dividing the image e.g. censor everything neck down"): the region across the // picture on one side of a line -- `below: 0.38` everything under 38% of its // height, `above`, `between: [a, b]` -- and, optionally, on one side of an // upright line (`left: 0.5` everything left of the middle, `right`); both: // where they meet. It is one more region beside the boxes (none of either: // the whole frame), and the label sits at its corner. `style` is how a region // is censored: `blur` (the default, its `strength`), `black` (a solid fill) or // `pixelate` (blocks, coarser with the strength). // // A PRESENTATION choice, like the rest of the censor: it stays ON in an // uncensored cut (post-slurs.mjs toggles the slurs only). // // NOTHING IS REWRITTEN: the attachment's file stays as it was captured. The // blur is a real one (ffmpeg gblur, sigma grown with the strength and the // picture's size), drawn where public output is made -- the render. Play and // the post bench preview it (a CSS blur, the boxes as backdrop blurs; a black // fill; the pixels drawn coarse on a canvas). // // PURE and dependency-free. export const POST_BLUR_DEFAULTS = Object.freeze({ strength: 7, label: "Censored", style: "blur" }); export const POST_BLUR_BOXES_MAX = 8; /** How a censored region is drawn. */ export const POST_BLUR_STYLES = Object.freeze(["blur", "black", "pixelate"]); /** A band's edges: one across (below, above, between) and, optionally, one upright (left, right). */ export const POST_BAND_ACROSS = Object.freeze(["below", "above", "between"]); export const POST_BAND_UPRIGHT = Object.freeze(["left", "right"]); const BLUR_KEYS = ["on", "strength", "boxes", "label", "band", "style"]; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const fin = (v) => typeof v === "number" && Number.isFinite(v); const r3 = (v) => Math.round(v * 1000) / 1000; const r4 = (v) => Math.round(v * 10000) / 10000; /** Is attachment `m` blurred? */ export const blurOn = (m) => isObj(m?.blur) && m.blur.on === true; /** * THE BAND's BOX, pure: `[x, y, w, h]` as fractions of the picture as shown, * or null when `band` names no edge (or the region is empty). `below: a` is * y from a to the foot, `above: a` from the top to a, `between: [a, b]` a to * b; `left: a` x from 0 to a, `right: a` from a to the right edge. */ export function bandBox(band) { if (!isObj(band)) return null; let y0 = 0, y1 = 1, x0 = 0, x1 = 1; let any = false; if (fin(band.below)) [y0, any] = [band.below, true]; else if (fin(band.above)) [y1, any] = [band.above, true]; else if (Array.isArray(band.between) && band.between.length === 2 && band.between.every(fin)) { y0 = Math.min(...band.between); y1 = Math.max(...band.between); any = true; } if (fin(band.left)) [x1, any] = [band.left, true]; else if (fin(band.right)) [x0, any] = [band.right, true]; const c = (v) => Math.min(1, Math.max(0, v)); [x0, x1, y0, y1] = [c(x0), c(x1), c(y0), c(y1)]; if (!any || x1 - x0 < 0.01 || y1 - y0 < 0.01) return null; return [r4(x0), r4(y0), r4(x1 - x0), r4(y1 - y0)]; } /** Every reason a band is refused. */ export function bandErrors(band, where) { if (band === undefined) return []; if (!isObj(band)) return [`${where} must be {below?|above?|between?, left?|right?} (fractions of the picture)`]; const errors = []; for (const k of Object.keys(band)) if (![...POST_BAND_ACROSS, ...POST_BAND_UPRIGHT].includes(k)) errors.push(`${where}.${k} is not a band edge (${[...POST_BAND_ACROSS, ...POST_BAND_UPRIGHT].join(", ")})`); const across = POST_BAND_ACROSS.filter((k) => band[k] !== undefined); const upright = POST_BAND_UPRIGHT.filter((k) => band[k] !== undefined); if (across.length > 1) errors.push(`${where}: one of ${POST_BAND_ACROSS.join(", ")} (not ${across.join(" and ")})`); if (upright.length > 1) errors.push(`${where}: one of left, right (not both)`); if (!across.length && !upright.length) errors.push(`${where} names no edge (below, above, between, left or right)`); const frac = (v) => fin(v) && v > 0 && v < 1; for (const k of ["below", "above", "left", "right"]) if (band[k] !== undefined && !frac(band[k])) errors.push(`${where}.${k} must be a fraction of the picture, between 0 and 1 (0.38: 38% of the way ${k === "left" || k === "right" ? "across" : "down"})`); if (band.between !== undefined) { const b = band.between; if (!(Array.isArray(b) && b.length === 2 && b.every(frac) && b[1] - b[0] >= 0.01)) errors.push(`${where}.between must be [a, b], fractions of the picture's height with a < b`); } if (!errors.length && !bandBox(band)) errors.push(`${where} leaves no picture to censor`); return errors; } /** * Its blur with the defaults filled: `{ strength, boxes, band, label, style }` * (label null: none; `band` the band's box, also the LAST of `boxes` -- the * regions drawn; none of either: the whole frame), or null when it is not * blurred. */ export function blurOf(m) { if (!blurOn(m)) return null; const b = m.blur; const band = bandBox(b.band); const boxes = Array.isArray(b.boxes) ? b.boxes.filter((x) => Array.isArray(x) && x.length === 4) : []; return { strength: fin(b.strength) ? Math.min(10, Math.max(1, b.strength)) : POST_BLUR_DEFAULTS.strength, boxes: band ? [...boxes, band] : boxes, band, label: b.label === false ? null : typeof b.label === "string" && b.label.trim() ? b.label.trim() : POST_BLUR_DEFAULTS.label, style: POST_BLUR_STYLES.includes(b.style) ? b.style : POST_BLUR_DEFAULTS.style, }; } /** Where the label sits, as fractions of the picture: the band's corner, else the picture's. */ export const labelBoxOf = (bl) => bl?.band ?? [0, 0, 1, 1]; /** A pixelated region's blocks `[across, down]`: a block is 0.8% of the picture's width per step of strength (at 7, 5.6%). */ export function pixelGrid(strength, rw, rh, width, height) { const block = Math.max(0.005, strength * 0.008); return [Math.max(1, Math.round(rw / block)), Math.max(1, Math.round((rh * (height / Math.max(1, width))) / block))]; } /** The blur's sigma on a picture `width` px wide: 0.4% of the width per step of strength (at 7, ~2.8%). */ export const blurSigma = (strength, width) => Math.max(2, Math.round(width * strength * 0.004 * 10) / 10); /** The same as a share of the width, for a preview (CSS `cqw`). */ export const blurCqw = (strength) => r3(strength * 0.4); /** Is `b` a box of fractions inside the picture? */ export const boxOk = (b) => Array.isArray(b) && b.length === 4 && b.every((v) => fin(v) && v >= 0 && v <= 1) && b[2] >= 0.01 && b[3] >= 0.01 && b[0] + b[2] <= 1.0001 && b[1] + b[3] <= 1.0001; /** Every reason `media[i].blur` is refused. */ export function blurErrors(blur, where) { if (blur === undefined) return []; if (!isObj(blur)) return [`${where} must be {on, strength?, boxes?, label?, band?, style?}`]; const errors = []; for (const k of Object.keys(blur)) if (!BLUR_KEYS.includes(k)) errors.push(`${where}.${k} is not a blur knob (${BLUR_KEYS.join(", ")})`); if (typeof blur.on !== "boolean") errors.push(`${where}.on must be true or false`); if (blur.strength !== undefined && !(fin(blur.strength) && blur.strength >= 1 && blur.strength <= 10)) errors.push(`${where}.strength must be 1–10`); if (blur.boxes !== undefined) { if (!Array.isArray(blur.boxes) || blur.boxes.length > POST_BLUR_BOXES_MAX) errors.push(`${where}.boxes must be a list of at most ${POST_BLUR_BOXES_MAX} [x, y, w, h]`); else blur.boxes.forEach((b, j) => !boxOk(b) && errors.push(`${where}.boxes[${j}] must be [x, y, w, h] as fractions of the picture (each 0–1, inside it)`)); } if (blur.label !== undefined && blur.label !== false && !(typeof blur.label === "string" && blur.label.length <= 40)) errors.push(`${where}.label must be words (at most 40 characters) or false`); errors.push(...bandErrors(blur.band, `${where}.band`)); if (blur.style !== undefined && !POST_BLUR_STYLES.includes(blur.style)) errors.push(`${where}.style must be one of ${POST_BLUR_STYLES.join(", ")}`); return errors; } /** A label's words as ffmpeg drawtext's `text=` takes them, inside single quotes. */ const dtText = (s) => String(s).replace(/\\/g, "\\\\").replace(/'/g, "\u2019").replace(/:/g, "\\:").replace(/%/g, "\\%"); /** * The filters that censor attachment `m`, drawn `w` × `h` px: `[inLabel]` → * `[outLabel]`. The whole frame, or each region (its boxes, its band) in its * style: blurred (split, cropped, blurred and laid back over), black (a filled * box) or pixelated (cropped, scaled down and back up without smoothing). A * label is drawn by labelFilter, on the composed frame. */ export function blurFilters(inLabel, outLabel, m, { width, height }) { const b = blurOf(m); if (!b) return [`[${inLabel}]null[${outLabel}]`]; const sigma = blurSigma(b.strength, width); const regions = b.boxes.length ? b.boxes : [[0, 0, 1, 1]]; const px = (box) => { const x = Math.round(box[0] * width); const y = Math.round(box[1] * height); return { x, y, w: Math.max(2, Math.min(width - x, Math.round(box[2] * width))), h: Math.max(2, Math.min(height - y, Math.round(box[3] * height))) }; }; if (b.style === "black") { const fills = regions.map((box) => { const r = px(box); return `drawbox=x=${r.x}:y=${r.y}:w=${r.w}:h=${r.h}:color=black@1:t=fill`; }); return [`[${inLabel}]${fills.join(",")}[${outLabel}]`]; } const fx = (box, r) => { if (b.style === "pixelate") { const [nx, ny] = pixelGrid(b.strength, box[2], box[3], width, height); return `scale=${nx}:${ny}:flags=neighbor,scale=${r.w}:${r.h}:flags=neighbor`; } return `gblur=sigma=${sigma}:steps=3`; }; if (!b.boxes.length) return [`[${inLabel}]${fx([0, 0, 1, 1], { w: width, h: height })}[${outLabel}]`]; const out = []; let cur = inLabel; b.boxes.forEach((box, j) => { const r = px(box); const base = `${outLabel}_k${j}`; out.push(`[${cur}]split[${base}a][${base}b]`); out.push(`[${base}b]crop=${r.w}:${r.h}:${r.x}:${r.y},${fx(box, r)}[${base}c]`); const next = j === b.boxes.length - 1 ? outLabel : `${base}o`; out.push(`[${base}a][${base}c]overlay=x=${r.x}:y=${r.y}[${next}]`); cur = next; }); return out; } /** * The label's drawtext on the frame `[inLabel]` → `[outLabel]`: the words at * the censored region's top-left corner -- the band's, else the * attachment's (its box `b` on the frame) -- while it is up, or null when it * has none (or there is no face to draw it in). */ export function labelFilter(inLabel, outLabel, m, b, { from, to, fontfile, scale = 1 }) { const bl = blurOf(m); if (!bl?.label || !fontfile) return null; const size = Math.max(12, Math.round(26 * scale)); const pad = Math.round(size * 0.5); const at = labelBoxOf(bl); const x = Math.round(b.x + at[0] * b.width) + pad; const y = Math.round(b.y + at[1] * b.height) + pad; return ( `[${inLabel}]drawtext=fontfile='${fontfile}':text='${dtText(bl.label)}':fontsize=${size}:fontcolor=white:box=1:boxcolor=black@0.7:boxborderw=${Math.round(size * 0.35)}:` + `x=${x}:y=${y}:enable='between(t,${r3(from)},${r3(to)})'[${outLabel}]` ); } /** Every reason a markup's `censor` list is refused (posts-markup.json). */ export function censorMarkupErrors(list, where) { if (list === undefined) return []; if (!Array.isArray(list)) return [`${where} must be a list of {media, blur}`]; const errors = []; list.forEach((c, i) => { if (!isObj(c) || !(Number.isInteger(c.media) && c.media >= 0 && c.media < 8)) errors.push(`${where}[${i}].media must be the attachment's index (0–7)`); if (isObj(c) && c.blur !== undefined) errors.push(...blurErrors({ on: true, ...(isObj(c.blur) ? c.blur : { bad: c.blur }) }, `${where}[${i}].blur`)); }); return errors; } /** * Post item `entry` with its channel markup's censoring laid on (`censor: * [{media, blur?}]` in the post's markup; `blur` absent: the whole frame, the * defaults): an attachment with a `blur` of its own keeps it. Returns the * entry itself when nothing changes. * * @param {Record} entry * @param {{ markup?: Record}>}> } | null | undefined} red */ export function censorEntry(entry, red) { const list = red?.markup?.[String(entry?.post ?? "")]?.censor; if (!Array.isArray(list) || !list.length || !Array.isArray(entry?.media)) return entry; let changed = false; const media = entry.media.map((m, i) => { const c = list.find((x) => x?.media === i); if (!c || !isObj(m) || m.blur !== undefined) return m; changed = true; return { ...m, blur: { on: true, ...(isObj(c.blur) ? c.blur : {}) } }; }); return changed ? { ...entry, media } : entry; } /** * A CENSOR in the knob's words (`umtool posts set-item … media..censor=`): * colon-separated parts -- an edge and its place (`below:0.38`, `above:0.2`, * `between:0.2,0.6`, `left:0.5`, `right:0.5`), a style (`blur`, `black`, * `pixelate`) and a strength (1–10) -- as `{ band?, style?, strength? }`, or * `{ error }`. `below:0.38:blur`, `below:0.4:left:0.5:pixelate:8`. */ export function censorSpecOf(text) { const parts = String(text ?? "").trim().split(":").map((x) => x.trim()).filter(Boolean); if (!parts.length) return { error: "a censor is :[: