// A clip's picture, blurred at its segment encode -- the per-clip `blur` knob // and the cut's `render.blur` switch. // // timeline entry: "blur": { "on": true, "caption": "…", "strength": 5, "from": 230.5, "to": 260 } // render: "blur": "off" (the uncensored variant; absent = "on") // // Why: a clip whose source picture shows something a platform's graphic- // content rules refuse (the operator's case, 2026-10-23: a dog having a // seizure) still has to be in the cut -- its SOUND is the evidence -- and the // cut still has to be shareable. So the source picture is blurred and a line // says so ("Original footage uncensored, blurred here for TOS"); the sound is // never touched. A take with `render.blur: "off"` renders the same cut // uncensored, beside it. // // ONLY THE SOURCE PICTURE. The blur is applied to the clip's picture after it // is scaled to the size it plays at and BEFORE it is padded into the frame: // the pad, the citation header, the footer, the QR, the rail, the deck (laid // over the whole concat later), the speaker captions (burned over the // finished frame) and the stamps are all drawn after it, so none of them is // blurred. The caption is drawn on the blurred picture itself, top centre, in // the deck's face (render.fontBold, as the montage's overlays are). // // `strength` is the Gaussian's sigma as a PERCENT of the picture's height as // it plays (2–15, default 5): 54 px on a 1080-line picture, ~40 px in the // deck's footage box. Measured on the picture after its scale, so the same // number blurs alike at any source resolution, and every variant made from // the master (720p, fit24) is a downscale of an already blurred picture. // // `from` / `to` (source seconds, inside the clip's window) blur only that span // -- either alone runs to the clip's edge -- and the caption shows over that // span only. Without them the whole clip is blurred. // // No imports from build-video.mjs: it imports this file, and so do the lint and // umtool's writers (updateClip validates through validateClipBlur). The // cut's switch (`render.blur`) is one of the look's keys: its rules are the // common look.mjs's, re-exported here. import { RENDER_BLUR_VALUES, renderBlurOn, validateRenderBlur } from "yt-dlp-transcript-common/lib/report/look.mjs"; import { offsetIn } from "./gaps.mjs"; export { RENDER_BLUR_VALUES, renderBlurOn, validateRenderBlur }; /** What a caption says when the clip sets none. */ export const BLUR_CAPTION_DEFAULT = "Original footage uncensored, blurred here for TOS"; export const BLUR_CAPTION_MAX = 120; /** sigma, as a percent of the picture's height. */ export const BLUR_STRENGTH = Object.freeze({ min: 2, max: 15, default: 5 }); /** The keys a clip's `blur` may carry. */ export const CLIP_BLUR_KEYS = Object.freeze(["on", "caption", "strength", "from", "to"]); /** The shortest span a partial blur may cover (s). */ export const BLUR_SPAN_MIN = 0.2; /** The window tolerance a stored 2 dp edge gets (the writer's and the build's). */ const EPS = 0.02; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const num = (v) => typeof v === "number" && Number.isFinite(v); const q = (v) => JSON.stringify(v); /** * Every reason a clip's `blur` cannot be used, as sentences naming `where` * (empty: it can). `undefined` / `null` is none. Unknown keys are refused. * With `entry`, `from`/`to` are checked against its window too. * * @param {unknown} blur * @param {string} [where] * @param {{ start?: number, end?: number } | null} [entry] * @returns {string[]} */ export function validateClipBlur(blur, where = "blur", entry = null) { if (blur === undefined || blur === null) return []; if (!isObj(blur)) return [`${where} must be { on: true, caption?, strength?, from?, to? }`]; const b = /** @type {Record} */ (blur); const errors = []; for (const k of Object.keys(b)) { if (!CLIP_BLUR_KEYS.includes(k)) errors.push(`${where}.${k} is not a blur setting (${CLIP_BLUR_KEYS.join(", ")})`); } if (b.on !== true) errors.push(`${where}.on must be true (remove the key to stop blurring this clip)`); if (b.caption !== undefined && (typeof b.caption !== "string" || !b.caption.trim() || b.caption.length > BLUR_CAPTION_MAX)) { errors.push(`${where}.caption must be a text of 1 to ${BLUR_CAPTION_MAX} characters (absent: "${BLUR_CAPTION_DEFAULT}")`); } if (b.strength !== undefined && (!num(b.strength) || b.strength < BLUR_STRENGTH.min || b.strength > BLUR_STRENGTH.max)) { errors.push(`${where}.strength must be a number from ${BLUR_STRENGTH.min} to ${BLUR_STRENGTH.max} (the blur's radius, % of the picture's height; default ${BLUR_STRENGTH.default})`); } for (const k of ["from", "to"]) { if (b[k] !== undefined && (!num(b[k]) || /** @type {number} */ (b[k]) < 0)) errors.push(`${where}.${k} must be a source second ≥ 0`); } if (num(b.from) && num(b.to) && /** @type {number} */ (b.to) - /** @type {number} */ (b.from) < BLUR_SPAN_MIN) { errors.push(`${where}: from ${b.from} must be at least ${BLUR_SPAN_MIN} s before to ${b.to}`); } if (entry && num(entry.start) && num(entry.end)) { const s = /** @type {number} */ (entry.start); const e = /** @type {number} */ (entry.end); for (const k of ["from", "to"]) { const v = b[k]; if (num(v) && (/** @type {number} */ (v) < s - EPS || /** @type {number} */ (v) > e + EPS)) { errors.push(`${where}.${k} ${v} must lie inside the clip's window ${s}–${e} — widen the window, or clear blur.${k}`); } } } return errors; } /** * Every clip's `blur` in a manifest, checked (the build's, the lint's and the * structural writers'). `render.blur` is the look's (validateLook). */ export function validateClipBlurEntries(manifest) { const errors = []; const montage = manifest?.render?.montage !== undefined && manifest?.render?.montage !== null; (manifest?.timeline ?? []).forEach((e, i) => { if (e?.blur === undefined) return; const where = `timeline[${i}] (${e?.id ?? "?"}).blur`; if (e?.type && e.type !== "clip") { errors.push(`${where}: only a clip's picture can be blurred`); return; } if (montage && e.blur !== null) errors.push(`${where}: a montage re-cuts its clips' pictures — blur does not combine with render.montage`); errors.push(...validateClipBlur(e.blur, where, e)); }); return errors; } /** * The blur this clip renders with, every default filled -- `{ strength, * caption, from, to }` (from/to null: the clip's edge) -- or null: the clip * has none, it is not valid, or the cut renders uncensored. */ export function clipBlurOf(entry, render) { const b = isObj(entry?.blur) ? entry.blur : null; if (!b || !renderBlurOn(render) || validateClipBlur(b).length) return null; return { strength: num(b.strength) ? b.strength : BLUR_STRENGTH.default, caption: typeof b.caption === "string" && b.caption.trim() ? b.caption.trim() : BLUR_CAPTION_DEFAULT, from: num(b.from) ? b.from : null, to: num(b.to) ? b.to : null, }; } const f3 = (n) => String(Number(n.toFixed(3))); /** * Where a blur lands in a segment that starts at source second `segStart` and * runs `seconds`: `{ whole: true }` (all of it), `{ a, b }` (the segment's * own seconds), or null (the span misses the segment entirely). `gaps` * (source seconds, gaps.mjs): the segment's left-out seconds, so an edge * after one lands that much earlier, and an edge inside one at its join. */ export function blurSpan(blur, segStart, seconds, gaps = null) { if (!blur) return null; if (blur.from === null && blur.to === null) return { whole: true }; const a = Math.max(0, blur.from === null || blur.from === undefined ? -Infinity : offsetIn(blur.from, segStart, gaps)); const b = Math.min(seconds, blur.to === null || blur.to === undefined ? Infinity : offsetIn(blur.to, segStart, gaps)); if (!(b > a)) return null; if (a <= 0 && b >= seconds) return { whole: true }; return { a, b }; } /** * The caption's size and lines for a picture `w` × `h` (as it plays): a font * 4.2 % of the height (14–64 px), broken to what fits 84 % of the width, the * top line 5 % down. */ export function blurCaptionLayout(caption, w, h) { const fontSize = Math.max(14, Math.min(64, Math.round(Math.min(h * 0.042, w * 0.03)))); // Plex Mono's advance is 0.6 em. const cols = Math.max(12, Math.floor((w * 0.84) / (fontSize * 0.6))); const lines = []; let line = ""; for (const word of String(caption).split(/\s+/).filter(Boolean)) { if (line && (line + " " + word).length > cols) { lines.push(line); line = word; } else line = line ? `${line} ${word}` : word; } if (line) lines.push(line); return { fontSize, lines, lineHeight: Math.round(fontSize * 1.45), top: Math.round(h * 0.05), pad: Math.max(4, Math.round(fontSize * 0.35)) }; } /** * The filters that blur a picture `w` × `h` (as it plays, before it is padded * into the frame) and draw the caption on it: gblur, then one drawtext per * line (read from `lineFiles[k]`, which the caller writes -- textfile=, so * nothing needs filter escaping), each with `enable` over a partial span. * Empty when `span` is null. `h` is the picture's height as it plays. * * @param {{ blur: { strength: number }, span: { whole?: boolean, a?: number, b?: number } | null, * h: number, layout: ReturnType, lineFiles: string[], render: any }} o * @returns {string[]} */ export function blurPictureFilters({ blur, span, h, layout, lineFiles, render }) { if (!blur || !span) return []; const enable = span.whole ? "" : `:enable='between(t,${f3(span.a)},${f3(span.b)})'`; const sigma = Math.max(1, Math.round((blur.strength / 100) * h * 10) / 10); const pal = render?.palette ?? {}; const font = render?.fontBold ?? render?.fontRegular; const out = [`gblur=sigma=${sigma}:steps=3${enable}`]; lineFiles.forEach((file, k) => { out.push( [ `drawtext=textfile='${file}'`, ...(font ? [`fontfile='${font}'`] : []), `fontsize=${layout.fontSize}`, `fontcolor=${pal.fg ?? "white"}`, "box=1", `boxcolor=${pal.bg ?? "black"}@0.78`, `boxborderw=${layout.pad}`, "x=(w-text_w)/2", `y=${layout.top + k * layout.lineHeight}`, ].join(":") + enable, ); }); return out; } /** * The size a picture `sw` × `sh` plays at when fitted into `bw` × `bh` keeping * its aspect (ffmpeg's force_original_aspect_ratio=decrease, to the pixel). */ export function fittedSize(sw, sh, bw, bh) { if (!(sw > 0 && sh > 0)) return { w: bw, h: bh }; const k = Math.min(bw / sw, bh / sh); return { w: Math.max(1, Math.round(sw * k)), h: Math.max(1, Math.round(sh * k)) }; } /** What a segment's cut record says about its blur (null: none), so --chrome-only can tell. */ export function blurRecordOf(blur) { if (!blur) return null; return { strength: blur.strength, caption: blur.caption, from: blur.from, to: blur.to }; } /** * Why segments on disk cannot be re-laid for this cut, as sentences: a * segment blurred where the cut now wants it clear (render.blur turned off), * or clear where it wants a blur. The picture is IN the segment, so only a * rebuild changes it. A record with no `blur` was built without one. * * @param {{ entries: object[], records: Array, render: object }} o */ export function blurProblems({ entries, records, render }) { const wrong = []; entries.forEach((e, i) => { if (/** @type {any} */ (e)?.type !== "clip") return; const want = clipBlurOf(e, render); const got = /** @type {any} */ (records[i])?.blur ?? null; if (!want && !got) return; const same = (k) => (want?.[k] ?? null) === (got?.[k] ?? null); if (want && got && same("strength") && same("caption") && same("from") && same("to")) return; const how = (x) => (x ? "blurred" : "clear"); wrong.push(`${/** @type {any} */ (e).id} (${how(got)} on disk, ${how(want)}${want && got ? " another way" : ""} in this cut)`); }); if (!wrong.length) return []; return [ `${wrong.length} segment(s) carry another blur than this cut asks for: ${wrong.join(", ")}. ` + "The blur is in the segment's picture -- run a normal build (with --skip-fetch it re-cuts from the cached windows), not --chrome-only.", ]; }