rushes 0.2.0
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/.claude-plugin/marketplace.json +14 -0
- package/.claude-plugin/plugin.json +10 -0
- package/.mcp.json +8 -0
- package/AGENTS.md +110 -0
- package/LICENSE +21 -0
- package/README.md +204 -0
- package/dist/cli/demo.js +339 -0
- package/dist/cli/doctor.js +307 -0
- package/dist/cli/index.js +11 -0
- package/dist/cli/main.js +469 -0
- package/dist/core/batches.js +58 -0
- package/dist/core/errors.js +37 -0
- package/dist/core/exportNotes.js +132 -0
- package/dist/core/ids.js +42 -0
- package/dist/core/media.js +128 -0
- package/dist/core/notes.js +117 -0
- package/dist/core/paths.js +17 -0
- package/dist/core/project.js +200 -0
- package/dist/core/schema.js +220 -0
- package/dist/core/script.js +113 -0
- package/dist/core/store.js +132 -0
- package/dist/core/tabs.js +24 -0
- package/dist/mcp/client.js +50 -0
- package/dist/mcp/ensure.js +69 -0
- package/dist/mcp/stdio.js +47 -0
- package/dist/mcp/tools.js +191 -0
- package/dist/server/app.js +710 -0
- package/dist/server/assets.js +234 -0
- package/dist/server/files.js +170 -0
- package/dist/server/lock.js +148 -0
- package/dist/server/loudness.js +331 -0
- package/dist/server/proxy.js +575 -0
- package/dist/server/reveal.js +71 -0
- package/dist/server/start.js +125 -0
- package/dist/server/watch.js +133 -0
- package/dist/setup/env.js +42 -0
- package/dist/setup/harnesses.js +93 -0
- package/dist/setup/setup.js +92 -0
- package/package.json +54 -0
- package/skills/rushes/SKILL.md +30 -0
- package/web-dist/assets/figtree-latin-400-normal-BD4aNku5.woff +0 -0
- package/web-dist/assets/figtree-latin-400-normal-g7Dtegnw.woff2 +0 -0
- package/web-dist/assets/figtree-latin-500-normal-BWnGEVsr.woff2 +0 -0
- package/web-dist/assets/figtree-latin-500-normal-B_yy1phn.woff +0 -0
- package/web-dist/assets/figtree-latin-600-normal-BM_oTo4n.woff +0 -0
- package/web-dist/assets/figtree-latin-600-normal-Cv_xCTDl.woff2 +0 -0
- package/web-dist/assets/figtree-latin-700-normal-BCCt2T1q.woff +0 -0
- package/web-dist/assets/figtree-latin-700-normal-th6qEP7c.woff2 +0 -0
- package/web-dist/assets/figtree-latin-ext-400-normal-CXAzuTZb.woff2 +0 -0
- package/web-dist/assets/figtree-latin-ext-400-normal-Gbh-3PTk.woff +0 -0
- package/web-dist/assets/figtree-latin-ext-500-normal-BOEJvrb_.woff +0 -0
- package/web-dist/assets/figtree-latin-ext-500-normal-Du0rIuHj.woff2 +0 -0
- package/web-dist/assets/figtree-latin-ext-600-normal-CQqsimlV.woff +0 -0
- package/web-dist/assets/figtree-latin-ext-600-normal-DJlfifZj.woff2 +0 -0
- package/web-dist/assets/figtree-latin-ext-700-normal-BoDi4KL2.woff +0 -0
- package/web-dist/assets/figtree-latin-ext-700-normal-CS4sQkGV.woff2 +0 -0
- package/web-dist/assets/index-BjDIT_wL.css +1 -0
- package/web-dist/assets/index-DF6Rl-Ac.js +3 -0
- package/web-dist/assets/jetbrains-mono-cyrillic-400-normal-BEIGL1Tu.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-cyrillic-400-normal-ugxPyKxw.woff +0 -0
- package/web-dist/assets/jetbrains-mono-cyrillic-500-normal-DJqRU3vO.woff +0 -0
- package/web-dist/assets/jetbrains-mono-cyrillic-500-normal-DmUKJPL_.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-greek-400-normal-B9oWc5Lo.woff +0 -0
- package/web-dist/assets/jetbrains-mono-greek-400-normal-C190GLew.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-greek-500-normal-D7SFKleX.woff +0 -0
- package/web-dist/assets/jetbrains-mono-greek-500-normal-JpySY46c.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-latin-400-normal-6-qcROiO.woff +0 -0
- package/web-dist/assets/jetbrains-mono-latin-400-normal-V6pRDFza.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-latin-500-normal-BWZEU5yA.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-latin-500-normal-CJOVTJB7.woff +0 -0
- package/web-dist/assets/jetbrains-mono-latin-ext-400-normal-Bc8Ftmh3.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-latin-ext-400-normal-fXTG6kC5.woff +0 -0
- package/web-dist/assets/jetbrains-mono-latin-ext-500-normal-Cut-4mMH.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-latin-ext-500-normal-ckzbgY84.woff +0 -0
- package/web-dist/assets/jetbrains-mono-vietnamese-400-normal-CqNFfHCs.woff +0 -0
- package/web-dist/assets/jetbrains-mono-vietnamese-500-normal-DNRqzVM1.woff +0 -0
- package/web-dist/index.html +14 -0
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
import { spawn as nodeSpawn } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { access, constants, mkdir, readdir, rename, rm, stat } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { RushesError, NotFoundError } from "../core/errors.js";
|
|
6
|
+
import { hasFfprobe, probe as ffprobe, proxyNeed } from "../core/media.js";
|
|
7
|
+
import { fromManifestPath } from "../core/paths.js";
|
|
8
|
+
/** Grace period between SIGTERM and SIGKILL when a job is cancelled. */
|
|
9
|
+
export const KILL_GRACE_MS = 2_000;
|
|
10
|
+
/** Only the tail of ffmpeg's stderr is kept: a long encode prints a stats line twice a second. */
|
|
11
|
+
const STDERR_TAIL = 16_384;
|
|
12
|
+
/** A runner over `spawn`. `defaultFfmpeg` is this over Node's own; tests drive the kill logic with a fake child. */
|
|
13
|
+
export function makeFfmpegRunner(spawn = nodeSpawn) {
|
|
14
|
+
return (args, opts = {}) => new Promise((resolve) => {
|
|
15
|
+
const { signal, onStdout } = opts;
|
|
16
|
+
if (signal?.aborted) {
|
|
17
|
+
resolve({ code: 1, stderr: "" });
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
let stderr = "";
|
|
21
|
+
const child = spawn("ffmpeg", args, {
|
|
22
|
+
stdio: ["ignore", onStdout ? "pipe" : "ignore", "pipe"],
|
|
23
|
+
env: { ...process.env, LC_ALL: "C" },
|
|
24
|
+
});
|
|
25
|
+
const onErr = (d) => {
|
|
26
|
+
stderr += d.toString();
|
|
27
|
+
if (stderr.length > STDERR_TAIL * 2)
|
|
28
|
+
stderr = stderr.slice(-STDERR_TAIL);
|
|
29
|
+
};
|
|
30
|
+
const onOut = (d) => onStdout?.(d);
|
|
31
|
+
child.stderr?.on("data", onErr);
|
|
32
|
+
child.stdout?.on("data", onOut);
|
|
33
|
+
let killTimer;
|
|
34
|
+
const onAbort = () => {
|
|
35
|
+
child.kill("SIGTERM");
|
|
36
|
+
killTimer = setTimeout(() => {
|
|
37
|
+
// Still alive past the grace period (SIGTERM ignored or still shutting down): escalate.
|
|
38
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
39
|
+
child.kill("SIGKILL");
|
|
40
|
+
}, KILL_GRACE_MS);
|
|
41
|
+
};
|
|
42
|
+
signal?.addEventListener("abort", onAbort);
|
|
43
|
+
const cleanup = () => {
|
|
44
|
+
child.stderr?.off("data", onErr);
|
|
45
|
+
child.stdout?.off("data", onOut);
|
|
46
|
+
signal?.removeEventListener("abort", onAbort);
|
|
47
|
+
if (killTimer)
|
|
48
|
+
clearTimeout(killTimer);
|
|
49
|
+
};
|
|
50
|
+
child.on("error", () => {
|
|
51
|
+
cleanup();
|
|
52
|
+
resolve({ code: 1, stderr });
|
|
53
|
+
});
|
|
54
|
+
child.on("close", (code) => {
|
|
55
|
+
cleanup();
|
|
56
|
+
resolve({ code: code ?? 1, stderr: stderr.slice(-STDERR_TAIL) });
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
export const defaultFfmpeg = makeFfmpegRunner();
|
|
61
|
+
/** The visible folder proxies live in, at the project root. */
|
|
62
|
+
export const PROXY_DIR = "proxies";
|
|
63
|
+
/** A proxy file this server writes: proxies/<film-slug>_<version>_proxy.mp4. */
|
|
64
|
+
export const PROXY_PATH = /^proxies\/[a-z0-9][a-z0-9-]*_v\d+_proxy\.mp4$/;
|
|
65
|
+
/** "proxies/hero-60s_v3_proxy.mp4" */
|
|
66
|
+
export function proxyPath(video, version) {
|
|
67
|
+
return `${PROXY_DIR}/${video}_${version}_proxy.mp4`;
|
|
68
|
+
}
|
|
69
|
+
/** The half-written file for `proxyPath`: never recorded, deleted on cancel, failure and start-up. */
|
|
70
|
+
export function partialPath(video, version) {
|
|
71
|
+
return `${PROXY_DIR}/${video}_${version}_proxy.partial.mp4`;
|
|
72
|
+
}
|
|
73
|
+
/** "ffmpeg version 4.4.2-0ubuntu0.22.04.1 Copyright …" -> 4.4. Null for a git build ("N-112345-g…") or junk. */
|
|
74
|
+
export function parseFfmpegVersion(text) {
|
|
75
|
+
const m = /ffmpeg version n?(\d+)\.(\d+)/.exec(text);
|
|
76
|
+
return m ? { major: Number(m[1]), minor: Number(m[2]) } : null;
|
|
77
|
+
}
|
|
78
|
+
/** `-fps_mode` arrived in ffmpeg 5.1; before that the same option is `-vsync`. An unknown version is taken as current. */
|
|
79
|
+
export function hasFpsMode(v) {
|
|
80
|
+
return v === null || v.major > 5 || (v.major === 5 && v.minor >= 1);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* §19.5's encode, exactly: H.264 at most 1920 px on the long edge, same frame rate and length,
|
|
84
|
+
* yuv420p, AAC, faststart. On ffmpeg before 5.1, `-vsync passthrough` stands in for
|
|
85
|
+
* `-fps_mode passthrough`, which it doesn't know.
|
|
86
|
+
*/
|
|
87
|
+
export function encodeArgs(orig, out, version = null) {
|
|
88
|
+
const passthrough = hasFpsMode(version) ? ["-fps_mode", "passthrough"] : ["-vsync", "passthrough"];
|
|
89
|
+
return [
|
|
90
|
+
"-hide_banner", "-y", "-i", orig,
|
|
91
|
+
"-vf", "scale='if(gt(iw,ih),min(1920,iw),-2)':'if(gt(iw,ih),-2,min(1920,ih))'",
|
|
92
|
+
"-c:v", "libx264", "-preset", "veryfast", "-crf", "23", "-pix_fmt", "yuv420p", ...passthrough,
|
|
93
|
+
"-c:a", "aac", "-b:a", "160k", "-movflags", "+faststart",
|
|
94
|
+
"-progress", "pipe:1", out,
|
|
95
|
+
];
|
|
96
|
+
}
|
|
97
|
+
/** The proxy's size from the original's, by the same rule as the scale filter (even, at most 1920 on the long edge). */
|
|
98
|
+
export function scaledSize(width, height) {
|
|
99
|
+
const even = (n) => Math.max(2, Math.round(n / 2) * 2);
|
|
100
|
+
if (width > height) {
|
|
101
|
+
const w = Math.min(1920, width);
|
|
102
|
+
return { width: w, height: w === width ? even(height) : even((height * w) / width) };
|
|
103
|
+
}
|
|
104
|
+
const h = Math.min(1920, height);
|
|
105
|
+
return { width: h === height ? even(width) : even((width * h) / height), height: h };
|
|
106
|
+
}
|
|
107
|
+
export const NEED_CACHE_LIMIT = 500;
|
|
108
|
+
/** Background probes for GET /api/state run this many at a time. */
|
|
109
|
+
export const PROBE_CONCURRENCY = 2;
|
|
110
|
+
/**
|
|
111
|
+
* The proxy jobs of one server, at most one per cut. Progress and the end state of every job go
|
|
112
|
+
* out as a `proxy` event on the store, which the SSE route broadcasts to every open tab.
|
|
113
|
+
*/
|
|
114
|
+
export class ProxyJobs {
|
|
115
|
+
store;
|
|
116
|
+
run;
|
|
117
|
+
probeFile;
|
|
118
|
+
isAvailable;
|
|
119
|
+
availableOnce = null;
|
|
120
|
+
versionOnce = null;
|
|
121
|
+
byVersion = new Map();
|
|
122
|
+
byId = new Map();
|
|
123
|
+
closing = false;
|
|
124
|
+
// Why each file (by path, size and mtime) needs a proxy, so a file revision is probed once.
|
|
125
|
+
needs = new Map();
|
|
126
|
+
needCacheLimit;
|
|
127
|
+
// Background probes: queued by revision key, run PROBE_CONCURRENCY at a time.
|
|
128
|
+
probeQueue = new Map();
|
|
129
|
+
probing = new Set();
|
|
130
|
+
probeIdle = null;
|
|
131
|
+
// Frame extractions in flight, by original and time: identical requests share one ffmpeg.
|
|
132
|
+
frames = new Map();
|
|
133
|
+
// Every frame extraction and probe in flight, so close() can stop them rather than leave orphans.
|
|
134
|
+
inflight = new Set();
|
|
135
|
+
constructor(store, opts = {}) {
|
|
136
|
+
this.store = store;
|
|
137
|
+
this.run = opts.run ?? defaultFfmpeg;
|
|
138
|
+
this.probeFile = opts.probe ?? ((abs, signal) => ffprobe(abs, { signal }));
|
|
139
|
+
this.needCacheLimit = opts.needCacheLimit ?? NEED_CACHE_LIMIT;
|
|
140
|
+
const injected = opts.available;
|
|
141
|
+
this.isAvailable = () => injected
|
|
142
|
+
? injected()
|
|
143
|
+
: (async () => {
|
|
144
|
+
const [{ ok }, probeOk] = await Promise.all([this.ffmpegInfo(), hasFfprobe()]);
|
|
145
|
+
return ok && probeOk;
|
|
146
|
+
})();
|
|
147
|
+
}
|
|
148
|
+
/** `ffmpeg -version`, run once: whether it ran, and its version (null when unparseable). */
|
|
149
|
+
ffmpegInfo() {
|
|
150
|
+
this.versionOnce ??= (async () => {
|
|
151
|
+
let text = "";
|
|
152
|
+
try {
|
|
153
|
+
const r = await this.run(["-version"], { onStdout: (c) => (text += c.toString()) });
|
|
154
|
+
return { ok: r.code === 0, version: parseFfmpegVersion(text) };
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return { ok: false, version: null };
|
|
158
|
+
}
|
|
159
|
+
})();
|
|
160
|
+
return this.versionOnce;
|
|
161
|
+
}
|
|
162
|
+
/** Whether ffmpeg and ffprobe are both there. Checked once per server. */
|
|
163
|
+
available() {
|
|
164
|
+
this.availableOnce ??= Promise.resolve()
|
|
165
|
+
.then(() => this.isAvailable())
|
|
166
|
+
.catch(() => false);
|
|
167
|
+
return this.availableOnce;
|
|
168
|
+
}
|
|
169
|
+
/** Throws 501 `no_ffmpeg` when ffmpeg or ffprobe is missing. */
|
|
170
|
+
async requireFfmpeg() {
|
|
171
|
+
if (!(await this.available())) {
|
|
172
|
+
throw new RushesError("Proxies and full-quality frames need ffmpeg, which isn't installed. Run `rushes doctor` for how to add it.", 501, "no_ffmpeg");
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/** True once the server has started closing: no new job starts. */
|
|
176
|
+
get isClosing() {
|
|
177
|
+
return this.closing;
|
|
178
|
+
}
|
|
179
|
+
/** The running job for a cut, if there is one. */
|
|
180
|
+
find(video, version) {
|
|
181
|
+
const r = this.byVersion.get(key(video, version));
|
|
182
|
+
return r ? { ...r.job } : undefined;
|
|
183
|
+
}
|
|
184
|
+
/** Every running job, for GET /api/state. */
|
|
185
|
+
list() {
|
|
186
|
+
return [...this.byId.values()].map((r) => ({ ...r.job }));
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Starts making a proxy for a cut, or returns the job already making one (Review Focus 2: two
|
|
190
|
+
* tabs or a double click get one job between them). The caller checks the cut exists first.
|
|
191
|
+
* Refused (503 `closing`) once the server has started closing.
|
|
192
|
+
*/
|
|
193
|
+
start(video, version) {
|
|
194
|
+
const existing = this.byVersion.get(key(video, version));
|
|
195
|
+
if (existing)
|
|
196
|
+
return { ...existing.job };
|
|
197
|
+
if (this.closing)
|
|
198
|
+
throw new RushesError("Rushes is closing, so it isn't starting a proxy", 503, "closing");
|
|
199
|
+
const job = { id: randomUUID(), video, version, pct: 0, state: "running" };
|
|
200
|
+
const r = { job, controller: new AbortController(), committing: false, cancelled: false, settled: undefined };
|
|
201
|
+
this.byVersion.set(key(video, version), r);
|
|
202
|
+
this.byId.set(job.id, r);
|
|
203
|
+
this.emit(job);
|
|
204
|
+
r.settled = this.execute(r).finally(() => {
|
|
205
|
+
this.byVersion.delete(key(video, version));
|
|
206
|
+
this.byId.delete(job.id);
|
|
207
|
+
});
|
|
208
|
+
return { ...job };
|
|
209
|
+
}
|
|
210
|
+
/** Resolves with the job's end state. Throws 404 when no such job is running. */
|
|
211
|
+
wait(jobId) {
|
|
212
|
+
const r = this.byId.get(jobId);
|
|
213
|
+
if (!r)
|
|
214
|
+
throw new NotFoundError("proxy job", jobId);
|
|
215
|
+
return r.settled;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Stops a job: SIGTERM, then SIGKILL after 2 s, and the partial file deleted. Resolves once
|
|
219
|
+
* it's all cleaned up, with the job's end state -- `done`, not `cancelled`, if the encode had
|
|
220
|
+
* already finished and was being recorded (nothing is left to kill then).
|
|
221
|
+
*/
|
|
222
|
+
async cancel(jobId) {
|
|
223
|
+
const r = this.byId.get(jobId);
|
|
224
|
+
if (!r)
|
|
225
|
+
throw new NotFoundError("proxy job", jobId);
|
|
226
|
+
if (!r.committing) {
|
|
227
|
+
r.cancelled = true;
|
|
228
|
+
r.controller.abort();
|
|
229
|
+
}
|
|
230
|
+
return r.settled;
|
|
231
|
+
}
|
|
232
|
+
/** Stops taking new jobs, cancels every running one, and stops every frame extraction and probe in flight. Used when the server closes. */
|
|
233
|
+
async close() {
|
|
234
|
+
this.closing = true;
|
|
235
|
+
for (const c of this.inflight)
|
|
236
|
+
c.abort();
|
|
237
|
+
await Promise.all([...this.byId.keys()].map((id) => this.cancel(id).catch(() => undefined)));
|
|
238
|
+
}
|
|
239
|
+
/** Runs `work` with a signal close() aborts. Once closing, the signal starts aborted. */
|
|
240
|
+
async tracked(work) {
|
|
241
|
+
const c = new AbortController();
|
|
242
|
+
if (this.closing)
|
|
243
|
+
c.abort();
|
|
244
|
+
this.inflight.add(c);
|
|
245
|
+
try {
|
|
246
|
+
return await work(c.signal);
|
|
247
|
+
}
|
|
248
|
+
finally {
|
|
249
|
+
this.inflight.delete(c);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/** A file's duration, codec and size (ffprobe, unless a test injects a fake). Stopped by close(): nothing known then. */
|
|
253
|
+
probe(abs) {
|
|
254
|
+
return this.tracked((signal) => this.probeFile(abs, signal));
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Why a cut needs a proxy (§19.5), or null, without waiting on a probe (ruling B): a file
|
|
258
|
+
* revision (path, size and mtime) already probed is answered from the cache; one not yet probed
|
|
259
|
+
* is null for now and queued for a background probe, two at a time. When that probe finds a
|
|
260
|
+
* need, a `change` event goes out so every tab reads the state again. Null without ffmpeg, and
|
|
261
|
+
* for a file that isn't there.
|
|
262
|
+
*/
|
|
263
|
+
async needNow(abs) {
|
|
264
|
+
if (!(await this.available()))
|
|
265
|
+
return null;
|
|
266
|
+
const rev = await revision(abs);
|
|
267
|
+
if (!rev)
|
|
268
|
+
return null;
|
|
269
|
+
if (this.needs.has(rev.key))
|
|
270
|
+
return this.needs.get(rev.key);
|
|
271
|
+
if (!this.probing.has(rev.key) && !this.probeQueue.has(rev.key)) {
|
|
272
|
+
this.probeQueue.set(rev.key, { abs, size: rev.size });
|
|
273
|
+
this.pumpProbes();
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
/** As `needNow`, from a probe the caller already has (adding a cut): no second probe, no waiting. */
|
|
278
|
+
async needFrom(abs, known) {
|
|
279
|
+
if (!(await this.available()))
|
|
280
|
+
return null;
|
|
281
|
+
const rev = await revision(abs);
|
|
282
|
+
if (!rev)
|
|
283
|
+
return null;
|
|
284
|
+
const reason = proxyNeed(known, rev.size)?.reason ?? null;
|
|
285
|
+
this.remember(rev.key, reason);
|
|
286
|
+
return reason;
|
|
287
|
+
}
|
|
288
|
+
/** Resolves once no background probe is queued or running. For tests. */
|
|
289
|
+
probesIdle() {
|
|
290
|
+
if (this.probing.size === 0 && this.probeQueue.size === 0)
|
|
291
|
+
return Promise.resolve();
|
|
292
|
+
if (!this.probeIdle) {
|
|
293
|
+
let resolve;
|
|
294
|
+
const promise = new Promise((r) => (resolve = r));
|
|
295
|
+
this.probeIdle = { promise, resolve };
|
|
296
|
+
}
|
|
297
|
+
return this.probeIdle.promise;
|
|
298
|
+
}
|
|
299
|
+
remember(k, reason) {
|
|
300
|
+
this.needs.delete(k);
|
|
301
|
+
this.needs.set(k, reason);
|
|
302
|
+
while (this.needs.size > this.needCacheLimit)
|
|
303
|
+
this.needs.delete(this.needs.keys().next().value);
|
|
304
|
+
}
|
|
305
|
+
pumpProbes() {
|
|
306
|
+
while (this.probing.size < PROBE_CONCURRENCY && this.probeQueue.size > 0) {
|
|
307
|
+
const [k, item] = this.probeQueue.entries().next().value;
|
|
308
|
+
this.probeQueue.delete(k);
|
|
309
|
+
this.probing.add(k);
|
|
310
|
+
void (async () => {
|
|
311
|
+
let reason = null;
|
|
312
|
+
try {
|
|
313
|
+
const p = await this.probe(item.abs);
|
|
314
|
+
reason = proxyNeed(p, item.size)?.reason ?? null;
|
|
315
|
+
}
|
|
316
|
+
catch {
|
|
317
|
+
reason = null;
|
|
318
|
+
}
|
|
319
|
+
this.remember(k, reason);
|
|
320
|
+
// Announced before the slot is freed, so "no probe running" (probesIdle) also means
|
|
321
|
+
// every change a probe owed has gone out.
|
|
322
|
+
if (reason !== null)
|
|
323
|
+
await this.announceNeed();
|
|
324
|
+
this.probing.delete(k);
|
|
325
|
+
this.pumpProbes();
|
|
326
|
+
if (this.probing.size === 0 && this.probeQueue.size === 0 && this.probeIdle) {
|
|
327
|
+
this.probeIdle.resolve();
|
|
328
|
+
this.probeIdle = null;
|
|
329
|
+
}
|
|
330
|
+
})();
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/** A `change` for project.json at its current rev, so every tab reads the state (and the new need) again. */
|
|
334
|
+
async announceNeed() {
|
|
335
|
+
try {
|
|
336
|
+
const { rev } = await this.store.read("project");
|
|
337
|
+
this.store.emit("change", { file: "project", rev });
|
|
338
|
+
}
|
|
339
|
+
catch {
|
|
340
|
+
// project.json unreadable right now: the watcher reports that; nothing to announce.
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* The PNG of the frame at `seconds` in `orig`. Identical requests in flight share one ffmpeg
|
|
345
|
+
* (Grab Frame pressed twice, or two tabs on the same frame), so a burst never stacks up decodes.
|
|
346
|
+
*/
|
|
347
|
+
frame(orig, seconds) {
|
|
348
|
+
const k = `${orig}\0${seconds.toFixed(6)}`;
|
|
349
|
+
const flying = this.frames.get(k);
|
|
350
|
+
if (flying)
|
|
351
|
+
return flying;
|
|
352
|
+
if (this.closing)
|
|
353
|
+
return Promise.resolve(null);
|
|
354
|
+
const flight = this.tracked((signal) => extractFrame(this.run, orig, seconds, FRAME_TIMEOUT_MS, signal)).finally(() => this.frames.delete(k));
|
|
355
|
+
this.frames.set(k, flight);
|
|
356
|
+
return flight;
|
|
357
|
+
}
|
|
358
|
+
emit(job) {
|
|
359
|
+
const e = { job: job.id, video: job.video, version: job.version, pct: job.pct, state: job.state };
|
|
360
|
+
if (job.reason !== undefined)
|
|
361
|
+
e.reason = job.reason;
|
|
362
|
+
this.store.emit("proxy", e);
|
|
363
|
+
}
|
|
364
|
+
async execute(r) {
|
|
365
|
+
const { job } = r;
|
|
366
|
+
const root = this.store.root;
|
|
367
|
+
const partial = fromManifestPath(root, partialPath(job.video, job.version));
|
|
368
|
+
const final = fromManifestPath(root, proxyPath(job.video, job.version));
|
|
369
|
+
const end = (state, reason) => {
|
|
370
|
+
job.state = state;
|
|
371
|
+
if (state === "done")
|
|
372
|
+
job.pct = 100;
|
|
373
|
+
if (reason !== undefined)
|
|
374
|
+
job.reason = reason;
|
|
375
|
+
this.emit(job);
|
|
376
|
+
return { ...job };
|
|
377
|
+
};
|
|
378
|
+
const fail = async (reason) => {
|
|
379
|
+
await rm(partial, { force: true }).catch(() => undefined);
|
|
380
|
+
return end("failed", reason);
|
|
381
|
+
};
|
|
382
|
+
const cancelled = async () => {
|
|
383
|
+
await rm(partial, { force: true }).catch(() => undefined);
|
|
384
|
+
return end("cancelled");
|
|
385
|
+
};
|
|
386
|
+
try {
|
|
387
|
+
const project = await this.store.read("project");
|
|
388
|
+
const version = findVersion(project, job.video, job.version);
|
|
389
|
+
if (!version)
|
|
390
|
+
return await fail("That cut isn't in the project any more");
|
|
391
|
+
// A hand-edited id ("../x") must never put a file outside proxies/.
|
|
392
|
+
if (!PROXY_PATH.test(proxyPath(job.video, job.version)))
|
|
393
|
+
return await fail("Rushes can't name a proxy for this cut");
|
|
394
|
+
const orig = fromManifestPath(root, version.file);
|
|
395
|
+
if (!(await readableFile(orig)))
|
|
396
|
+
return await fail("The original file is missing");
|
|
397
|
+
const [origProbe, ffmpeg] = await Promise.all([this.probe(orig).catch(() => null), this.ffmpegInfo()]);
|
|
398
|
+
const duration = version.duration ?? origProbe?.duration ?? null;
|
|
399
|
+
await mkdir(join(root, PROXY_DIR), { recursive: true });
|
|
400
|
+
if (r.cancelled)
|
|
401
|
+
return await cancelled();
|
|
402
|
+
let buffered = "";
|
|
403
|
+
const onStdout = (chunk) => {
|
|
404
|
+
buffered += chunk.toString();
|
|
405
|
+
const lines = buffered.split("\n");
|
|
406
|
+
buffered = lines.pop() ?? "";
|
|
407
|
+
for (const line of lines) {
|
|
408
|
+
const m = /^out_time_us=(\d+)/.exec(line.trim());
|
|
409
|
+
// With no known length (null, or 0), progress stays at 0 until the job is done.
|
|
410
|
+
if (!m || !duration)
|
|
411
|
+
continue;
|
|
412
|
+
const pct = Math.min(99, Math.floor((Number(m[1]) / 1e6 / duration) * 100));
|
|
413
|
+
if (pct > job.pct && job.state === "running" && !r.cancelled) {
|
|
414
|
+
job.pct = pct;
|
|
415
|
+
this.emit(job);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
const res = await this.run(encodeArgs(orig, partial, ffmpeg.version), { signal: r.controller.signal, onStdout });
|
|
420
|
+
if (r.cancelled)
|
|
421
|
+
return await cancelled();
|
|
422
|
+
if (res.code !== 0)
|
|
423
|
+
return await fail(ffmpegReason(res.stderr));
|
|
424
|
+
r.committing = true;
|
|
425
|
+
let made;
|
|
426
|
+
try {
|
|
427
|
+
made = await stat(partial);
|
|
428
|
+
}
|
|
429
|
+
catch {
|
|
430
|
+
return await fail("ffmpeg finished without writing the proxy");
|
|
431
|
+
}
|
|
432
|
+
// Rename first, record second: a record only ever points at a whole file.
|
|
433
|
+
await rename(partial, final);
|
|
434
|
+
// Not stopped by close(): a committing job runs to the end, and this probe is of a local file it just wrote.
|
|
435
|
+
const out = await this.probeFile(final).catch(() => null);
|
|
436
|
+
const size = out?.width && out?.height
|
|
437
|
+
? { width: out.width, height: out.height }
|
|
438
|
+
: origProbe?.width && origProbe?.height
|
|
439
|
+
? scaledSize(origProbe.width, origProbe.height)
|
|
440
|
+
: null;
|
|
441
|
+
if (!size) {
|
|
442
|
+
await this.dropUnrecorded(final, job);
|
|
443
|
+
return await fail("Couldn't read the proxy's size");
|
|
444
|
+
}
|
|
445
|
+
let recorded = false;
|
|
446
|
+
try {
|
|
447
|
+
await this.store.update("project", (p) => {
|
|
448
|
+
const v = findVersion(p, job.video, job.version);
|
|
449
|
+
if (!v)
|
|
450
|
+
return;
|
|
451
|
+
v.proxy = { file: proxyPath(job.video, job.version), width: size.width, height: size.height, bytes: made.size, createdAt: new Date().toISOString() };
|
|
452
|
+
recorded = true;
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
catch (e) {
|
|
456
|
+
await this.dropUnrecorded(final, job);
|
|
457
|
+
return await fail(`Couldn't record the proxy: ${e.message}`);
|
|
458
|
+
}
|
|
459
|
+
if (!recorded) {
|
|
460
|
+
await this.dropUnrecorded(final, job);
|
|
461
|
+
return await fail("That cut isn't in the project any more");
|
|
462
|
+
}
|
|
463
|
+
return end("done");
|
|
464
|
+
}
|
|
465
|
+
catch (e) {
|
|
466
|
+
if (r.cancelled && !r.committing)
|
|
467
|
+
return await cancelled();
|
|
468
|
+
return await fail(`Couldn't make the proxy: ${e.message}`);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* A proxy that was renamed into place but couldn't be recorded is removed -- unless the cut's
|
|
473
|
+
* existing record already points at that same file (a re-render): then the file is a valid
|
|
474
|
+
* proxy the record still describes, and deleting it would leave the record pointing at nothing.
|
|
475
|
+
*/
|
|
476
|
+
async dropUnrecorded(final, job) {
|
|
477
|
+
let keep = false;
|
|
478
|
+
try {
|
|
479
|
+
const v = findVersion(await this.store.read("project"), job.video, job.version);
|
|
480
|
+
keep = v?.proxy?.file === proxyPath(job.video, job.version);
|
|
481
|
+
}
|
|
482
|
+
catch {
|
|
483
|
+
keep = false;
|
|
484
|
+
}
|
|
485
|
+
if (!keep)
|
|
486
|
+
await rm(final, { force: true }).catch(() => undefined);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
function key(video, version) {
|
|
490
|
+
return `${video}/${version}`;
|
|
491
|
+
}
|
|
492
|
+
/** A file's revision key (path, size and mtime) and size, or null when it isn't a file. */
|
|
493
|
+
async function revision(abs) {
|
|
494
|
+
try {
|
|
495
|
+
const info = await stat(abs);
|
|
496
|
+
if (!info.isFile())
|
|
497
|
+
return null;
|
|
498
|
+
return { key: `${abs}\0${info.size}\0${info.mtimeMs}`, size: info.size };
|
|
499
|
+
}
|
|
500
|
+
catch {
|
|
501
|
+
return null;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
export function findVersion(p, video, version) {
|
|
505
|
+
return p.videos.find((v) => v.id === video)?.versions.find((v) => v.id === version);
|
|
506
|
+
}
|
|
507
|
+
async function readableFile(abs) {
|
|
508
|
+
try {
|
|
509
|
+
const info = await stat(abs);
|
|
510
|
+
if (!info.isFile())
|
|
511
|
+
return false;
|
|
512
|
+
await access(abs, constants.R_OK);
|
|
513
|
+
return true;
|
|
514
|
+
}
|
|
515
|
+
catch {
|
|
516
|
+
return false;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
/** ffmpeg's last words, in a sentence. */
|
|
520
|
+
function ffmpegReason(stderr) {
|
|
521
|
+
const lines = stderr
|
|
522
|
+
.split(/[\r\n]+/)
|
|
523
|
+
.map((l) => l.trim())
|
|
524
|
+
.filter((l) => l && !/^(frame=|size=|video:|\[out#)/.test(l));
|
|
525
|
+
const last = lines[lines.length - 1];
|
|
526
|
+
return last ? `ffmpeg stopped: ${last.slice(0, 300)}` : "ffmpeg stopped with an error";
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Start-up clean-up (Review Focus 1): a server that stopped mid-proxy leaves a `*.partial.mp4`
|
|
530
|
+
* behind. Nothing ever recorded it, so it's deleted. Returns the names removed.
|
|
531
|
+
*/
|
|
532
|
+
export async function removePartials(root) {
|
|
533
|
+
let names;
|
|
534
|
+
try {
|
|
535
|
+
names = await readdir(join(root, PROXY_DIR));
|
|
536
|
+
}
|
|
537
|
+
catch {
|
|
538
|
+
return [];
|
|
539
|
+
}
|
|
540
|
+
const partials = names.filter((n) => n.endsWith(".partial.mp4"));
|
|
541
|
+
await Promise.all(partials.map((n) => rm(join(root, PROXY_DIR, n), { force: true }).catch(() => undefined)));
|
|
542
|
+
return partials;
|
|
543
|
+
}
|
|
544
|
+
/** The PNG for one frame, as ffmpeg writes it. */
|
|
545
|
+
export function frameArgs(orig, seconds) {
|
|
546
|
+
return [
|
|
547
|
+
"-hide_banner", "-v", "error",
|
|
548
|
+
// -ss before -i seeks fast to the keyframe before, then accurate_seek decodes forward to the exact frame.
|
|
549
|
+
"-accurate_seek", "-ss", seconds.toFixed(6), "-i", orig,
|
|
550
|
+
"-frames:v", "1", "-an", "-pix_fmt", "rgb24", "-c:v", "png", "-f", "image2pipe", "pipe:1",
|
|
551
|
+
];
|
|
552
|
+
}
|
|
553
|
+
/** How long a single-frame extraction may take before it's killed. */
|
|
554
|
+
export const FRAME_TIMEOUT_MS = 30_000;
|
|
555
|
+
/** Runs `frameArgs` and collects the PNG. Null when ffmpeg failed or wrote nothing (a time past the end). */
|
|
556
|
+
export async function extractFrame(run, orig, seconds, timeoutMs = FRAME_TIMEOUT_MS, signal) {
|
|
557
|
+
const chunks = [];
|
|
558
|
+
const controller = new AbortController();
|
|
559
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
560
|
+
const stop = () => controller.abort();
|
|
561
|
+
if (signal?.aborted)
|
|
562
|
+
controller.abort();
|
|
563
|
+
signal?.addEventListener("abort", stop, { once: true });
|
|
564
|
+
try {
|
|
565
|
+
const res = await run(frameArgs(orig, seconds), { signal: controller.signal, onStdout: (c) => chunks.push(c) });
|
|
566
|
+
if (res.code !== 0 || controller.signal.aborted)
|
|
567
|
+
return null;
|
|
568
|
+
const png = Buffer.concat(chunks);
|
|
569
|
+
return png.length > 0 ? png : null;
|
|
570
|
+
}
|
|
571
|
+
finally {
|
|
572
|
+
clearTimeout(timer);
|
|
573
|
+
signal?.removeEventListener("abort", stop);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { stat } from "node:fs/promises";
|
|
3
|
+
import { dirname } from "node:path";
|
|
4
|
+
/**
|
|
5
|
+
* §16.3: the only extensions Open will act on, lower-case and without the dot. Anything else is
|
|
6
|
+
* refused with 415 `unsafe_type` before any stat or spawn happens -- Rushes never opens a script,
|
|
7
|
+
* app or archive, since on macOS opening those can run code.
|
|
8
|
+
*/
|
|
9
|
+
// svg is deliberately excluded: on macOS an SVG often opens in a browser and can carry script.
|
|
10
|
+
export const OPEN_SAFE_EXT = new Set([
|
|
11
|
+
"md", "txt", "pdf", "srt", "vtt",
|
|
12
|
+
"png", "jpg", "jpeg", "gif", "webp",
|
|
13
|
+
"mp4", "mov", "m4v", "webm", "mkv",
|
|
14
|
+
"wav", "mp3", "m4a", "aac", "flac", "ogg",
|
|
15
|
+
"prproj", "drp",
|
|
16
|
+
]);
|
|
17
|
+
/**
|
|
18
|
+
* Reveals `abs` in Finder (macOS), Explorer (Windows) or the default file manager (elsewhere),
|
|
19
|
+
* spawned without a shell, detached, with its own stdio ignored. Errors are swallowed (logged to
|
|
20
|
+
* stderr) rather than thrown, since a failed reveal shouldn't fail the request that asked for it.
|
|
21
|
+
*
|
|
22
|
+
* A directory (used for §16.5's `POST /api/reveal { project: true }`, which reveals the project
|
|
23
|
+
* root) is opened directly rather than selected inside its parent: macOS gets `open <abs>`
|
|
24
|
+
* instead of `open -R <abs>`, Windows gets `explorer.exe <abs>` instead of `/select,<abs>`, and
|
|
25
|
+
* the other platforms' `xdg-open` already takes a directory as-is.
|
|
26
|
+
*
|
|
27
|
+
* Honours RUSHES_NO_REVEAL=1: instead of spawning anything, it logs `reveal <abs>` to stderr.
|
|
28
|
+
* Tests set this (see vitest.config.ts and the e2e fixture) so nothing ever opens Finder.
|
|
29
|
+
*/
|
|
30
|
+
export const osRevealer = async (abs) => {
|
|
31
|
+
if (process.env.RUSHES_NO_REVEAL === "1") {
|
|
32
|
+
console.error(`reveal ${abs}`);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const isDir = await stat(abs).then((s) => s.isDirectory(), () => false);
|
|
36
|
+
const [cmd, args] = process.platform === "darwin"
|
|
37
|
+
? isDir ? ["open", [abs]] : ["open", ["-R", abs]]
|
|
38
|
+
: process.platform === "win32"
|
|
39
|
+
? isDir ? ["explorer.exe", [abs]] : ["explorer.exe", ["/select," + abs]]
|
|
40
|
+
: ["xdg-open", [isDir ? abs : dirname(abs)]];
|
|
41
|
+
try {
|
|
42
|
+
const child = spawn(cmd, args, { detached: true, stdio: "ignore" });
|
|
43
|
+
child.on("error", (e) => console.error(`Couldn't reveal ${abs}: ${e.message}`));
|
|
44
|
+
child.unref();
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
console.error(`Couldn't reveal ${abs}: ${e.message}`);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Opens `abs` in its default application: `open <abs>` on macOS, `explorer.exe <abs>` on Windows
|
|
52
|
+
* (never `cmd /c start`, since that runs through a shell), `xdg-open <abs>` elsewhere. Spawned
|
|
53
|
+
* without a shell, detached, with its own stdio ignored; errors are logged rather than thrown.
|
|
54
|
+
*
|
|
55
|
+
* Honours RUSHES_NO_REVEAL=1 exactly as osRevealer does: it logs `open <abs>` and opens nothing.
|
|
56
|
+
*/
|
|
57
|
+
export const osOpener = async (abs) => {
|
|
58
|
+
if (process.env.RUSHES_NO_REVEAL === "1") {
|
|
59
|
+
console.error(`open ${abs}`);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const [cmd, args] = process.platform === "darwin" ? ["open", [abs]] : process.platform === "win32" ? ["explorer.exe", [abs]] : ["xdg-open", [abs]];
|
|
63
|
+
try {
|
|
64
|
+
const child = spawn(cmd, args, { detached: true, stdio: "ignore" });
|
|
65
|
+
child.on("error", (e) => console.error(`Couldn't open ${abs}: ${e.message}`));
|
|
66
|
+
child.unref();
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
console.error(`Couldn't open ${abs}: ${e.message}`);
|
|
70
|
+
}
|
|
71
|
+
};
|