@open-take/compositor 0.4.2 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +203 -132
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +611 -251
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/audit-cursor.ts +487 -0
- package/src/index.ts +15 -8
- package/src/render.ts +5 -1
- package/src/types.ts +7 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-take/compositor",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "Polish compositor (D3) — event log + captured frames -> polished mp4 + editable revideo composition.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"funding": "https://ko-fi.com/pascal18663",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@revideo/core": "0.11.0",
|
|
24
24
|
"@revideo/ui": "0.11.0",
|
|
25
25
|
"@revideo/vite-plugin": "0.11.0",
|
|
26
|
-
"@open-take/revideo-renderer": "0.4.
|
|
26
|
+
"@open-take/revideo-renderer": "0.4.4"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "^22.10.1",
|
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
// auditCursor: verify the DRAWN cursor in a finished mp4 against the
|
|
2
|
+
// compositor's own INTENT. No vision model and no duplicated geometry — the
|
|
3
|
+
// engine already knows where it meant to draw the pointer (math.buildLegs /
|
|
4
|
+
// cursorPos / stageCamera give the tip in the finished frame), so the audit
|
|
5
|
+
// only has to LOOK there: the cursor polygon is rasterised at that beat's
|
|
6
|
+
// on-screen scale and matched by masked normalised cross-correlation in a
|
|
7
|
+
// ±SEARCH_WIN_PX window around the expected tip. Field-validated: a 1% skew
|
|
8
|
+
// is caught while 22/22 beats of good footage pass clean (median error
|
|
9
|
+
// 0.21px, ncc ≈ 0.83).
|
|
10
|
+
//
|
|
11
|
+
// The pixel-independent parts (template, NCC search, tip prediction, issue
|
|
12
|
+
// building) are pure and exported for tests; only auditCursor itself spawns
|
|
13
|
+
// ffmpeg (one grayscale frame per beat, piped raw — no image decoder dep).
|
|
14
|
+
|
|
15
|
+
import { spawn } from "node:child_process";
|
|
16
|
+
import { resolveFfmpeg } from "./ffmpeg";
|
|
17
|
+
import { buildLegs, cursorPos, stageCamera } from "./math";
|
|
18
|
+
import type { Pt, TakeComposition } from "./types";
|
|
19
|
+
import type { CompositionIssue } from "./validate";
|
|
20
|
+
|
|
21
|
+
/** The pointer polygon, tip at (0,0), stage units. Copied from
|
|
22
|
+
* src/scene/scene.tsx (CURSOR) — the scene is compiled by revideo's vite and
|
|
23
|
+
* exports nothing, so the vertex list is duplicated here ON PURPOSE: any
|
|
24
|
+
* drift between the drawn cursor and the audited one shows up in review. */
|
|
25
|
+
export const CURSOR_POLYGON: readonly (readonly [number, number])[] = [
|
|
26
|
+
[0, 0],
|
|
27
|
+
[0, 17],
|
|
28
|
+
[4.5, 13],
|
|
29
|
+
[7.5, 19.5],
|
|
30
|
+
[10, 18.3],
|
|
31
|
+
[7, 11.7],
|
|
32
|
+
[12, 11.7],
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
/** supersample factor for the template rasteriser */
|
|
36
|
+
const SS = 4;
|
|
37
|
+
/** template pixels below this mask weight do not score */
|
|
38
|
+
const MASK_MIN = 0.15;
|
|
39
|
+
/** search radius around the expected tip, output px */
|
|
40
|
+
const SEARCH_WIN_PX = 45;
|
|
41
|
+
/** Below this the template never locked onto a cursor at all — a different
|
|
42
|
+
* failure from "matched but offset". Measured with the Python prototype's
|
|
43
|
+
* LANCZOS template: ncc 0.825 on a clean take, 0.611 at 8% skew, 0.421 at
|
|
44
|
+
* 13% (the shipped-to-a-PR failure). Re-measured with THIS module's box
|
|
45
|
+
* downsample on a real 10-beat draft render: all 9 pointer beats found at
|
|
46
|
+
* 0.00px error, ncc 0.626–0.944 — the 0.5 floor holds for this template. */
|
|
47
|
+
const NCC_MATCH_MIN = 0.5;
|
|
48
|
+
/** the scene's cursor body fill, rgb(20,20,24) ≈ 20/255 gray */
|
|
49
|
+
const BODY_GRAY = 20 / 255;
|
|
50
|
+
|
|
51
|
+
const DEFAULT_SAMPLE_AFTER_MS = 500;
|
|
52
|
+
const DEFAULT_TOL_PX = 6;
|
|
53
|
+
|
|
54
|
+
export type AuditCursorOpts = {
|
|
55
|
+
/** ms after a beat's landing to sample (clamped inside the beat's hold) */
|
|
56
|
+
sampleAfterMs?: number;
|
|
57
|
+
/** max tolerated tip error, VIEWPORT px (default 6; good takes median 0.21) */
|
|
58
|
+
tolPx?: number;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type AuditRow = {
|
|
62
|
+
/** event index */
|
|
63
|
+
beat: number;
|
|
64
|
+
/** sample instant in the DELIVERED video (startMs head trim applied), s */
|
|
65
|
+
tSec: number;
|
|
66
|
+
/** expected cursor-tip position in the finished frame, output px */
|
|
67
|
+
expect: Pt;
|
|
68
|
+
/** best-match tip position, output px (absent when unverifiable) */
|
|
69
|
+
found?: Pt;
|
|
70
|
+
/** tip error in viewport px (output-px error / camera scale) */
|
|
71
|
+
errViewportPx?: number;
|
|
72
|
+
/** masked NCC score of the best match */
|
|
73
|
+
ncc?: number;
|
|
74
|
+
/** the expected tip is not in the delivered frame — unverifiable, never an error */
|
|
75
|
+
offscreen?: boolean;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export type GrayFrame = { data: Float32Array; width: number; height: number };
|
|
79
|
+
|
|
80
|
+
export type CursorTemplate = {
|
|
81
|
+
width: number;
|
|
82
|
+
height: number;
|
|
83
|
+
/** grayscale sprite, 0..1 (dark body, white outline) */
|
|
84
|
+
pixels: Float32Array;
|
|
85
|
+
/** score only where the sprite is — the patch around it is page content */
|
|
86
|
+
mask: Float32Array;
|
|
87
|
+
/** local coords of the cursor TIP inside the template */
|
|
88
|
+
tip: Pt;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/** Rasterise the cursor sprite at its on-screen scale (cursor.scale × camera
|
|
92
|
+
* scale): supersampled scanline polygon fill + a distance-field outline
|
|
93
|
+
* stroke, box-downsampled — matching how the scene draws it (dark body,
|
|
94
|
+
* 2 stage-px white stroke) closely enough for NCC to lock on. */
|
|
95
|
+
export function makeCursorTemplate(scale: number): CursorTemplate {
|
|
96
|
+
const pad = 3;
|
|
97
|
+
const width = Math.ceil(12 * scale) + 6 + pad; // polygon extents 12 × 19.5
|
|
98
|
+
const height = Math.ceil(19.5 * scale) + 6 + pad;
|
|
99
|
+
const sW = width * SS;
|
|
100
|
+
const sH = height * SS;
|
|
101
|
+
const pts = CURSOR_POLYGON.map(
|
|
102
|
+
([x, y]) => [(x * scale + pad / 2) * SS, (y * scale + pad / 2) * SS] as const,
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
// even-odd scanline fill at supersample resolution
|
|
106
|
+
const fill = new Float32Array(sW * sH);
|
|
107
|
+
for (let y = 0; y < sH; y++) {
|
|
108
|
+
const yc = y + 0.5;
|
|
109
|
+
const xs: number[] = [];
|
|
110
|
+
for (let i = 0; i < pts.length; i++) {
|
|
111
|
+
const [x1, y1] = pts[i]!;
|
|
112
|
+
const [x2, y2] = pts[(i + 1) % pts.length]!;
|
|
113
|
+
if (y1 <= yc !== y2 <= yc) xs.push(x1 + ((yc - y1) * (x2 - x1)) / (y2 - y1));
|
|
114
|
+
}
|
|
115
|
+
xs.sort((a, b) => a - b);
|
|
116
|
+
for (let k = 0; k + 1 < xs.length; k += 2) {
|
|
117
|
+
const x0 = Math.max(0, Math.ceil(xs[k]! - 0.5));
|
|
118
|
+
const x1 = Math.min(sW - 1, Math.floor(xs[k + 1]! - 0.5));
|
|
119
|
+
for (let x = x0; x <= x1; x++) fill[y * sW + x] = 1;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// outline stroke: the scene draws lineWidth 2 in stage units, so on screen
|
|
124
|
+
// it is ~2·camScale ≈ `scale` px at the shipped cursor.scale of 2 — the
|
|
125
|
+
// same approximation the field-validated prototype used.
|
|
126
|
+
const half = Math.max(2, Math.round(scale * SS)) / 2;
|
|
127
|
+
const strk = new Float32Array(sW * sH);
|
|
128
|
+
for (let i = 0; i < pts.length; i++) {
|
|
129
|
+
const [ax, ay] = pts[i]!;
|
|
130
|
+
const [bx, by] = pts[(i + 1) % pts.length]!;
|
|
131
|
+
const dx = bx - ax;
|
|
132
|
+
const dy = by - ay;
|
|
133
|
+
const len2 = dx * dx + dy * dy || 1;
|
|
134
|
+
const x0 = Math.max(0, Math.floor(Math.min(ax, bx) - half - 1));
|
|
135
|
+
const x1 = Math.min(sW - 1, Math.ceil(Math.max(ax, bx) + half + 1));
|
|
136
|
+
const y0 = Math.max(0, Math.floor(Math.min(ay, by) - half - 1));
|
|
137
|
+
const y1 = Math.min(sH - 1, Math.ceil(Math.max(ay, by) + half + 1));
|
|
138
|
+
for (let y = y0; y <= y1; y++) {
|
|
139
|
+
for (let x = x0; x <= x1; x++) {
|
|
140
|
+
const px = x + 0.5 - ax;
|
|
141
|
+
const py = y + 0.5 - ay;
|
|
142
|
+
const u = Math.max(0, Math.min(1, (px * dx + py * dy) / len2));
|
|
143
|
+
const ex = px - u * dx;
|
|
144
|
+
const ey = py - u * dy;
|
|
145
|
+
if (ex * ex + ey * ey <= half * half) strk[y * sW + x] = 1;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// box-downsample SS× to the final template
|
|
151
|
+
const down = (src: Float32Array): Float32Array => {
|
|
152
|
+
const out = new Float32Array(width * height);
|
|
153
|
+
for (let y = 0; y < height; y++) {
|
|
154
|
+
for (let x = 0; x < width; x++) {
|
|
155
|
+
let s = 0;
|
|
156
|
+
for (let sy = 0; sy < SS; sy++)
|
|
157
|
+
for (let sx = 0; sx < SS; sx++) s += src[(y * SS + sy) * sW + x * SS + sx]!;
|
|
158
|
+
out[y * width + x] = s / (SS * SS);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return out;
|
|
162
|
+
};
|
|
163
|
+
const f = down(fill);
|
|
164
|
+
const s = down(strk);
|
|
165
|
+
const pixels = new Float32Array(width * height);
|
|
166
|
+
const mask = new Float32Array(width * height);
|
|
167
|
+
for (let i = 0; i < pixels.length; i++) {
|
|
168
|
+
const interior = Math.min(1, Math.max(0, f[i]! - s[i]!));
|
|
169
|
+
pixels[i] = interior * BODY_GRAY + s[i]!; // dark body, white outline
|
|
170
|
+
mask[i] = Math.min(1, interior + s[i]!);
|
|
171
|
+
}
|
|
172
|
+
return { width, height, pixels, mask, tip: { x: pad / 2, y: pad / 2 } };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Masked-NCC search over a ±win px window around the expected tip. Returns
|
|
176
|
+
* the best score and the tip position it was found at; `found` is absent
|
|
177
|
+
* only when no template placement fits inside the frame at all. */
|
|
178
|
+
export function findCursor(
|
|
179
|
+
frame: GrayFrame,
|
|
180
|
+
expect: Pt,
|
|
181
|
+
scale: number,
|
|
182
|
+
win = SEARCH_WIN_PX,
|
|
183
|
+
): { ncc: number; found?: Pt } {
|
|
184
|
+
const tpl = makeCursorTemplate(scale);
|
|
185
|
+
const { width: fw, height: fh, data } = frame;
|
|
186
|
+
// The template vector is constant across placements: hoist the mask index
|
|
187
|
+
// list (as frame-row offsets) and the zero-meaned template values.
|
|
188
|
+
const offs: number[] = [];
|
|
189
|
+
const b: number[] = [];
|
|
190
|
+
for (let ty = 0; ty < tpl.height; ty++) {
|
|
191
|
+
for (let tx = 0; tx < tpl.width; tx++) {
|
|
192
|
+
const j = ty * tpl.width + tx;
|
|
193
|
+
if (tpl.mask[j]! > MASK_MIN) {
|
|
194
|
+
offs.push(ty * fw + tx);
|
|
195
|
+
b.push(tpl.pixels[j]!);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const n = offs.length;
|
|
200
|
+
if (n < 12) return { ncc: -1 };
|
|
201
|
+
let bMean = 0;
|
|
202
|
+
for (const v of b) bMean += v;
|
|
203
|
+
bMean /= n;
|
|
204
|
+
let db2 = 0;
|
|
205
|
+
for (let j = 0; j < n; j++) {
|
|
206
|
+
b[j] = b[j]! - bMean;
|
|
207
|
+
db2 += b[j]! * b[j]!;
|
|
208
|
+
}
|
|
209
|
+
const db = Math.sqrt(db2);
|
|
210
|
+
if (db < 1e-6) return { ncc: -1 };
|
|
211
|
+
|
|
212
|
+
let best = -2;
|
|
213
|
+
let found: Pt | undefined;
|
|
214
|
+
for (let dy = -win; dy <= win; dy++) {
|
|
215
|
+
for (let dx = -win; dx <= win; dx++) {
|
|
216
|
+
const x0 = Math.round(expect.x + dx - tpl.tip.x);
|
|
217
|
+
const y0 = Math.round(expect.y + dy - tpl.tip.y);
|
|
218
|
+
if (x0 < 0 || y0 < 0 || x0 + tpl.width > fw || y0 + tpl.height > fh) continue;
|
|
219
|
+
const base = y0 * fw + x0;
|
|
220
|
+
// Σb = 0, so cov = Σab and var(a) = Σa² − (Σa)²/n — one pass per patch.
|
|
221
|
+
let sa = 0;
|
|
222
|
+
let saa = 0;
|
|
223
|
+
let sab = 0;
|
|
224
|
+
for (let j = 0; j < n; j++) {
|
|
225
|
+
const a = data[base + offs[j]!]!;
|
|
226
|
+
sa += a;
|
|
227
|
+
saa += a * a;
|
|
228
|
+
sab += a * b[j]!;
|
|
229
|
+
}
|
|
230
|
+
const da = Math.sqrt(Math.max(0, saa - (sa * sa) / n));
|
|
231
|
+
const score = da < 1e-6 ? -1 : sab / (da * db);
|
|
232
|
+
if (score > best) {
|
|
233
|
+
best = score;
|
|
234
|
+
found = { x: expect.x + dx, y: expect.y + dy };
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return { ncc: best, found };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export type PredictedTip = {
|
|
242
|
+
/** event index */
|
|
243
|
+
beat: number;
|
|
244
|
+
/** sample instant on the composition (capture) timeline, ms */
|
|
245
|
+
sampleMs: number;
|
|
246
|
+
/** the same instant in the DELIVERED video (startMs head trim applied), s */
|
|
247
|
+
tSec: number;
|
|
248
|
+
/** expected cursor-tip position in the finished frame, output px */
|
|
249
|
+
expect: Pt;
|
|
250
|
+
/** camera scale at the sample instant (output px per video px) */
|
|
251
|
+
camScale: number;
|
|
252
|
+
/** the tip is not in the delivered frame (zoomed out of frame, or the head
|
|
253
|
+
* trim cut the sample) — unverifiable, never an error */
|
|
254
|
+
offscreen: boolean;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
/** Where the compositor MEANT to draw the pointer tip, per pointer-landing
|
|
258
|
+
* beat, in finished-frame px — via the engine's own public math, so there is
|
|
259
|
+
* no second geometry to drift. */
|
|
260
|
+
export function predictCursorTips(
|
|
261
|
+
comp: TakeComposition,
|
|
262
|
+
sampleAfterMs = DEFAULT_SAMPLE_AFTER_MS,
|
|
263
|
+
): PredictedTip[] {
|
|
264
|
+
const legs = buildLegs(comp);
|
|
265
|
+
const cam = stageCamera(comp);
|
|
266
|
+
const { width: oW, height: oH, fps } = comp.output;
|
|
267
|
+
const startMs = comp.startMs ?? 0;
|
|
268
|
+
const frameMs = 1000 / fps;
|
|
269
|
+
const out: PredictedTip[] = [];
|
|
270
|
+
for (const [beat, e] of comp.events.entries()) {
|
|
271
|
+
if (!e.point) continue; // hand-edited JSON can drop it; nothing to verify then
|
|
272
|
+
if (e.kind === "press" && !e.bbox) continue; // bare press: cursor parked, no target
|
|
273
|
+
// Sample inside the beat's HOLD: after the landing (the ripple has fired,
|
|
274
|
+
// the camera settle is all but done) but before the cursor departs again —
|
|
275
|
+
// the next travel leg, or this beat's own drag stroke. Legs are t0-ordered,
|
|
276
|
+
// so the first departure past tMs is the binding one.
|
|
277
|
+
let sampleMs = e.tMs + sampleAfterMs;
|
|
278
|
+
for (const l of legs) {
|
|
279
|
+
const departMs = l.t0 * 1000;
|
|
280
|
+
// strict >: a rushed succession clamps the next leg's t0 to EXACTLY this
|
|
281
|
+
// landing (math.ts buildLegs), and that departure is still binding — a
|
|
282
|
+
// "+1" grace would sample mid-flight on the skipped leg, converting
|
|
283
|
+
// cursor speed into ~17px of false position error at 60fps.
|
|
284
|
+
if (departMs > e.tMs) {
|
|
285
|
+
sampleMs = Math.min(sampleMs, departMs - frameMs);
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
sampleMs = Math.max(Math.min(sampleMs, comp.durationMs - frameMs), e.tMs);
|
|
290
|
+
const tS = sampleMs / 1000;
|
|
291
|
+
const c = cursorPos(tS, legs, comp); // video px
|
|
292
|
+
const k = cam.at(tS);
|
|
293
|
+
// scene geometry: a video-px point lands at (p − camera center)·scale + out/2
|
|
294
|
+
const expect: Pt = {
|
|
295
|
+
x: (c.x - k.center.x) * k.scale + oW / 2,
|
|
296
|
+
y: (c.y - k.center.y) * k.scale + oH / 2,
|
|
297
|
+
};
|
|
298
|
+
const tSec = (sampleMs - startMs) / 1000;
|
|
299
|
+
const offscreen = tSec < 0 || expect.x < 0 || expect.x >= oW || expect.y < 0 || expect.y >= oH;
|
|
300
|
+
out.push({ beat, sampleMs, tSec, expect, camScale: k.scale, offscreen });
|
|
301
|
+
}
|
|
302
|
+
return out;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const median = (xs: number[]): number => {
|
|
306
|
+
const s = xs.slice().sort((a, b) => a - b);
|
|
307
|
+
const m = s.length >> 1;
|
|
308
|
+
return s.length % 2 ? s[m]! : (s[m - 1]! + s[m]!) / 2;
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
const signed = (v: number): string => `${v >= 0 ? "+" : ""}${v.toFixed(1)}`;
|
|
312
|
+
|
|
313
|
+
/** Turn measured rows into issues (pure — unit-testable without a video). */
|
|
314
|
+
export function auditIssues(rows: AuditRow[], tolPx = DEFAULT_TOL_PX): CompositionIssue[] {
|
|
315
|
+
const issues: CompositionIssue[] = [];
|
|
316
|
+
const offscreen = rows.filter((r) => r.offscreen);
|
|
317
|
+
if (offscreen.length) {
|
|
318
|
+
const names = offscreen
|
|
319
|
+
.map((r) => `events[${r.beat}] (tip at ${r.expect.x.toFixed(0)},${r.expect.y.toFixed(0)})`)
|
|
320
|
+
.join(", ");
|
|
321
|
+
issues.push({
|
|
322
|
+
severity: "warn",
|
|
323
|
+
path: "events",
|
|
324
|
+
message: `${offscreen.length} beat${offscreen.length === 1 ? "" : "s"} cannot be audited — the zoom pushed the expected cursor tip out of the output frame: ${names}`,
|
|
325
|
+
fix: "check those beats by eye; to make them auditable, pull zoom.center toward the pointer or lower zoom.scale so the pointer stays in frame",
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
const checked = rows.filter((r) => !r.offscreen && r.ncc != null && r.found);
|
|
329
|
+
const unmatched = checked.filter((r) => (r.ncc ?? -1) < NCC_MATCH_MIN);
|
|
330
|
+
const offsetFails = checked.filter(
|
|
331
|
+
(r) => (r.ncc ?? -1) >= NCC_MATCH_MIN && (r.errViewportPx ?? 0) > tolPx,
|
|
332
|
+
);
|
|
333
|
+
if (!unmatched.length && !offsetFails.length) return issues;
|
|
334
|
+
const parts = [
|
|
335
|
+
...offsetFails.map(
|
|
336
|
+
(r) =>
|
|
337
|
+
`events[${r.beat}] drawn ${r.errViewportPx!.toFixed(1)}px off (ncc ${r.ncc!.toFixed(2)} — the cursor matched, at the wrong place)`,
|
|
338
|
+
),
|
|
339
|
+
...unmatched.map(
|
|
340
|
+
(r) =>
|
|
341
|
+
`events[${r.beat}] never matched (best ncc ${r.ncc!.toFixed(2)} < ${NCC_MATCH_MIN} — no cursor found near the expected tip, so its offset means nothing)`,
|
|
342
|
+
),
|
|
343
|
+
];
|
|
344
|
+
// A shared offset across the failing beats is the classic failure (the
|
|
345
|
+
// every-cursor-13%-off release): a renderer↔math transform skew, not
|
|
346
|
+
// per-beat noise — say so, with the measured vector.
|
|
347
|
+
let fix =
|
|
348
|
+
"re-render and re-audit; a repeat means the renderer disagrees with math.cursorPos/stageCamera — the fix is in the renderer, no composition edit moves the drawn cursor";
|
|
349
|
+
if (offsetFails.length >= 2) {
|
|
350
|
+
const vecs = offsetFails.map((r) => {
|
|
351
|
+
const dx = r.found!.x - r.expect.x;
|
|
352
|
+
const dy = r.found!.y - r.expect.y;
|
|
353
|
+
const errOut = Math.hypot(dx, dy);
|
|
354
|
+
const k = errOut > 0 ? (r.errViewportPx ?? 0) / errOut : 0; // output px → viewport px
|
|
355
|
+
return { x: dx * k, y: dy * k };
|
|
356
|
+
});
|
|
357
|
+
const mx = median(vecs.map((v) => v.x));
|
|
358
|
+
const my = median(vecs.map((v) => v.y));
|
|
359
|
+
const spread = Math.max(...vecs.map((v) => Math.hypot(v.x - mx, v.y - my)));
|
|
360
|
+
if (spread <= 2)
|
|
361
|
+
fix = `every failing beat shares one offset ≈ (${signed(mx)}, ${signed(my)}) viewport px — a systematic renderer↔math skew; fix the cursor transform in the renderer and re-render (no composition edit moves the drawn cursor)`;
|
|
362
|
+
}
|
|
363
|
+
issues.push({
|
|
364
|
+
severity: "error",
|
|
365
|
+
path: "events",
|
|
366
|
+
message: `the drawn cursor misses the composition's own intent on ${offsetFails.length + unmatched.length} of ${checked.length} verifiable beats (tolerance ${tolPx}px): ${parts.join("; ")}`,
|
|
367
|
+
fix,
|
|
368
|
+
});
|
|
369
|
+
return issues;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Decode ONE frame of the delivered mp4 at tSec to raw grayscale pixels,
|
|
373
|
+
* piped straight from ffmpeg — no image-decoding dependency. */
|
|
374
|
+
function extractGrayFrame(
|
|
375
|
+
ffmpeg: string,
|
|
376
|
+
mp4Path: string,
|
|
377
|
+
tSec: number,
|
|
378
|
+
width: number,
|
|
379
|
+
height: number,
|
|
380
|
+
): Promise<GrayFrame> {
|
|
381
|
+
return new Promise((res, rej) => {
|
|
382
|
+
const child = spawn(
|
|
383
|
+
ffmpeg,
|
|
384
|
+
[
|
|
385
|
+
"-hide_banner",
|
|
386
|
+
"-v",
|
|
387
|
+
"error",
|
|
388
|
+
"-ss",
|
|
389
|
+
tSec.toFixed(4),
|
|
390
|
+
"-i",
|
|
391
|
+
mp4Path,
|
|
392
|
+
"-frames:v",
|
|
393
|
+
"1",
|
|
394
|
+
"-f",
|
|
395
|
+
"rawvideo",
|
|
396
|
+
"-pix_fmt",
|
|
397
|
+
"gray",
|
|
398
|
+
"pipe:1",
|
|
399
|
+
],
|
|
400
|
+
{ stdio: ["ignore", "pipe", "pipe"] },
|
|
401
|
+
);
|
|
402
|
+
const chunks: Buffer[] = [];
|
|
403
|
+
let errText = "";
|
|
404
|
+
child.stdout.on("data", (d: Buffer) => chunks.push(d));
|
|
405
|
+
child.stderr.on("data", (d) => {
|
|
406
|
+
errText += d;
|
|
407
|
+
});
|
|
408
|
+
child.on("error", rej);
|
|
409
|
+
child.on("close", (code) => {
|
|
410
|
+
const bytes = Buffer.concat(chunks);
|
|
411
|
+
const need = width * height;
|
|
412
|
+
if (code !== 0)
|
|
413
|
+
return rej(
|
|
414
|
+
new Error(
|
|
415
|
+
`ffmpeg exited ${code} extracting the ${tSec.toFixed(2)}s frame from ${mp4Path}${errText.trim() ? `: ${errText.trim()}` : ""}`,
|
|
416
|
+
),
|
|
417
|
+
);
|
|
418
|
+
// EXACTLY one gray frame: a longer payload means the mp4's true
|
|
419
|
+
// dimensions exceed the composition's — reinterpreting its first
|
|
420
|
+
// oW·oH bytes with the wrong row stride would garble the image and
|
|
421
|
+
// turn every beat into a false "never matched" renderer defect.
|
|
422
|
+
if (bytes.length !== need)
|
|
423
|
+
return rej(
|
|
424
|
+
new Error(
|
|
425
|
+
`ffmpeg returned ${bytes.length} bytes for the ${tSec.toFixed(2)}s frame — expected one ${width}x${height} gray frame (${need} bytes); is ${mp4Path} the delivered render of this composition?`,
|
|
426
|
+
),
|
|
427
|
+
);
|
|
428
|
+
const data = new Float32Array(need);
|
|
429
|
+
for (let i = 0; i < need; i++) data[i] = bytes[i]! / 255;
|
|
430
|
+
res({ data, width, height });
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** Audit the drawn cursor of a delivered mp4 against the composition that
|
|
436
|
+
* rendered it. One row per pointer-landing beat; issues in the
|
|
437
|
+
* validateComposition shape ("error" = the render is wrong, "warn" = a beat
|
|
438
|
+
* the audit cannot see). */
|
|
439
|
+
export async function auditCursor(
|
|
440
|
+
comp: TakeComposition,
|
|
441
|
+
mp4Path: string,
|
|
442
|
+
opts: AuditCursorOpts = {},
|
|
443
|
+
): Promise<{ rows: AuditRow[]; issues: CompositionIssue[] }> {
|
|
444
|
+
const tolPx = opts.tolPx ?? DEFAULT_TOL_PX;
|
|
445
|
+
const preds = predictCursorTips(comp, opts.sampleAfterMs ?? DEFAULT_SAMPLE_AFTER_MS);
|
|
446
|
+
if (!preds.length)
|
|
447
|
+
// an empty verdict must not read as "audit clean" — nothing was opened,
|
|
448
|
+
// nothing was verified
|
|
449
|
+
return {
|
|
450
|
+
rows: [],
|
|
451
|
+
issues: [
|
|
452
|
+
{
|
|
453
|
+
severity: "warn",
|
|
454
|
+
path: "events",
|
|
455
|
+
message:
|
|
456
|
+
"no pointer-landing beats to audit — the drawn cursor was never verified on this take",
|
|
457
|
+
},
|
|
458
|
+
],
|
|
459
|
+
};
|
|
460
|
+
const ffmpeg = await resolveFfmpeg();
|
|
461
|
+
const { width: oW, height: oH } = comp.output;
|
|
462
|
+
const rows: AuditRow[] = [];
|
|
463
|
+
for (const p of preds) {
|
|
464
|
+
if (p.offscreen) {
|
|
465
|
+
rows.push({ beat: p.beat, tSec: p.tSec, expect: p.expect, offscreen: true });
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
const frame = await extractGrayFrame(ffmpeg, mp4Path, p.tSec, oW, oH);
|
|
469
|
+
const { ncc, found } = findCursor(frame, p.expect, comp.cursor.scale * p.camScale);
|
|
470
|
+
if (!found) {
|
|
471
|
+
// every placement in the search window fell off the frame edge — as
|
|
472
|
+
// unverifiable as an offscreen tip, and reported with them
|
|
473
|
+
rows.push({ beat: p.beat, tSec: p.tSec, expect: p.expect, offscreen: true });
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
const errOutPx = Math.hypot(found.x - p.expect.x, found.y - p.expect.y);
|
|
477
|
+
rows.push({
|
|
478
|
+
beat: p.beat,
|
|
479
|
+
tSec: p.tSec,
|
|
480
|
+
expect: p.expect,
|
|
481
|
+
found,
|
|
482
|
+
errViewportPx: errOutPx / p.camScale,
|
|
483
|
+
ncc,
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
return { rows, issues: auditIssues(rows, tolPx) };
|
|
487
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -7,16 +7,23 @@
|
|
|
7
7
|
// Edit `comp` (zoom decisions, framing, cursor) and re-render — the
|
|
8
8
|
// composition is the editable source of truth.
|
|
9
9
|
|
|
10
|
-
export
|
|
11
|
-
|
|
10
|
+
export {
|
|
11
|
+
type AuditCursorOpts,
|
|
12
|
+
type AuditRow,
|
|
13
|
+
auditCursor,
|
|
14
|
+
auditIssues,
|
|
15
|
+
predictCursorTips,
|
|
16
|
+
} from "./audit-cursor";
|
|
17
|
+
export { type Beat, directCamera, type Framing } from "./camera";
|
|
12
18
|
export { resolveFfmpeg, resolveFfprobe } from "./ffmpeg";
|
|
13
|
-
export
|
|
14
|
-
export {
|
|
15
|
-
export
|
|
19
|
+
export * as math from "./math";
|
|
20
|
+
export { type PlanOpts, planComposition } from "./plan";
|
|
21
|
+
export * from "./presets";
|
|
22
|
+
export { type RenderTakeOpts, type RenderTakeResult, renderTake } from "./render";
|
|
23
|
+
export * from "./types";
|
|
16
24
|
export {
|
|
17
|
-
validateComposition,
|
|
18
|
-
formatIssues,
|
|
19
25
|
type CompositionIssue,
|
|
26
|
+
formatIssues,
|
|
20
27
|
type ValidateOpts,
|
|
28
|
+
validateComposition,
|
|
21
29
|
} from "./validate";
|
|
22
|
-
export * as math from "./math";
|
package/src/render.ts
CHANGED
|
@@ -95,6 +95,10 @@ export type RenderTakeResult = {
|
|
|
95
95
|
* into empty space, a press whose zoom departs before the keypress, a tail
|
|
96
96
|
* that delivers a frozen screen. Empty when `skipValidate` is set. */
|
|
97
97
|
warnings: CompositionIssue[];
|
|
98
|
+
/** the composition this render actually used — callers that judge the
|
|
99
|
+
* output (post-shoot gates) must judge THIS object, not a re-read of
|
|
100
|
+
* composition.json, which a concurrent editor save may have moved on. */
|
|
101
|
+
composition: TakeComposition;
|
|
98
102
|
};
|
|
99
103
|
|
|
100
104
|
function run(cmd: string, args: string[]): Promise<void> {
|
|
@@ -508,7 +512,7 @@ async function renderTakeExclusive(opts: RenderTakeOpts): Promise<RenderTakeResu
|
|
|
508
512
|
await writeFile(compositionPath, JSON.stringify(persisted, null, 2));
|
|
509
513
|
}
|
|
510
514
|
|
|
511
|
-
return { mp4Path: resolve(opts.outPath), compositionPath, warnings };
|
|
515
|
+
return { mp4Path: resolve(opts.outPath), compositionPath, warnings, composition };
|
|
512
516
|
} finally {
|
|
513
517
|
await cleanupScratch(scratch);
|
|
514
518
|
}
|
package/src/types.ts
CHANGED
|
@@ -164,9 +164,15 @@ export type CaptureLog = {
|
|
|
164
164
|
tEndMs?: number;
|
|
165
165
|
/** Steps the capture DROPPED (target not found / endpoint unresolved) — the
|
|
166
166
|
* take still completes, but a missing beat must reach the end-of-run
|
|
167
|
-
* summary and (
|
|
167
|
+
* summary and (strict by default) the exit code, not just an early stderr line
|
|
168
168
|
* buried under render progress. `step` is the plan's 0-based step index. */
|
|
169
169
|
skipped?: { step: number; action: string; target?: string; reason: string }[];
|
|
170
|
+
/** Pre-capture target-resolution findings (runtime/src/precheck.ts): every
|
|
171
|
+
* plan target resolved against the page as loaded, with the capture's own
|
|
172
|
+
* locator semantics, BEFORE the screencast started. Errors abort the
|
|
173
|
+
* capture, so only warnings ever reach a written log — kept here so a take
|
|
174
|
+
* on disk records what the gate already said about its plan. */
|
|
175
|
+
precheck?: { severity: "error" | "warn"; path: string; message: string; fix?: string }[];
|
|
170
176
|
/** Beats whose editorial hold ran out before the PAGE had finished — the
|
|
171
177
|
* capture kept waiting (see runtime/src/settle.ts) and records how much
|
|
172
178
|
* longer it needed. This is the measurement that replaces guessing
|