rushes 0.2.0 → 0.2.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/.claude-plugin/plugin.json +1 -1
- package/AGENTS.md +17 -3
- package/README.md +34 -2
- package/dist/cli/doctor.js +1 -1
- package/dist/cli/main.js +100 -8
- package/dist/core/found.js +333 -0
- package/dist/core/media.js +2 -0
- package/dist/core/schema.js +19 -0
- package/dist/core/store.js +13 -1
- package/dist/mcp/client.js +13 -1
- package/dist/mcp/tools.js +74 -5
- package/dist/server/app.js +159 -8
- package/dist/server/files.js +61 -2
- package/dist/server/found.js +1199 -0
- package/dist/server/peaks.js +471 -0
- package/dist/server/start.js +24 -3
- package/package.json +1 -1
- package/skills/rushes/SKILL.md +4 -2
- package/web-dist/assets/index-DpiDziCn.js +3 -0
- package/web-dist/assets/index-DqCXH_lZ.css +1 -0
- package/web-dist/index.html +2 -2
- package/web-dist/assets/index-BjDIT_wL.css +0 -1
- package/web-dist/assets/index-DF6Rl-Ac.js +0 -3
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { lstat, mkdir, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { fromManifestPath } from "../core/paths.js";
|
|
5
|
+
import { RUSHES_DIR } from "../core/store.js";
|
|
6
|
+
import { defaultFfmpeg } from "./proxy.js";
|
|
7
|
+
// §19.9: the Picture waveform. When a cut is added (or the dashboard asks for one that has none
|
|
8
|
+
// yet), ffmpeg decodes the original's audio, mono at 8 kHz, and the server reduces it to about
|
|
9
|
+
// 2000 peaks while it streams, so a long film never sits in memory. The result is a small JSON
|
|
10
|
+
// file in .rushes/peaks/, named for the cut and the file's revision, written to a temp name and
|
|
11
|
+
// renamed into place: a temp file never poses as a result.
|
|
12
|
+
/** How many peaks a cut gets, at most. */
|
|
13
|
+
export const PEAK_BUCKETS = 2000;
|
|
14
|
+
/** The sample rate ffmpeg resamples to: plenty for a 2000-bucket outline, and quick to decode. */
|
|
15
|
+
export const PEAK_RATE = 8000;
|
|
16
|
+
/** Inside .rushes/: .rushes/peaks/<video>_<version>_<hash>.json */
|
|
17
|
+
export const PEAKS_DIR = "peaks";
|
|
18
|
+
/** Waveforms are made this many at a time, like the proxy-need probes. */
|
|
19
|
+
export const PEAKS_CONCURRENCY = 2;
|
|
20
|
+
/** A decode is given at least this long, and never more than PEAKS_TIMEOUT_MAX_MS. */
|
|
21
|
+
export const PEAKS_TIMEOUT_MS = 5 * 60_000;
|
|
22
|
+
export const PEAKS_TIMEOUT_MAX_MS = 60 * 60_000;
|
|
23
|
+
/** The slowest a decode is expected to read the original: 5 MB a second, a slow network volume. */
|
|
24
|
+
const PEAKS_READ_BYTES_PER_S = 5e6;
|
|
25
|
+
/** How long a decode of a file this size may run before it's killed: 60 s plus the size read at 5 MB/s, held to 5–60 minutes. */
|
|
26
|
+
export function peaksTimeoutMs(bytes) {
|
|
27
|
+
const ms = 60_000 + (Math.max(0, bytes) / PEAKS_READ_BYTES_PER_S) * 1000;
|
|
28
|
+
return Math.round(Math.min(PEAKS_TIMEOUT_MAX_MS, Math.max(PEAKS_TIMEOUT_MS, ms)));
|
|
29
|
+
}
|
|
30
|
+
/** A peaks file is at most this big (2000 peaks are about 12 KB); anything larger isn't read. */
|
|
31
|
+
export const PEAKS_MAX_BYTES = 64 * 1024;
|
|
32
|
+
/** How many failed revisions are remembered (so a broken file isn't decoded on every request). */
|
|
33
|
+
const FAILED_LIMIT = 500;
|
|
34
|
+
/** ffmpeg's words for a file with no audio stream to decode ("Output file #0 does not contain any stream" before 7.0). */
|
|
35
|
+
const NO_AUDIO = /does not contain any stream/i;
|
|
36
|
+
const VIDEO_ID = /^[a-z0-9][a-z0-9-]*$/;
|
|
37
|
+
const VERSION_ID = /^v\d+$/;
|
|
38
|
+
/** A peaks file this server writes. */
|
|
39
|
+
export const PEAKS_FILE = /^[a-z0-9][a-z0-9-]*_v\d+_[0-9a-f]{16}\.json$/;
|
|
40
|
+
/**
|
|
41
|
+
* The containers a cut may be read as: the ordinary video and audio ones. Playlist and list
|
|
42
|
+
* formats (HLS, concat, and the like) are left out, so a "cut" that is really a list can't make
|
|
43
|
+
* ffmpeg open other files (fix round 3, M8).
|
|
44
|
+
*/
|
|
45
|
+
export const PEAKS_FORMATS = "mov,mp4,m4a,3gp,3g2,mj2,matroska,webm,avi,mpegts,mxf,wav,w64,aiff,mp3,aac,flac,ogg,caf,asf,mpeg,flv,dv";
|
|
46
|
+
/**
|
|
47
|
+
* ffmpeg's arguments. Only local files (`file`, and `pipe` for stdout) in ordinary containers are
|
|
48
|
+
* read. The first audio stream is decoded, if there is one (`-map 0:a:0?`; with none, ffmpeg says
|
|
49
|
+
* the output "does not contain any stream", which is recorded as no audio). `aresample` pads with
|
|
50
|
+
* silence from 0:00 when the audio starts late, so the peaks line up with the picture. Then it's
|
|
51
|
+
* mixed to mono at 8 kHz and written as raw 32-bit floats on stdout.
|
|
52
|
+
*/
|
|
53
|
+
export function peaksArgs(abs) {
|
|
54
|
+
return [
|
|
55
|
+
"-hide_banner", "-nostdin", "-v", "error",
|
|
56
|
+
"-protocol_whitelist", "file,pipe",
|
|
57
|
+
"-format_whitelist", PEAKS_FORMATS,
|
|
58
|
+
"-i", abs,
|
|
59
|
+
"-map", "0:a:0?", "-vn", "-sn", "-dn",
|
|
60
|
+
"-af", "aresample=async=1:first_pts=0",
|
|
61
|
+
"-ac", "1", "-ar", String(PEAK_RATE), "-f", "f32le", "pipe:1",
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Reduces a stream of f32le samples to at most `buckets` peaks (the largest absolute amplitude in
|
|
66
|
+
* each), without knowing the length up front. Samples go into blocks; when 2 × `buckets` blocks
|
|
67
|
+
* are full, neighbouring pairs merge and the block size doubles, so memory stays fixed however
|
|
68
|
+
* long the film. `finish` maps the blocks onto the buckets by time and normalises to 0..1.
|
|
69
|
+
*/
|
|
70
|
+
export class PeakReducer {
|
|
71
|
+
buckets;
|
|
72
|
+
rate;
|
|
73
|
+
cap;
|
|
74
|
+
blocks;
|
|
75
|
+
count = 0;
|
|
76
|
+
blockSize = 1;
|
|
77
|
+
inBlock = 0;
|
|
78
|
+
current = 0;
|
|
79
|
+
carry = null;
|
|
80
|
+
samples = 0;
|
|
81
|
+
constructor(buckets = PEAK_BUCKETS, rate = PEAK_RATE) {
|
|
82
|
+
this.buckets = buckets;
|
|
83
|
+
this.rate = rate;
|
|
84
|
+
this.cap = Math.max(2, buckets * 2);
|
|
85
|
+
this.blocks = new Float64Array(this.cap);
|
|
86
|
+
}
|
|
87
|
+
/** Adds a chunk of stdout. A float split across two chunks is carried over to the next. */
|
|
88
|
+
push(chunk) {
|
|
89
|
+
let buf = chunk;
|
|
90
|
+
if (this.carry) {
|
|
91
|
+
buf = Buffer.concat([this.carry, chunk]);
|
|
92
|
+
this.carry = null;
|
|
93
|
+
}
|
|
94
|
+
const whole = buf.length - (buf.length % 4);
|
|
95
|
+
for (let o = 0; o < whole; o += 4)
|
|
96
|
+
this.sample(buf.readFloatLE(o));
|
|
97
|
+
if (whole < buf.length)
|
|
98
|
+
this.carry = Buffer.from(buf.subarray(whole));
|
|
99
|
+
}
|
|
100
|
+
/** How many blocks are held right now (never more than 2 × buckets). For tests. */
|
|
101
|
+
held() {
|
|
102
|
+
return this.count;
|
|
103
|
+
}
|
|
104
|
+
sample(v) {
|
|
105
|
+
const a = Number.isFinite(v) ? Math.abs(v) : 0;
|
|
106
|
+
if (a > this.current)
|
|
107
|
+
this.current = a;
|
|
108
|
+
this.samples++;
|
|
109
|
+
if (++this.inBlock === this.blockSize)
|
|
110
|
+
this.close();
|
|
111
|
+
}
|
|
112
|
+
close() {
|
|
113
|
+
this.blocks[this.count++] = this.current;
|
|
114
|
+
this.current = 0;
|
|
115
|
+
this.inBlock = 0;
|
|
116
|
+
if (this.count === this.cap) {
|
|
117
|
+
for (let i = 0; i < this.cap / 2; i++)
|
|
118
|
+
this.blocks[i] = Math.max(this.blocks[2 * i], this.blocks[2 * i + 1]);
|
|
119
|
+
this.count = this.cap / 2;
|
|
120
|
+
this.blockSize *= 2;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** The peaks, normalised so the loudest is 1 and rounded to 3 decimals; silence is all zeros. */
|
|
124
|
+
finish() {
|
|
125
|
+
if (this.inBlock > 0)
|
|
126
|
+
this.close();
|
|
127
|
+
const total = this.samples;
|
|
128
|
+
const n = Math.min(this.buckets, this.count);
|
|
129
|
+
const raw = new Array(n).fill(0);
|
|
130
|
+
const size = this.blockSize;
|
|
131
|
+
for (let i = 0; i < n; i++) {
|
|
132
|
+
// Each block belongs to the bucket its first sample falls in, so a loud moment lands in one bucket.
|
|
133
|
+
const from = (i * total) / n;
|
|
134
|
+
const to = ((i + 1) * total) / n;
|
|
135
|
+
let a = Math.ceil(from / size);
|
|
136
|
+
let b = Math.min(this.count, Math.ceil(to / size));
|
|
137
|
+
if (b <= a) {
|
|
138
|
+
a = Math.min(this.count - 1, Math.floor(from / size));
|
|
139
|
+
b = a + 1;
|
|
140
|
+
}
|
|
141
|
+
let peak = 0;
|
|
142
|
+
for (let k = a; k < b; k++)
|
|
143
|
+
if (this.blocks[k] > peak)
|
|
144
|
+
peak = this.blocks[k];
|
|
145
|
+
raw[i] = peak;
|
|
146
|
+
}
|
|
147
|
+
const max = raw.reduce((m, p) => (p > m ? p : m), 0);
|
|
148
|
+
const peaks = raw.map((p) => (max > 0 ? Math.round((p / max) * 1000) / 1000 : 0));
|
|
149
|
+
return { peaks, samples: total, duration: Math.round((total / this.rate) * 1000) / 1000 };
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** A revision of the original: path, size and mtime, as a short hash for the file name. */
|
|
153
|
+
export function peaksHash(file, size, mtimeMs) {
|
|
154
|
+
return createHash("sha256").update(`${file}\0${size}\0${mtimeMs}`).digest("hex").slice(0, 16);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The waveform jobs of one server: a queue, two at a time, never awaited by GET /api/state.
|
|
158
|
+
* Every decode is killed when the server closes. When a peaks file lands, a `change` goes out so
|
|
159
|
+
* every open tab asks again.
|
|
160
|
+
*/
|
|
161
|
+
export class PeakJobs {
|
|
162
|
+
store;
|
|
163
|
+
run;
|
|
164
|
+
isAvailable;
|
|
165
|
+
availableOnce = null;
|
|
166
|
+
timeoutMs;
|
|
167
|
+
buckets;
|
|
168
|
+
queue = new Map();
|
|
169
|
+
running = new Map();
|
|
170
|
+
failed = new Map();
|
|
171
|
+
closing = false;
|
|
172
|
+
idleWaiter = null;
|
|
173
|
+
constructor(store, opts = {}) {
|
|
174
|
+
this.store = store;
|
|
175
|
+
this.run = opts.run ?? defaultFfmpeg;
|
|
176
|
+
this.timeoutMs = opts.timeoutMs;
|
|
177
|
+
// Never more than PEAK_BUCKETS: readSaved refuses anything longer, so a bigger file would be made forever.
|
|
178
|
+
this.buckets = Math.max(1, Math.min(PEAK_BUCKETS, Math.floor(opts.buckets ?? PEAK_BUCKETS)));
|
|
179
|
+
const run = this.run;
|
|
180
|
+
this.isAvailable = opts.available ?? (async () => (await run(["-version"])).code === 0);
|
|
181
|
+
}
|
|
182
|
+
/** .rushes/peaks */
|
|
183
|
+
get dir() {
|
|
184
|
+
return join(this.store.dir, PEAKS_DIR);
|
|
185
|
+
}
|
|
186
|
+
/** Whether ffmpeg is there. Checked once per server. */
|
|
187
|
+
available() {
|
|
188
|
+
this.availableOnce ??= Promise.resolve()
|
|
189
|
+
.then(() => this.isAvailable())
|
|
190
|
+
.catch(() => false);
|
|
191
|
+
return this.availableOnce;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* The waveform for a cut: ready, still being made, none (no audio stream), or why not. A cut
|
|
195
|
+
* with nothing on disk yet is queued and answered "computing" straight away: nothing here
|
|
196
|
+
* waits on ffmpeg.
|
|
197
|
+
*/
|
|
198
|
+
async answer(video, version, file) {
|
|
199
|
+
if (!(await this.available()))
|
|
200
|
+
return { state: "failed", reason: "ffmpeg isn't installed" };
|
|
201
|
+
const item = await this.item(video, version, file);
|
|
202
|
+
if (!item)
|
|
203
|
+
return { state: "missing" };
|
|
204
|
+
if (item === "unnamed")
|
|
205
|
+
return { state: "failed", reason: "Rushes can't name a waveform for this cut" };
|
|
206
|
+
if (this.queue.has(item.name) || this.running.has(item.name))
|
|
207
|
+
return { state: "computing" };
|
|
208
|
+
const reason = this.failed.get(item.name);
|
|
209
|
+
if (reason !== undefined)
|
|
210
|
+
return { state: "failed", reason };
|
|
211
|
+
const saved = await this.readSaved(item.name);
|
|
212
|
+
if (saved)
|
|
213
|
+
return "audio" in saved ? { state: "silent" } : { state: "ready", data: saved };
|
|
214
|
+
this.enqueue(item);
|
|
215
|
+
return { state: "computing" };
|
|
216
|
+
}
|
|
217
|
+
/** Queues a cut that has no waveform yet (a cut just added). Returns once queued; never waits on the decode. */
|
|
218
|
+
async queueCut(video, version, file) {
|
|
219
|
+
if (this.closing || !(await this.available()))
|
|
220
|
+
return;
|
|
221
|
+
const item = await this.item(video, version, file);
|
|
222
|
+
if (!item || item === "unnamed")
|
|
223
|
+
return;
|
|
224
|
+
if (this.queue.has(item.name) || this.running.has(item.name) || this.failed.has(item.name))
|
|
225
|
+
return;
|
|
226
|
+
if (await this.readSaved(item.name))
|
|
227
|
+
return;
|
|
228
|
+
this.enqueue(item);
|
|
229
|
+
}
|
|
230
|
+
/** Resolves once nothing is queued or running. For tests. */
|
|
231
|
+
idle() {
|
|
232
|
+
if (this.queue.size === 0 && this.running.size === 0)
|
|
233
|
+
return Promise.resolve();
|
|
234
|
+
if (!this.idleWaiter) {
|
|
235
|
+
let resolve;
|
|
236
|
+
const promise = new Promise((r) => (resolve = r));
|
|
237
|
+
this.idleWaiter = { promise, resolve };
|
|
238
|
+
}
|
|
239
|
+
return this.idleWaiter.promise;
|
|
240
|
+
}
|
|
241
|
+
/** Stops taking new work, drops the queue and kills every decode in flight. Used when the server closes. */
|
|
242
|
+
async close() {
|
|
243
|
+
this.closing = true;
|
|
244
|
+
this.queue.clear();
|
|
245
|
+
for (const c of this.running.values())
|
|
246
|
+
c.abort();
|
|
247
|
+
await this.idle();
|
|
248
|
+
}
|
|
249
|
+
async item(video, version, file) {
|
|
250
|
+
// A hand-edited id ("../x") must never name a file outside peaks/.
|
|
251
|
+
if (!VIDEO_ID.test(video) || !VERSION_ID.test(version))
|
|
252
|
+
return "unnamed";
|
|
253
|
+
const abs = fromManifestPath(this.store.root, file);
|
|
254
|
+
let info;
|
|
255
|
+
try {
|
|
256
|
+
info = await stat(abs);
|
|
257
|
+
}
|
|
258
|
+
catch {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
if (!info.isFile())
|
|
262
|
+
return null;
|
|
263
|
+
const hash = peaksHash(file, info.size, info.mtimeMs);
|
|
264
|
+
return { video, version, file, abs, hash, size: info.size, name: `${video}_${version}_${hash}.json` };
|
|
265
|
+
}
|
|
266
|
+
/** A peaks file on disk, or null when there's none (or it doesn't read as one: it's made again). */
|
|
267
|
+
async readSaved(name) {
|
|
268
|
+
let text;
|
|
269
|
+
try {
|
|
270
|
+
const path = join(this.dir, name);
|
|
271
|
+
const info = await stat(path);
|
|
272
|
+
if (!info.isFile() || info.size > PEAKS_MAX_BYTES)
|
|
273
|
+
return null;
|
|
274
|
+
text = await readFile(path, "utf8");
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
try {
|
|
280
|
+
const j = JSON.parse(text);
|
|
281
|
+
if (j.v !== 1)
|
|
282
|
+
return null;
|
|
283
|
+
if (j.audio === false)
|
|
284
|
+
return { v: 1, audio: false };
|
|
285
|
+
const peaks = j.peaks;
|
|
286
|
+
if (typeof j.duration === "number" && Number.isFinite(j.duration) && j.duration >= 0 &&
|
|
287
|
+
Array.isArray(peaks) && peaks.length > 0 && peaks.length <= PEAK_BUCKETS && peaks.length === j.buckets &&
|
|
288
|
+
peaks.every((p) => typeof p === "number" && p >= 0 && p <= 1)) {
|
|
289
|
+
return { v: 1, buckets: peaks.length, duration: j.duration, peaks: peaks };
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
catch {
|
|
293
|
+
/* not JSON: made again */
|
|
294
|
+
}
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
enqueue(item) {
|
|
298
|
+
// Two callers (an add and a tab, or two tabs) can both get here for the same cut: one decode.
|
|
299
|
+
if (this.closing || this.queue.has(item.name) || this.running.has(item.name))
|
|
300
|
+
return;
|
|
301
|
+
this.queue.set(item.name, item);
|
|
302
|
+
this.pump();
|
|
303
|
+
}
|
|
304
|
+
pump() {
|
|
305
|
+
while (!this.closing && this.running.size < PEAKS_CONCURRENCY && this.queue.size > 0) {
|
|
306
|
+
const [name, item] = this.queue.entries().next().value;
|
|
307
|
+
this.queue.delete(name);
|
|
308
|
+
const controller = new AbortController();
|
|
309
|
+
this.running.set(name, controller);
|
|
310
|
+
void this.make(item, controller).finally(() => {
|
|
311
|
+
if (this.running.get(name) === controller)
|
|
312
|
+
this.running.delete(name);
|
|
313
|
+
this.pump();
|
|
314
|
+
if (this.queue.size === 0 && this.running.size === 0 && this.idleWaiter) {
|
|
315
|
+
this.idleWaiter.resolve();
|
|
316
|
+
this.idleWaiter = null;
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
remember(name, reason) {
|
|
322
|
+
this.failed.delete(name);
|
|
323
|
+
this.failed.set(name, reason);
|
|
324
|
+
while (this.failed.size > FAILED_LIMIT)
|
|
325
|
+
this.failed.delete(this.failed.keys().next().value);
|
|
326
|
+
}
|
|
327
|
+
async make(item, controller) {
|
|
328
|
+
const reducer = new PeakReducer(this.buckets);
|
|
329
|
+
let timedOut = false;
|
|
330
|
+
const timer = setTimeout(() => {
|
|
331
|
+
timedOut = true;
|
|
332
|
+
controller.abort();
|
|
333
|
+
}, this.timeoutMs ?? peaksTimeoutMs(item.size));
|
|
334
|
+
let res;
|
|
335
|
+
try {
|
|
336
|
+
res = await this.run(peaksArgs(item.abs), { signal: controller.signal, onStdout: (c) => reducer.push(c) });
|
|
337
|
+
}
|
|
338
|
+
catch (e) {
|
|
339
|
+
res = { code: 1, stderr: e.message };
|
|
340
|
+
}
|
|
341
|
+
finally {
|
|
342
|
+
clearTimeout(timer);
|
|
343
|
+
}
|
|
344
|
+
// Closing: nothing is recorded, and the next start makes it again.
|
|
345
|
+
if (this.closing && !timedOut)
|
|
346
|
+
return;
|
|
347
|
+
if (timedOut)
|
|
348
|
+
return this.remember(item.name, "ffmpeg took too long reading the audio");
|
|
349
|
+
let record;
|
|
350
|
+
if (res.code !== 0) {
|
|
351
|
+
if (!NO_AUDIO.test(res.stderr))
|
|
352
|
+
return this.remember(item.name, lastLine(res.stderr));
|
|
353
|
+
record = { v: 1, audio: false };
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
const out = reducer.finish();
|
|
357
|
+
record = out.samples === 0 ? { v: 1, audio: false } : { v: 1, buckets: out.peaks.length, duration: out.duration, peaks: out.peaks };
|
|
358
|
+
}
|
|
359
|
+
// The file changed while it was being read: this answer is for a revision that's gone.
|
|
360
|
+
const now = await this.item(item.video, item.version, item.file).catch(() => null);
|
|
361
|
+
if (!now || now === "unnamed" || now.name !== item.name)
|
|
362
|
+
return;
|
|
363
|
+
const tmp = join(this.dir, `.${item.name}.${randomUUID()}.tmp`);
|
|
364
|
+
try {
|
|
365
|
+
// Not recursive: if .rushes/ itself has gone, nothing is written.
|
|
366
|
+
await mkdir(this.dir).catch((e) => {
|
|
367
|
+
if (e.code !== "EEXIST")
|
|
368
|
+
throw e;
|
|
369
|
+
});
|
|
370
|
+
if (!(await realPeaksDir(this.dir)))
|
|
371
|
+
throw new Error(".rushes/peaks isn't a folder Rushes can write to");
|
|
372
|
+
await writeFile(tmp, JSON.stringify(record));
|
|
373
|
+
await rename(tmp, join(this.dir, item.name));
|
|
374
|
+
}
|
|
375
|
+
catch (e) {
|
|
376
|
+
await rm(tmp, { force: true }).catch(() => undefined);
|
|
377
|
+
return this.remember(item.name, `Couldn't save the waveform: ${e.message}`);
|
|
378
|
+
}
|
|
379
|
+
await this.removeStale(item);
|
|
380
|
+
await this.announce();
|
|
381
|
+
}
|
|
382
|
+
/** Every other revision's peaks for the same cut. */
|
|
383
|
+
async removeStale(item) {
|
|
384
|
+
const prefix = `${item.video}_${item.version}_`;
|
|
385
|
+
if (!(await realPeaksDir(this.dir)))
|
|
386
|
+
return;
|
|
387
|
+
let names;
|
|
388
|
+
try {
|
|
389
|
+
names = await readdir(this.dir);
|
|
390
|
+
}
|
|
391
|
+
catch {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
const stale = names.filter((n) => n !== item.name && n.startsWith(prefix) && PEAKS_FILE.test(n));
|
|
395
|
+
await Promise.all(stale.map((n) => rm(join(this.dir, n), { force: true }).catch(() => undefined)));
|
|
396
|
+
}
|
|
397
|
+
/** A `change` for project.json at its current rev, so every tab asks for its waveform again. */
|
|
398
|
+
async announce() {
|
|
399
|
+
try {
|
|
400
|
+
const { rev } = await this.store.read("project");
|
|
401
|
+
this.store.emit("change", { file: "project", rev });
|
|
402
|
+
}
|
|
403
|
+
catch {
|
|
404
|
+
// project.json unreadable right now: the watcher reports that.
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Whether .rushes/peaks is a real folder, not a link to one somewhere else (fix round 3, M3).
|
|
410
|
+
* Nothing is saved into, swept from or cleaned out of a peaks folder that isn't.
|
|
411
|
+
*/
|
|
412
|
+
async function realPeaksDir(dir) {
|
|
413
|
+
const info = await lstat(dir).catch(() => null);
|
|
414
|
+
return !!info && info.isDirectory() && !info.isSymbolicLink();
|
|
415
|
+
}
|
|
416
|
+
function lastLine(stderr) {
|
|
417
|
+
const lines = stderr.split(/[\r\n]+/).map((l) => l.trim()).filter(Boolean);
|
|
418
|
+
const last = lines[lines.length - 1];
|
|
419
|
+
return last ? `ffmpeg couldn't read the audio: ${last.slice(0, 300)}` : "ffmpeg couldn't read the audio";
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Start-up clean-up: a server stopped while saving a waveform may leave a temp file behind in
|
|
423
|
+
* .rushes/peaks/. Nothing reads one, but it goes. Returns the names removed.
|
|
424
|
+
*/
|
|
425
|
+
export async function removePeakTemps(root) {
|
|
426
|
+
const dir = join(root, RUSHES_DIR, PEAKS_DIR);
|
|
427
|
+
if (!(await realPeaksDir(dir)))
|
|
428
|
+
return [];
|
|
429
|
+
let names;
|
|
430
|
+
try {
|
|
431
|
+
names = await readdir(dir);
|
|
432
|
+
}
|
|
433
|
+
catch {
|
|
434
|
+
return [];
|
|
435
|
+
}
|
|
436
|
+
const temps = names.filter((n) => n.endsWith(".tmp"));
|
|
437
|
+
await Promise.all(temps.map((n) => rm(join(dir, n), { force: true }).catch(() => undefined)));
|
|
438
|
+
return temps;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Start-up clean-up: a peaks file whose cut is no longer in the project (a film or version
|
|
442
|
+
* removed by hand) is deleted. A file belongs to a cut when it starts `<video>_<version>_`, the
|
|
443
|
+
* trailing underscore included, so v1 never claims v10's file. Only files with a name Rushes
|
|
444
|
+
* writes (PEAKS_FILE), directly inside a real .rushes/peaks/ folder, are ever removed. Returns
|
|
445
|
+
* the names removed.
|
|
446
|
+
*/
|
|
447
|
+
export async function removeOrphanPeaks(root, project) {
|
|
448
|
+
const dir = join(root, RUSHES_DIR, PEAKS_DIR);
|
|
449
|
+
if (!(await realPeaksDir(dir)))
|
|
450
|
+
return [];
|
|
451
|
+
let names;
|
|
452
|
+
try {
|
|
453
|
+
names = await readdir(dir);
|
|
454
|
+
}
|
|
455
|
+
catch {
|
|
456
|
+
return [];
|
|
457
|
+
}
|
|
458
|
+
const owners = project.videos.flatMap((v) => v.versions.map((ver) => `${v.id}_${ver.id}_`));
|
|
459
|
+
const orphans = [];
|
|
460
|
+
for (const n of names) {
|
|
461
|
+
// Only names Rushes writes; anything else in the folder is someone else's.
|
|
462
|
+
if (!PEAKS_FILE.test(n) || owners.some((prefix) => n.startsWith(prefix)))
|
|
463
|
+
continue;
|
|
464
|
+
const info = await lstat(join(dir, n)).catch(() => null);
|
|
465
|
+
if (!info || info.isDirectory())
|
|
466
|
+
continue;
|
|
467
|
+
orphans.push(n);
|
|
468
|
+
}
|
|
469
|
+
await Promise.all(orphans.map((n) => rm(join(dir, n), { force: true }).catch(() => undefined)));
|
|
470
|
+
return orphans;
|
|
471
|
+
}
|
package/dist/server/start.js
CHANGED
|
@@ -8,6 +8,8 @@ import { createApp } from "./app.js";
|
|
|
8
8
|
import { removeLock, writeLock } from "./lock.js";
|
|
9
9
|
import { watchStore } from "./watch.js";
|
|
10
10
|
import { ProxyJobs, removePartials } from "./proxy.js";
|
|
11
|
+
import { PeakJobs, removeOrphanPeaks, removePeakTemps } from "./peaks.js";
|
|
12
|
+
import { FoundScanner, ffprobeDuration, foundAnnouncer } from "./found.js";
|
|
11
13
|
export const DEFAULT_PORT = 4580;
|
|
12
14
|
function listen(server, port, host) {
|
|
13
15
|
return new Promise((ok, fail) => {
|
|
@@ -42,7 +44,12 @@ export async function startServer(rootDir, opts = {}) {
|
|
|
42
44
|
// Built on this server's own store, so its progress events reach this server's SSE clients.
|
|
43
45
|
// Every running job is cancelled when the server closes.
|
|
44
46
|
const proxyJobs = new ProxyJobs(store, opts.proxy);
|
|
45
|
-
|
|
47
|
+
// §19.9: waveforms use their own runner (a test's fake proxy encoder is no audio decoder), and
|
|
48
|
+
// ffmpeg is there exactly when proxies say so.
|
|
49
|
+
const peakJobs = new PeakJobs(store, { available: () => proxyJobs.available(), ...opts.peaks });
|
|
50
|
+
// §20: the project's other files. Its first scan starts once this server owns the project (below).
|
|
51
|
+
const found = new FoundScanner({ store, probe: opts.found?.probe ?? ffprobeDuration, announce: foundAnnouncer(store), limits: opts.found?.limits });
|
|
52
|
+
const appOpts = { webDir: opts.webDir, onShutdown: () => void close(), reveal: opts.reveal, proxyJobs, peakJobs, found };
|
|
46
53
|
const app = createApp(store, appOpts);
|
|
47
54
|
const listener = getRequestListener(app.fetch);
|
|
48
55
|
let lastRequest = Date.now();
|
|
@@ -73,6 +80,7 @@ export async function startServer(rootDir, opts = {}) {
|
|
|
73
80
|
}
|
|
74
81
|
catch (e) {
|
|
75
82
|
// Another server owns this project, or the lock couldn't be written: don't leave a socket open.
|
|
83
|
+
found.close();
|
|
76
84
|
server.closeAllConnections?.();
|
|
77
85
|
await new Promise((ok) => server.close(() => ok()));
|
|
78
86
|
throw e;
|
|
@@ -87,9 +95,20 @@ export async function startServer(rootDir, opts = {}) {
|
|
|
87
95
|
// (a version's proxy is set only after a completed render), so it goes. Only the lock winner
|
|
88
96
|
// does this, so it can never delete a file another live server is still writing.
|
|
89
97
|
await removePartials(root);
|
|
98
|
+
// Likewise a waveform that was being saved: its temp file is never read, and goes.
|
|
99
|
+
await removePeakTemps(root);
|
|
100
|
+
// And a waveform whose cut is no longer in the project.
|
|
101
|
+
await removeOrphanPeaks(root, await store.read("project")).catch(() => undefined);
|
|
90
102
|
appOpts.projectId = id;
|
|
91
103
|
const url = `http://${host}:${port}`;
|
|
92
104
|
const stopWatching = await watchStore(store);
|
|
105
|
+
// §20.1: a first look through the folder on open. Never awaited, so it never delays start-up;
|
|
106
|
+
// the tabs hear a change when it lands.
|
|
107
|
+
// Then §20.4's current set comes in (a no-op without a cut, or when it's already in).
|
|
108
|
+
void found
|
|
109
|
+
.scan()
|
|
110
|
+
.then(() => found.adoptCurrentSet())
|
|
111
|
+
.catch(() => undefined);
|
|
93
112
|
let idleTimer;
|
|
94
113
|
let done = () => undefined;
|
|
95
114
|
const closed = new Promise((ok) => { done = ok; });
|
|
@@ -98,9 +117,11 @@ export async function startServer(rootDir, opts = {}) {
|
|
|
98
117
|
closing ??= (async () => {
|
|
99
118
|
clearTimeout(idleTimer);
|
|
100
119
|
stopWatching();
|
|
120
|
+
// No probe starts after this, and those in flight are stopped.
|
|
121
|
+
found.close();
|
|
101
122
|
// A running proxy's ffmpeg must not outlive the server; its partial file is deleted too.
|
|
102
123
|
// From here no new job starts either (a POST or an autoProxy cut racing the close).
|
|
103
|
-
await proxyJobs.close();
|
|
124
|
+
await Promise.all([proxyJobs.close(), peakJobs.close()]);
|
|
104
125
|
server.closeAllConnections?.();
|
|
105
126
|
await new Promise((ok) => server.close(() => ok()));
|
|
106
127
|
await removeLock(root, token);
|
|
@@ -121,5 +142,5 @@ export async function startServer(rootDir, opts = {}) {
|
|
|
121
142
|
idleTimer = setTimeout(tick, idleMs);
|
|
122
143
|
idleTimer.unref?.();
|
|
123
144
|
}
|
|
124
|
-
return { url, port, id, dashboardUrl: `${url}/p/${id}/`, store, close, closed };
|
|
145
|
+
return { url, port, id, dashboardUrl: `${url}/p/${id}/`, store, found, close, closed };
|
|
125
146
|
}
|
package/package.json
CHANGED
package/skills/rushes/SKILL.md
CHANGED
|
@@ -14,7 +14,7 @@ If the user pastes a prompt copied from a locked tab, act on it directly — it
|
|
|
14
14
|
- `rushes_set_script` for the VO script. It merges by `id`, so send only the sections you changed; sections you leave out are kept. `rushes_get_script` reads the whole script;
|
|
15
15
|
- `rushes_add_variant` for each voice read, music bed or SFX pass. Voice reads carry `stage: "voice"` and a `round` (its name — reads in one round are compared side by side; a new direction gets a new round); say what the read is, or what changed, in `description`. Music and SFX carry `meta.description` for their lane card;
|
|
16
16
|
- `rushes_set_shots` for the storyboard's shots, once you have the first cut; send new timings when a later cut moves them.
|
|
17
|
-
2. Call `rushes_open` and tell the user it's ready. Each project's dashboard lives at its own address, `/p/<id>/`.
|
|
17
|
+
2. Call `rushes_open` and tell the user it's ready, with what it brought in and what it left (below). Each project's dashboard lives at its own address, `/p/<id>/`.
|
|
18
18
|
3. When the user sends a batch, call `rushes_get_batch` and fix every note in it. A note carries the `shot` it falls in; on an audio stage, call `rushes_get_picks` first — on Mix it also returns `levels` (dB per lane), which you read, not set. Its `on` says what it's about: `"<lane>/<variant>"` for a bed, pass or voice read (on Voiceover the lane names the round), `"<lane>/<variant>:<cue>"` for an SFX cue, `"vo"` for the Mix VO lane, or `null` for the whole mix; older notes may carry a section id or `"<section>:<take>"`. On Music, Sound effects and Mix treat `marks` (e.g. "Fall", "Quieter 3 dB") as part of the note; Voiceover notes carry none — they're Whole (`Speaker`, `Pacing`, `Tone`, `Overall`) or Point (`Fix this`, `Keep this`) instead. Picks are set from the dashboard only — no tool or CLI command sets one.
|
|
19
19
|
4. Register the new cut with `rushes_add_version`. On a locked video the reply carries a `warning`.
|
|
20
20
|
5. Reply to all the notes in one `rushes_reply` call. For each note set `status: "done"`, a one-line `reply`, `fixT` (when the fix is visible in the new cut, in seconds) and `fixVersion`.
|
|
@@ -25,6 +25,8 @@ Save renders, voice reads, beds and passes under the project's `renders/`, `audi
|
|
|
25
25
|
|
|
26
26
|
The **Assets** tab is a library: Screenshots, Cuts, Voiceover, Music, Sound effects, Scripts & docs, Images, Captions, Exports, Delivery, Edit files and Proxies. Top-level `.md`, `.txt`, `.pdf`, `.srt` and `.vtt` files, and anything in `exports/`, are found automatically. Register everything else with `rushes_add_file` (`kind`, `file`, optional `name`, `note`, `video`): deliverables and platform exports as `delivery`, NLE project files as `edit`, storyboards/references/logos as `image`, briefs as `doc`. When the user wants to share notes, call `rushes_export_notes` — it writes a dated Markdown file to `exports/` and returns the path.
|
|
27
27
|
|
|
28
|
+
When asked to open or review work in Rushes, call `rushes_open`, then tell the user what it brought in and what it left. Rushes looks through the project folder itself and brings in the current set of files that go with the newest cut (at most one voice read, one music bed and one sound effects pass, unpicked), leaving the rest in Assets › Found. `rushes_open` returns `broughtIn` (with each file's `origin`), `alreadyIn`, `failed` and `found` (counts by kind, called `counts` by `rushes_scan`), and waits about 20 seconds, answering `scanning: true` if the folder is big. Pass `include` for files you made for this cut — `include: [{ path, kind?, round? }]` — and they come in whatever they score, and win over the scoring, for that cut, on later scans too. `rushes_open` also takes `film`. `rushes_scan` (CLI `rushes scan`) looks again, also brings in the current set, and lists the candidates left, with reasons; `rushes_bring_in` (CLI `rushes bring-in <file>...`) brings chosen files in (up to 12 files of each kind per call) and refuses any path outside the project folder. Nothing is ever picked. The user reviews the rest in Assets › Found (**Bring in**, **Not these**, **Look again**); a locked Voiceover, Music or Sound effects tab shows "14 music files found in this project" with a **Review** button when files of its kind were found.
|
|
29
|
+
|
|
28
30
|
No project to show yet? Run `npx -y rushes demo <folder> --no-browser`, then `rushes_open` with `project: "<folder>"`. The demo builds a complete working example on the user's machine, with a cut, script, voice reads, music and notes already in place. Always pass `--no-browser`: without it the command stays running as the server and never returns.
|
|
29
31
|
|
|
30
|
-
Rushes has
|
|
32
|
+
Rushes has nineteen tools: `rushes_open`, `rushes_status`, `rushes_add_version`, `rushes_add_variant`, `rushes_set_script`, `rushes_get_script`, `rushes_add_take`, `rushes_list_notes`, `rushes_list_assets`, `rushes_get_batch`, `rushes_reply`, `rushes_get_picks`, `rushes_set_shots`, `rushes_lock_picture`, `rushes_add_file`, `rushes_export_notes`, `rushes_scan`, `rushes_bring_in` and `rushes_doctor` (environment and project health check) — `rushes_add_take` stays only for compatibility and isn't used for voiceover. The full contract is in `AGENTS.md` at the package root.
|