Compare commits

..
Author SHA1 Message Date
shaneandshinyobjectz 28d89f8ffd Goal check: montage reads frames from a file, and keeps its findings beside the video (#301)
* The montage pack reads a video's frames from a file, not down a pipe

13 MB of small frames through the pipe took 9.7 s of a 35 s cut's read on a cloud box; into a file, 4.6 s.

* A video's montage findings are kept beside it, measured again when it or its beats change

Reading a 35 s cut's frames took 7 s each time the goal was checked, on files that hadn't changed.

---------

Co-authored-by: shinyobjectz <[email protected]>
2026-12-01 21:40:48 -08:00
shaneandshinyobjectz 7b8317764a A made video's floor report is kept beside it, measured again only for cause (#300)
The measure took 26 s a cut and ran again on the same file at publish and at done. A kept report with an error is always measured again: what it names can be put right without the video changing.

Co-authored-by: shinyobjectz <[email protected]>
2026-12-01 20:59:21 -08:00
4 changed files with 61 additions and 10 deletions

No files matched your search

+1 -1
View File
@@ -22,7 +22,7 @@ const NOT_WORK = new Set(["review", ".git", "node_modules", ".raea", "target", "
/** what a check keeps beside a file it read (its report, its words, its scene): a reading of the work, not a change
* to it. A done check that heard a cut's words wrote them beside it, and the critic's verdict of a minute before read
* as older than the work (12-01) */
const READING = /\.(check|words|scene|floor)\.json$/
const READING = /\.(check|words|scene|floor|montage)\.json$/
/** when the work last changed (ms): the newest file under dir, its own bookkeeping left out (a bounded walk) */
export function newest(dir: string): number | null {
+41 -8
View File
@@ -13,6 +13,9 @@
// eye, plane and tone are warnings at each cut, in Murch's order; one becomes an error when it breaks at more than half
// of a file's cuts (a habit, not a choice). A cut on a word is the sound pack's (edit-on-sound), not this one's.
import { spawnSync } from "node:child_process"
import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import path from "node:path"
import { check, found } from "raea/check"
import type { Beat } from "raea/brief.gen"
import type { FileFacts } from "../facts.ts"
@@ -42,9 +45,18 @@ export type Finding = { level: "error" | "warn"; kind: "pace" | "tempo" | "eye"
export function read(file: string, fps: number, width: number, height: number): Read {
const w = READ_W
const h = Math.max(2, Math.round((READ_W * height) / width / 2) * 2)
const r = spawnSync("ffmpeg", ["-v", "error", "-i", file, "-vf", `scale=${w}:${h},format=rgb24`, "-f", "rawvideo", "-"], { maxBuffer: 1 << 30 })
if (r.status !== 0) throw new Error(`ffmpeg couldn't read ${file}: ${String(r.stderr).trim().slice(-200)}`)
const buf = r.stdout as Buffer
// into a file, not down a pipe: 13 MB of frames through the pipe took 9.7 s of a 35 s cut's read on a cloud box, and
// 4.6 s into a file (12-01)
const dir = mkdtempSync(path.join(tmpdir(), "raea-montage-read-"))
let buf: Buffer
try {
const raw = path.join(dir, "frames.raw")
const r = spawnSync("ffmpeg", ["-v", "error", "-an", "-i", file, "-vf", `scale=${w}:${h},format=rgb24`, "-f", "rawvideo", raw], { encoding: "utf8" })
if (r.status !== 0) throw new Error(`ffmpeg couldn't read ${file}: ${String(r.stderr).trim().slice(-200)}`)
buf = readFileSync(raw)
} finally {
rmSync(dir, { recursive: true, force: true })
}
const size = w * h * 3
const frames: Frame[] = []
for (let o = 0; o + size <= buf.length; o += size) {
@@ -235,6 +247,25 @@ export function acrossCuts(r: Read, cuts: number[], o: { faces?: Faces[]; rate?:
return out
}
/** a video's montage findings, kept beside it */
export const MONTAGE_EXT = "montage.json"
/** A video's findings from `measure`, kept beside it (FILE.montage.json) and measured again only when the video is
* newer or what they were measured against (`given`: its beats, its scene reading) is another. Reading a 35 s cut's
* frames took 7 s each time the goal was checked, on files that hadn't changed (12-01). */
export function keptOr(abs: string, given: string, measure: () => Finding[]): Finding[] {
const kept = `${abs.replace(/\.[^./]+$/, "")}.${MONTAGE_EXT}`
try {
const k = JSON.parse(readFileSync(kept, "utf8")) as { given: string; findings: Finding[] }
if (statSync(kept).mtimeMs >= statSync(abs).mtimeMs && k.given === given) return k.findings
} catch {}
const findings = measure()
try {
writeFileSync(kept, `${JSON.stringify({ given, findings })}\n`)
} catch {}
return findings
}
const where = ([x, y]: [number, number]) => `${y < 0.34 ? "top" : y > 0.66 ? "bottom" : "middle"} ${x < 0.34 ? "left" : x > 0.66 ? "right" : "centre"}`
export default check<FileFacts>(
@@ -244,9 +275,6 @@ export default check<FileFacts>(
const c = f.contract?.kind === "cut" ? f.contract.cut : undefined
const plan = c ? cutOf(c, f.path) : null
const beats = plan?.plan.beats ?? []
const r = read(f.abs, f.fps, f.width, f.height)
if (r.frames.length < 2) return
const cuts = cutsOf(r)
let faces: Faces[] | undefined
let rate = 0
try {
@@ -258,7 +286,12 @@ export default check<FileFacts>(
if (!(e instanceof Unmet)) throw e
}
const say = (x: Finding) => found({ level: x.level, says: x.says, seconds: x.at === null ? undefined : x.until === undefined ? x.at : [x.at, x.until] })
for (const x of pace(beats, cuts, r.fps, r.frames.length / r.fps)) say(x)
for (const x of acrossCuts(r, cuts, { faces, rate, beatsAt: beats.map((b) => b.at) })) say(x)
for (const x of keptOr(f.abs, JSON.stringify({ beats, faces: faces?.length ?? null, rate }), () => {
const r = read(f.abs, f.fps, f.width!, f.height!)
if (r.frames.length < 2) return []
const cuts = cutsOf(r)
return [...pace(beats, cuts, r.fps, r.frames.length / r.fps), ...acrossCuts(r, cuts, { faces, rate, beatsAt: beats.map((b) => b.at) })]
}))
say(x)
},
)
+18
View File
@@ -4,6 +4,7 @@ import { mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import path from "node:path"
import { floorOf, type Measured } from "../src/packs/floor.ts"
import { keptOr } from "../src/packs/montage.ts"
test("a floor report is kept: measured once, again when the video is newer, and always while it holds an error", () => {
const d = mkdtempSync(path.join(tmpdir(), "raea-floor-kept-"))
@@ -32,3 +33,20 @@ test("a floor report is kept: measured once, again when the video is newer, and
expect(runs).toBe(4)
rmSync(d, { recursive: true, force: true })
})
test("montage findings are kept: measured again when the video is newer or its beats are others", () => {
const d = mkdtempSync(path.join(tmpdir(), "raea-montage-kept-"))
const v = path.join(d, "a.mp4")
writeFileSync(v, "x")
let runs = 0
const by = () => (runs++, [{ level: "warn" as const, kind: "pace" as const, at: 1, says: "long" }])
keptOr(v, "a", by)
expect(keptOr(v, "a", by)).toEqual([{ level: "warn", kind: "pace", at: 1, says: "long" }])
expect(runs).toBe(1)
keptOr(v, "b", by)
expect(runs).toBe(2)
utimesSync(v, new Date(Date.now() + 60_000), new Date(Date.now() + 60_000))
keptOr(v, "b", by)
expect(runs).toBe(3)
rmSync(d, { recursive: true, force: true })
})
+1 -1
View File
@@ -87,7 +87,7 @@ test("a reading kept beside a file isn't the work changing", () => {
const cut = path.join(d, "out/cuts/a_b_c_v1_9x16.mp4")
writeFileSync(cut, "x")
utimesSync(cut, 1000, 1000)
for (const ext of ["words.json", "scene.json", "check.json", "floor.json"]) writeFileSync(path.join(d, `out/cuts/a_b_c_v1_9x16.${ext}`), "{}")
for (const ext of ["words.json", "scene.json", "check.json", "floor.json", "montage.json"]) writeFileSync(path.join(d, `out/cuts/a_b_c_v1_9x16.${ext}`), "{}")
expect(newest(d)).toBe(1_000_000)
writeFileSync(path.join(d, "out/cuts/a_b_c_v1_9x16.render.json"), "{}")
expect(newest(d)!).toBeGreaterThan(1_000_000)