@open-take/runtime 0.1.0 → 0.1.2
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/{cdp-capture-CWFbnK9C.js → cdp-capture-BkVH_3Uv.js} +15 -9
- package/dist/cdp-capture-BkVH_3Uv.js.map +1 -0
- package/dist/cdp-capture-rF3RlyKG.js +3 -0
- package/dist/index.d.ts +23 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +893 -754
- package/dist/index.js.map +1 -1
- package/editor-dist/assets/OFL.txt +222 -0
- package/editor-dist/assets/{index-vf4XvOcQ.css → index-C5jr6lU8.css} +1 -1
- package/editor-dist/assets/index-CzKiDamc.js +49 -0
- package/editor-dist/index.html +2 -2
- package/package.json +2 -2
- package/dist/cdp-capture-0qW8oOct.js +0 -3
- package/dist/cdp-capture-CWFbnK9C.js.map +0 -1
- package/editor-dist/assets/index-W0-YQO1h.js +0 -49
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { captureTake, captureTakeCDP, ensureChrome, inspectPage, resolveChrome } from "./cdp-capture-
|
|
2
|
-
import { appendFile, copyFile, mkdir, mkdtemp, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
1
|
+
import { captureTake, captureTakeCDP, ensureChrome, inspectPage, resolveChrome } from "./cdp-capture-BkVH_3Uv.js";
|
|
2
|
+
import { appendFile, copyFile, mkdir, mkdtemp, readFile, readdir, rename, rm, stat, unlink, writeFile } from "node:fs/promises";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { basename, dirname, extname, join, normalize, resolve } from "node:path";
|
|
5
5
|
import { FINISH, LOOKS, MOTION, ZOOM_LEVELS, finishName, formatIssues, formatIssues as formatIssues$1, lookName, motionName, planComposition, renderTake, resolveFfmpeg, validateComposition, validateComposition as validateComposition$1, zoomLevelName } from "@open-take/compositor";
|
|
@@ -70,819 +70,957 @@ async function stagePrev(mp4Path, prevPath) {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
//#endregion
|
|
73
|
-
//#region src/
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
".mjs": "text/javascript; charset=utf-8",
|
|
78
|
-
".css": "text/css; charset=utf-8",
|
|
79
|
-
".json": "application/json; charset=utf-8",
|
|
80
|
-
".svg": "image/svg+xml",
|
|
81
|
-
".png": "image/png",
|
|
82
|
-
".jpg": "image/jpeg",
|
|
83
|
-
".ico": "image/x-icon",
|
|
84
|
-
".woff2": "font/woff2",
|
|
85
|
-
".woff": "font/woff",
|
|
86
|
-
".map": "application/json; charset=utf-8"
|
|
87
|
-
};
|
|
88
|
-
/** Locate the built editor SPA: a bundled copy (published package) first, then
|
|
89
|
-
* the monorepo build. */
|
|
90
|
-
function resolveEditorDist() {
|
|
91
|
-
const here = fileURLToPath(import.meta.url);
|
|
92
|
-
const pkgRoot = resolve(here, "..", "..");
|
|
93
|
-
const candidates = [resolve(pkgRoot, "editor-dist"), resolve(pkgRoot, "..", "..", "apps", "editor", "dist")];
|
|
94
|
-
return candidates.find((c) => existsHtml(c)) ?? null;
|
|
95
|
-
}
|
|
96
|
-
function existsHtml(dir) {
|
|
97
|
-
return existsFile(join(dir, "index.html"));
|
|
98
|
-
}
|
|
99
|
-
function existsFile(path) {
|
|
73
|
+
//#region src/review.ts
|
|
74
|
+
/** Sibling capture log (`<capture>.json`), if present — enables the
|
|
75
|
+
* capture-lock check; silently absent otherwise (matches index.ts). */
|
|
76
|
+
async function loadLogSibling(capturePath) {
|
|
100
77
|
try {
|
|
101
|
-
return
|
|
78
|
+
return JSON.parse(await readFile(resolve(capturePath).replace(/\.mp4$/i, ".json"), "utf8"));
|
|
102
79
|
} catch {
|
|
103
|
-
return
|
|
80
|
+
return void 0;
|
|
104
81
|
}
|
|
105
82
|
}
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
83
|
+
const KIND_LABEL = {
|
|
84
|
+
click: "click",
|
|
85
|
+
type: "type",
|
|
86
|
+
drag: "drag",
|
|
87
|
+
scroll: "scroll",
|
|
88
|
+
hover: "hover",
|
|
89
|
+
press: "key"
|
|
113
90
|
};
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (size > capBytes) throw new Error("request body too large");
|
|
120
|
-
chunks.push(c);
|
|
121
|
-
}
|
|
122
|
-
return Buffer.concat(chunks).toString("utf8");
|
|
91
|
+
function beatTitle(e) {
|
|
92
|
+
if (e.label) return e.label;
|
|
93
|
+
if (e.kind === "type" && e.text) return `"${e.text}"`;
|
|
94
|
+
if (e.kind === "press" && e.keys) return e.keys;
|
|
95
|
+
return e.kind;
|
|
123
96
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
97
|
+
const trunc = (s, n) => s.length > n ? `${s.slice(0, n - 1)}…` : s;
|
|
98
|
+
function fmtTime(ms) {
|
|
99
|
+
const s = Math.max(0, Math.round(ms / 1e3));
|
|
100
|
+
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
|
|
101
|
+
}
|
|
102
|
+
/** "zoom medium (×1.5)" | "zoom ×1.85 (custom)" | "wide" */
|
|
103
|
+
function zoomText(e) {
|
|
104
|
+
if (!e.zoom.enabled) return "wide";
|
|
105
|
+
const name = zoomLevelName(e.zoom.scale);
|
|
106
|
+
const x = `×${Number(e.zoom.scale.toFixed(2))}`;
|
|
107
|
+
return name ? `zoom ${name} (${x})` : `zoom ${x} (custom)`;
|
|
108
|
+
}
|
|
109
|
+
function buildBeatSheet(comp, name) {
|
|
110
|
+
const n = comp.events.length;
|
|
111
|
+
const look = lookName(comp.framing) ?? "custom";
|
|
112
|
+
const pace = motionName(comp.cursor) ?? "custom";
|
|
113
|
+
const finish = finishName(comp.motionBlur) ?? "custom";
|
|
114
|
+
const lines = [`${name} · ${(comp.durationMs / 1e3).toFixed(1)}s · ${n} beat${n === 1 ? "" : "s"} · look ${look} · pace ${pace} · finish ${finish}`];
|
|
115
|
+
const first = comp.events[0];
|
|
116
|
+
if (!first || first.zoom.inAtMs > 400) lines.push(` in 0:00 full view, cursor glides in`);
|
|
117
|
+
comp.events.forEach((e, i) => {
|
|
118
|
+
const num = String(i + 1).padStart(2, " ");
|
|
119
|
+
const kind = KIND_LABEL[e.kind].padEnd(6, " ");
|
|
120
|
+
const title = trunc(beatTitle(e), 24).padEnd(25, " ");
|
|
121
|
+
lines.push(` ${num} ${fmtTime(e.tMs)} ${kind} ${title} ${zoomText(e)}`);
|
|
122
|
+
});
|
|
123
|
+
const last = comp.events[n - 1];
|
|
124
|
+
if (last) {
|
|
125
|
+
const endMs = last.tMs + (last.durationMs ?? 0) + comp.cursor.holdMs;
|
|
126
|
+
lines.push(` out ${fmtTime(endMs)} zoom releases, holds to ${fmtTime(comp.durationMs)}`);
|
|
130
127
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
128
|
+
lines.push(`say it like: "beat 2: no zoom" · "tighter on beat 3" · "look: slate" · "pace: brisk"`);
|
|
129
|
+
return lines.join("\n");
|
|
130
|
+
}
|
|
131
|
+
const SAY_IT_CARD = `SAY IT, I'LL CUT IT
|
|
132
|
+
instant (~10s) zoom off / tighter / looser on a beat · re-center a beat (name the
|
|
133
|
+
element) · hold longer · pace: calm / natural / brisk · look:
|
|
134
|
+
midnight / ink / slate / ocean / plum / ember / paper / plain ·
|
|
135
|
+
finish: smooth / crisp / heavy · quicker intro · shorter tail
|
|
136
|
+
re-shoot (~1m) change what's clicked or typed · add / cut / reorder beats ·
|
|
137
|
+
retime an action
|
|
138
|
+
compare taste questions come back as an A/B reel — answer with a letter
|
|
139
|
+
("B"), or "between A and B", or "none, go darker"`;
|
|
140
|
+
/** Badge windows: each beat owns [its zoom.inAtMs, the next beat's) — the pill
|
|
141
|
+
* swaps as the camera starts moving toward the beat. INTRO/TAIL bracket them. */
|
|
142
|
+
function buildBadges(comp) {
|
|
143
|
+
const n = comp.events.length;
|
|
144
|
+
const badges = [];
|
|
145
|
+
let prevStart = 0;
|
|
146
|
+
const starts = comp.events.map((e) => {
|
|
147
|
+
const s = Math.max(e.zoom.inAtMs, prevStart + 1);
|
|
148
|
+
prevStart = s;
|
|
149
|
+
return s;
|
|
150
|
+
});
|
|
151
|
+
if (n === 0 || starts[0] > 400) badges.push({
|
|
152
|
+
fromMs: 0,
|
|
153
|
+
toMs: starts[0] ?? comp.durationMs,
|
|
154
|
+
text: "INTRO"
|
|
155
|
+
});
|
|
156
|
+
comp.events.forEach((e, i) => {
|
|
157
|
+
const last = i === n - 1;
|
|
158
|
+
const endOwn = e.tMs + (e.durationMs ?? 0) + comp.cursor.holdMs;
|
|
159
|
+
const to = last ? Math.min(endOwn, comp.durationMs) : starts[i + 1];
|
|
160
|
+
badges.push({
|
|
161
|
+
fromMs: starts[i],
|
|
162
|
+
toMs: to,
|
|
163
|
+
text: `BEAT ${i + 1}/${n} · ${KIND_LABEL[e.kind]} ${trunc(beatTitle(e), 24)} · ${zoomText(e)}`
|
|
147
164
|
});
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
} else {
|
|
153
|
-
res.writeHead(200, {
|
|
154
|
-
"Content-Length": st.size,
|
|
155
|
-
"Accept-Ranges": "bytes",
|
|
156
|
-
"Content-Type": type
|
|
165
|
+
if (last && to < comp.durationMs + 1e3) badges.push({
|
|
166
|
+
fromMs: to,
|
|
167
|
+
toMs: comp.durationMs + 1500,
|
|
168
|
+
text: "TAIL"
|
|
157
169
|
});
|
|
158
|
-
|
|
159
|
-
|
|
170
|
+
});
|
|
171
|
+
return badges;
|
|
160
172
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
173
|
+
/** Draft-quality transform: 30fps cap + motion blur OFF — up to ~12× faster
|
|
174
|
+
* than a blurred 60fps master, close enough for composition-level notes.
|
|
175
|
+
* (2 blur samples would render 2× the frames for a tmix window that rounds to
|
|
176
|
+
* 1 — all cost, zero blur.) FEEL questions (blur, pace, zoom silkiness) must
|
|
177
|
+
* be judged on full quality — use `ab`. */
|
|
178
|
+
function toDraft(comp) {
|
|
179
|
+
const { motionBlur: _mb,...rest } = comp;
|
|
180
|
+
return {
|
|
181
|
+
...rest,
|
|
182
|
+
output: {
|
|
183
|
+
...comp.output,
|
|
184
|
+
fps: Math.min(30, comp.output.fps)
|
|
185
|
+
}
|
|
186
|
+
};
|
|
170
187
|
}
|
|
171
|
-
async function
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
188
|
+
async function renderReview(take, opts = {}) {
|
|
189
|
+
await requireTakeFiles(take, { capture: true });
|
|
190
|
+
const comp = JSON.parse(await readFile(take.compositionPath, "utf8"));
|
|
191
|
+
const captureLog = await loadLogSibling(take.capturePath);
|
|
192
|
+
const chromePath = await ensureChrome(opts.chromePath);
|
|
193
|
+
const decorated = {
|
|
194
|
+
...toDraft(comp),
|
|
195
|
+
review: {
|
|
196
|
+
watermark: "REVIEW",
|
|
197
|
+
badges: buildBadges(comp)
|
|
181
198
|
}
|
|
182
199
|
};
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
200
|
+
await renderTake({
|
|
201
|
+
composition: decorated,
|
|
202
|
+
videoPath: take.capturePath,
|
|
203
|
+
outPath: take.reviewPath,
|
|
204
|
+
captureLog,
|
|
205
|
+
logProgress: opts.logProgress ?? true,
|
|
206
|
+
chromePath,
|
|
207
|
+
writeCompositionSibling: false
|
|
208
|
+
});
|
|
209
|
+
return {
|
|
210
|
+
reviewPath: take.reviewPath,
|
|
211
|
+
sheet: buildBeatSheet(comp, take.name)
|
|
190
212
|
};
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
}
|
|
262
|
-
if (path === "/api/render" && method === "POST") {
|
|
263
|
-
if (job?.status === "running") {
|
|
264
|
-
sendJson(res, 409, { error: "a render is already in progress" });
|
|
265
|
-
return;
|
|
266
|
-
}
|
|
267
|
-
const comp = JSON.parse(await readBody(req));
|
|
268
|
-
const captureLog = await loadCaptureLog() ?? void 0;
|
|
269
|
-
const issues = validateComposition$1(comp, captureLog ? { captureLog } : {});
|
|
270
|
-
const errors = issues.filter((i) => i.severity === "error");
|
|
271
|
-
if (errors.length) {
|
|
272
|
-
sendJson(res, 400, { issues });
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
await persistComposition(comp);
|
|
276
|
-
const id = `${Date.now().toString(36)}-${++jobSeq}`;
|
|
277
|
-
job = {
|
|
278
|
-
id,
|
|
279
|
-
progress: 0,
|
|
280
|
-
status: "running",
|
|
281
|
-
clients: new Set()
|
|
282
|
-
};
|
|
283
|
-
sendJson(res, 200, { jobId: id });
|
|
284
|
-
runRender(job, comp, captureLog);
|
|
285
|
-
return;
|
|
213
|
+
}
|
|
214
|
+
function parseSetFlag(set, beat) {
|
|
215
|
+
const eq = set.indexOf("=");
|
|
216
|
+
if (eq < 1) throw new Error(`ab: --set expects <knob>=<v1>,<v2>[,<v3>] (got "${set}")`);
|
|
217
|
+
const key = set.slice(0, eq).trim();
|
|
218
|
+
const values = set.slice(eq + 1).split(",").map((v) => v.trim()).filter(Boolean);
|
|
219
|
+
if (values.length < 1) throw new Error(`ab: --set ${key}= needs at least one value`);
|
|
220
|
+
if (values.length > 3) throw new Error(`ab: at most 3 requested values (plus current) per reel`);
|
|
221
|
+
if (key === "zoom") {
|
|
222
|
+
if (!beat) throw new Error(`ab: --set zoom=… needs --beat <n> (which beat to reframe)`);
|
|
223
|
+
return {
|
|
224
|
+
knob: {
|
|
225
|
+
kind: "zoom",
|
|
226
|
+
beat
|
|
227
|
+
},
|
|
228
|
+
values
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
if (key === "look" || key === "pace" || key === "finish") return {
|
|
232
|
+
knob: { kind: key },
|
|
233
|
+
values
|
|
234
|
+
};
|
|
235
|
+
if (key.includes(".")) return {
|
|
236
|
+
knob: {
|
|
237
|
+
kind: "path",
|
|
238
|
+
path: key
|
|
239
|
+
},
|
|
240
|
+
values
|
|
241
|
+
};
|
|
242
|
+
throw new Error(`ab: unknown knob "${key}" — use zoom (with --beat), look, pace, finish, or a raw dot-path like cursor.holdMs`);
|
|
243
|
+
}
|
|
244
|
+
function setPath(comp, path, raw) {
|
|
245
|
+
const t = raw.trim();
|
|
246
|
+
const value = t === "true" ? true : t === "false" ? false : t === "null" ? null : Number.isFinite(Number(raw)) && t !== "" ? Number(raw) : raw;
|
|
247
|
+
const parts = path.split(".");
|
|
248
|
+
const next = structuredClone(comp);
|
|
249
|
+
let node = next;
|
|
250
|
+
for (const p of parts.slice(0, -1)) {
|
|
251
|
+
const child = node[p];
|
|
252
|
+
if (typeof child !== "object" || child == null) throw new Error(`ab: path "${path}" — "${p}" is not an object`);
|
|
253
|
+
node = child;
|
|
254
|
+
}
|
|
255
|
+
node[parts[parts.length - 1]] = value;
|
|
256
|
+
return next;
|
|
257
|
+
}
|
|
258
|
+
/** Current-state description for a knob (the implicit "keep it" variant). */
|
|
259
|
+
function currentDesc(comp, knob) {
|
|
260
|
+
if (knob.kind === "zoom") {
|
|
261
|
+
const e = comp.events[knob.beat - 1];
|
|
262
|
+
return e ? zoomText(e) : "?";
|
|
263
|
+
}
|
|
264
|
+
if (knob.kind === "look") return lookName(comp.framing) ?? "custom look";
|
|
265
|
+
if (knob.kind === "pace") return motionName(comp.cursor) ?? "custom pace";
|
|
266
|
+
if (knob.kind === "finish") return finishName(comp.motionBlur) ?? "custom finish";
|
|
267
|
+
const parts = knob.path.split(".");
|
|
268
|
+
let node = comp;
|
|
269
|
+
for (const p of parts) node = node?.[p];
|
|
270
|
+
return `${knob.path}=${JSON.stringify(node)}`;
|
|
271
|
+
}
|
|
272
|
+
function applyKnob(comp, knob, value) {
|
|
273
|
+
if (knob.kind === "zoom") {
|
|
274
|
+
const i = knob.beat - 1;
|
|
275
|
+
const e = comp.events[i];
|
|
276
|
+
if (!e) throw new Error(`ab: --beat ${knob.beat} out of range (1-${comp.events.length})`);
|
|
277
|
+
const events = comp.events.slice();
|
|
278
|
+
if (value === "off" || value === "wide") events[i] = {
|
|
279
|
+
...e,
|
|
280
|
+
zoom: {
|
|
281
|
+
...e.zoom,
|
|
282
|
+
enabled: false
|
|
286
283
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
Connection: "keep-alive"
|
|
298
|
-
});
|
|
299
|
-
emit(res, "progress", { progress: job.progress });
|
|
300
|
-
if (job.status === "done") {
|
|
301
|
-
emit(res, "done", { mp4Url: job.mp4Url });
|
|
302
|
-
res.end();
|
|
303
|
-
return;
|
|
304
|
-
}
|
|
305
|
-
if (job.status === "error") {
|
|
306
|
-
emit(res, "failed", { message: job.error });
|
|
307
|
-
res.end();
|
|
308
|
-
return;
|
|
284
|
+
};
|
|
285
|
+
else {
|
|
286
|
+
const scale = value in ZOOM_LEVELS ? ZOOM_LEVELS[value] : Number(value);
|
|
287
|
+
if (!Number.isFinite(scale)) throw new Error(`ab: zoom value "${value}" — use off/light/medium/tight/close or a number`);
|
|
288
|
+
events[i] = {
|
|
289
|
+
...e,
|
|
290
|
+
zoom: {
|
|
291
|
+
...e.zoom,
|
|
292
|
+
enabled: true,
|
|
293
|
+
scale
|
|
309
294
|
}
|
|
310
|
-
|
|
311
|
-
req.on("close", () => job?.clients.delete(res));
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
if (path.startsWith("/api/")) {
|
|
315
|
-
sendJson(res, 404, { error: "no such endpoint" });
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
if (method !== "GET") {
|
|
319
|
-
sendJson(res, 405, { error: "method not allowed" });
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
const rel = normalize(decodeURIComponent(path)).replace(/^(\.\.[/\\])+/, "");
|
|
323
|
-
let filePath = join(dist, rel === "/" ? "index.html" : rel);
|
|
324
|
-
if (!filePath.startsWith(dist)) {
|
|
325
|
-
sendJson(res, 403, { error: "forbidden" });
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
328
|
-
const fst = await stat(filePath).catch(() => null);
|
|
329
|
-
if (!fst?.isFile()) filePath = join(dist, "index.html");
|
|
330
|
-
await serveFile(req, res, filePath);
|
|
331
|
-
} catch (err) {
|
|
332
|
-
sendJson(res, 500, { error: err instanceof Error ? err.message : String(err) });
|
|
295
|
+
};
|
|
333
296
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
});
|
|
348
|
-
j.status = "done";
|
|
349
|
-
j.progress = 1;
|
|
350
|
-
j.mp4Url = `/api/take/output?v=${Date.now().toString(36)}`;
|
|
351
|
-
for (const c of j.clients) {
|
|
352
|
-
emit(c, "done", { mp4Url: j.mp4Url });
|
|
353
|
-
c.end();
|
|
297
|
+
return {
|
|
298
|
+
...comp,
|
|
299
|
+
events
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
if (knob.kind === "look") {
|
|
303
|
+
const look = LOOKS[value];
|
|
304
|
+
if (!look) throw new Error(`ab: unknown look "${value}" — one of ${Object.keys(LOOKS).join("/")}`);
|
|
305
|
+
return {
|
|
306
|
+
...comp,
|
|
307
|
+
framing: {
|
|
308
|
+
...comp.framing,
|
|
309
|
+
...structuredClone(look)
|
|
354
310
|
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
if (knob.kind === "pace") {
|
|
314
|
+
const pace = MOTION[value];
|
|
315
|
+
if (!pace) throw new Error(`ab: unknown pace "${value}" — one of ${Object.keys(MOTION).join("/")}`);
|
|
316
|
+
const oldZoomInMs = comp.cursor.zoomInMs;
|
|
317
|
+
const events = comp.events.map((e) => e.zoom.inAtMs === Math.max(0, e.tMs - oldZoomInMs) ? {
|
|
318
|
+
...e,
|
|
319
|
+
zoom: {
|
|
320
|
+
...e.zoom,
|
|
321
|
+
inAtMs: Math.max(0, e.tMs - pace.zoomInMs)
|
|
361
322
|
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
|
|
323
|
+
} : e);
|
|
324
|
+
return {
|
|
325
|
+
...comp,
|
|
326
|
+
events,
|
|
327
|
+
cursor: {
|
|
328
|
+
...comp.cursor,
|
|
329
|
+
...pace
|
|
330
|
+
}
|
|
331
|
+
};
|
|
365
332
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
333
|
+
if (knob.kind === "finish") {
|
|
334
|
+
if (!(value in FINISH)) throw new Error(`ab: unknown finish "${value}" — one of ${Object.keys(FINISH).join("/")}`);
|
|
335
|
+
const mb = FINISH[value];
|
|
336
|
+
const { motionBlur: _mb,...rest } = comp;
|
|
337
|
+
return mb ? {
|
|
338
|
+
...rest,
|
|
339
|
+
motionBlur: { ...mb }
|
|
340
|
+
} : rest;
|
|
341
|
+
}
|
|
342
|
+
return setPath(comp, knob.path, value);
|
|
374
343
|
}
|
|
375
|
-
function
|
|
376
|
-
|
|
344
|
+
function variantDesc(knob, value, comp) {
|
|
345
|
+
if (knob.kind === "zoom") {
|
|
346
|
+
const e = comp.events[knob.beat - 1];
|
|
347
|
+
return e ? zoomText(e) : value;
|
|
348
|
+
}
|
|
349
|
+
if (knob.kind === "path") return `${knob.path}=${value}`;
|
|
350
|
+
return value;
|
|
377
351
|
}
|
|
378
|
-
/**
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
352
|
+
/** The reel's time window (ms): a beat's zoom arc with lead-in/out padding, so
|
|
353
|
+
* a taste question costs a ~4s render per variant, not the whole take. The
|
|
354
|
+
* window is the UNION across all variant compositions — a variant with a
|
|
355
|
+
* longer hold/zoom-out (or a rebased inAtMs) must not be cut mid-settle, since
|
|
356
|
+
* the settle is exactly what the reel exists to judge. */
|
|
357
|
+
function abWindow(comps, beat, full) {
|
|
358
|
+
if (full) return void 0;
|
|
359
|
+
const base = comps[0];
|
|
360
|
+
if (!base || base.events.length === 0) return void 0;
|
|
361
|
+
if (beat != null && (beat < 1 || beat > base.events.length)) throw new Error(`ab: --beat ${beat} out of range (1-${base.events.length})`);
|
|
362
|
+
let i = beat != null ? beat - 1 : base.events.findIndex((e) => e.zoom.enabled);
|
|
363
|
+
if (i < 0) i = 0;
|
|
364
|
+
let from = Number.POSITIVE_INFINITY;
|
|
365
|
+
let to = 0;
|
|
366
|
+
for (const c of comps) {
|
|
367
|
+
const e = c.events[i];
|
|
368
|
+
if (!e) continue;
|
|
369
|
+
from = Math.min(from, Math.max(0, e.zoom.inAtMs - 1200));
|
|
370
|
+
const next = c.events[i + 1];
|
|
371
|
+
const ownEnd = e.tMs + (e.durationMs ?? 0) + c.cursor.holdMs + c.cursor.zoomOutMs;
|
|
372
|
+
to = Math.max(to, (next ? next.zoom.inAtMs : ownEnd) + 400);
|
|
373
|
+
}
|
|
374
|
+
if (!Number.isFinite(from)) return void 0;
|
|
375
|
+
return [from, Math.max(to, from + 1500)];
|
|
393
376
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
377
|
+
const LETTERS = [
|
|
378
|
+
"A",
|
|
379
|
+
"B",
|
|
380
|
+
"C",
|
|
381
|
+
"D"
|
|
382
|
+
];
|
|
383
|
+
async function renderAbReel(take, opts) {
|
|
384
|
+
await requireTakeFiles(take, { capture: true });
|
|
385
|
+
const comp = JSON.parse(await readFile(take.compositionPath, "utf8"));
|
|
386
|
+
const captureLog = await loadLogSibling(take.capturePath);
|
|
387
|
+
const chromePath = await ensureChrome(opts.chromePath);
|
|
388
|
+
const { knob, values } = parseSetFlag(opts.set, opts.beat);
|
|
389
|
+
const cur = currentDesc(comp, knob);
|
|
390
|
+
const variants = [{
|
|
391
|
+
letter: "A",
|
|
392
|
+
desc: `current (${cur})`,
|
|
393
|
+
comp
|
|
394
|
+
}];
|
|
395
|
+
for (const v of values) {
|
|
396
|
+
const applied = applyKnob(comp, knob, v);
|
|
397
|
+
const desc = variantDesc(knob, v, applied);
|
|
398
|
+
if (desc === cur || variants.some((x) => x.desc === desc)) continue;
|
|
399
|
+
variants.push({
|
|
400
|
+
letter: LETTERS[variants.length],
|
|
401
|
+
desc,
|
|
402
|
+
comp: applied
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
if (variants.length < 2) throw new Error(`ab: every requested value matches the current state — nothing to compare`);
|
|
406
|
+
for (const v of variants) {
|
|
407
|
+
const issues = validateComposition$1(v.comp, captureLog ? { captureLog } : {});
|
|
408
|
+
const errors = issues.filter((i) => i.severity === "error");
|
|
409
|
+
if (errors.length) throw new Error(`ab: variant ${v.letter} (${v.desc}) is invalid:\n${formatIssues$1(errors)}`);
|
|
410
|
+
}
|
|
411
|
+
const windowMs = abWindow(variants.map((v) => v.comp), opts.beat, opts.full ?? false);
|
|
412
|
+
const rangeSec = windowMs ? [windowMs[0] / 1e3, windowMs[1] / 1e3] : void 0;
|
|
413
|
+
const clips = [];
|
|
400
414
|
try {
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
415
|
+
for (const v of variants) {
|
|
416
|
+
const base = opts.draft ? toDraft(v.comp) : v.comp;
|
|
417
|
+
const decorated = {
|
|
418
|
+
...base,
|
|
419
|
+
review: { label: `${v.letter} · ${v.desc}` }
|
|
420
|
+
};
|
|
421
|
+
const clip = `${take.base}.ab.${v.letter}.mp4`;
|
|
422
|
+
if (opts.logProgress !== false) process.stderr.write(`variant ${v.letter} · ${v.desc}${rangeSec ? ` · window ${rangeSec[0].toFixed(1)}–${rangeSec[1].toFixed(1)}s` : ""}\n`);
|
|
423
|
+
await renderTake({
|
|
424
|
+
composition: decorated,
|
|
425
|
+
videoPath: take.capturePath,
|
|
426
|
+
outPath: clip,
|
|
427
|
+
captureLog,
|
|
428
|
+
logProgress: opts.logProgress ?? true,
|
|
429
|
+
chromePath,
|
|
430
|
+
rangeSec,
|
|
431
|
+
writeCompositionSibling: false
|
|
432
|
+
});
|
|
433
|
+
clips.push(clip);
|
|
434
|
+
}
|
|
435
|
+
await concatWithGaps([...clips, ...clips], take.abPath, {
|
|
436
|
+
width: comp.output.width,
|
|
437
|
+
height: comp.output.height,
|
|
438
|
+
fps: opts.draft ? Math.min(30, comp.output.fps) : comp.output.fps
|
|
439
|
+
});
|
|
440
|
+
} finally {
|
|
441
|
+
await Promise.all(clips.map((c) => rm(c, { force: true })));
|
|
404
442
|
}
|
|
443
|
+
const legend = [
|
|
444
|
+
`A/B reel: ${take.abPath} (${knobLabel(knob)})`,
|
|
445
|
+
...variants.map((v) => ` ${v.letter} ${v.desc}`),
|
|
446
|
+
`answer with a letter — or "between A and B" / "none, try …"`
|
|
447
|
+
].join("\n");
|
|
448
|
+
return {
|
|
449
|
+
abPath: take.abPath,
|
|
450
|
+
legend
|
|
451
|
+
};
|
|
405
452
|
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
if (
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
const title = trunc(beatTitle(e), 24).padEnd(25, " ");
|
|
444
|
-
lines.push(` ${num} ${fmtTime(e.tMs)} ${kind} ${title} ${zoomText(e)}`);
|
|
453
|
+
function knobLabel(knob) {
|
|
454
|
+
if (knob.kind === "zoom") return `beat ${knob.beat} · zoom`;
|
|
455
|
+
if (knob.kind === "path") return knob.path;
|
|
456
|
+
return knob.kind;
|
|
457
|
+
}
|
|
458
|
+
/** before/after reel from the existing prev + current masters — no render at
|
|
459
|
+
* all, pure ffmpeg trims. Order convention: BEFORE then AFTER, twice. */
|
|
460
|
+
async function renderBeforeAfter(take, opts = {}) {
|
|
461
|
+
await requireTakeFiles(take);
|
|
462
|
+
if (!await stat(take.prevPath).catch(() => null)) throw new Error(`ab --before-after: no ${take.prevPath} yet (it appears after a re-render)`);
|
|
463
|
+
const comp = JSON.parse(await readFile(take.compositionPath, "utf8"));
|
|
464
|
+
const win = abWindow([comp], opts.beat, opts.full ?? false);
|
|
465
|
+
const cut = async (src, out) => {
|
|
466
|
+
const args = [
|
|
467
|
+
"-y",
|
|
468
|
+
"-loglevel",
|
|
469
|
+
"error",
|
|
470
|
+
"-i",
|
|
471
|
+
src
|
|
472
|
+
];
|
|
473
|
+
if (win) args.push("-ss", String(win[0] / 1e3), "-t", String((win[1] - win[0]) / 1e3));
|
|
474
|
+
args.push("-c:v", "libx264", "-crf", "18", "-pix_fmt", "yuv420p", "-an", out);
|
|
475
|
+
await ffmpeg(args);
|
|
476
|
+
};
|
|
477
|
+
const before = `${take.base}.ab.before.mp4`;
|
|
478
|
+
const after = `${take.base}.ab.after.mp4`;
|
|
479
|
+
await cut(take.prevPath, before);
|
|
480
|
+
await cut(take.mp4Path, after);
|
|
481
|
+
await concatWithGaps([
|
|
482
|
+
before,
|
|
483
|
+
after,
|
|
484
|
+
before,
|
|
485
|
+
after
|
|
486
|
+
], take.abPath, {
|
|
487
|
+
width: comp.output.width,
|
|
488
|
+
height: comp.output.height,
|
|
489
|
+
fps: comp.output.fps
|
|
445
490
|
});
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
return lines.join("\n");
|
|
491
|
+
await Promise.all([rm(before, { force: true }), rm(after, { force: true })]);
|
|
492
|
+
const legend = [`before/after reel: ${take.abPath}`, ` plays BEFORE then AFTER, twice — say "keep the old one" to revert`].join("\n");
|
|
493
|
+
return {
|
|
494
|
+
abPath: take.abPath,
|
|
495
|
+
legend
|
|
496
|
+
};
|
|
453
497
|
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
let prevStart = 0;
|
|
469
|
-
const starts = comp.events.map((e) => {
|
|
470
|
-
const s = Math.max(e.zoom.inAtMs, prevStart + 1);
|
|
471
|
-
prevStart = s;
|
|
472
|
-
return s;
|
|
498
|
+
async function ffmpeg(args) {
|
|
499
|
+
const bin = await resolveFfmpeg();
|
|
500
|
+
return new Promise((res, rej) => {
|
|
501
|
+
const c = spawn(bin, args, { stdio: [
|
|
502
|
+
"ignore",
|
|
503
|
+
"ignore",
|
|
504
|
+
"pipe"
|
|
505
|
+
] });
|
|
506
|
+
let err = "";
|
|
507
|
+
c.stderr.on("data", (d) => {
|
|
508
|
+
err += d;
|
|
509
|
+
});
|
|
510
|
+
c.on("error", rej);
|
|
511
|
+
c.on("close", (code) => code === 0 ? res() : rej(new Error(`ffmpeg exited ${code}: ${err}`)));
|
|
473
512
|
});
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
513
|
+
}
|
|
514
|
+
/** Concat clips with a 0.35s black gap between each, re-encoding via the concat
|
|
515
|
+
* FILTER (robust to per-clip encoder variance; everything shares WxH/fps). The
|
|
516
|
+
* gap comes from a lavfi color source — no font/text filters needed. */
|
|
517
|
+
async function concatWithGaps(clips, outPath, v) {
|
|
518
|
+
const args = [
|
|
519
|
+
"-y",
|
|
520
|
+
"-loglevel",
|
|
521
|
+
"error"
|
|
522
|
+
];
|
|
523
|
+
for (const c of clips) args.push("-i", resolve(c));
|
|
524
|
+
args.push("-f", "lavfi", "-t", "0.35", "-i", `color=c=black:s=${v.width}x${v.height}:r=${v.fps}`);
|
|
525
|
+
const gapIdx = clips.length;
|
|
526
|
+
const seq = [];
|
|
527
|
+
const gapUses = clips.length - 1;
|
|
528
|
+
const split = gapUses > 1 ? `[${gapIdx}:v]split=${gapUses}${Array.from({ length: gapUses }, (_, i) => `[g${i}]`).join("")};` : "";
|
|
529
|
+
clips.forEach((_, i) => {
|
|
530
|
+
seq.push(`[${i}:v]`);
|
|
531
|
+
if (i < clips.length - 1) seq.push(gapUses > 1 ? `[g${i}]` : `[${gapIdx}:v]`);
|
|
478
532
|
});
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
fromMs: to,
|
|
490
|
-
toMs: comp.durationMs + 1500,
|
|
491
|
-
text: "TAIL"
|
|
533
|
+
const filter = `${split}${seq.join("")}concat=n=${seq.length}:v=1:a=0[v]`;
|
|
534
|
+
args.push("-filter_complex", filter, "-map", "[v]", "-c:v", "libx264", "-crf", "18", "-pix_fmt", "yuv420p", "-r", String(v.fps), "-an", resolve(outPath));
|
|
535
|
+
await ffmpeg(args);
|
|
536
|
+
}
|
|
537
|
+
function spawnOpener(spec, fallbackLine) {
|
|
538
|
+
try {
|
|
539
|
+
const c = spawn(spec.command, spec.args, {
|
|
540
|
+
stdio: "ignore",
|
|
541
|
+
detached: true,
|
|
542
|
+
windowsVerbatimArguments: spec.windowsVerbatimArguments
|
|
492
543
|
});
|
|
493
|
-
|
|
494
|
-
|
|
544
|
+
c.on("error", () => process.stdout.write(fallbackLine));
|
|
545
|
+
c.unref();
|
|
546
|
+
} catch {
|
|
547
|
+
process.stdout.write(fallbackLine);
|
|
548
|
+
}
|
|
495
549
|
}
|
|
496
|
-
/**
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
550
|
+
/** Wrap a path/URL for a verbatim Windows command line. Safe unquoted-inside:
|
|
551
|
+
* `"` is illegal in Windows filenames, and cmd stops treating `&` `^` `|` as
|
|
552
|
+
* metacharacters once quoted. */
|
|
553
|
+
const winQuote = (s) => `"${s}"`;
|
|
554
|
+
/** Pure command specs keep platform quoting independently testable on every
|
|
555
|
+
* CI runner, including the Windows-only `start` title argument rule. */
|
|
556
|
+
function getOpenCommand(target, platform = process.platform) {
|
|
557
|
+
if (platform === "darwin") return {
|
|
558
|
+
command: "open",
|
|
559
|
+
args: [target],
|
|
560
|
+
windowsVerbatimArguments: false
|
|
561
|
+
};
|
|
562
|
+
if (platform === "win32") return {
|
|
563
|
+
command: "cmd",
|
|
564
|
+
args: [
|
|
565
|
+
"/c",
|
|
566
|
+
"start",
|
|
567
|
+
"\"\"",
|
|
568
|
+
winQuote(target)
|
|
569
|
+
],
|
|
570
|
+
windowsVerbatimArguments: true
|
|
571
|
+
};
|
|
503
572
|
return {
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
fps: Math.min(30, comp.output.fps)
|
|
508
|
-
}
|
|
573
|
+
command: "xdg-open",
|
|
574
|
+
args: [target],
|
|
575
|
+
windowsVerbatimArguments: false
|
|
509
576
|
};
|
|
510
577
|
}
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
}
|
|
578
|
+
function getRevealCommand(absolutePath, platform = process.platform) {
|
|
579
|
+
if (platform === "darwin") return {
|
|
580
|
+
command: "open",
|
|
581
|
+
args: ["-R", absolutePath],
|
|
582
|
+
windowsVerbatimArguments: false
|
|
583
|
+
};
|
|
584
|
+
if (platform === "win32") return {
|
|
585
|
+
command: "explorer",
|
|
586
|
+
args: [`/select,${winQuote(absolutePath)}`],
|
|
587
|
+
windowsVerbatimArguments: true
|
|
522
588
|
};
|
|
523
|
-
await renderTake({
|
|
524
|
-
composition: decorated,
|
|
525
|
-
videoPath: take.capturePath,
|
|
526
|
-
outPath: take.reviewPath,
|
|
527
|
-
captureLog,
|
|
528
|
-
logProgress: opts.logProgress ?? true,
|
|
529
|
-
chromePath,
|
|
530
|
-
writeCompositionSibling: false
|
|
531
|
-
});
|
|
532
589
|
return {
|
|
533
|
-
|
|
534
|
-
|
|
590
|
+
command: "xdg-open",
|
|
591
|
+
args: [dirname(absolutePath)],
|
|
592
|
+
windowsVerbatimArguments: false
|
|
535
593
|
};
|
|
536
594
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
595
|
+
/** Open a file with the OS default player; degrade to printing the path. */
|
|
596
|
+
function openPath(p) {
|
|
597
|
+
const abs = resolve(p);
|
|
598
|
+
openWithOs(abs, `open: ${abs}\n`);
|
|
599
|
+
}
|
|
600
|
+
/** Hand a path or URL to the OS default handler. Windows needs `cmd /c start`
|
|
601
|
+
* with an EMPTY first argument: `start` reads a lone quoted argument as the
|
|
602
|
+
* window TITLE, so `start "<file>"` opens a blank console instead of the file. */
|
|
603
|
+
function openWithOs(target, fallbackLine) {
|
|
604
|
+
spawnOpener(getOpenCommand(target), fallbackLine);
|
|
605
|
+
}
|
|
606
|
+
/** Reveal a file in Finder/Explorer; degrade to printing the path. */
|
|
607
|
+
function revealPath(p) {
|
|
608
|
+
const abs = resolve(p);
|
|
609
|
+
spawnOpener(getRevealCommand(abs), `at: ${abs}\n`);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
//#endregion
|
|
613
|
+
//#region src/edit-server.ts
|
|
614
|
+
const CONTENT_TYPE = {
|
|
615
|
+
".html": "text/html; charset=utf-8",
|
|
616
|
+
".js": "text/javascript; charset=utf-8",
|
|
617
|
+
".mjs": "text/javascript; charset=utf-8",
|
|
618
|
+
".css": "text/css; charset=utf-8",
|
|
619
|
+
".json": "application/json; charset=utf-8",
|
|
620
|
+
".svg": "image/svg+xml",
|
|
621
|
+
".png": "image/png",
|
|
622
|
+
".jpg": "image/jpeg",
|
|
623
|
+
".ico": "image/x-icon",
|
|
624
|
+
".woff2": "font/woff2",
|
|
625
|
+
".woff": "font/woff",
|
|
626
|
+
".map": "application/json; charset=utf-8"
|
|
627
|
+
};
|
|
628
|
+
/** Locate the built editor SPA: prefer the live monorepo build during
|
|
629
|
+
* development, then fall back to the bundled copy used by the published
|
|
630
|
+
* package. */
|
|
631
|
+
function resolveEditorDist() {
|
|
632
|
+
const here = fileURLToPath(import.meta.url);
|
|
633
|
+
const pkgRoot = resolve(here, "..", "..");
|
|
634
|
+
const candidates = [resolve(pkgRoot, "..", "..", "apps", "editor", "dist"), resolve(pkgRoot, "editor-dist")];
|
|
635
|
+
return candidates.find((c) => existsHtml(c)) ?? null;
|
|
636
|
+
}
|
|
637
|
+
function existsHtml(dir) {
|
|
638
|
+
return existsFile(join(dir, "index.html"));
|
|
639
|
+
}
|
|
640
|
+
function existsFile(path) {
|
|
641
|
+
try {
|
|
642
|
+
return statSync(path).isFile();
|
|
643
|
+
} catch {
|
|
644
|
+
return false;
|
|
553
645
|
}
|
|
554
|
-
if (key === "look" || key === "pace" || key === "finish") return {
|
|
555
|
-
knob: { kind: key },
|
|
556
|
-
values
|
|
557
|
-
};
|
|
558
|
-
if (key.includes(".")) return {
|
|
559
|
-
knob: {
|
|
560
|
-
kind: "path",
|
|
561
|
-
path: key
|
|
562
|
-
},
|
|
563
|
-
values
|
|
564
|
-
};
|
|
565
|
-
throw new Error(`ab: unknown knob "${key}" — use zoom (with --beat), look, pace, finish, or a raw dot-path like cursor.holdMs`);
|
|
566
646
|
}
|
|
567
|
-
|
|
568
|
-
const
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
647
|
+
const sendJson = (res, status, obj) => {
|
|
648
|
+
const body = JSON.stringify(obj);
|
|
649
|
+
res.writeHead(status, {
|
|
650
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
651
|
+
"Content-Length": Buffer.byteLength(body)
|
|
652
|
+
});
|
|
653
|
+
res.end(body);
|
|
654
|
+
};
|
|
655
|
+
async function readBody(req, capBytes = 32 * 1024 * 1024) {
|
|
656
|
+
const chunks = [];
|
|
657
|
+
let size = 0;
|
|
658
|
+
for await (const c of req) {
|
|
659
|
+
size += c.length;
|
|
660
|
+
if (size > capBytes) throw new Error("request body too large");
|
|
661
|
+
chunks.push(c);
|
|
577
662
|
}
|
|
578
|
-
|
|
579
|
-
return next;
|
|
663
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
580
664
|
}
|
|
581
|
-
/**
|
|
582
|
-
function
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
665
|
+
/** Stream a file with HTTP Range support (206) — required for <video> seeking. */
|
|
666
|
+
async function serveFile(req, res, filePath) {
|
|
667
|
+
const st = await stat(filePath).catch(() => null);
|
|
668
|
+
if (!st?.isFile()) {
|
|
669
|
+
sendJson(res, 404, { error: "not found" });
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
const type = CONTENT_TYPE[extname(filePath).toLowerCase()] ?? "application/octet-stream";
|
|
673
|
+
const range = req.headers.range;
|
|
674
|
+
if (range) {
|
|
675
|
+
const m = /bytes=(\d*)-(\d*)/.exec(range);
|
|
676
|
+
const start = m?.[1] ? Number(m[1]) : 0;
|
|
677
|
+
const end = m?.[2] ? Number(m[2]) : st.size - 1;
|
|
678
|
+
if (start >= st.size || end >= st.size || start > end) {
|
|
679
|
+
res.writeHead(416, { "Content-Range": `bytes */${st.size}` });
|
|
680
|
+
res.end();
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
res.writeHead(206, {
|
|
684
|
+
"Content-Range": `bytes ${start}-${end}/${st.size}`,
|
|
685
|
+
"Accept-Ranges": "bytes",
|
|
686
|
+
"Content-Length": end - start + 1,
|
|
687
|
+
"Content-Type": type
|
|
688
|
+
});
|
|
689
|
+
createReadStream(filePath, {
|
|
690
|
+
start,
|
|
691
|
+
end
|
|
692
|
+
}).pipe(res);
|
|
693
|
+
} else {
|
|
694
|
+
res.writeHead(200, {
|
|
695
|
+
"Content-Length": st.size,
|
|
696
|
+
"Accept-Ranges": "bytes",
|
|
697
|
+
"Content-Type": type
|
|
698
|
+
});
|
|
699
|
+
createReadStream(filePath).pipe(res);
|
|
586
700
|
}
|
|
587
|
-
if (knob.kind === "look") return lookName(comp.framing) ?? "custom look";
|
|
588
|
-
if (knob.kind === "pace") return motionName(comp.cursor) ?? "custom pace";
|
|
589
|
-
if (knob.kind === "finish") return finishName(comp.motionBlur) ?? "custom finish";
|
|
590
|
-
const parts = knob.path.split(".");
|
|
591
|
-
let node = comp;
|
|
592
|
-
for (const p of parts) node = node?.[p];
|
|
593
|
-
return `${knob.path}=${JSON.stringify(node)}`;
|
|
594
701
|
}
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
702
|
+
/** Open the editor in the default browser. Shares the OS-opener with review.ts
|
|
703
|
+
* — a private copy here is how the Windows `start`-eats-the-first-quoted-arg
|
|
704
|
+
* bug survived in one place after being noticed in the other. */
|
|
705
|
+
function openBrowser(url) {
|
|
706
|
+
openWithOs(url, `open: ${url}\n`);
|
|
707
|
+
}
|
|
708
|
+
async function startEditServer(opts, deps = {}) {
|
|
709
|
+
const take = await resolveTakePaths(opts.takePath);
|
|
710
|
+
for (const f of [take.compositionPath, take.capturePath]) if (!(await stat(f).catch(() => null))?.isFile()) throw new Error(`missing take file: ${f}`);
|
|
711
|
+
const dist = resolveEditorDist();
|
|
712
|
+
if (!dist) throw new Error("editor build not found — run `pnpm --filter @open-take/editor build` first");
|
|
713
|
+
const loadCaptureLog = async () => {
|
|
714
|
+
try {
|
|
715
|
+
return JSON.parse(await readFile(take.captureLogPath, "utf8"));
|
|
716
|
+
} catch {
|
|
717
|
+
return null;
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
let job = null;
|
|
721
|
+
let jobSeq = 0;
|
|
722
|
+
let renderStarting = false;
|
|
723
|
+
const compositionMtime = async () => (await stat(take.compositionPath).catch(() => null))?.mtimeMs ?? 0;
|
|
724
|
+
let writeLock = Promise.resolve();
|
|
725
|
+
const exclusive = (fn) => {
|
|
726
|
+
const run = writeLock.then(fn, fn);
|
|
727
|
+
writeLock = run.catch(() => {});
|
|
728
|
+
return run;
|
|
729
|
+
};
|
|
730
|
+
/** Persist unless someone else wrote first. `baseMtime` is the mtime the
|
|
731
|
+
* client last read. Every API write must provide it; 保留我的 re-bases to
|
|
732
|
+
* the 409's mtime and remains guarded against an even newer write.
|
|
733
|
+
*
|
|
734
|
+
* We check once before writing the temp file and again immediately before
|
|
735
|
+
* rename, so an agent write during serialization/I/O is refused too. No
|
|
736
|
+
* portable filesystem API provides compare-and-swap by mtime: a different
|
|
737
|
+
* process writing in the final stat→rename window can still be overwritten
|
|
738
|
+
* unless every writer honors the same lock. */
|
|
739
|
+
const persistGuarded = async (comp, baseMtime) => exclusive(async () => {
|
|
740
|
+
const onDisk = await compositionMtime();
|
|
741
|
+
if (baseMtime !== void 0 && onDisk !== baseMtime) return {
|
|
742
|
+
ok: false,
|
|
743
|
+
mtime: onDisk
|
|
607
744
|
};
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
745
|
+
const tmp = `${take.compositionPath}.tmp`;
|
|
746
|
+
let committed = false;
|
|
747
|
+
try {
|
|
748
|
+
await writeFile(tmp, JSON.stringify(comp, null, 2));
|
|
749
|
+
await deps.beforeCompositionCommit?.();
|
|
750
|
+
if (baseMtime !== void 0) {
|
|
751
|
+
const beforeRename = await compositionMtime();
|
|
752
|
+
if (beforeRename !== onDisk) return {
|
|
753
|
+
ok: false,
|
|
754
|
+
mtime: beforeRename
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
await rename(tmp, take.compositionPath);
|
|
758
|
+
committed = true;
|
|
759
|
+
return {
|
|
760
|
+
ok: true,
|
|
761
|
+
mtime: await compositionMtime()
|
|
618
762
|
};
|
|
763
|
+
} finally {
|
|
764
|
+
if (!committed) await unlink(tmp).catch(() => {});
|
|
765
|
+
}
|
|
766
|
+
});
|
|
767
|
+
/** The write guard is mandatory. A missing base used to mean "force write",
|
|
768
|
+
* which made a bare JSON body an invisible escape hatch around conflict
|
|
769
|
+
* protection. Callers must first GET /api/take and echo its mtime. */
|
|
770
|
+
const readWrite = async (req, res) => {
|
|
771
|
+
let parsed;
|
|
772
|
+
try {
|
|
773
|
+
parsed = JSON.parse(await readBody(req));
|
|
774
|
+
} catch {
|
|
775
|
+
sendJson(res, 400, { error: "invalid JSON request body" });
|
|
776
|
+
return null;
|
|
777
|
+
}
|
|
778
|
+
const candidate = parsed && typeof parsed === "object" ? parsed : null;
|
|
779
|
+
if (!candidate?.composition || typeof candidate.baseMtime !== "number" || !Number.isFinite(candidate.baseMtime)) {
|
|
780
|
+
sendJson(res, 428, { error: "write precondition required: send { composition, baseMtime } using the mtime from GET /api/take" });
|
|
781
|
+
return null;
|
|
619
782
|
}
|
|
620
783
|
return {
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
};
|
|
624
|
-
}
|
|
625
|
-
if (knob.kind === "look") {
|
|
626
|
-
const look = LOOKS[value];
|
|
627
|
-
if (!look) throw new Error(`ab: unknown look "${value}" — one of ${Object.keys(LOOKS).join("/")}`);
|
|
628
|
-
return {
|
|
629
|
-
...comp,
|
|
630
|
-
framing: {
|
|
631
|
-
...comp.framing,
|
|
632
|
-
...structuredClone(look)
|
|
633
|
-
}
|
|
784
|
+
composition: candidate.composition,
|
|
785
|
+
baseMtime: candidate.baseMtime
|
|
634
786
|
};
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
787
|
+
};
|
|
788
|
+
const server = createServer(async (req, res) => {
|
|
789
|
+
try {
|
|
790
|
+
const url$1 = new URL(req.url ?? "/", "http://localhost");
|
|
791
|
+
const path = url$1.pathname;
|
|
792
|
+
const method = req.method ?? "GET";
|
|
793
|
+
if (method === "POST") {
|
|
794
|
+
const host = req.headers.host ?? "";
|
|
795
|
+
const origin = req.headers.origin;
|
|
796
|
+
const sameOrigin = !origin || origin === `http://${host}`;
|
|
797
|
+
const isJson = (req.headers["content-type"] ?? "").includes("application/json");
|
|
798
|
+
if (!/^(127\.0\.0\.1|localhost)(:\d+)?$/.test(host) || !sameOrigin || !isJson) {
|
|
799
|
+
sendJson(res, 403, { error: "forbidden" });
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
645
802
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
803
|
+
if (path === "/api/take" && method === "GET") {
|
|
804
|
+
const mtime = await compositionMtime();
|
|
805
|
+
const composition = JSON.parse(await readFile(take.compositionPath, "utf8"));
|
|
806
|
+
const captureLog = await loadCaptureLog();
|
|
807
|
+
sendJson(res, 200, {
|
|
808
|
+
composition,
|
|
809
|
+
captureLog,
|
|
810
|
+
source: { name: take.name },
|
|
811
|
+
videoUrl: "/api/take/video",
|
|
812
|
+
mp4Url: "/api/take/output",
|
|
813
|
+
mtime
|
|
814
|
+
});
|
|
815
|
+
return;
|
|
653
816
|
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
817
|
+
if (path === "/api/take/video" && method === "GET") {
|
|
818
|
+
await serveFile(req, res, take.capturePath);
|
|
819
|
+
return;
|
|
820
|
+
}
|
|
821
|
+
if (path === "/api/take/mtime" && method === "GET") {
|
|
822
|
+
sendJson(res, 200, { mtime: await compositionMtime() });
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
if (path === "/api/notes" && method === "POST") {
|
|
826
|
+
const { text } = JSON.parse(await readBody(req));
|
|
827
|
+
if (!text?.trim()) {
|
|
828
|
+
sendJson(res, 400, { error: "empty note" });
|
|
829
|
+
return;
|
|
830
|
+
}
|
|
831
|
+
const line = text.trim().replace(/\s+/g, " ").slice(0, 2e3);
|
|
832
|
+
await appendFile(`${take.base}.notes.md`, `- ${line}\n`);
|
|
833
|
+
process.stdout.write(`NOTE ${JSON.stringify({
|
|
834
|
+
take: take.name,
|
|
835
|
+
text: line
|
|
836
|
+
})}\n`);
|
|
837
|
+
sendJson(res, 200, { ok: true });
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
if (path === "/api/take/output" && method === "GET") {
|
|
841
|
+
await serveFile(req, res, take.mp4Path);
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
if (path === "/api/composition" && method === "POST") {
|
|
845
|
+
const payload = await readWrite(req, res);
|
|
846
|
+
if (!payload) return;
|
|
847
|
+
const { composition: comp, baseMtime } = payload;
|
|
848
|
+
const captureLog = await loadCaptureLog();
|
|
849
|
+
const issues = validateComposition$1(comp, captureLog ? { captureLog } : {});
|
|
850
|
+
const errors = issues.filter((i) => i.severity === "error");
|
|
851
|
+
if (errors.length) {
|
|
852
|
+
sendJson(res, 400, { issues });
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
const saved = await persistGuarded(comp, baseMtime);
|
|
856
|
+
if (!saved.ok) {
|
|
857
|
+
sendJson(res, 409, {
|
|
858
|
+
conflict: true,
|
|
859
|
+
mtime: saved.mtime
|
|
860
|
+
});
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
sendJson(res, 200, {
|
|
864
|
+
ok: true,
|
|
865
|
+
issues,
|
|
866
|
+
mtime: saved.mtime
|
|
867
|
+
});
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
if (path === "/api/render" && method === "POST") {
|
|
871
|
+
if (renderStarting || job?.status === "running") {
|
|
872
|
+
sendJson(res, 409, { error: "a render is already in progress" });
|
|
873
|
+
return;
|
|
874
|
+
}
|
|
875
|
+
renderStarting = true;
|
|
876
|
+
try {
|
|
877
|
+
const payload = await readWrite(req, res);
|
|
878
|
+
if (!payload) return;
|
|
879
|
+
const { composition: comp, baseMtime } = payload;
|
|
880
|
+
const captureLog = await loadCaptureLog() ?? void 0;
|
|
881
|
+
const issues = validateComposition$1(comp, captureLog ? { captureLog } : {});
|
|
882
|
+
const errors = issues.filter((i) => i.severity === "error");
|
|
883
|
+
if (errors.length) {
|
|
884
|
+
sendJson(res, 400, { issues });
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
const saved = await persistGuarded(comp, baseMtime);
|
|
888
|
+
if (!saved.ok) {
|
|
889
|
+
sendJson(res, 409, {
|
|
890
|
+
conflict: true,
|
|
891
|
+
mtime: saved.mtime
|
|
892
|
+
});
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
const id = `${Date.now().toString(36)}-${++jobSeq}`;
|
|
896
|
+
job = {
|
|
897
|
+
id,
|
|
898
|
+
progress: 0,
|
|
899
|
+
status: "running",
|
|
900
|
+
clients: new Set()
|
|
901
|
+
};
|
|
902
|
+
sendJson(res, 200, {
|
|
903
|
+
jobId: id,
|
|
904
|
+
mtime: saved.mtime
|
|
905
|
+
});
|
|
906
|
+
runRender(job, comp, captureLog);
|
|
907
|
+
} finally {
|
|
908
|
+
renderStarting = false;
|
|
909
|
+
}
|
|
910
|
+
return;
|
|
911
|
+
}
|
|
912
|
+
const ev = /^\/api\/render\/([^/]+)\/events$/.exec(path);
|
|
913
|
+
if (ev && method === "GET") {
|
|
914
|
+
const id = ev[1];
|
|
915
|
+
if (!job || job.id !== id) {
|
|
916
|
+
sendJson(res, 404, { error: "unknown job" });
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
res.writeHead(200, {
|
|
920
|
+
"Content-Type": "text/event-stream",
|
|
921
|
+
"Cache-Control": "no-cache",
|
|
922
|
+
Connection: "keep-alive"
|
|
923
|
+
});
|
|
924
|
+
emit(res, "progress", { progress: job.progress });
|
|
925
|
+
if (job.status === "done") {
|
|
926
|
+
emit(res, "done", { mp4Url: job.mp4Url });
|
|
927
|
+
res.end();
|
|
928
|
+
return;
|
|
929
|
+
}
|
|
930
|
+
if (job.status === "error") {
|
|
931
|
+
emit(res, "failed", { message: job.error });
|
|
932
|
+
res.end();
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
job.clients.add(res);
|
|
936
|
+
req.on("close", () => job?.clients.delete(res));
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
if (path.startsWith("/api/")) {
|
|
940
|
+
sendJson(res, 404, { error: "no such endpoint" });
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
if (method !== "GET") {
|
|
944
|
+
sendJson(res, 405, { error: "method not allowed" });
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
const rel = normalize(decodeURIComponent(path)).replace(/^(\.\.[/\\])+/, "");
|
|
948
|
+
let filePath = join(dist, rel === "/" ? "index.html" : rel);
|
|
949
|
+
if (!filePath.startsWith(dist)) {
|
|
950
|
+
sendJson(res, 403, { error: "forbidden" });
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
const fst = await stat(filePath).catch(() => null);
|
|
954
|
+
if (!fst?.isFile()) filePath = join(dist, "index.html");
|
|
955
|
+
await serveFile(req, res, filePath);
|
|
956
|
+
} catch (err) {
|
|
957
|
+
sendJson(res, 500, { error: err instanceof Error ? err.message : String(err) });
|
|
958
|
+
}
|
|
959
|
+
});
|
|
960
|
+
async function runRender(j, comp, captureLog) {
|
|
961
|
+
try {
|
|
962
|
+
await (deps.renderComposition ?? renderComposition)({
|
|
963
|
+
composition: comp,
|
|
964
|
+
capturePath: take.capturePath,
|
|
965
|
+
outPath: take.mp4Path,
|
|
750
966
|
captureLog,
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
967
|
+
chromePath: opts.chromePath,
|
|
968
|
+
writeCompositionSibling: false,
|
|
969
|
+
onProgress: (p) => {
|
|
970
|
+
j.progress = p;
|
|
971
|
+
for (const c of j.clients) emit(c, "progress", { progress: p });
|
|
972
|
+
}
|
|
755
973
|
});
|
|
756
|
-
|
|
974
|
+
j.status = "done";
|
|
975
|
+
j.progress = 1;
|
|
976
|
+
j.mp4Url = `/api/take/output?v=${Date.now().toString(36)}`;
|
|
977
|
+
for (const c of j.clients) {
|
|
978
|
+
emit(c, "done", { mp4Url: j.mp4Url });
|
|
979
|
+
c.end();
|
|
980
|
+
}
|
|
981
|
+
} catch (err) {
|
|
982
|
+
j.status = "error";
|
|
983
|
+
j.error = err instanceof Error ? err.message : String(err);
|
|
984
|
+
for (const c of j.clients) {
|
|
985
|
+
emit(c, "failed", { message: j.error });
|
|
986
|
+
c.end();
|
|
987
|
+
}
|
|
988
|
+
} finally {
|
|
989
|
+
j.clients.clear();
|
|
757
990
|
}
|
|
758
|
-
await concatWithGaps([...clips, ...clips], take.abPath, {
|
|
759
|
-
width: comp.output.width,
|
|
760
|
-
height: comp.output.height,
|
|
761
|
-
fps: opts.draft ? Math.min(30, comp.output.fps) : comp.output.fps
|
|
762
|
-
});
|
|
763
|
-
} finally {
|
|
764
|
-
await Promise.all(clips.map((c) => rm(c, { force: true })));
|
|
765
991
|
}
|
|
766
|
-
const
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
].join("\n");
|
|
992
|
+
const port = await listen(server, opts.port ?? 4178);
|
|
993
|
+
const url = `http://127.0.0.1:${port}/`;
|
|
994
|
+
process.stdout.write(`open-take editor → ${url}\n(serving take "${take.name}"; Ctrl-C to stop)\n`);
|
|
995
|
+
if (opts.open !== false) openBrowser(url);
|
|
771
996
|
return {
|
|
772
|
-
|
|
773
|
-
|
|
997
|
+
url,
|
|
998
|
+
close: () => new Promise((r) => server.close(() => r()))
|
|
774
999
|
};
|
|
775
1000
|
}
|
|
776
|
-
function
|
|
777
|
-
|
|
778
|
-
if (knob.kind === "path") return knob.path;
|
|
779
|
-
return knob.kind;
|
|
780
|
-
}
|
|
781
|
-
/** before/after reel from the existing prev + current masters — no render at
|
|
782
|
-
* all, pure ffmpeg trims. Order convention: BEFORE then AFTER, twice. */
|
|
783
|
-
async function renderBeforeAfter(take, opts = {}) {
|
|
784
|
-
await requireTakeFiles(take);
|
|
785
|
-
if (!await stat(take.prevPath).catch(() => null)) throw new Error(`ab --before-after: no ${take.prevPath} yet (it appears after a re-render)`);
|
|
786
|
-
const comp = JSON.parse(await readFile(take.compositionPath, "utf8"));
|
|
787
|
-
const win = abWindow([comp], opts.beat, opts.full ?? false);
|
|
788
|
-
const cut = async (src, out) => {
|
|
789
|
-
const args = [
|
|
790
|
-
"-y",
|
|
791
|
-
"-loglevel",
|
|
792
|
-
"error",
|
|
793
|
-
"-i",
|
|
794
|
-
src
|
|
795
|
-
];
|
|
796
|
-
if (win) args.push("-ss", String(win[0] / 1e3), "-t", String((win[1] - win[0]) / 1e3));
|
|
797
|
-
args.push("-c:v", "libx264", "-crf", "18", "-pix_fmt", "yuv420p", "-an", out);
|
|
798
|
-
await ffmpeg(args);
|
|
799
|
-
};
|
|
800
|
-
const before = `${take.base}.ab.before.mp4`;
|
|
801
|
-
const after = `${take.base}.ab.after.mp4`;
|
|
802
|
-
await cut(take.prevPath, before);
|
|
803
|
-
await cut(take.mp4Path, after);
|
|
804
|
-
await concatWithGaps([
|
|
805
|
-
before,
|
|
806
|
-
after,
|
|
807
|
-
before,
|
|
808
|
-
after
|
|
809
|
-
], take.abPath, {
|
|
810
|
-
width: comp.output.width,
|
|
811
|
-
height: comp.output.height,
|
|
812
|
-
fps: comp.output.fps
|
|
813
|
-
});
|
|
814
|
-
await Promise.all([rm(before, { force: true }), rm(after, { force: true })]);
|
|
815
|
-
const legend = [`before/after reel: ${take.abPath}`, ` plays BEFORE then AFTER, twice — say "keep the old one" to revert`].join("\n");
|
|
816
|
-
return {
|
|
817
|
-
abPath: take.abPath,
|
|
818
|
-
legend
|
|
819
|
-
};
|
|
1001
|
+
function emit(res, event, data) {
|
|
1002
|
+
res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
|
|
820
1003
|
}
|
|
821
|
-
|
|
822
|
-
|
|
1004
|
+
/** Bind to 127.0.0.1 only; if the port is taken, try the next few. Resolves the
|
|
1005
|
+
* port actually BOUND, which differs from the requested one when port 0 asks
|
|
1006
|
+
* the OS to choose (how the tests get an isolated server). */
|
|
1007
|
+
function listen(server, startPort) {
|
|
823
1008
|
return new Promise((res, rej) => {
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
"
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
async function concatWithGaps(clips, outPath, v) {
|
|
839
|
-
const args = [
|
|
840
|
-
"-y",
|
|
841
|
-
"-loglevel",
|
|
842
|
-
"error"
|
|
843
|
-
];
|
|
844
|
-
for (const c of clips) args.push("-i", resolve(c));
|
|
845
|
-
args.push("-f", "lavfi", "-t", "0.35", "-i", `color=c=black:s=${v.width}x${v.height}:r=${v.fps}`);
|
|
846
|
-
const gapIdx = clips.length;
|
|
847
|
-
const seq = [];
|
|
848
|
-
const gapUses = clips.length - 1;
|
|
849
|
-
const split = gapUses > 1 ? `[${gapIdx}:v]split=${gapUses}${Array.from({ length: gapUses }, (_, i) => `[g${i}]`).join("")};` : "";
|
|
850
|
-
clips.forEach((_, i) => {
|
|
851
|
-
seq.push(`[${i}:v]`);
|
|
852
|
-
if (i < clips.length - 1) seq.push(gapUses > 1 ? `[g${i}]` : `[${gapIdx}:v]`);
|
|
1009
|
+
let port = startPort;
|
|
1010
|
+
const tryOne = () => {
|
|
1011
|
+
server.once("error", (e) => {
|
|
1012
|
+
if (e.code === "EADDRINUSE" && port < startPort + 20) {
|
|
1013
|
+
port++;
|
|
1014
|
+
tryOne();
|
|
1015
|
+
} else rej(e);
|
|
1016
|
+
});
|
|
1017
|
+
server.listen(port, "127.0.0.1", () => {
|
|
1018
|
+
const addr = server.address();
|
|
1019
|
+
res(addr && typeof addr === "object" ? addr.port : port);
|
|
1020
|
+
});
|
|
1021
|
+
};
|
|
1022
|
+
tryOne();
|
|
853
1023
|
});
|
|
854
|
-
const filter = `${split}${seq.join("")}concat=n=${seq.length}:v=1:a=0[v]`;
|
|
855
|
-
args.push("-filter_complex", filter, "-map", "[v]", "-c:v", "libx264", "-crf", "18", "-pix_fmt", "yuv420p", "-r", String(v.fps), "-an", resolve(outPath));
|
|
856
|
-
await ffmpeg(args);
|
|
857
|
-
}
|
|
858
|
-
/** Spawn a fire-and-forget opener. spawn ENOENT arrives ASYNCHRONOUSLY on the
|
|
859
|
-
* child's "error" event (a try/catch around spawn never sees it and the
|
|
860
|
-
* process would die with an unhandled error) — so degrade via the listener. */
|
|
861
|
-
function spawnOpener(cmd, args, fallbackLine, useShell = false) {
|
|
862
|
-
try {
|
|
863
|
-
const c = spawn(cmd, args, {
|
|
864
|
-
stdio: "ignore",
|
|
865
|
-
detached: true,
|
|
866
|
-
shell: useShell
|
|
867
|
-
});
|
|
868
|
-
c.on("error", () => process.stdout.write(fallbackLine));
|
|
869
|
-
c.unref();
|
|
870
|
-
} catch {
|
|
871
|
-
process.stdout.write(fallbackLine);
|
|
872
|
-
}
|
|
873
|
-
}
|
|
874
|
-
/** Open a file with the OS default player; degrade to printing the path. */
|
|
875
|
-
function openPath(p) {
|
|
876
|
-
const abs = resolve(p);
|
|
877
|
-
const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
|
|
878
|
-
spawnOpener(cmd, [abs], `open: ${abs}\n`, process.platform === "win32");
|
|
879
|
-
}
|
|
880
|
-
/** Reveal a file in Finder/Explorer; degrade to printing the path. */
|
|
881
|
-
function revealPath(p) {
|
|
882
|
-
const abs = resolve(p);
|
|
883
|
-
if (process.platform === "darwin") spawnOpener("open", ["-R", abs], `at: ${abs}\n`);
|
|
884
|
-
else if (process.platform === "win32") spawnOpener("explorer", [`/select,${abs}`], `at: ${abs}\n`, true);
|
|
885
|
-
else spawnOpener("xdg-open", [dirname(abs)], `at: ${abs}\n`);
|
|
886
1024
|
}
|
|
887
1025
|
|
|
888
1026
|
//#endregion
|
|
@@ -958,7 +1096,8 @@ async function renderComposition(opts) {
|
|
|
958
1096
|
logProgress: opts.logProgress ?? false,
|
|
959
1097
|
chromePath,
|
|
960
1098
|
captureLog: opts.captureLog,
|
|
961
|
-
onProgress: opts.onProgress
|
|
1099
|
+
onProgress: opts.onProgress,
|
|
1100
|
+
writeCompositionSibling: opts.writeCompositionSibling
|
|
962
1101
|
});
|
|
963
1102
|
}
|
|
964
1103
|
/** Convenience: load a `*.composition.json` and re-render over a saved capture.
|