nanoodle 0.2.0 → 0.5.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.
@@ -0,0 +1,998 @@
1
+ /**
2
+ * Local media ops that the browser runs with canvas / Web Audio / MediaRecorder / MP4CAT.
3
+ *
4
+ * Strategy (mirrors nanoodle/ play.html + index.html):
5
+ * 1. Pure-JS path first — same algorithms the app uses when it can avoid re-encode
6
+ * (MP4CAT remux for matching mp4s, PCM-WAV trim, PNG canvas-equivalent resize).
7
+ * 2. ffmpeg/ffprobe on PATH when pure JS can't handle the format (mismatched combine,
8
+ * JPEG resize, video frame grab, soundtrack mux, non-WAV audio, …). Soft dependency
9
+ * — not an npm package.
10
+ *
11
+ * Eventually the pure path is meant to cover everything the browser does and replace the
12
+ * ffmpeg "custom executor" path; until then ffmpeg remains the heavy fallback.
13
+ *
14
+ * Outputs are data: URLs so they plug into the existing MediaRef / network-inline pipeline.
15
+ */
16
+ import { inflate, deflate } from "./zlib.mjs";
17
+ import { NanoodleError } from "./errors.mjs";
18
+ import { bytesToDataUrl, dataUrlBytes, sniffMime, MEDIA_INLINE_MAX } from "./media.mjs";
19
+ import { MP4CAT } from "./mp4cat.mjs";
20
+
21
+ const MAX_FRAMES = 12;
22
+ /** Refuse pure PNG decode above this edge (memory guard; canvas-class bound). */
23
+ const MAX_IMAGE_DIM = 8192;
24
+ /** Refuse pure WAV decode above this many interleaved samples (~17 min mono @ 48 kHz). */
25
+ const MAX_WAV_SAMPLES = 50_000_000;
26
+ const PROC_STDOUT_MAX = 32 * 1024 * 1024;
27
+
28
+ /* ---------- process helpers ------------------------------------------------ */
29
+
30
+ // The ffmpeg fallback needs Node builtins; they load lazily so this module
31
+ // imports cleanly in a browser, where only the pure-JS paths ever run.
32
+ let spawn, mkdtemp, readFile, writeFile, rm, tmpdir, join;
33
+ async function ensureNodeDeps() {
34
+ if (spawn) return;
35
+ const [cp, fsp, os, path] = await Promise.all([
36
+ import("node:child_process"),
37
+ import("node:fs/promises"),
38
+ import("node:os"),
39
+ import("node:path"),
40
+ ]);
41
+ spawn = cp.spawn;
42
+ mkdtemp = fsp.mkdtemp; readFile = fsp.readFile; writeFile = fsp.writeFile; rm = fsp.rm;
43
+ tmpdir = os.tmpdir; join = path.join;
44
+ }
45
+
46
+ function abortError(signal) {
47
+ const r = signal && signal.reason;
48
+ if (r instanceof Error) return r;
49
+ return new NanoodleError(r != null ? String(r) : "run aborted", { code: "aborted" });
50
+ }
51
+
52
+ export function throwIfAborted(signal) {
53
+ if (signal && signal.aborted) throw abortError(signal);
54
+ }
55
+
56
+ async function runProc(bin, args, { timeoutMs = 120000, signal } = {}) {
57
+ await ensureNodeDeps();
58
+ return new Promise((resolve, reject) => {
59
+ if (signal && signal.aborted) return reject(abortError(signal));
60
+ const child = spawn(bin, args, { stdio: ["ignore", "pipe", "pipe"] });
61
+ let stdout = Buffer.alloc(0), stderr = Buffer.alloc(0);
62
+ let settled = false;
63
+ const finish = (fn) => {
64
+ if (settled) return;
65
+ settled = true;
66
+ clearTimeout(to);
67
+ if (signal) signal.removeEventListener("abort", onAbort);
68
+ fn();
69
+ };
70
+ const to = setTimeout(() => {
71
+ child.kill("SIGKILL");
72
+ finish(() => reject(new NanoodleError(`${bin} timed out after ${timeoutMs}ms`, { code: "timeout" })));
73
+ }, timeoutMs);
74
+ const onAbort = () => {
75
+ child.kill("SIGKILL");
76
+ finish(() => reject(abortError(signal)));
77
+ };
78
+ if (signal) signal.addEventListener("abort", onAbort, { once: true });
79
+ child.stdout.on("data", (d) => {
80
+ if (stdout.length < PROC_STDOUT_MAX) {
81
+ stdout = Buffer.concat([stdout, d.length + stdout.length > PROC_STDOUT_MAX
82
+ ? d.subarray(0, PROC_STDOUT_MAX - stdout.length) : d]);
83
+ }
84
+ });
85
+ child.stderr.on("data", (d) => {
86
+ // keep a trailing window for error messages
87
+ stderr = Buffer.concat([stderr, d]);
88
+ if (stderr.length > 64 * 1024) stderr = stderr.subarray(stderr.length - 64 * 1024);
89
+ });
90
+ child.on("error", (e) => {
91
+ if (e && e.code === "ENOENT") {
92
+ finish(() => reject(new NanoodleError(
93
+ `local media nodes need ffmpeg on PATH (not found: ${bin}). ` +
94
+ "Install ffmpeg, or run this graph in the nanoodle browser app.")));
95
+ } else finish(() => reject(e));
96
+ });
97
+ child.on("close", (code) => {
98
+ if (code === 0) finish(() => resolve({ stdout, stderr: stderr.toString("utf8") }));
99
+ else finish(() => reject(new NanoodleError(
100
+ `${bin} failed (exit ${code}): ${(stderr.toString("utf8") || "").trim().slice(-400) || "no stderr"}`)));
101
+ });
102
+ });
103
+ }
104
+
105
+ function isMissingFfmpeg(err) {
106
+ return err instanceof NanoodleError && /need ffmpeg on PATH/i.test(err.message || "");
107
+ }
108
+
109
+ async function withTemp(fn) {
110
+ await ensureNodeDeps();
111
+ const dir = await mkdtemp(join(tmpdir(), "nanoodle-media-"));
112
+ try { return await fn(dir); }
113
+ finally { await rm(dir, { recursive: true, force: true }).catch(() => {}); }
114
+ }
115
+
116
+ /** data:/https URL (or raw string MediaRef url) → bytes. */
117
+ async function urlBytes(url, fetchFn) {
118
+ if (url == null) throw new NanoodleError("no media input");
119
+ const u = typeof url === "object" && url.url != null ? url.url : String(url);
120
+ if (/^data:/i.test(u)) return dataUrlBytes(u).bytes;
121
+ if (/^https?:/i.test(u)) {
122
+ const fetchImpl = fetchFn || globalThis.fetch;
123
+ if (!fetchImpl) throw new NanoodleError("can't download media: no fetch available");
124
+ const r = await fetchImpl(u);
125
+ if (!r.ok) throw new NanoodleError(`couldn't download media (${r.status}): ${u.slice(0, 120)}`);
126
+ return new Uint8Array(await r.arrayBuffer());
127
+ }
128
+ throw new NanoodleError("media must be a data: or http(s) URL");
129
+ }
130
+
131
+ async function writeInput(dir, name, url, fetchFn) {
132
+ await ensureNodeDeps();
133
+ const bytes = await urlBytes(url, fetchFn);
134
+ // preserve a sensible extension so ffmpeg picks the demuxer
135
+ let ext = ".bin";
136
+ if (/^data:/i.test(String(typeof url === "object" ? url.url : url))) {
137
+ const mime = sniffMime(bytes);
138
+ ext = mime.includes("png") ? ".png"
139
+ : mime.includes("jpeg") ? ".jpg"
140
+ : mime.includes("webp") ? ".webp"
141
+ : mime.includes("gif") ? ".gif"
142
+ : mime.includes("wav") ? ".wav"
143
+ : mime.includes("mpeg") || mime.includes("mp3") ? ".mp3"
144
+ : mime.includes("mp4") ? ".mp4"
145
+ : mime.includes("webm") ? ".webm"
146
+ : ".bin";
147
+ } else {
148
+ const m = /\.([a-z0-9]{2,5})(?:\?|$)/i.exec(String(typeof url === "object" ? url.url : url));
149
+ if (m) ext = "." + m[1].toLowerCase();
150
+ }
151
+ const path = join(dir, name + ext);
152
+ await writeFile(path, bytes);
153
+ return path;
154
+ }
155
+
156
+ async function dataUrlFromFile(path, mimeHint) {
157
+ await ensureNodeDeps();
158
+ const buf = await readFile(path);
159
+ const u8 = new Uint8Array(buf);
160
+ const mime = mimeHint || sniffMime(u8);
161
+ return bytesToDataUrl(u8, mime);
162
+ }
163
+
164
+ function dataUrlFromBytes(bytes, mime) {
165
+ return bytesToDataUrl(bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes), mime);
166
+ }
167
+
168
+ /* ---------- resizePlan (verbatim from index.html / play.html) -------------- */
169
+
170
+ /** @returns {{ cw, ch, dx, dy, dw, dh }|null} */
171
+ export function resizePlan(sw, sh, mode, tw, th) {
172
+ if (!(tw > 0) && !(th > 0)) return null;
173
+ if (mode === "fit") {
174
+ let scale;
175
+ if (tw > 0 && th > 0) scale = Math.min(tw / sw, th / sh);
176
+ else if (tw > 0) scale = tw / sw;
177
+ else scale = th / sh;
178
+ if (scale > 1) scale = 1; // never upscale
179
+ const w = Math.max(1, Math.round(sw * scale));
180
+ const h = Math.max(1, Math.round(sh * scale));
181
+ return { cw: w, ch: h, dx: 0, dy: 0, dw: w, dh: h };
182
+ }
183
+ const bw = tw > 0 ? tw : Math.max(1, Math.round(th * sw / sh));
184
+ const bh = th > 0 ? th : Math.max(1, Math.round(tw * sh / sw));
185
+ if (mode === "exact") return { cw: bw, ch: bh, dx: 0, dy: 0, dw: bw, dh: bh };
186
+ // fill & crop: cover, centered
187
+ const scale = Math.max(bw / sw, bh / sh);
188
+ const dw = sw * scale, dh = sh * scale;
189
+ return { cw: bw, ch: bh, dx: (bw - dw) / 2, dy: (bh - dh) / 2, dw, dh };
190
+ }
191
+
192
+ /* ---------- pure PNG (canvas-equivalent resize for PNG sources) ------------ */
193
+
194
+ function crc32(buf) {
195
+ let c = ~0;
196
+ for (let i = 0; i < buf.length; i++) {
197
+ c ^= buf[i];
198
+ for (let k = 0; k < 8; k++) c = (c >>> 1) ^ (0xedb88320 & -(c & 1));
199
+ }
200
+ return (~c) >>> 0;
201
+ }
202
+
203
+ function catBytes(parts) {
204
+ let total = 0;
205
+ for (const p of parts) total += p.length;
206
+ const out = new Uint8Array(total);
207
+ let o = 0;
208
+ for (const p of parts) { out.set(p, o); o += p.length; }
209
+ return out;
210
+ }
211
+
212
+ function pngChunk(type, data) {
213
+ const out = new Uint8Array(12 + data.length);
214
+ const dv = new DataView(out.buffer);
215
+ dv.setUint32(0, data.length);
216
+ for (let i = 0; i < 4; i++) out[4 + i] = type.charCodeAt(i);
217
+ out.set(data, 8);
218
+ dv.setUint32(8 + data.length, crc32(out.subarray(4, 8 + data.length)));
219
+ return out;
220
+ }
221
+
222
+ /** Decode 8-bit RGB/RGBA/gray/gray+alpha PNG → { w, h, rgba:Uint8ClampedArray }. */
223
+ async function decodePng(bytes) {
224
+ const u8 = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
225
+ if (u8.length < 8 || u8[0] !== 0x89 || u8[1] !== 0x50) {
226
+ throw new NanoodleError("couldn't read that image to resize");
227
+ }
228
+ const sig = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
229
+ for (let i = 0; i < 8; i++) if (u8[i] !== sig[i]) throw new NanoodleError("couldn't read that image to resize");
230
+
231
+ let w = 0, h = 0, bitDepth = 8, colorType = 2;
232
+ const idats = [];
233
+ let p = 8;
234
+ while (p + 8 <= u8.length) {
235
+ // >>> 0: PNG chunk lengths are unsigned; JS << is signed 32-bit
236
+ const len = ((u8[p] << 24) | (u8[p + 1] << 16) | (u8[p + 2] << 8) | u8[p + 3]) >>> 0;
237
+ if (p + 12 + len > u8.length) throw new NanoodleError("couldn't read that image to resize");
238
+ const type = String.fromCharCode(u8[p + 4], u8[p + 5], u8[p + 6], u8[p + 7]);
239
+ const data = u8.subarray(p + 8, p + 8 + len);
240
+ p += 12 + len;
241
+ if (type === "IHDR") {
242
+ w = ((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]) >>> 0;
243
+ h = ((data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7]) >>> 0;
244
+ bitDepth = data[8];
245
+ colorType = data[9];
246
+ if (data[10] !== 0 || data[11] !== 0 || data[12] !== 0) {
247
+ throw new NanoodleError("couldn't read that image to resize"); // compressed/filter/interlace
248
+ }
249
+ } else if (type === "IDAT") {
250
+ idats.push(data);
251
+ } else if (type === "IEND") break;
252
+ }
253
+ if (!(w > 0) || !(h > 0) || bitDepth !== 8) {
254
+ throw new NanoodleError("couldn't read that image to resize");
255
+ }
256
+ if (w > MAX_IMAGE_DIM || h > MAX_IMAGE_DIM) {
257
+ throw new NanoodleError(
258
+ `image is too large to resize in-process (${w}×${h}; max ${MAX_IMAGE_DIM}px) — use smaller source dimensions`);
259
+ }
260
+ // colorType: 0 gray, 2 RGB, 4 gray+A, 6 RGBA (no palette)
261
+ if (colorType !== 0 && colorType !== 2 && colorType !== 4 && colorType !== 6) {
262
+ throw new NanoodleError("couldn't read that image to resize");
263
+ }
264
+ const cpp = colorType === 0 ? 1 : colorType === 2 ? 3 : colorType === 4 ? 2 : 4;
265
+ const raw = await inflate(catBytes(idats));
266
+ const stride = w * cpp;
267
+ const expected = h * (1 + stride);
268
+ if (raw.length < expected) throw new NanoodleError("couldn't read that image to resize");
269
+
270
+ const unfiltered = new Uint8Array(h * stride);
271
+ for (let y = 0; y < h; y++) {
272
+ const ftype = raw[y * (1 + stride)];
273
+ const row = raw.subarray(y * (1 + stride) + 1, y * (1 + stride) + 1 + stride);
274
+ const dest = unfiltered.subarray(y * stride, (y + 1) * stride);
275
+ const prev = y > 0 ? unfiltered.subarray((y - 1) * stride, y * stride) : null;
276
+ for (let x = 0; x < stride; x++) {
277
+ const left = x >= cpp ? dest[x - cpp] : 0;
278
+ const up = prev ? prev[x] : 0;
279
+ const upLeft = prev && x >= cpp ? prev[x - cpp] : 0;
280
+ let v = row[x];
281
+ if (ftype === 1) v = (v + left) & 255; // Sub
282
+ else if (ftype === 2) v = (v + up) & 255; // Up
283
+ else if (ftype === 3) v = (v + ((left + up) >> 1)) & 255; // Average
284
+ else if (ftype === 4) { // Paeth
285
+ const p0 = left + up - upLeft;
286
+ const pa = Math.abs(p0 - left), pb = Math.abs(p0 - up), pc = Math.abs(p0 - upLeft);
287
+ const pr = pa <= pb && pa <= pc ? left : pb <= pc ? up : upLeft;
288
+ v = (v + pr) & 255;
289
+ } else if (ftype !== 0) {
290
+ throw new NanoodleError("couldn't read that image to resize");
291
+ }
292
+ dest[x] = v;
293
+ }
294
+ }
295
+
296
+ const rgba = new Uint8ClampedArray(w * h * 4);
297
+ for (let i = 0, px = 0; i < w * h; i++, px += cpp) {
298
+ const o = i * 4;
299
+ if (colorType === 0) {
300
+ rgba[o] = rgba[o + 1] = rgba[o + 2] = unfiltered[px];
301
+ rgba[o + 3] = 255;
302
+ } else if (colorType === 2) {
303
+ rgba[o] = unfiltered[px]; rgba[o + 1] = unfiltered[px + 1]; rgba[o + 2] = unfiltered[px + 2];
304
+ rgba[o + 3] = 255;
305
+ } else if (colorType === 4) {
306
+ rgba[o] = rgba[o + 1] = rgba[o + 2] = unfiltered[px];
307
+ rgba[o + 3] = unfiltered[px + 1];
308
+ } else {
309
+ rgba[o] = unfiltered[px]; rgba[o + 1] = unfiltered[px + 1];
310
+ rgba[o + 2] = unfiltered[px + 2]; rgba[o + 3] = unfiltered[px + 3];
311
+ }
312
+ }
313
+ return { w, h, rgba };
314
+ }
315
+
316
+ async function encodePngRgba(w, h, rgba) {
317
+ // Filter type 0 (None) per row — simple, correct.
318
+ const stride = w * 4;
319
+ const raw = new Uint8Array(h * (1 + stride));
320
+ for (let y = 0; y < h; y++) {
321
+ raw[y * (1 + stride)] = 0;
322
+ raw.set(rgba.subarray(y * stride, (y + 1) * stride), y * (1 + stride) + 1);
323
+ }
324
+ const compressed = await deflate(raw, { level: 9 });
325
+ const ihdr = new Uint8Array(13);
326
+ const dv = new DataView(ihdr.buffer);
327
+ dv.setUint32(0, w);
328
+ dv.setUint32(4, h);
329
+ ihdr[8] = 8; // bit depth
330
+ ihdr[9] = 6; // RGBA
331
+ ihdr[10] = 0; ihdr[11] = 0; ihdr[12] = 0;
332
+ const sig = Uint8Array.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
333
+ return catBytes([
334
+ sig,
335
+ pngChunk("IHDR", ihdr),
336
+ pngChunk("IDAT", compressed),
337
+ pngChunk("IEND", new Uint8Array(0)),
338
+ ]);
339
+ }
340
+
341
+ /** Bilinear sample of source RGBA at floating pixel coords (canvas-like smoothing). */
342
+ function sampleBilinear(src, sw, sh, x, y) {
343
+ const x0 = Math.floor(x), y0 = Math.floor(y);
344
+ const x1 = Math.min(sw - 1, x0 + 1), y1 = Math.min(sh - 1, y0 + 1);
345
+ const fx = x - x0, fy = y - y0;
346
+ const i00 = (y0 * sw + x0) * 4, i10 = (y0 * sw + x1) * 4;
347
+ const i01 = (y1 * sw + x0) * 4, i11 = (y1 * sw + x1) * 4;
348
+ const out = new Uint8ClampedArray(4);
349
+ for (let c = 0; c < 4; c++) {
350
+ const v =
351
+ src[i00 + c] * (1 - fx) * (1 - fy) +
352
+ src[i10 + c] * fx * (1 - fy) +
353
+ src[i01 + c] * (1 - fx) * fy +
354
+ src[i11 + c] * fx * fy;
355
+ out[c] = Math.round(v);
356
+ }
357
+ return out;
358
+ }
359
+
360
+ /**
361
+ * Inpaint mask prep (play.html `maskToSource` / index.html canvas path):
362
+ * composite the mask onto opaque black at the **source image's** exact pixel size
363
+ * so maskDataUrl dimensions always match imageDataUrl. White (or opaque white-on-
364
+ * transparent brush strokes) = repaint; black = keep.
365
+ *
366
+ * Pure PNG path first; ffmpeg soft-fallback for JPEG/WebP/etc.
367
+ *
368
+ * @param {string} maskUrl data: or http(s)
369
+ * @param {string} sourceUrl data: or http(s) — dimensions only (pixels not sent as mask)
370
+ * @returns {Promise<string>} data:image/png;base64,…
371
+ */
372
+ export async function maskToSource(maskUrl, sourceUrl, { fetch: fetchFn, signal } = {}) {
373
+ throwIfAborted(signal);
374
+ if (!maskUrl) throw new NanoodleError("couldn't read the mask");
375
+ if (!sourceUrl) throw new NanoodleError("couldn't read the source image");
376
+
377
+ const srcBytes = await urlBytes(sourceUrl, fetchFn);
378
+ throwIfAborted(signal);
379
+ const maskBytes = await urlBytes(maskUrl, fetchFn);
380
+ throwIfAborted(signal);
381
+
382
+ const srcMime = sniffMime(srcBytes);
383
+ const maskMime = sniffMime(maskBytes);
384
+ if (srcMime === "image/png" && maskMime === "image/png") {
385
+ try {
386
+ return dataUrlFromBytes(await maskToSourcePngPure(maskBytes, srcBytes), "image/png");
387
+ } catch (e) {
388
+ if (e instanceof NanoodleError) {
389
+ if (/couldn't read the (mask|source)/i.test(e.message || "")) throw e;
390
+ if (/too large/i.test(e.message || "")) throw e;
391
+ // exotic PNG → try ffmpeg
392
+ } else {
393
+ throw e;
394
+ }
395
+ }
396
+ }
397
+
398
+ return maskToSourceFfmpeg(maskUrl, sourceUrl, { fetch: fetchFn, signal });
399
+ }
400
+
401
+ /** Pure PNG composite matching canvas: black fill + drawImage(mask → source size). */
402
+ async function maskToSourcePngPure(maskBytes, srcBytes) {
403
+ let src, mask;
404
+ try { src = await decodePng(srcBytes); }
405
+ catch { throw new NanoodleError("couldn't read the source image"); }
406
+ try { mask = await decodePng(maskBytes); }
407
+ catch { throw new NanoodleError("couldn't read the mask"); }
408
+
409
+ const sw = src.w, sh = src.h;
410
+ if (!(sw > 0) || !(sh > 0)) throw new NanoodleError("couldn't read the source image");
411
+ if (sw > MAX_IMAGE_DIM || sh > MAX_IMAGE_DIM) {
412
+ throw new NanoodleError(
413
+ `image is too large to composite mask in-process (${sw}×${sh}; max ${MAX_IMAGE_DIM}px)`);
414
+ }
415
+
416
+ // Opaque black canvas (keep = black), same size as source.
417
+ const out = new Uint8ClampedArray(sw * sh * 4);
418
+ for (let i = 0; i < sw * sh; i++) {
419
+ const o = i * 4;
420
+ out[o] = 0; out[o + 1] = 0; out[o + 2] = 0; out[o + 3] = 255;
421
+ }
422
+
423
+ // drawImage(mask, 0, 0, sw, sh) with source-over (browser default).
424
+ const mw = mask.w, mh = mask.h;
425
+ const mrgba = mask.rgba;
426
+ for (let y = 0; y < sh; y++) {
427
+ for (let x = 0; x < sw; x++) {
428
+ const u = (x + 0.5) / sw * mw - 0.5;
429
+ const v = (y + 0.5) / sh * mh - 0.5;
430
+ const sx = Math.max(0, Math.min(mw - 1, u));
431
+ const sy = Math.max(0, Math.min(mh - 1, v));
432
+ const pix = sampleBilinear(mrgba, mw, mh, sx, sy);
433
+ const a = pix[3] / 255;
434
+ const o = (y * sw + x) * 4;
435
+ // source-over onto opaque black dst
436
+ out[o] = Math.round(pix[0] * a + out[o] * (1 - a));
437
+ out[o + 1] = Math.round(pix[1] * a + out[o + 1] * (1 - a));
438
+ out[o + 2] = Math.round(pix[2] * a + out[o + 2] * (1 - a));
439
+ out[o + 3] = 255;
440
+ }
441
+ }
442
+ return encodePngRgba(sw, sh, out);
443
+ }
444
+
445
+ async function maskToSourceFfmpeg(maskUrl, sourceUrl, { fetch: fetchFn, signal } = {}) {
446
+ return withTemp(async (dir) => {
447
+ throwIfAborted(signal);
448
+ const srcPath = await writeInput(dir, "src", sourceUrl, fetchFn);
449
+ const maskPath = await writeInput(dir, "mask", maskUrl, fetchFn);
450
+ const probe = await runProc("ffprobe", [
451
+ "-v", "error", "-select_streams", "v:0",
452
+ "-show_entries", "stream=width,height", "-of", "csv=p=0:s=x", srcPath,
453
+ ], { signal }).catch(() => {
454
+ throw new NanoodleError("couldn't read the source image");
455
+ });
456
+ const dims = String(probe.stdout).trim().split("x").map(Number);
457
+ const sw = dims[0], sh = dims[1];
458
+ if (!(sw > 0) || !(sh > 0)) throw new NanoodleError("couldn't read the source image");
459
+ const outPath = join(dir, "mask-out.png");
460
+ // black base of source size + scaled mask overlaid (matches canvas path)
461
+ try {
462
+ await runProc("ffmpeg", [
463
+ "-y",
464
+ "-f", "lavfi", "-i", `color=c=black:s=${sw}x${sh}:d=1`,
465
+ "-i", maskPath,
466
+ "-filter_complex",
467
+ `[1:v]scale=${sw}:${sh}:flags=bilinear,format=rgba[m];[0:v][m]overlay=0:0:format=auto`,
468
+ "-frames:v", "1", outPath,
469
+ ], { signal });
470
+ } catch {
471
+ throw new NanoodleError("couldn't read the mask");
472
+ }
473
+ return dataUrlFromFile(outPath, "image/png");
474
+ });
475
+ }
476
+
477
+ /**
478
+ * Pure resize matching canvas drawImage(img, dx, dy, dw, dh) onto cw×ch.
479
+ * PNG only (JPEG needs ffmpeg). Output always PNG (preserves alpha like browser PNG path).
480
+ */
481
+ async function resizeCropPngPure(bytes, mode, tw, th) {
482
+ const { w: sw, h: sh, rgba } = await decodePng(bytes);
483
+ const p = resizePlan(sw, sh, mode, tw, th);
484
+ if (!p) throw new NanoodleError("set a width or height to resize to");
485
+ // Canvas default: transparent black outside the draw rect.
486
+ const out = new Uint8ClampedArray(p.cw * p.ch * 4);
487
+ for (let y = 0; y < p.ch; y++) {
488
+ for (let x = 0; x < p.cw; x++) {
489
+ // Inverse of drawImage(img, dx, dy, dw, dh): dest pixel → continuous source coords.
490
+ const u = (x + 0.5 - p.dx) / p.dw * sw - 0.5;
491
+ const v = (y + 0.5 - p.dy) / p.dh * sh - 0.5;
492
+ if (u < -0.5 || v < -0.5 || u > sw - 0.5 || v > sh - 0.5) continue;
493
+ const sx = Math.max(0, Math.min(sw - 1, u));
494
+ const sy = Math.max(0, Math.min(sh - 1, v));
495
+ const pix = sampleBilinear(rgba, sw, sh, sx, sy);
496
+ const o = (y * p.cw + x) * 4;
497
+ out[o] = pix[0]; out[o + 1] = pix[1]; out[o + 2] = pix[2]; out[o + 3] = pix[3];
498
+ }
499
+ }
500
+ return encodePngRgba(p.cw, p.ch, out);
501
+ }
502
+
503
+ /* ---------- pure WAV (encodeWavMono + PCM trim — from play.html) ----------- */
504
+
505
+ /** encodeWavMono from play.html — Float32 mono samples → PCM16 WAV bytes. */
506
+ export function encodeWavMono(samples, sampleRate) {
507
+ const n = samples.length, dataLen = n * 2;
508
+ const ab = new ArrayBuffer(44 + dataLen);
509
+ const dv = new DataView(ab);
510
+ const ws = (o, s) => { for (let i = 0; i < s.length; i++) dv.setUint8(o + i, s.charCodeAt(i)); };
511
+ ws(0, "RIFF"); dv.setUint32(4, 36 + dataLen, true); ws(8, "WAVE");
512
+ ws(12, "fmt "); dv.setUint32(16, 16, true); dv.setUint16(20, 1, true);
513
+ dv.setUint16(22, 1, true); dv.setUint32(24, sampleRate, true);
514
+ dv.setUint32(28, sampleRate * 2, true); dv.setUint16(32, 2, true); dv.setUint16(34, 16, true);
515
+ ws(36, "data"); dv.setUint32(40, dataLen, true);
516
+ let off = 44;
517
+ for (let i = 0; i < n; i++) {
518
+ const v = Math.max(-1, Math.min(1, samples[i]));
519
+ dv.setInt16(off, v < 0 ? v * 0x8000 : v * 0x7fff, true);
520
+ off += 2;
521
+ }
522
+ return new Uint8Array(ab);
523
+ }
524
+
525
+ /** Parse PCM WAV → { sampleRate, channels, samples: Float32Array interleaved }. */
526
+ function parsePcmWav(bytes) {
527
+ const u8 = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
528
+ if (u8.length < 44) throw new NanoodleError("couldn't decode that audio for trimming (unsupported format?)");
529
+ const dv = new DataView(u8.buffer, u8.byteOffset, u8.byteLength);
530
+ const ascii = (o, n) => String.fromCharCode(...u8.subarray(o, o + n));
531
+ if (ascii(0, 4) !== "RIFF" || ascii(8, 4) !== "WAVE") {
532
+ throw new NanoodleError("couldn't decode that audio for trimming (unsupported format?)");
533
+ }
534
+ let sampleRate = 0, channels = 0, bits = 0, dataOff = -1, dataLen = 0;
535
+ let p = 12;
536
+ while (p + 8 <= u8.length) {
537
+ const id = ascii(p, 4);
538
+ const size = dv.getUint32(p + 4, true);
539
+ const body = p + 8;
540
+ if (id === "fmt ") {
541
+ const format = dv.getUint16(body, true);
542
+ if (format !== 1 && format !== 3) {
543
+ throw new NanoodleError("couldn't decode that audio for trimming (unsupported format?)");
544
+ }
545
+ channels = dv.getUint16(body + 2, true);
546
+ sampleRate = dv.getUint32(body + 4, true);
547
+ bits = dv.getUint16(body + 14, true);
548
+ // store format in bits high for float32: mark via bits===32 && format===3
549
+ if (format === 3) bits = -32; // float32 sentinel
550
+ } else if (id === "data") {
551
+ dataOff = body;
552
+ // clamp claimed size to bytes actually present (truncated / lying headers)
553
+ dataLen = Math.min(size, Math.max(0, u8.length - body));
554
+ break;
555
+ }
556
+ p = body + size + (size & 1); // word-align
557
+ }
558
+ if (dataOff < 0 || !(sampleRate > 0) || !(channels > 0)) {
559
+ throw new NanoodleError("couldn't decode that audio for trimming (unsupported format?)");
560
+ }
561
+ let samples;
562
+ if (bits === 16) {
563
+ const n = Math.floor(dataLen / 2);
564
+ if (n > MAX_WAV_SAMPLES) {
565
+ throw new NanoodleError("audio is too long to trim in-process — use a shorter clip");
566
+ }
567
+ samples = new Float32Array(n);
568
+ for (let i = 0; i < n; i++) samples[i] = dv.getInt16(dataOff + i * 2, true) / 0x8000;
569
+ } else if (bits === 8) {
570
+ const n = dataLen;
571
+ if (n > MAX_WAV_SAMPLES) {
572
+ throw new NanoodleError("audio is too long to trim in-process — use a shorter clip");
573
+ }
574
+ samples = new Float32Array(n);
575
+ for (let i = 0; i < n; i++) samples[i] = (u8[dataOff + i] - 128) / 128;
576
+ } else if (bits === -32) {
577
+ const n = Math.floor(dataLen / 4);
578
+ if (n > MAX_WAV_SAMPLES) {
579
+ throw new NanoodleError("audio is too long to trim in-process — use a shorter clip");
580
+ }
581
+ samples = new Float32Array(n);
582
+ for (let i = 0; i < n; i++) samples[i] = dv.getFloat32(dataOff + i * 4, true);
583
+ } else if (bits === 32) {
584
+ const n = Math.floor(dataLen / 4);
585
+ if (n > MAX_WAV_SAMPLES) {
586
+ throw new NanoodleError("audio is too long to trim in-process — use a shorter clip");
587
+ }
588
+ samples = new Float32Array(n);
589
+ for (let i = 0; i < n; i++) samples[i] = dv.getInt32(dataOff + i * 4, true) / 0x80000000;
590
+ } else {
591
+ throw new NanoodleError("couldn't decode that audio for trimming (unsupported format?)");
592
+ }
593
+ return { sampleRate, channels, samples };
594
+ }
595
+
596
+ function downmixMono(samples, channels) {
597
+ if (channels === 1) return samples;
598
+ const frames = Math.floor(samples.length / channels);
599
+ const out = new Float32Array(frames);
600
+ for (let i = 0; i < frames; i++) {
601
+ let s = 0;
602
+ for (let c = 0; c < channels; c++) s += samples[i * channels + c];
603
+ out[i] = s / channels;
604
+ }
605
+ return out;
606
+ }
607
+
608
+ /** Linear resample mono Float32 to target rate (OfflineAudioContext stand-in). */
609
+ function resampleMono(samples, fromRate, toRate) {
610
+ if (fromRate === toRate) return samples;
611
+ const ratio = fromRate / toRate;
612
+ const n = Math.max(1, Math.floor(samples.length / ratio));
613
+ const out = new Float32Array(n);
614
+ for (let i = 0; i < n; i++) {
615
+ const x = i * ratio;
616
+ const i0 = Math.floor(x);
617
+ const i1 = Math.min(samples.length - 1, i0 + 1);
618
+ const f = x - i0;
619
+ out[i] = samples[i0] * (1 - f) + samples[i1] * f;
620
+ }
621
+ return out;
622
+ }
623
+
624
+ /**
625
+ * Pure PCM-WAV trim — same slice semantics as play.html trimAudioToWavUrl.
626
+ * wholeIfBlank: extractaudio path (blank length → rest of clip).
627
+ */
628
+ function trimPcmWavPure(bytes, start, len, rate, { wholeIfBlank = false } = {}) {
629
+ const { sampleRate, channels, samples } = parsePcmWav(bytes);
630
+ let mono = downmixMono(samples, channels);
631
+ const dur = mono.length / sampleRate;
632
+ const s0 = start || 0;
633
+ if (s0 >= dur) {
634
+ throw new NanoodleError(
635
+ `the start point (${Math.round(s0 * 10) / 10}s) is past the end of this clip, which is only ${dur.toFixed(1)}s long — pick an earlier start`);
636
+ }
637
+ const s = Math.max(0, Math.min(s0, Math.max(0, dur - 0.05)));
638
+ let take;
639
+ if (wholeIfBlank && !(len > 0)) take = Math.max(0.05, dur - s);
640
+ else {
641
+ const L = Number.isFinite(Number(len)) && Number(len) > 0 ? Number(len) : 30;
642
+ take = Math.max(0.05, Math.min(L, dur - s));
643
+ }
644
+ const i0 = Math.floor(s * sampleRate);
645
+ const n = Math.max(1, Math.floor(take * sampleRate));
646
+ const sliced = mono.subarray(i0, Math.min(mono.length, i0 + n));
647
+ const targetRate = rate || 16000;
648
+ const out = resampleMono(sliced, sampleRate, targetRate);
649
+ return encodeWavMono(out, targetRate);
650
+ }
651
+
652
+ /* ---------- resize (pure PNG → ffmpeg) ------------------------------------- */
653
+
654
+ /**
655
+ * Resize/crop an image URL. mode: fit | fill | exact.
656
+ * Pure path: PNG via zlib (canvas-equivalent geometry). JPEG/WebP/… → ffmpeg.
657
+ * Browser keeps PNG for PNG sources (alpha); others → JPEG q≈0.92 (ffmpeg -q:v 2).
658
+ */
659
+ export async function resizeCropImage(url, mode, tw, th, { fetch: fetchFn, signal } = {}) {
660
+ throwIfAborted(signal);
661
+ const w = Math.max(0, parseInt(tw, 10) || 0);
662
+ const h = Math.max(0, parseInt(th, 10) || 0);
663
+ if (!w && !h) throw new NanoodleError("set a width or height to resize to");
664
+ const m = mode || "fit";
665
+
666
+ const bytes = await urlBytes(url, fetchFn);
667
+ throwIfAborted(signal);
668
+ const mime = sniffMime(bytes);
669
+
670
+ if (mime === "image/png") {
671
+ try {
672
+ const out = await resizeCropPngPure(bytes, m, w, h);
673
+ const dataUrl = dataUrlFromBytes(out, "image/png");
674
+ if (dataUrl.length > MEDIA_INLINE_MAX) {
675
+ throw new NanoodleError("resized image is still over the ~4 MB inline limit — pick smaller dimensions");
676
+ }
677
+ return dataUrl;
678
+ } catch (e) {
679
+ // rethrow user-facing limits; only fall through for "couldn't read" / exotic PNG
680
+ if (e instanceof NanoodleError) {
681
+ if (/width or height|inline limit|too large to resize/i.test(e.message || "")) throw e;
682
+ if (!/couldn't read that image/i.test(e.message || "")) throw e;
683
+ } else {
684
+ throw e; // unexpected (OOM etc.) — don't mask with ffmpeg
685
+ }
686
+ // exotic PNG → try ffmpeg
687
+ }
688
+ }
689
+
690
+ return resizeCropImageFfmpeg(url, m, w, h, { fetch: fetchFn, signal });
691
+ }
692
+
693
+ async function resizeCropImageFfmpeg(url, m, w, h, { fetch: fetchFn, signal } = {}) {
694
+ return withTemp(async (dir) => {
695
+ throwIfAborted(signal);
696
+ const inPath = await writeInput(dir, "in", url, fetchFn);
697
+ const probe = await runProc("ffprobe", [
698
+ "-v", "error", "-select_streams", "v:0",
699
+ "-show_entries", "stream=width,height", "-of", "csv=p=0:s=x", inPath,
700
+ ], { signal });
701
+ const dims = String(probe.stdout).trim().split("x").map(Number);
702
+ const sw = dims[0], sh = dims[1];
703
+ if (!(sw > 0) || !(sh > 0)) throw new NanoodleError("couldn't read that image to resize");
704
+ const p = resizePlan(sw, sh, m, w, h);
705
+ if (!p) throw new NanoodleError("set a width or height to resize to");
706
+
707
+ const srcUrl = typeof url === "object" && url.url != null ? url.url : String(url);
708
+ const wantPng = /^data:image\/png/i.test(srcUrl) || /\.png$/i.test(inPath);
709
+ const outPath = join(dir, wantPng ? "out.png" : "out.jpg");
710
+
711
+ let vf;
712
+ if (m === "fit" || m === "exact") {
713
+ vf = `scale=${p.cw}:${p.ch}`;
714
+ } else {
715
+ vf = `scale=${p.cw}:${p.ch}:force_original_aspect_ratio=increase,crop=${p.cw}:${p.ch}`;
716
+ }
717
+
718
+ const args = ["-y", "-i", inPath, "-vf", vf];
719
+ if (wantPng) args.push("-frames:v", "1", outPath);
720
+ else args.push("-frames:v", "1", "-q:v", "2", outPath);
721
+ await runProc("ffmpeg", args, { signal });
722
+
723
+ const out = await dataUrlFromFile(outPath, wantPng ? "image/png" : "image/jpeg");
724
+ if (out.length > MEDIA_INLINE_MAX) {
725
+ throw new NanoodleError("resized image is still over the ~4 MB inline limit — pick smaller dimensions");
726
+ }
727
+ return out;
728
+ });
729
+ }
730
+
731
+ /* ---------- audio trim / extract (pure WAV → ffmpeg) ----------------------- */
732
+
733
+ /**
734
+ * Decode audio (or demux audio from video), slice [start, start+len], mono at `rate` Hz → data:audio/wav.
735
+ * Pure path for PCM WAV (encodeWavMono + slice, same defaults as play.html).
736
+ * len<=0 means "to end" for extract; for trim browser default length is 30 when blank.
737
+ */
738
+ export async function trimAudioToWav(url, start, len, rate = 16000, { fetch: fetchFn, wholeIfBlank = false, signal } = {}) {
739
+ throwIfAborted(signal);
740
+ const bytes = await urlBytes(url, fetchFn);
741
+ throwIfAborted(signal);
742
+ const mime = sniffMime(bytes);
743
+
744
+ if (mime === "audio/wav") {
745
+ try {
746
+ const wav = trimPcmWavPure(bytes, start, len, rate, { wholeIfBlank });
747
+ return dataUrlFromBytes(wav, "audio/wav");
748
+ } catch (e) {
749
+ // past-end / too-long are final; only "unsupported format" falls through to ffmpeg
750
+ if (e instanceof NanoodleError) {
751
+ if (/past the end|too long to trim/i.test(e.message || "")) throw e;
752
+ if (!/unsupported format/i.test(e.message || "")) throw e;
753
+ } else {
754
+ throw e;
755
+ }
756
+ // non-PCM or exotic WAV → ffmpeg
757
+ }
758
+ }
759
+
760
+ return trimAudioToWavFfmpeg(url, start, len, rate, { fetch: fetchFn, wholeIfBlank, signal });
761
+ }
762
+
763
+ async function trimAudioToWavFfmpeg(url, start, len, rate = 16000, { fetch: fetchFn, wholeIfBlank = false, signal } = {}) {
764
+ return withTemp(async (dir) => {
765
+ throwIfAborted(signal);
766
+ const inPath = await writeInput(dir, "in", url, fetchFn);
767
+ const outPath = join(dir, "out.wav");
768
+ const s = Math.max(0, Number(start) || 0);
769
+
770
+ let dur = null;
771
+ try {
772
+ const pr = await runProc("ffprobe", [
773
+ "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", inPath,
774
+ ], { signal });
775
+ dur = parseFloat(String(pr.stdout).trim());
776
+ } catch (e) {
777
+ if (e instanceof NanoodleError && (e.code === "aborted" || e.code === "timeout" || /timed out|aborted/i.test(e.message || ""))) throw e;
778
+ if (signal && signal.aborted) throw abortError(signal);
779
+ /* some containers lack duration; ffmpeg -t still works */
780
+ }
781
+
782
+ if (dur != null && isFinite(dur) && s >= dur) {
783
+ throw new NanoodleError(
784
+ `the start point (${Math.round(s * 10) / 10}s) is past the end of this clip, which is only ${dur.toFixed(1)}s long — pick an earlier start`);
785
+ }
786
+
787
+ let take;
788
+ if (wholeIfBlank && !(len > 0)) {
789
+ take = dur != null && isFinite(dur) ? Math.max(0.05, dur - s) : null;
790
+ } else {
791
+ const L = Number.isFinite(Number(len)) && Number(len) > 0 ? Number(len) : 30;
792
+ take = dur != null && isFinite(dur) ? Math.max(0.05, Math.min(L, dur - s)) : L;
793
+ }
794
+
795
+ const args = ["-y", "-ss", String(s), "-i", inPath];
796
+ if (take != null) args.push("-t", String(take));
797
+ args.push("-vn", "-ac", "1", "-ar", String(rate || 16000), "-f", "wav", outPath);
798
+ try {
799
+ await runProc("ffmpeg", args, { signal });
800
+ } catch (e) {
801
+ if (e instanceof NanoodleError && (e.code === "aborted" || e.code === "timeout")) throw e;
802
+ const msg = e.message || "";
803
+ if (/does not contain any stream|Output file does not contain|no audio/i.test(msg)
804
+ || /Stream map|matches no streams/i.test(msg)) {
805
+ throw new NanoodleError("this video is silent — generated videos usually have no audio track to extract");
806
+ }
807
+ if (/Invalid data|could not find codec/i.test(msg)) {
808
+ throw new NanoodleError("couldn't decode that audio for trimming (unsupported format?)");
809
+ }
810
+ throw e;
811
+ }
812
+ return dataUrlFromFile(outPath, "audio/wav");
813
+ });
814
+ }
815
+
816
+ export async function extractAudioToWav(url, start, len, rate = 16000, opts = {}) {
817
+ return trimAudioToWav(url, start, len, rate, { ...opts, wholeIfBlank: true });
818
+ }
819
+
820
+ /* ---------- vframes (ffmpeg — needs a video decoder) ----------------------- */
821
+
822
+ export async function extractVideoFrames(url, { count = 1, gap = 0.5, dir = "end", fetch: fetchFn, onProgress, signal } = {}) {
823
+ throwIfAborted(signal);
824
+ const n = Math.max(1, Math.min(MAX_FRAMES, parseInt(count, 10) || 1));
825
+ const stepSec = Number.isFinite(Number(gap)) ? Math.max(0, Number(gap)) : 0.5;
826
+ const fromEnd = (dir || "end") === "end";
827
+ const EPS = 0.04;
828
+
829
+ return withTemp(async (dir) => {
830
+ const inPath = await writeInput(dir, "in", url, fetchFn);
831
+ throwIfAborted(signal);
832
+ const pr = await runProc("ffprobe", [
833
+ "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", inPath,
834
+ ], { signal });
835
+ const dur = parseFloat(String(pr.stdout).trim());
836
+ if (!isFinite(dur) || dur <= 0) throw new NanoodleError("video has no readable duration");
837
+
838
+ const out = {};
839
+ for (let i = 0; i < n; i++) {
840
+ throwIfAborted(signal);
841
+ if (onProgress) onProgress(`extracting frame ${i + 1}/${n}…`);
842
+ let t = fromEnd ? (dur - EPS - i * stepSec) : (i * stepSec);
843
+ t = Math.max(0, Math.min(Math.max(0, dur - EPS), t));
844
+ const framePath = join(dir, `f${i + 1}.jpg`);
845
+ await runProc("ffmpeg", [
846
+ "-y", "-ss", String(t), "-i", inPath, "-frames:v", "1", "-q:v", "2", framePath,
847
+ ], { signal });
848
+ out["frame" + (i + 1)] = await dataUrlFromFile(framePath, "image/jpeg");
849
+ }
850
+ return out;
851
+ });
852
+ }
853
+
854
+ /* ---------- combine videos (MP4CAT pure → ffmpeg) -------------------------- */
855
+
856
+ /**
857
+ * Concatenate clips in order — same dispatcher shape as play.html concatVideos:
858
+ * 1. When every clip is mp4 with matching codec params → MP4CAT lossless remux
859
+ * (dedup is ignored on remux, as in the browser — dropping the first sample
860
+ * would kill the keyframe).
861
+ * 2. Else re-encode via ffmpeg (browser falls back to MediaRecorder).
862
+ */
863
+ export async function concatVideos(urls, dedup = true, { fetch: fetchFn, onProgress, signal } = {}) {
864
+ if (!urls || urls.length < 2) throw new NanoodleError("wire at least two clips to combine");
865
+ throwIfAborted(signal);
866
+
867
+ // Pure path: load all bytes, try MP4CAT (exact browser primary path)
868
+ try {
869
+ const bufs = [];
870
+ for (let i = 0; i < urls.length; i++) {
871
+ throwIfAborted(signal);
872
+ if (onProgress) onProgress(`loading clip ${i + 1}/${urls.length}…`);
873
+ bufs.push(await urlBytes(urls[i], fetchFn));
874
+ }
875
+ if (bufs.every((b) => MP4CAT.isMp4(b)) && MP4CAT.mp4ParamsMatch(bufs)) {
876
+ if (onProgress) onProgress("combining…");
877
+ // Browser remux never applies dedup (would drop a keyframe).
878
+ const out = MP4CAT.concatMp4(bufs, { dedup: false });
879
+ return dataUrlFromBytes(out, "video/mp4");
880
+ }
881
+ } catch (e) {
882
+ if (e && (e.code === "aborted" || e.code === "timeout")) throw e;
883
+ if (e instanceof NanoodleError) {
884
+ if (/wire at least two|no media|download media|aborted|timed out/i.test(e.message || "")) throw e;
885
+ throw e; // other deliberate errors (not remux glitches)
886
+ }
887
+ // MP4CAT throws plain Error on parse issues → fall through to ffmpeg
888
+ }
889
+
890
+ return concatVideosFfmpeg(urls, dedup, { fetch: fetchFn, onProgress, signal });
891
+ }
892
+
893
+ async function concatVideosFfmpeg(urls, dedup = true, { fetch: fetchFn, onProgress, signal } = {}) {
894
+ return withTemp(async (dir) => {
895
+ const paths = [];
896
+ for (let i = 0; i < urls.length; i++) {
897
+ throwIfAborted(signal);
898
+ if (onProgress) onProgress(`loading clip ${i + 1}/${urls.length}…`);
899
+ paths.push(await writeInput(dir, `c${i}`, urls[i], fetchFn));
900
+ }
901
+
902
+ // When dedup: trim ~1/30s from the start of clips 2..N (approximate seam-frame drop —
903
+ // MediaRecorder path equivalent; pure remux path above never drops frames).
904
+ const prepared = [];
905
+ for (let i = 0; i < paths.length; i++) {
906
+ throwIfAborted(signal);
907
+ if (dedup && i > 0) {
908
+ const trimmed = join(dir, `t${i}.mp4`);
909
+ await runProc("ffmpeg", [
910
+ "-y", "-ss", "0.033", "-i", paths[i],
911
+ "-c:v", "libx264", "-preset", "ultrafast", "-crf", "18",
912
+ "-c:a", "aac", "-b:a", "128k", "-movflags", "+faststart", trimmed,
913
+ ], { signal });
914
+ prepared.push(trimmed);
915
+ } else {
916
+ prepared.push(paths[i]);
917
+ }
918
+ }
919
+
920
+ const listPath = join(dir, "list.txt");
921
+ const listBody = prepared.map((p) => `file '${p.replace(/'/g, "'\\''")}'`).join("\n") + "\n";
922
+ await writeFile(listPath, listBody);
923
+
924
+ const outPath = join(dir, "out.mp4");
925
+ if (onProgress) onProgress("combining…");
926
+ try {
927
+ await runProc("ffmpeg", [
928
+ "-y", "-f", "concat", "-safe", "0", "-i", listPath, "-c", "copy", "-movflags", "+faststart", outPath,
929
+ ], { signal });
930
+ } catch (e) {
931
+ if (e instanceof NanoodleError && (e.code === "aborted" || e.code === "timeout" || /aborted|timed out/i.test(e.message || ""))) throw e;
932
+ await runProc("ffmpeg", [
933
+ "-y", "-f", "concat", "-safe", "0", "-i", listPath,
934
+ "-c:v", "libx264", "-preset", "ultrafast", "-crf", "18",
935
+ "-c:a", "aac", "-b:a", "128k", "-movflags", "+faststart", outPath,
936
+ ], { signal });
937
+ }
938
+ return dataUrlFromFile(outPath, "video/mp4");
939
+ });
940
+ }
941
+
942
+ /* ---------- soundtrack mux (ffmpeg — no pure AAC encoder yet) -------------- */
943
+
944
+ /**
945
+ * Replace video audio with the given track. loop=true loops audio to fill video length.
946
+ * Browser re-records via MediaRecorder; headless uses ffmpeg. (Pure mp4 audio-track
947
+ * replace is a future pure-path candidate once we can encode AAC without ffmpeg.)
948
+ */
949
+ export async function muxSoundtrack(videoUrl, audioUrl, loop = false, { fetch: fetchFn, onProgress, signal } = {}) {
950
+ return withTemp(async (dir) => {
951
+ throwIfAborted(signal);
952
+ if (onProgress) onProgress("adding soundtrack…");
953
+ const vPath = await writeInput(dir, "v", videoUrl, fetchFn);
954
+ const aPath = await writeInput(dir, "a", audioUrl, fetchFn);
955
+ const outPath = join(dir, "out.mp4");
956
+
957
+ let vdur = null;
958
+ try {
959
+ const pr = await runProc("ffprobe", [
960
+ "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", vPath,
961
+ ], { signal });
962
+ vdur = parseFloat(String(pr.stdout).trim());
963
+ } catch (e) {
964
+ if (e instanceof NanoodleError && (e.code === "aborted" || e.code === "timeout" || /timed out|aborted/i.test(e.message || ""))) throw e;
965
+ if (signal && signal.aborted) throw abortError(signal);
966
+ /* optional */
967
+ }
968
+
969
+ const args = ["-y", "-i", vPath];
970
+ if (loop) args.push("-stream_loop", "-1");
971
+ args.push("-i", aPath, "-map", "0:v:0", "-map", "1:a:0", "-c:v", "copy", "-c:a", "aac", "-b:a", "128k");
972
+ if (loop && vdur != null && isFinite(vdur)) args.push("-t", String(vdur));
973
+ else args.push("-shortest");
974
+ args.push("-movflags", "+faststart", outPath);
975
+
976
+ try {
977
+ await runProc("ffmpeg", args, { signal });
978
+ } catch (e) {
979
+ if (isMissingFfmpeg(e)) throw e;
980
+ if (e instanceof NanoodleError && (e.code === "aborted" || e.code === "timeout" || /aborted|timed out/i.test(e.message || ""))) throw e;
981
+ const args2 = ["-y", "-i", vPath];
982
+ if (loop) args2.push("-stream_loop", "-1");
983
+ args2.push("-i", aPath, "-map", "0:v:0", "-map", "1:a:0",
984
+ "-c:v", "libx264", "-preset", "ultrafast", "-crf", "18",
985
+ "-c:a", "aac", "-b:a", "128k");
986
+ if (loop && vdur != null && isFinite(vdur)) args2.push("-t", String(vdur));
987
+ else args2.push("-shortest");
988
+ args2.push("-movflags", "+faststart", outPath);
989
+ await runProc("ffmpeg", args2, { signal });
990
+ }
991
+ return dataUrlFromFile(outPath, "video/mp4");
992
+ });
993
+ }
994
+
995
+ export { MAX_FRAMES, MAX_IMAGE_DIM, MP4CAT };
996
+ // PNG codec internals — used by the nanoodle repo's dual-engine parity harness
997
+ // (pixel-level mask comparison + headless canvas shim). Not part of the stable API.
998
+ export { decodePng, encodePngRgba };