mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-06 05:33:38 -10:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
94bb5e6f9c |
No files matched your search
@@ -1,7 +1,7 @@
|
||||
// pace.ts on made-up words and levels: a short run between two long pauses joins a neighbour; level evening measures
|
||||
// only clips with a second of speech and gives a short clip its own take's gain
|
||||
import { expect, test } from "bun:test";
|
||||
import { evenLevels, linesIn, pace, unsqueezed, type Levels } from "../src/pace";
|
||||
import { evenLevels, pace, type Levels } from "../src/pace";
|
||||
|
||||
/** levels at 10 ms: speech (-20 dB) inside each word, quiet (-70 dB) elsewhere, over `seconds` */
|
||||
function levels(ws: { from: number; to: number }[], seconds: number): Levels {
|
||||
@@ -66,15 +66,3 @@ test("a line stopping mid-sentence with no pause runs on to its sentence's end;
|
||||
const r = pace(long, l2, [[0, 2]], { hook: 0 });
|
||||
expect(r.pieces[0].to).toBeLessThanOrEqual(1.06 + 1 / 30);
|
||||
});
|
||||
|
||||
test("a phrase a transcriber heard twice and squeezed into one instant is dropped, and the line is found where it was said", () => {
|
||||
// 12-05, run 28: "Sign up for the news, sign up for the newsletter", with "up for the news sign" all at 10.32-10.34 s
|
||||
const w = (text: string, a: number, b: number) => ({ text, from: a / 1000, to: b / 1000 });
|
||||
const heard = [w("for", 9280, 9360), w("the", 9360, 9440), w("newsletter.", 9440, 9460), w("Sign", 10080, 10320), w("up", 10320, 10340), w("for", 10320, 10340), w("the", 10320, 10340), w("news", 10320, 10340), w("sign", 10320, 10340), w("up", 10320, 10400), w("for", 10400, 10560), w("the", 10560, 10640), w("newsletter", 10640, 10960)];
|
||||
const ws = unsqueezed(heard);
|
||||
expect(ws.map((x) => x.text).join(" ")).toBe("for the newsletter. Sign up for the newsletter");
|
||||
const { spans } = linesIn(ws, ["sign up for the newsletter"]);
|
||||
expect(ws[spans[0][0]].from).toBe(10.08);
|
||||
// two short words together are speech, kept
|
||||
expect(unsqueezed([w("a", 0, 20), w("cat", 20, 40), w("sat", 300, 500)]).length).toBe(3);
|
||||
});
|
||||
@@ -255,44 +255,6 @@ ${Array.from({ length: 40 }, (_, i) => `.u${i} { margin: ${i}px; }`).join("\n")}
|
||||
}
|
||||
});
|
||||
|
||||
// a take's jump cuts read through the ffmpeg CLI (cutplan.ts ffmpegCuts): where raea media cuts finds them
|
||||
test.skipIf(spawnSync("ffmpeg", ["-version"]).status !== 0)("raea cut reads a take's jump cuts through ffmpeg, where the engine finds them", async () => {
|
||||
const { ffmpegCuts } = await import("../src/cutplan.ts");
|
||||
const dir = mkdtempSync(join(tmpdir(), "raea-ffcuts-"));
|
||||
try {
|
||||
const color = (c: string) => ["-f", "lavfi", "-i", `color=c=${c}:s=320x180:r=30:d=2`];
|
||||
spawnSync("ffmpeg", ["-v", "error", "-y", ...color("red"), ...color("blue"), ...color("green"), "-filter_complex", "[0][1][2]concat=n=3:v=1:a=0", "-c:v", "libx264", "-pix_fmt", "yuv420p", join(dir, "t.mp4")]);
|
||||
expect(ffmpegCuts(join(dir, "t.mp4"), 0.08)).toEqual([2, 4]);
|
||||
expect(ffmpegCuts(join(dir, "missing.mp4"), 0.08)).toBeNull();
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
// raea cloud's footage/faces.json (prep's faces, found on its farm): perception.json takes each clip's face from it,
|
||||
// with no still and no raea-perceive (a box has none). A made clip and made-up faces, never real footage
|
||||
test.skipIf(spawnSync("ffmpeg", ["-version"]).status !== 0)("raea cut takes each clip's face from footage/faces.json", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "raea-cut-faces-"));
|
||||
const run = (...a: string[]) => spawnSync("bun", [join(here, "src/native.ts"), bin, ...a], { cwd: dir, encoding: "utf8", env: { ...process.env, RAEA_PERCEIVE: "/nonexistent/raea-perceive" } });
|
||||
try {
|
||||
mkdirSync(join(dir, "media"));
|
||||
mkdirSync(join(dir, "footage"));
|
||||
spawnSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "color=c=gray:s=160x120:r=30:d=4", "-c:v", "libx264", "-pix_fmt", "yuv420p", join(dir, "media/take01.mp4")]);
|
||||
const said: [string, number, number][] = [["Hello", 200, 600], ["there.", 650, 1000], ["This", 1500, 1700], ["is", 1750, 1850], ["the", 1900, 2000], ["second", 2100, 2500], ["line.", 2550, 2900]];
|
||||
writeFileSync(join(dir, "media/take01.words.json"), JSON.stringify(said.map(([text, start_ms, end_ms]) => ({ text, start_ms, end_ms }))));
|
||||
// no face by the first clip's middle (0.6 s, nearest 1 s); two at 2 s, by the second's (2.4 s): the larger is kept
|
||||
const face = [0.25, 0.25, 0.5, 0.5, 0.9];
|
||||
writeFileSync(join(dir, "footage/faces.json"), JSON.stringify({ take01: { w: 160, h: 120, t: [0, 1, 2, 3], s: [[], [], [[0.1, 0.1, 0.1, 0.1, 0.99], face], [face]] } }));
|
||||
const r = run("cut", "take01", "hello there", "the second line");
|
||||
expect(r.stderr).toBe("");
|
||||
const band = { safe: { top: 220, bottom: 1500 } };
|
||||
// 160x120 covering 1080x1920 is 16x, centred: x 0.25*160*16 - 740, y 0.25*120*16, w 0.5*160*16, h 0.5*120*16
|
||||
expect(JSON.parse(readFileSync(join(dir, "perception.json"), "utf8"))).toEqual({ "0": band, "33": { ...band, face: { x: -100, y: 480, w: 1280, h: 960 } } });
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
// a stock talking take (fixtures/CREDITS.md): the face branch of frame and perception.json. The footage fixtures are
|
||||
// the maintainers' (tools/public-first/private.paths), so a checkout without them skips this
|
||||
test.skipIf(!existsSync(join(raea, "engine/media/fixtures")))("raea frame and raea cut find the face in a talking take", () => {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// How raea cut times a take's lines: the take's light copy when raea cloud keeps one, its jump cuts (kept in raea's
|
||||
// cache) and sound level, and pace.ts's timing, kept off the take's own jump cuts. native.ts gives it the engine.
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
@@ -12,35 +11,6 @@ export type TakeIO = { at: (p: string) => string; engineRun: (a: string[]) => st
|
||||
|
||||
const ms = (s: number) => Math.round(s * 1000) / 1000;
|
||||
|
||||
/** A take's jump cuts read through the ffmpeg CLI where there is one: its luma at 160x90, every frame, scored as raea
|
||||
* media cuts scores them (ffmpeg's select scene: each frame's mean absolute difference from the last, less the
|
||||
* difference before it, over 100). ffmpeg decodes on every core; the engine's own decoder on a Linux box is openh264 on
|
||||
* one (12-05: 38 s for a 3.5-minute light copy, 66 s for a 6-minute one, against 4 s for ffmpeg), and a box step has
|
||||
* 3 minutes. The small picture leaves a hard cut's score where it was and a frame's grain out. null where ffmpeg
|
||||
* can't read it: the engine reads it then. */
|
||||
export function ffmpegCuts(file: string, threshold: number): number[] | null {
|
||||
const [w, h] = [160, 90];
|
||||
const r = spawnSync("ffmpeg", ["-v", "info", "-nostats", "-i", file, "-an", "-vf", `scale=${w}:${h}:flags=area,format=gray,showinfo`, "-f", "rawvideo", "-"], { maxBuffer: 1 << 30 });
|
||||
if (r.error || r.status !== 0 || !r.stdout?.length) return null;
|
||||
// each frame's time, from showinfo's lines in order
|
||||
const times = [...String(r.stderr).matchAll(/\bn:\s*\d+\s+pts:\s*-?\d+\s+pts_time:(-?[\d.]+)/g)].map((m) => Number(m[1]));
|
||||
const n = w * h;
|
||||
const frames = Math.floor(r.stdout.length / n);
|
||||
if (!frames || times.length !== frames) return null;
|
||||
const y = r.stdout as Buffer;
|
||||
const out: number[] = [];
|
||||
let prev = 0;
|
||||
for (let f = 1; f < frames; f++) {
|
||||
let sad = 0;
|
||||
for (let i = f * n, j = (f - 1) * n, e = i + n; i < e; i++, j++) sad += Math.abs(y[i] - y[j]);
|
||||
const mafd = sad / n;
|
||||
const score = Math.min(Math.max(Math.min(mafd, Math.abs(mafd - prev)) / 100, 0), 1);
|
||||
prev = mafd;
|
||||
if (score > threshold) out.push(ms(times[f]));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** cutPlan and takeLevels over `io`, each take's levels kept for the run */
|
||||
export function timing({ at, engineRun, engineJson, isAudio }: TakeIO) {
|
||||
/** The take to read for timing: its light copy when raea cloud keeps one (.raea-cloud/proxies/NAME.mp4 in the project,
|
||||
@@ -67,7 +37,7 @@ export function timing({ at, engineRun, engineJson, isAudio }: TakeIO) {
|
||||
} catch {
|
||||
kept = "";
|
||||
}
|
||||
const cuts = ffmpegCuts(file, 0.08) ?? (engineJson(["media", "cuts", file, "--threshold", "0.08"]).cuts as { at: number }[]).map((c) => c.at);
|
||||
const cuts = (engineJson(["media", "cuts", file, "--threshold", "0.08"]).cuts as { at: number }[]).map((c) => c.at);
|
||||
if (kept)
|
||||
try {
|
||||
mkdirSync(dirname(kept), { recursive: true });
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
// A take's faces from raea cloud's footage/faces.json, for raea cut's perception.json (native.ts perception)
|
||||
import { readFileSync } from "node:fs";
|
||||
import { basename, extname } from "node:path";
|
||||
|
||||
type Box = { x: number; y: number; w: number; h: number };
|
||||
|
||||
/** footage/faces.json in the project (raea cloud's prep: each take's faces about once a second, found on its farm, as
|
||||
* shares of the take's picture, [x, y, w, h, score] each), read once. A box has no raea-perceive, so a still per clip
|
||||
* found no face there and cost each `raea cut --append` every earlier clip's lookup again (12-05: a step of 13 cuts ran
|
||||
* past a box's 3 minutes, and no cut on a box had a face in its perception.json) */
|
||||
let facesKept: Record<string, { w?: number; h?: number; t?: number[]; s?: number[][][] }> | null | undefined;
|
||||
function facesFile() {
|
||||
if (facesKept === undefined)
|
||||
try {
|
||||
facesKept = JSON.parse(readFileSync(`${process.cwd()}/footage/faces.json`, "utf8"));
|
||||
} catch {
|
||||
facesKept = null;
|
||||
}
|
||||
return facesKept;
|
||||
}
|
||||
|
||||
/** The largest face at the sample of faces.json nearest `t` (never one more than 1.5 s away), with the take's size: a
|
||||
* face, none there (face undefined), or null when faces.json doesn't hold the take */
|
||||
export function faceKept(file: string, t: number): { face?: Box; sw: number; sh: number } | null {
|
||||
const take = facesFile()?.[basename(file, extname(file))];
|
||||
const ts = take?.t, ss = take?.s;
|
||||
if (!take || !Array.isArray(ts) || !Array.isArray(ss) || !ts.length || !take.w || !take.h) return null;
|
||||
let k = 0;
|
||||
ts.forEach((x, i) => {
|
||||
if (Math.abs(x - t) < Math.abs(ts[k] - t)) k = i;
|
||||
});
|
||||
if (Math.abs(ts[k] - t) > 1.5) return null;
|
||||
const faces = (ss[k] ?? []).filter((f) => Array.isArray(f) && f.length >= 4);
|
||||
if (!faces.length) return { sw: take.w, sh: take.h };
|
||||
const [x, y, w, h] = faces.reduce((a, b) => (b[2] * b[3] > a[2] * a[3] ? b : a));
|
||||
return { face: { x, y, w, h }, sw: take.w, sh: take.h };
|
||||
}
|
||||
@@ -10,9 +10,8 @@ import { basename, delimiter, dirname, extname } from "node:path";
|
||||
import { brandKit, pixelsOf } from "./brand";
|
||||
import { readSite, stylesOf } from "./scrape";
|
||||
import { wordsOf } from "./heard";
|
||||
import { evenLevels, unsqueezed, linesIn, markJumps, speechOf, toSound, type PaceOptions } from "./pace";
|
||||
import { evenLevels, markJumps, speechOf, toSound, type PaceOptions } from "./pace";
|
||||
import { timing } from "./cutplan";
|
||||
import { faceKept } from "./faces";
|
||||
import { harness, library, show, task } from "./app";
|
||||
import { fromCwd } from "./show";
|
||||
import { clothesHost } from "./wardrobe";
|
||||
@@ -91,11 +90,9 @@ function words(take: string, hear = false): Word[] {
|
||||
if (!hear) return [];
|
||||
hearTake(take, file);
|
||||
}
|
||||
return unsqueezed(
|
||||
(JSON.parse(readFileSync(file, "utf8")) as { text: string; start_ms: number; end_ms: number }[])
|
||||
.filter((w) => w.text?.trim())
|
||||
.map((w) => ({ text: w.text.trim(), from: w.start_ms / 1000, to: w.end_ms / 1000 })),
|
||||
);
|
||||
return (JSON.parse(readFileSync(file, "utf8")) as { text: string; start_ms: number; end_ms: number }[])
|
||||
.filter((w) => w.text?.trim())
|
||||
.map((w) => ({ text: w.text.trim(), from: w.start_ms / 1000, to: w.end_ms / 1000 }));
|
||||
}
|
||||
|
||||
/** A tool a clean Mac lacks, found or installed once by raea's tools/ensure.sh (docs/app.feature AP6), which sits in
|
||||
@@ -278,10 +275,8 @@ function perception(edit: { width: number; height: number; fps: number }, clips:
|
||||
if (!seen.has(key)) {
|
||||
let face: Box | undefined;
|
||||
try {
|
||||
// faces.json's when it holds the take, else a still of it read here
|
||||
const kept = faceKept(c.file, mid);
|
||||
const f = kept ? kept.face : faceAt(c.file, mid);
|
||||
const { sw, sh } = kept ?? probeOf(c.file);
|
||||
const f = faceAt(c.file, mid);
|
||||
const { sw, sh } = probeOf(c.file);
|
||||
if (f && sw && sh) {
|
||||
const s = Math.max(edit.width / sw, edit.height / sh);
|
||||
const [ox, oy] = [(edit.width - sw * s) / 2, (edit.height - sh * s) / 2];
|
||||
|
||||
@@ -18,23 +18,6 @@
|
||||
|
||||
/** a word in its take's seconds */
|
||||
export type Word = { text: string; from: number; to: number };
|
||||
|
||||
/** A transcript's words less the runs it squeezed: three or more words each SQUEEZED s long or less, all starting within
|
||||
* 50 ms of the first. No one says three words in 50 ms; a transcriber writes them when it hears a phrase twice and
|
||||
* times the copy at one instant (12-05, run 28: Body Shop's "Sign up for the news, sign up for the newsletter" came
|
||||
* as "Sign up for the news sign up for the newsletter" with "up for the news sign" all at 10.32-10.34 s, raea cut took
|
||||
* the line from that "sign", and the CTA clip started inside "up") */
|
||||
export const SQUEEZED = 0.03;
|
||||
export function unsqueezed<W extends Word>(ws: W[]): W[] {
|
||||
const out: W[] = [];
|
||||
for (let i = 0; i < ws.length; ) {
|
||||
let j = i;
|
||||
while (j < ws.length && ws[j].to - ws[j].from <= SQUEEZED && ws[j].from - ws[i].from <= 0.05) j++;
|
||||
if (j - i >= 3) i = j;
|
||||
else out.push(ws[i++]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
/** a take's sound level every 10 ms (dB), from `start` seconds */
|
||||
export type Levels = { start: number; db: number[] };
|
||||
/** a stretch of quiet in a take: from its first quiet 10 ms to the first sound after it (s) */
|
||||
|
||||
Reference in new issue
Block a user