// `umtool censor` (lane clip-kit, slice E): the boxes hidden on every clip cut from ONE recording -- a stream layout's
// chat overlay, a browser window showing other people's accounts -- written into `render.censor` (report-to-video
// clip-censor.mjs) through the look's writer. A clip's own boxes are `umtool set
censor.boxes=…`.
//
// umtool censor --video [--channel ] --box x,y,w,h[@from-to] [--box …]
// [--style blur|black|pixelate] [--strength 1-10] [--from s] [--to s] [--json]
// umtool censor --video [--channel ] --clear
// umtool censor --list
import { REPORTS_ROOT, resolveProject } from "../projects/core.mjs";
import { kindMakesTakes } from "../projects/kinds.mjs";
import { readManifest } from "../projects/report.mjs";
import { manifestToken, updateRender } from "./manifest.mjs";
export const CENSOR_USAGE = [
"usage: umtool censor --video [--channel ] --box x,y,w,h[@from-to] [--box …]",
" [--style blur|black|pixelate] [--strength 1-10] [--from s] [--to s] [--json]",
" umtool censor --video [--channel ] --clear",
" umtool censor --list",
"",
"Hides the same parts of the picture on every clip cut from one recording (render.censor): fractions of the",
"source picture, each box for the whole clip or, with @from-to (source seconds), that span. Blurred (default),",
"black or pixelated; on in the uncensored variant too (privacy, not TOS). Given again for the same recording, the",
"boxes replace its boxes. A clip's own: `umtool set censor.boxes='[[x,y,w,h]]'` (`censor=false` none).",
].join("\n");
class CliError extends Error {}
/** `x,y,w,h` or `x,y,w,h@from-to`, read. */
export function parseBox(text) {
const m = /^([0-9.]+),([0-9.]+),([0-9.]+),([0-9.]+)(?:@([0-9.]+)-([0-9.]+))?$/.exec(String(text ?? "").trim());
if (!m) throw new CliError(`--box ${text}: x,y,w,h (fractions of the picture), or x,y,w,h@from-to (source seconds)`);
const box = m.slice(1, 5).map(Number);
return m[5] !== undefined ? { box, from: Number(m[5]), to: Number(m[6]) } : box;
}
/** render.censor with one recording's boxes set (`entry`) or removed (null). */
export function withRecording(list, { video, channel = null }, entry) {
const same = (r) => r?.video === video && (r.channel ?? null) === channel;
const rest = (Array.isArray(list) ? list : []).filter((r) => !same(r));
return entry ? [...rest, entry] : rest;
}
/**
* `umtool censor …`
* @param {string[]} args
* @param {{ reportsRoot?: string, log?: (s: string) => void, err?: (s: string) => void }} [o]
*/
export async function censorCommand(args, { reportsRoot = REPORTS_ROOT, log = console.log, err = console.error } = {}) {
try {
const flags = { "--box": [] };
const pos = [];
for (let i = 0; i < args.length; i += 1) {
const a = args[i];
if (["--video", "--channel", "--style", "--strength", "--from", "--to", "--box"].includes(a)) {
if (i + 1 >= args.length) throw new CliError(`${a} needs a value`);
if (a === "--box") flags["--box"].push(args[++i]);
else flags[a] = args[++i];
} else if (["--clear", "--list", "--json", "--help"].includes(a)) flags[a] = true;
else if (a.startsWith("--")) throw new CliError(`unknown flag ${a}`);
else pos.push(a);
}
if (flags["--help"] || !pos.length) {
log(CENSOR_USAGE);
return flags["--help"] ? 0 : 2;
}
const r = await resolveProject(pos[0], reportsRoot);
if (r.ambiguous) throw new CliError(`"${pos[0]}" is the name of ${r.ambiguous.length} projects — pass its id`);
if (!r.project) throw new CliError(`no project matches "${pos[0]}"`);
if (!kindMakesTakes(r.project.kind)) throw new CliError(`${r.project.id} is a ${r.project.kind}, not a report video`);
const manifest = await readManifest(r.project.dir);
const now = Array.isArray(manifest?.render?.censor) ? manifest.render.censor : [];
if (flags["--list"]) {
log(flags["--json"] ? JSON.stringify(now, null, 2) : now.length ? now.map((e) => `${e.channel ? `${e.channel}/` : ""}${e.video}: ${e.boxes.length} box(es), ${e.style ?? "blur"}`).join("\n") : "no recording's regions are hidden (render.censor)");
return 0;
}
const video = typeof flags["--video"] === "string" ? flags["--video"].trim() : "";
if (!video) throw new CliError("--video : the recording whose clips hide the boxes");
const channel = typeof flags["--channel"] === "string" ? flags["--channel"].trim() : null;
let entry = null;
if (!flags["--clear"]) {
if (!flags["--box"].length) throw new CliError("--box x,y,w,h (one or more), or --clear");
const num = (k) => (flags[k] !== undefined ? Number(flags[k]) : undefined);
entry = {
video, ...(channel ? { channel } : {}), boxes: flags["--box"].map(parseBox),
...(flags["--style"] ? { style: flags["--style"] } : {}),
...(num("--strength") !== undefined ? { strength: num("--strength") } : {}),
...(num("--from") !== undefined ? { from: num("--from") } : {}),
...(num("--to") !== undefined ? { to: num("--to") } : {}),
};
}
const next = withRecording(now, { video, channel }, entry);
const w = await updateRender(r.project.dir, { censor: next.length ? next : null }, { token: await manifestToken(r.project.dir) });
const written = w.look.censor ?? [];
if (flags["--json"]) log(JSON.stringify(written, null, 2));
else log(entry ? `${channel ? `${channel}/` : ""}${video}: ${entry.boxes.length} box(es) hidden on every clip of it (${entry.style ?? "blur"})` : `${video}: no regions hidden`);
return 0;
} catch (e) {
if (e instanceof CliError || e?.name === "RenderRefused") {
err(`umtool censor: ${e instanceof Error ? e.message : e}`);
return 2;
}
throw e;
}
}