rushes 0.2.0 → 0.2.1
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/README.md +2 -1
- package/dist/server/app.js +33 -3
- package/dist/server/files.js +33 -1
- package/dist/server/peaks.js +471 -0
- package/dist/server/start.js +10 -2
- package/package.json +1 -1
- package/web-dist/assets/index-ChXjhVC5.js +3 -0
- package/web-dist/assets/index-Cj_tr1I-.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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rushes",
|
|
3
3
|
"description": "A local review desk for video made with AI agents: timecoded notes, script edits and audio picks your agent reads and acts on.",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"author": { "name": "Red Morley Hewitt" },
|
|
6
6
|
"homepage": "https://github.com/iamredmh/rushes",
|
|
7
7
|
"repository": "https://github.com/iamredmh/rushes",
|
package/README.md
CHANGED
|
@@ -6,6 +6,7 @@ Your agent renders a cut, writes a voiceover script or generates music beds. Rus
|
|
|
6
6
|
|
|
7
7
|
- **Script:** your edits sit beside the agent's lines.
|
|
8
8
|
- **Picture:** timecoded notes, ranges, boxes on the frame and frame grabs.
|
|
9
|
+
- **Picture waveform:** the cut's own audio, drawn quietly in the timeline, so you can see where the sound is.
|
|
9
10
|
- **Voiceover, Music, Sound effects:** variants you can switch between with no gap.
|
|
10
11
|
- **Mix:** everything playing together.
|
|
11
12
|
|
|
@@ -128,7 +129,7 @@ Hovering a locked tab shows a short tooltip, e.g. "Locked: ask your agent for mu
|
|
|
128
129
|
|
|
129
130
|
Press **G**, or the camera button, to grab the current frame. It's saved as a PNG to `screenshots/`, named for the film, version and timecode — grabbing the same frame twice overwrites the same file.
|
|
130
131
|
|
|
131
|
-
The **Assets** tab (key `7`) is a library with a folder sidebar: Screenshots, Cuts, Voiceover, Music, Sound effects, Scripts & docs, Images, Captions, Exports, Delivery, Edit files and Proxies. Each folder has search and a sort order; Screenshots, Images, Cuts and Delivery also get a grid/list toggle, and Screenshots, Cuts and Delivery get a film filter — every other folder is list-only. Audio plays inline, one at a time, and Markdown or text files get a read-only preview. Every item has five actions: Download, Save as (Chrome and Edge only — other browsers just get Download), Open, Show in Finder and Copy path. **Open** appears, and is accepted, only for a fixed list of safe, non-executable types — never scripts, apps or archives. Top-level `.md`, `.txt`, `.pdf`, `.srt` and `.vtt` files, plus anything in `exports/`, are picked up automatically; everything else your agent registers with `rushes_add_file`. The Exports folder also has an **Export notes** button, which writes every note to a dated Markdown file there.
|
|
132
|
+
The **Assets** tab (key `7`) is a library with a folder sidebar: Screenshots, Cuts, Voiceover, Music, Sound effects, Scripts & docs, Images, Captions, Exports, Delivery, Edit files and Proxies. Each folder has search and a sort order; Screenshots, Images, Cuts and Delivery also get a grid/list toggle, and Screenshots, Cuts and Delivery get a film filter — every other folder is list-only. Audio plays inline, one at a time, and Markdown or text files get a read-only preview. Every item has five actions: Download, Save as (Chrome and Edge only — other browsers just get Download), Open, Show in Finder and Copy path. A file your agent registered from outside the project folder (or reached through a link out of it) plays and downloads only if it is a media or document type (audio, video, images, PDF, Markdown, text, captions); for anything else, such as an editor project file on another drive, use Show in Finder or Copy path. **Open** appears, and is accepted, only for a fixed list of safe, non-executable types — never scripts, apps or archives. Top-level `.md`, `.txt`, `.pdf`, `.srt` and `.vtt` files, plus anything in `exports/`, are picked up automatically; everything else your agent registers with `rushes_add_file`. The Exports folder also has an **Export notes** button, which writes every note to a dated Markdown file there.
|
|
132
133
|
|
|
133
134
|
Rushes recommends this layout for a project folder:
|
|
134
135
|
|
package/dist/server/app.js
CHANGED
|
@@ -11,17 +11,18 @@ import { tabStates } from "../core/tabs.js";
|
|
|
11
11
|
import { fromManifestPath, toManifestPath } from "../core/paths.js";
|
|
12
12
|
import { probe } from "../core/media.js";
|
|
13
13
|
import { PROXY_PATH, ProxyJobs } from "./proxy.js";
|
|
14
|
+
import { PeakJobs } from "./peaks.js";
|
|
14
15
|
import { lstat, mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
15
16
|
import { randomUUID } from "node:crypto";
|
|
16
17
|
import { existsSync } from "node:fs";
|
|
17
18
|
import { basename, extname, join } from "node:path";
|
|
18
19
|
import { fileURLToPath } from "node:url";
|
|
19
|
-
import { GRAB_PATH, SCREENSHOT_PATH, contentDisposition, contentType, inside, isInlineSafeType, mediaSecurityHeaders, registeredMedia, sendFile } from "./files.js";
|
|
20
|
+
import { GRAB_PATH, SCREENSHOT_PATH, contentDisposition, contentType, inside, isInlineSafeType, mediaSecurityHeaders, registeredMedia, sendFile, servableFile } from "./files.js";
|
|
20
21
|
import { candidatePaths, listAssets, fpsFor, screenshotName } from "./assets.js";
|
|
21
22
|
import { osRevealer, osOpener, OPEN_SAFE_EXT } from "./reveal.js";
|
|
22
23
|
import { LaneStageSchema, SectionStatusSchema, StageSchema, BoxSchema, FileKindSchema, MarkSchema, ProjectIdSchema, ShotSchema, LEVEL_MIN, LEVEL_MAX, LEVEL_STEP } from "../core/schema.js";
|
|
23
24
|
import { defaultRunner, measureMix } from "./loudness.js";
|
|
24
|
-
export const VERSION = "0.2.
|
|
25
|
+
export const VERSION = "0.2.1";
|
|
25
26
|
async function body(c, schema) {
|
|
26
27
|
let json;
|
|
27
28
|
try {
|
|
@@ -155,6 +156,7 @@ export function createApp(store, opts = {}) {
|
|
|
155
156
|
const reveal = opts.reveal ?? osRevealer;
|
|
156
157
|
const open = opts.open ?? osOpener;
|
|
157
158
|
const jobs = opts.proxyJobs ?? new ProxyJobs(store);
|
|
159
|
+
const peaks = opts.peakJobs ?? new PeakJobs(store, { run: jobs.run, available: () => jobs.available() });
|
|
158
160
|
const app = new Hono();
|
|
159
161
|
// Every SSE client adds a change listener, so lift Node's default limit of ten.
|
|
160
162
|
store.setMaxListeners(0);
|
|
@@ -280,9 +282,13 @@ export function createApp(store, opts = {}) {
|
|
|
280
282
|
if (!info || !info.isFile() || info.isSymbolicLink())
|
|
281
283
|
throw new NotFoundError("media", path);
|
|
282
284
|
}
|
|
285
|
+
// §15.5: a registered file reached through a symlink out of the project is served only if it is media.
|
|
286
|
+
const file = await servableFile(store.root, fromManifestPath(store.root, path));
|
|
287
|
+
if (!file)
|
|
288
|
+
throw new NotFoundError("media", path);
|
|
283
289
|
const type = contentType(path);
|
|
284
290
|
const inlineSafe = isInlineSafeType(type);
|
|
285
|
-
const res = await sendFile(
|
|
291
|
+
const res = await sendFile(file, c.req.header("range"), inlineSafe ? type : "application/octet-stream");
|
|
286
292
|
res.headers.set("cross-origin-resource-policy", "same-origin");
|
|
287
293
|
for (const [name, value] of Object.entries(mediaSecurityHeaders()))
|
|
288
294
|
res.headers.set(name, value);
|
|
@@ -489,6 +495,8 @@ export function createApp(store, opts = {}) {
|
|
|
489
495
|
});
|
|
490
496
|
// §19.5: say when the cut is likely to play badly, and start its proxy straight away when the
|
|
491
497
|
// project asks for that. Without ffmpeg, nothing is offered (needFor is null).
|
|
498
|
+
// §19.9: its waveform is made in the background; nothing here waits for it.
|
|
499
|
+
void peaks.queueCut(result.video.id, result.version.id, file).catch(() => undefined);
|
|
492
500
|
const reason = await jobs.needFrom(abs, info);
|
|
493
501
|
const proxy = {};
|
|
494
502
|
if (reason) {
|
|
@@ -585,6 +593,28 @@ export function createApp(store, opts = {}) {
|
|
|
585
593
|
headers: { "content-type": "image/png", "cache-control": "no-store", "x-rushes-frame": String(frame), "content-length": String(png.length) },
|
|
586
594
|
});
|
|
587
595
|
});
|
|
596
|
+
// §19.9: the cut's own audio as about 2000 peaks, for the waveform in Picture's timeline. 200 with
|
|
597
|
+
// the peaks, 202 while they're being made (a `change` goes out when they land), 204 when the cut
|
|
598
|
+
// has no audio, 501 without ffmpeg (the dashboard then decodes a small file itself).
|
|
599
|
+
app.get("/api/videos/:video/versions/:version/peaks", async (c) => {
|
|
600
|
+
const { video, version } = await cutOf(c.req.param("video"), c.req.param("version"));
|
|
601
|
+
if (!(await peaks.available())) {
|
|
602
|
+
throw new RushesError("The waveform needs ffmpeg, which isn't installed. Run `rushes doctor` for how to add it.", 501, "no_ffmpeg");
|
|
603
|
+
}
|
|
604
|
+
const a = await peaks.answer(video.id, version.id, version.file);
|
|
605
|
+
switch (a.state) {
|
|
606
|
+
case "ready":
|
|
607
|
+
return c.json(a.data, 200, { "cache-control": "no-store" });
|
|
608
|
+
case "computing":
|
|
609
|
+
return c.json({ state: "computing" }, 202, { "cache-control": "no-store" });
|
|
610
|
+
case "silent":
|
|
611
|
+
return c.body(null, 204, { "cache-control": "no-store" });
|
|
612
|
+
case "missing":
|
|
613
|
+
throw new RushesError("The original file is missing", 404, "missing_file", { path: version.file });
|
|
614
|
+
case "failed":
|
|
615
|
+
throw new RushesError(a.reason, 422, "no_peaks");
|
|
616
|
+
}
|
|
617
|
+
});
|
|
588
618
|
app.put("/api/videos/:video/shots", async (c) => {
|
|
589
619
|
const b = await body(c, ShotsBody);
|
|
590
620
|
const { result } = await store.update("project", (p) => setShots(p, c.req.param("video"), b.version, b.shots));
|
package/dist/server/files.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createReadStream } from "node:fs";
|
|
2
|
-
import { stat } from "node:fs/promises";
|
|
2
|
+
import { realpath, stat } from "node:fs/promises";
|
|
3
3
|
import { extname, join, normalize, sep } from "node:path";
|
|
4
4
|
import { Readable } from "node:stream";
|
|
5
5
|
import { PROXY_PATH } from "./proxy.js";
|
|
@@ -85,6 +85,38 @@ export function registeredMedia(project, script) {
|
|
|
85
85
|
files.add(f.file);
|
|
86
86
|
return files;
|
|
87
87
|
}
|
|
88
|
+
/** §15.5: the only kinds of file /media serves when its real path lies outside the project (footage on another drive, say). */
|
|
89
|
+
export const OUTSIDE_MEDIA_EXT = new Set([
|
|
90
|
+
"wav", "mp3", "m4a", "aac", "aif", "aiff", "flac", "ogg", "opus", "mp4", "mov", "m4v", "webm", "mkv",
|
|
91
|
+
"png", "jpg", "jpeg", "gif", "webp", "pdf", "md", "txt", "srt", "vtt",
|
|
92
|
+
]);
|
|
93
|
+
/**
|
|
94
|
+
* The file /media sends for a path it has already accepted: its real path, every symlink
|
|
95
|
+
* resolved. A real path inside the project is served whatever it is, as before. Policy (§15.5):
|
|
96
|
+
* one outside the project is served only if the FINAL real file's extension is media
|
|
97
|
+
* (`OUTSIDE_MEDIA_EXT`), so a take swapped for a link to a key file is never served. Null means
|
|
98
|
+
* refuse. A file that isn't there gives back the plain path, so the send reports it missing.
|
|
99
|
+
*/
|
|
100
|
+
export async function servableFile(root, abs) {
|
|
101
|
+
let real;
|
|
102
|
+
try {
|
|
103
|
+
real = await realpath(abs);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return abs;
|
|
107
|
+
}
|
|
108
|
+
let realRoot;
|
|
109
|
+
try {
|
|
110
|
+
realRoot = await realpath(root);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
if (real === realRoot || real.startsWith(realRoot.endsWith(sep) ? realRoot : realRoot + sep))
|
|
116
|
+
return real;
|
|
117
|
+
const ext = extname(real).toLowerCase().replace(/^\./, "");
|
|
118
|
+
return OUTSIDE_MEDIA_EXT.has(ext) ? real : null;
|
|
119
|
+
}
|
|
88
120
|
/** Parse a single "bytes=a-b" range against a file size. Returns null for a missing or unusable range. */
|
|
89
121
|
export function parseRange(header, size) {
|
|
90
122
|
const m = header?.match(/^bytes=(\d*)-(\d*)$/);
|
|
@@ -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,7 @@ 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";
|
|
11
12
|
export const DEFAULT_PORT = 4580;
|
|
12
13
|
function listen(server, port, host) {
|
|
13
14
|
return new Promise((ok, fail) => {
|
|
@@ -42,7 +43,10 @@ export async function startServer(rootDir, opts = {}) {
|
|
|
42
43
|
// Built on this server's own store, so its progress events reach this server's SSE clients.
|
|
43
44
|
// Every running job is cancelled when the server closes.
|
|
44
45
|
const proxyJobs = new ProxyJobs(store, opts.proxy);
|
|
45
|
-
|
|
46
|
+
// §19.9: waveforms use their own runner (a test's fake proxy encoder is no audio decoder), and
|
|
47
|
+
// ffmpeg is there exactly when proxies say so.
|
|
48
|
+
const peakJobs = new PeakJobs(store, { available: () => proxyJobs.available(), ...opts.peaks });
|
|
49
|
+
const appOpts = { webDir: opts.webDir, onShutdown: () => void close(), reveal: opts.reveal, proxyJobs, peakJobs };
|
|
46
50
|
const app = createApp(store, appOpts);
|
|
47
51
|
const listener = getRequestListener(app.fetch);
|
|
48
52
|
let lastRequest = Date.now();
|
|
@@ -87,6 +91,10 @@ export async function startServer(rootDir, opts = {}) {
|
|
|
87
91
|
// (a version's proxy is set only after a completed render), so it goes. Only the lock winner
|
|
88
92
|
// does this, so it can never delete a file another live server is still writing.
|
|
89
93
|
await removePartials(root);
|
|
94
|
+
// Likewise a waveform that was being saved: its temp file is never read, and goes.
|
|
95
|
+
await removePeakTemps(root);
|
|
96
|
+
// And a waveform whose cut is no longer in the project.
|
|
97
|
+
await removeOrphanPeaks(root, await store.read("project")).catch(() => undefined);
|
|
90
98
|
appOpts.projectId = id;
|
|
91
99
|
const url = `http://${host}:${port}`;
|
|
92
100
|
const stopWatching = await watchStore(store);
|
|
@@ -100,7 +108,7 @@ export async function startServer(rootDir, opts = {}) {
|
|
|
100
108
|
stopWatching();
|
|
101
109
|
// A running proxy's ffmpeg must not outlive the server; its partial file is deleted too.
|
|
102
110
|
// From here no new job starts either (a POST or an autoProxy cut racing the close).
|
|
103
|
-
await proxyJobs.close();
|
|
111
|
+
await Promise.all([proxyJobs.close(), peakJobs.close()]);
|
|
104
112
|
server.closeAllConnections?.();
|
|
105
113
|
await new Promise((ok) => server.close(() => ok()));
|
|
106
114
|
await removeLock(root, token);
|