mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-01 12:44:11 -10:00
Shane on Dutch (11-29): words faded and cut in two, lines too quiet to hear, effects no one hears, and text laid over the people and the dog the shot is about. - raea sound check VIDEO (server/sound.ts): each voice clip's edit points against the take's words and its sound. A cut inside a word or on a word's sound is an error, and the message names the pause (or the boundary) to move it to. Lines more than 6 dB from the cut's median are an error, with the gainDb that evens them. An effect 10 dB or more under the voice is an error, since no one hears it. Buried or fighting music is a warning. offload.ts runs it after every render that has a manifest. - The farm's POST /v1/scene (deploy/modal/farm_scene.py) reads a frame: people as a mask, faces, COCO's things, and words in the shot (four small OpenCV zoo models on the CPU, each pinned by its hash). - server/place.ts turns a scene into costs. raea frames check now fails text over a face, a dog or a person when a clear place of its size exists, and names that place. raea frames place FILE lists the clear places in a take. Stacked lines move as one block, our own text isn't counted as the shot's words, and passers-by's small faces don't count. - The farm wrote a split render's probe manifest (frame 0 only) as the render's. Each run now writes its own and the farm joins them, so the checks see every frame and every clip. Tests: test/sound.ts (7), test/place.ts (4, on scene reads of three interview frames, boxes only), test/farm_plan.py (the joined manifest). Co-authored-by: shinyobjectz <[email protected]>
326 lines
18 KiB
TypeScript
326 lines
18 KiB
TypeScript
// `raea frames check` on a raea cloud box: what an export's pictures get wrong that a viewer sees at once, measured
|
|
// (11-25, Shane: "it cuts out faces all the time and also does split frame shots when they make no sense", "it makes
|
|
// text too small on some things, we should have a text/glyph readability check and measurement").
|
|
//
|
|
// raea frames check VIDEO [--manifest RENDER.json] [-o REPORT.json]
|
|
// faces: VIDEO's frames, two a second, through the farm's face detector (POST /v1/faces, OpenCV's YuNet; a box has
|
|
// no vision backend of its own). A face of a person in the shot (at least 6% of the frame's height) touching an
|
|
// edge is cut by the frame: for a second or more it's an error, shorter a warning.
|
|
// text: each text the render's manifest shows (raea render --manifest; a .tsx render through raea cloud writes
|
|
// VIDEO.render.json by itself), its font size in pixels of a 1080-wide frame: under 40 px is an error (no one
|
|
// reads it on a phone), under 48 px a warning (small for anything but a badge or small print).
|
|
// placement (11-29): each text against what's under it (the farm's POST /v1/scene: people, faces, the things a
|
|
// shot is about, words in the shot like a sign): text over a face, a thing or a person, where a clear place of
|
|
// its size exists, is said with that place (server/place.ts).
|
|
// raea frames place FILE [--at S[,S...]] [--box WxH] [-o REPORT.json]
|
|
// the clearest places in FILE (a take or an export, a picture or a video, at those seconds) for a box WxH in a
|
|
// 1080-wide frame (a caption, 840x110, by default): read the takes with it before laying text over them.
|
|
// Exit 1 on any error. A face at the edge that's meant (someone walking out of shot) is said, with its reason, in
|
|
// VIDEO.despite.txt beside the video: its findings are then warnings.
|
|
import { spawnSync } from "node:child_process"
|
|
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
|
|
import os from "node:os"
|
|
import path from "node:path"
|
|
import { pathToFileURL } from "node:url"
|
|
import { placeReport, textOver, type Scene } from "./place.ts"
|
|
|
|
/** frames sampled a second, and the width they're read at (a face's place is in shares of the picture) */
|
|
const RATE = 2
|
|
const SIDE = 540
|
|
/** a face this share of the frame's height or more is someone in the shot, not a passer-by far behind (a split
|
|
* screen's half holds a face about this big) */
|
|
export const FACE_MIN = 0.05
|
|
/** least font sizes, in pixels of a frame 1080 wide (its shorter side): the Direct Response skill's table */
|
|
export const TEXT_ERROR = 40
|
|
export const TEXT_WARN = 48
|
|
|
|
export type Finding = { level: "error" | "warn"; kind: "face-cut" | "split" | "text-small" | "text-over"; at: number | null; until?: number; says: string }
|
|
/** a line where the picture jumps across nearly the whole frame (the farm's): its axis (h, a row), where, and its share */
|
|
export type Seam = [string, number, number]
|
|
export type Faces = { w: number; h: number; faces: number[][]; seams?: Seam[] } | null
|
|
type Split = { axis: "h" | "v"; at: number }
|
|
|
|
/** a frame's split-screen seams: lone lines in its middle 60% (a bar's two edges close together are a caption's box, a
|
|
* badge or a letterbox, not a seam) */
|
|
export function splitsOf(fr: NonNullable<Faces>): Split[] {
|
|
const out: Split[] = []
|
|
for (const axis of ["h", "v"] as const) {
|
|
const dim = axis === "h" ? fr.h : fr.w
|
|
const at = [...new Set((fr.seams ?? []).filter((x) => x[0] === axis).map((x) => x[1]))].sort((a, b) => a - b)
|
|
const lines = at.filter((y, i) => i === 0 || y - at[i - 1] > 4)
|
|
for (const y of lines) {
|
|
const bar = lines.some((o) => o !== y && Math.abs(o - y) < dim * 0.15)
|
|
if (!bar && y > dim * 0.2 && y < dim * 0.8) out.push({ axis, at: y })
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
const near = (a: Split, b: Split) => a.axis === b.axis && Math.abs(a.at - b.at) <= 6
|
|
const big = (fr: NonNullable<Faces>) => fr.faces.filter((f) => f[3] >= fr.h * FACE_MIN)
|
|
|
|
/** what cuts a face in a frame: an edge of the frame, or a split screen's seam */
|
|
function cutBy(fr: NonNullable<Faces>, splits: Split[]): string | null {
|
|
for (const [x, y, fw, fh] of big(fr)) {
|
|
if (x <= 1) return "the left edge"
|
|
if (x + fw >= fr.w - 1) return "the right edge"
|
|
if (y <= 1) return "the top edge"
|
|
if (y + fh >= fr.h - 1) return "the bottom edge"
|
|
for (const sp of splits) {
|
|
const [a, b] = sp.axis === "h" ? [y, y + fh] : [x, x + fw]
|
|
if ((a < sp.at - 2 && b > sp.at + 2) || Math.abs(b - sp.at) <= 3 || Math.abs(a - sp.at) <= 3) return "the split screen's seam"
|
|
}
|
|
}
|
|
return null
|
|
}
|
|
|
|
/** runs of the same thing in consecutive frames, as [from, to) in frames */
|
|
function runs<T>(xs: (T | null)[], same: (a: T, b: T) => boolean = (a, b) => a === b): [number, number, T][] {
|
|
const out: [number, number, T][] = []
|
|
for (let i = 0; i < xs.length; ) {
|
|
const x = xs[i]
|
|
if (x === null) {
|
|
i++
|
|
continue
|
|
}
|
|
let j = i + 1
|
|
while (j < xs.length && xs[j] !== null && same(x, xs[j] as T)) j++
|
|
out.push([i, j, x])
|
|
i = j
|
|
}
|
|
return out
|
|
}
|
|
|
|
/** faces cut by the frame's edges or a split screen's seam (a second or more an error, shorter a warning), and split
|
|
* screens that hold one person (a split is for two things at once) */
|
|
export function cutFaces(frames: Faces[], rate = RATE): Finding[] {
|
|
const raw = frames.map((fr) => (fr ? splitsOf(fr) : []))
|
|
// a seam held for a second or more: a split screen; a line for a frame is a cut or a flash
|
|
const held = raw.map((ss, i) => ss.filter((s) => (raw[i - 1] ?? []).some((o) => near(s, o)) || (raw[i + 1] ?? []).some((o) => near(s, o))))
|
|
const out: Finding[] = []
|
|
const hits = frames.map((fr, i) => (fr ? cutBy(fr, held[i]) : null))
|
|
for (const [i, j, by] of runs(hits)) {
|
|
const from = i / rate
|
|
const to = j / rate
|
|
out.push({ level: to - from >= 1 ? "error" : "warn", kind: "face-cut", at: from, until: to, says: `a face is cut by ${by} from ${from.toFixed(1)} s to ${to.toFixed(1)} s: reframe on the person (both eyes, mouth and chin inside the picture)${by.includes("seam") ? ", or cut between two singles" : " or cut away"}` })
|
|
}
|
|
const split = held.map((ss) => ss[0] ?? null)
|
|
for (const [i, j, sp] of runs(split, near)) {
|
|
if (j - i < 2) continue
|
|
// whose face is on each side of the seam, frame by frame
|
|
let one = 0
|
|
let any = 0
|
|
for (let k = i; k < j; k++) {
|
|
const fr = frames[k]
|
|
if (!fr) continue
|
|
const sides = new Set(big(fr).map(([x, y, fw, fh]) => ((sp.axis === "h" ? y + fh / 2 : x + fw / 2) < sp.at ? 0 : 1)))
|
|
if (sides.size) any++
|
|
if (sides.size === 1) one++
|
|
}
|
|
const from = i / rate
|
|
const to = j / rate
|
|
const where = sp.axis === "h" ? "top and bottom" : "side by side"
|
|
if (any && one * 2 > any) out.push({ level: "warn", kind: "split", at: from, until: to, says: `a split screen (${where}) from ${from.toFixed(1)} s to ${to.toFixed(1)} s shows a face in only one half: split only for two things at once (two people reacting, a face and the product), else cut between singles` })
|
|
}
|
|
return out
|
|
}
|
|
|
|
/** texts under the size floor, from a render manifest: each text once, at its first frame */
|
|
export function smallText(manifest: any): Finding[] {
|
|
const w = Number(manifest?.width) || 1080
|
|
const h = Number(manifest?.height) || 1920
|
|
const fps = Number(manifest?.fps) || 30
|
|
const scale = 1080 / Math.min(w, h)
|
|
const seen = new Map<string, { at: number; px: number }>()
|
|
for (const fr of (manifest?.frames ?? []) as any[]) {
|
|
for (const t of (fr?.text ?? []) as any[]) {
|
|
const text = String(t?.text ?? "").trim()
|
|
if (!text || (t?.opacity ?? 1) <= 0.05) continue
|
|
const px = Number(t?.size) * scale
|
|
if (!Number.isFinite(px) || px <= 0) continue
|
|
const was = seen.get(text)
|
|
if (!was || px > was.px) seen.set(text, { at: was?.at ?? Number(fr?.frame ?? 0) / fps, px })
|
|
}
|
|
}
|
|
const out: Finding[] = []
|
|
for (const [text, { at, px }] of seen) {
|
|
const q = text.length > 40 ? `"${text.slice(0, 40)}..."` : `"${text}"`
|
|
if (px < TEXT_ERROR) out.push({ level: "error", kind: "text-small", at, says: `${q} is ${px.toFixed(0)} px (in a 1080-wide frame): under ${TEXT_ERROR} px no one reads it on a phone. Make it bigger, or cut words so it fits bigger` })
|
|
else if (px < TEXT_WARN) out.push({ level: "warn", kind: "text-small", at, says: `${q} is ${px.toFixed(0)} px: small for anything but a badge or small print (captions 52 px, hooks 64 px)` })
|
|
}
|
|
return out.sort((a, b) => (a.at ?? 0) - (b.at ?? 0))
|
|
}
|
|
|
|
function ffmpeg(args: string[]) {
|
|
const r = spawnSync("ffmpeg", args, { encoding: "utf8" })
|
|
if (r.status !== 0) throw new Error(`ffmpeg: ${(r.stderr ?? "").slice(-400)}`)
|
|
}
|
|
|
|
/** what the farm reads in a file's frames (`at`: those seconds, else RATE a second), from its POST /v1/scene, or from
|
|
* /v1/faces alone on a farm without it (then each scene is null) */
|
|
export async function scenes(file: string, at?: number[], f: typeof fetch = fetch): Promise<{ faces: Faces[]; scenes: Scene[] }> {
|
|
const url = process.env.RAEA_CLOUD_GPU_URL
|
|
const token = process.env.RAEA_CLOUD_GPU_TOKEN ?? ""
|
|
if (!url) throw new Error("the frames check needs the farm (RAEA_CLOUD_GPU_URL); this box has none set")
|
|
const dir = mkdtempSync(path.join(os.tmpdir(), "raea-frames-"))
|
|
try {
|
|
const scale = `scale='if(gt(iw,ih),-2,${SIDE})':'if(gt(iw,ih),${SIDE},-2)'`
|
|
if (/\.(png|jpe?g|webp)$/i.test(file)) ffmpeg(["-v", "error", "-y", "-i", file, "-vf", scale, "-q:v", "4", path.join(dir, "f00001.jpg")])
|
|
else if (at?.length) at.forEach((s, i) => ffmpeg(["-v", "error", "-y", "-ss", String(s), "-i", file, "-frames:v", "1", "-vf", scale, "-q:v", "4", path.join(dir, `f${String(i + 1).padStart(5, "0")}.jpg`)]))
|
|
else ffmpeg(["-v", "error", "-y", "-i", file, "-vf", `fps=${RATE},${scale}`, "-q:v", "4", path.join(dir, "f%05d.jpg")])
|
|
const jpgs = readdirSync(dir).filter((n) => n.endsWith(".jpg")).sort()
|
|
const outF: Faces[] = []
|
|
const outS: Scene[] = []
|
|
let scene = true
|
|
for (let i = 0; i < jpgs.length; i += 300) {
|
|
const body = JSON.stringify({ frames: jpgs.slice(i, i + 300).map((n) => readFileSync(path.join(dir, n)).toString("base64")) })
|
|
const call = async (route: string) => {
|
|
let r: Response | undefined
|
|
let why = ""
|
|
for (let k = 0; k < 4; k++) {
|
|
r = await f(`${url.replace(/\/$/, "")}${route}`, { method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, body, signal: AbortSignal.timeout(600_000) })
|
|
if (r.ok || r.status === 404) break
|
|
why = `${r.status} ${(await r.text()).slice(0, 300)}`
|
|
if (r.status < 500) break
|
|
await new Promise((ok) => setTimeout(ok, 1000 * 2 ** k))
|
|
}
|
|
return { r, why }
|
|
}
|
|
let { r, why } = scene ? await call("/v1/scene") : { r: undefined as Response | undefined, why: "" }
|
|
if (scene && r?.status === 404) scene = false
|
|
if (!scene) ({ r, why } = await call("/v1/faces"))
|
|
if (!r?.ok) throw new Error(`the farm couldn't look at ${path.basename(file)}: ${why || r?.status}`)
|
|
const got = ((await r.json()) as { frames: (Record<string, unknown> | null)[] }).frames
|
|
for (const g of got) {
|
|
if (!g) {
|
|
outF.push(null)
|
|
outS.push(null)
|
|
continue
|
|
}
|
|
const fs = (g.faces as unknown[][]) ?? []
|
|
outF.push({ w: g.w as number, h: g.h as number, faces: fs.map((x) => (typeof x[4] === "string" ? [x[0], x[1], x[2], x[3], x[5]] : x) as number[]), seams: g.seams as Seam[] | undefined })
|
|
outS.push(scene && g.grid ? ({ w: g.w, h: g.h, grid: g.grid, things: g.things ?? [], faces: fs.map((x) => (typeof x[4] === "string" ? x : [x[0], x[1], x[2], x[3], "face", x[4]])) } as Scene) : null)
|
|
}
|
|
}
|
|
return { faces: outF, scenes: outS }
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true })
|
|
}
|
|
}
|
|
|
|
/** VIDEO's faces, two frames a second, by the farm */
|
|
export async function faces(file: string, f: typeof fetch = fetch): Promise<Faces[]> {
|
|
const url = process.env.RAEA_CLOUD_GPU_URL
|
|
const token = process.env.RAEA_CLOUD_GPU_TOKEN ?? ""
|
|
if (!url) throw new Error("the faces check needs the farm (RAEA_CLOUD_GPU_URL); this box has none set")
|
|
const dir = mkdtempSync(path.join(os.tmpdir(), "raea-frames-"))
|
|
try {
|
|
ffmpeg(["-v", "error", "-y", "-i", file, "-vf", `fps=${RATE},scale='if(gt(iw,ih),-2,${SIDE})':'if(gt(iw,ih),${SIDE},-2)'`, "-q:v", "4", path.join(dir, "f%05d.jpg")])
|
|
const jpgs = readdirSync(dir).filter((n) => n.endsWith(".jpg")).sort()
|
|
const out: Faces[] = []
|
|
for (let i = 0; i < jpgs.length; i += 300) {
|
|
const body = JSON.stringify({ frames: jpgs.slice(i, i + 300).map((n) => readFileSync(path.join(dir, n)).toString("base64")) })
|
|
let r: Response | undefined
|
|
let why = ""
|
|
for (let k = 0; k < 4; k++) {
|
|
r = await f(`${url.replace(/\/$/, "")}/v1/faces`, { method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, body, signal: AbortSignal.timeout(300_000) })
|
|
if (r.ok) break
|
|
why = `${r.status} ${(await r.text()).slice(0, 300)}`
|
|
if (r.status < 500) break
|
|
await new Promise((ok) => setTimeout(ok, 1000 * 2 ** k))
|
|
}
|
|
if (!r?.ok) throw new Error(`the farm couldn't look at ${path.basename(file)}: ${why}`)
|
|
out.push(...((await r.json()) as { frames: Faces[] }).frames)
|
|
}
|
|
return out
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true })
|
|
}
|
|
}
|
|
|
|
function flag(args: string[], name: string): string | undefined {
|
|
const i = args.indexOf(name)
|
|
return i >= 0 ? args[i + 1] : undefined
|
|
}
|
|
|
|
/** the manifest a render of VIDEO wrote: --manifest, or VIDEO.render.json beside it */
|
|
export const manifestOf = (video: string) => `${video.slice(0, video.length - path.extname(video).length)}.render.json`
|
|
|
|
/** `raea frames place FILE [--at S,...] [--box WxH]` */
|
|
async function place(a: string[]): Promise<number> {
|
|
const file = a.find((x, i) => !x.startsWith("-") && !["--at", "--box", "-o"].includes(a[i - 1] ?? ""))
|
|
if (!file || !existsSync(file)) {
|
|
console.error("usage: raea frames place FILE [--at S[,S...]] [--box WxH] [-o REPORT.json] (the clearest places in a take or an export for a box WxH in a 1080-wide frame; a caption, 840x110, by default)")
|
|
return 2
|
|
}
|
|
const box = (flag(a, "--box") ?? "840x110").split("x").map(Number) as [number, number]
|
|
if (box.length !== 2 || !box.every((v) => v > 0)) {
|
|
console.error("raea frames place: --box is WxH in a 1080-wide frame's pixels, like 840x110")
|
|
return 2
|
|
}
|
|
let at = flag(a, "--at")?.split(",").map(Number).filter(Number.isFinite)
|
|
if (!at?.length && !/\.(png|jpe?g|webp)$/i.test(file)) {
|
|
const r = spawnSync("ffprobe", ["-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", file], { encoding: "utf8" })
|
|
const len = Number.parseFloat(r.stdout) || 1
|
|
const n = Math.min(12, Math.max(1, Math.floor(len)))
|
|
at = Array.from({ length: n }, (_, i) => Math.round(((i + 0.5) * len * 100) / n) / 100)
|
|
}
|
|
const { scenes: ss } = await scenes(file, at)
|
|
if (!ss.some(Boolean)) {
|
|
console.error("raea frames place: the farm has no scene reader yet (POST /v1/scene): nothing to place by")
|
|
return 1
|
|
}
|
|
const rep = placeReport(file, ss, box)
|
|
console.log(`${path.basename(file)}${at?.length ? ` at ${at.join(", ")} s` : ""}: the clearest places for a ${box[0]}x${box[1]} box in a ${rep.frame?.join("x")} frame, best first`)
|
|
for (const p of rep.places) console.log(` left ${p.left} px, top ${p.top} px (${p.at}) cost ${p.cost}${p.covers.length ? ` covers ${p.covers.join(", ")}` : " clear"}`)
|
|
const o = flag(a, "-o")
|
|
if (o) writeFileSync(o, JSON.stringify(rep, null, 1))
|
|
return 0
|
|
}
|
|
|
|
export async function main(args: string[]): Promise<number> {
|
|
if (args[0] === "place") return place(args.slice(1))
|
|
const a = args[0] === "check" ? args.slice(1) : args
|
|
const video = a.find((x, i) => !x.startsWith("-") && !["--manifest", "-o"].includes(a[i - 1] ?? ""))
|
|
if (!video || !existsSync(video)) {
|
|
console.error("usage: raea frames check VIDEO [--manifest RENDER.json] [-o REPORT.json] (faces cut by the frame, and text too small to read)")
|
|
return 2
|
|
}
|
|
const m = flag(a, "--manifest") ?? manifestOf(video)
|
|
const found: Finding[] = []
|
|
let looked = 0
|
|
const man = existsSync(m) ? JSON.parse(readFileSync(m, "utf8")) : null
|
|
let placed: number | null = null
|
|
try {
|
|
const got = await scenes(video)
|
|
looked = got.faces.length
|
|
found.push(...cutFaces(got.faces))
|
|
if (man && got.scenes.some(Boolean)) {
|
|
const over = textOver(man, got.scenes, RATE)
|
|
placed = over.length
|
|
found.push(...over)
|
|
}
|
|
} catch (e) {
|
|
console.log(`WARN the faces check couldn't run: ${(e as Error).message}`)
|
|
}
|
|
const texts = man ? smallText(man) : null
|
|
if (texts) found.push(...texts)
|
|
const despite = `${video.slice(0, video.length - path.extname(video).length)}.despite.txt`
|
|
const why = existsSync(despite) ? readFileSync(despite, "utf8").trim() : ""
|
|
const out = found.map((x) => (why && x.kind === "face-cut" ? { ...x, level: "warn" as const, says: `${x.says} (despite: ${why.slice(0, 200)})` } : x)).sort((p, q) => (p.at ?? 0) - (q.at ?? 0))
|
|
for (const x of out) console.log(`${x.level === "error" ? "ERROR" : "WARN "} ${x.at === null ? "" : `${x.at.toFixed(1)} s `}${x.says}`)
|
|
const errors = out.filter((x) => x.level === "error").length
|
|
const report = flag(a, "-o") ?? `${video.slice(0, video.length - path.extname(video).length)}.frames-check.json`
|
|
writeFileSync(report, JSON.stringify({ video, looked_at: looked, text_checked: texts !== null, placement_checked: placed !== null, findings: out }, null, 1))
|
|
console.log(`${errors ? "FAILED" : "PASSED"} ${path.basename(video)}: ${looked} frames looked at for faces, ${texts === null ? "no manifest for its text" : `${new Set(texts.map((t) => t.says)).size} small texts`}, ${errors} errors, ${out.length - errors} warnings; ${path.basename(report)}`)
|
|
return errors ? 1 : 0
|
|
}
|
|
|
|
if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
|
|
const a = process.argv.slice(2)
|
|
main(a[0] === "frames" ? a.slice(1) : a).then(
|
|
(c) => process.exit(c),
|
|
(e) => (console.error(`raea frames: ${(e as Error).message}`), process.exit(1)),
|
|
)
|
|
}
|