@margoengine/headless 0.0.0-stage → 1.0.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.
Files changed (45) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1231 -2
  4. package/dist/accumulate.d.ts +123 -0
  5. package/dist/accumulate.js +198 -0
  6. package/dist/avc-range.d.ts +15 -0
  7. package/dist/compare.d.ts +97 -0
  8. package/dist/compare.js +320 -0
  9. package/dist/crc32-Cc3b8qnn.js +23 -0
  10. package/dist/errors-CDgOgzCd.js +7 -0
  11. package/dist/ffmpeg-colour-B_r-2O-e.js +15 -0
  12. package/dist/ffmpeg-colour.d.ts +6 -0
  13. package/dist/ffmpeg-files.d.ts +129 -0
  14. package/dist/ffmpeg-files.js +406 -0
  15. package/dist/ffmpeg.d.ts +164 -0
  16. package/dist/ffmpeg.js +402 -0
  17. package/dist/formats.d.ts +46 -0
  18. package/dist/formats.js +685 -0
  19. package/dist/frames.d.ts +33 -0
  20. package/dist/frames.js +70 -0
  21. package/dist/gif.d.ts +20 -0
  22. package/dist/gif.js +7 -0
  23. package/dist/index.d.ts +66 -0
  24. package/dist/index.js +1 -0
  25. package/dist/look-base.d.ts +87 -0
  26. package/dist/look-motion.d.ts +175 -0
  27. package/dist/look.d.ts +775 -0
  28. package/dist/look.js +1963 -0
  29. package/dist/null.d.ts +39 -0
  30. package/dist/null.js +61 -0
  31. package/dist/readback-HJLttuJt.js +122 -0
  32. package/dist/readback.d.ts +55 -0
  33. package/dist/src-VufTH2fo.js +154 -0
  34. package/dist/stage.d.ts +52 -0
  35. package/dist/stage.js +106 -0
  36. package/dist/tiles.d.ts +38 -0
  37. package/dist/tiles.js +79 -0
  38. package/dist/video-mux.d.ts +77 -0
  39. package/dist/video.d.ts +176 -0
  40. package/dist/video.js +1114 -0
  41. package/dist/yuv420-pool.d.ts +26 -0
  42. package/dist/yuv420-pool.js +73 -0
  43. package/dist/yuv420.d.ts +33 -0
  44. package/dist/yuv420.js +186 -0
  45. package/package.json +128 -4
@@ -0,0 +1,406 @@
1
+ import { n as e, r as t, t as n } from "./errors-CDgOgzCd.js";
2
+ import { r, t as i } from "./ffmpeg-colour-B_r-2O-e.js";
3
+ import { spawn as a } from "node:child_process";
4
+ import { open as o, rename as s, rm as c, writeFile as l } from "node:fs/promises";
5
+ import { basename as u, dirname as d, join as f, resolve as p } from "node:path";
6
+ //#region src/ffmpeg-files.ts
7
+ var m = "h264_metadata=colour_primaries=1:transfer_characteristics=13:matrix_coefficients=1:video_full_range_flag=0:chroma_sample_loc_type=0", h = {
8
+ x264: {
9
+ codec: "libx264",
10
+ format: "yuv420p",
11
+ presets: [
12
+ "ultrafast",
13
+ "superfast",
14
+ "veryfast",
15
+ "faster",
16
+ "fast",
17
+ "medium",
18
+ "slow",
19
+ "slower",
20
+ "veryslow",
21
+ "placebo"
22
+ ],
23
+ args: (e, t) => [
24
+ ...t ? ["-preset", t] : [],
25
+ "-crf",
26
+ String(e)
27
+ ]
28
+ },
29
+ nvenc: {
30
+ codec: "h264_nvenc",
31
+ format: "yuv420p",
32
+ presets: [
33
+ "p1",
34
+ "p2",
35
+ "p3",
36
+ "p4",
37
+ "p5",
38
+ "p6",
39
+ "p7"
40
+ ],
41
+ args: (e, t = "p5") => [
42
+ "-preset",
43
+ t,
44
+ "-tune",
45
+ "hq",
46
+ "-rc",
47
+ "vbr",
48
+ "-cq",
49
+ String(e),
50
+ "-b:v",
51
+ "0"
52
+ ]
53
+ },
54
+ amf: {
55
+ codec: "h264_amf",
56
+ format: "nv12",
57
+ args: (e) => [
58
+ "-quality",
59
+ "quality",
60
+ "-rc",
61
+ "cqp",
62
+ "-qp_i",
63
+ String(e),
64
+ "-qp_p",
65
+ String(e),
66
+ "-qp_b",
67
+ String(e)
68
+ ]
69
+ },
70
+ qsv: {
71
+ codec: "h264_qsv",
72
+ format: "nv12",
73
+ args: (e) => ["-global_quality", String(e)]
74
+ }
75
+ };
76
+ function g(t = "x264", { quality: n = 20, preset: a } = {}) {
77
+ let o = Object.hasOwn(h, t) ? h[t] : void 0;
78
+ if (!o) throw e(`Unknown encoder ${String(t)}; use 'x264', 'nvenc', 'amf' or 'qsv'`, "headless");
79
+ if (!Number.isInteger(n) || n < 1 || n > 51) throw e("Encoder quality must be a whole number from 1 to 51", "headless");
80
+ if (a !== void 0 && !o.presets?.includes(a)) throw e(o.presets ? `The ${t} preset is one of ${o.presets.join(", ")}` : `The ${t} encoder takes no preset`, "headless");
81
+ return [
82
+ "-vf",
83
+ r(o.format),
84
+ ...i,
85
+ "-c:v",
86
+ o.codec,
87
+ ...o.args(n, a),
88
+ "-bsf:v",
89
+ m,
90
+ "-movflags",
91
+ "+faststart"
92
+ ];
93
+ }
94
+ function _(e, { ffmpeg: t = "ffmpeg", signal: r } = {}) {
95
+ r?.throwIfAborted();
96
+ let [i, ...o] = typeof t == "string" ? [t] : t;
97
+ return new Promise((t, s) => {
98
+ let c = "", l = a(i, [...o, ...e], {
99
+ stdio: [
100
+ "ignore",
101
+ "ignore",
102
+ "pipe"
103
+ ],
104
+ windowsHide: !0
105
+ }), u = () => l.kill();
106
+ r?.addEventListener("abort", u, { once: !0 }), l.stderr.setEncoding("utf8").on("data", (e) => c = (c + e).slice(-4096)), l.once("error", (e) => {
107
+ r?.removeEventListener("abort", u), s(e.code === "ENOENT" ? Object.assign(n("unsupported", "ffmpeg was not found", "headless"), { cause: e }) : e);
108
+ }), l.once("close", (e) => {
109
+ r?.removeEventListener("abort", u), r?.aborted ? s(r.reason) : e === 0 ? t() : s(Object.assign(n("failed", "ffmpeg failed", "headless"), { cause: c }));
110
+ });
111
+ });
112
+ }
113
+ async function v(e, t) {
114
+ let n = f(d(e), `.${u(e)}.${process.pid}.part${/\.[^./\\]+$/.exec(e)?.[0] ?? ""}`);
115
+ try {
116
+ await t(n), await s(n, e);
117
+ } catch (e) {
118
+ throw await c(n, { force: !0 }), e;
119
+ }
120
+ }
121
+ var y = (e, n, r) => {
122
+ if (typeof e != "string" || !e) throw t(`${r} needs ${n}`, "headless");
123
+ return e;
124
+ };
125
+ function b(e, t, n) {
126
+ let r = [];
127
+ for (let i = t; i + 8 <= n;) {
128
+ let t = e.readUInt32BE(i), a = 8;
129
+ if (t === 1) {
130
+ if (i + 16 > n) break;
131
+ t = Number(e.readBigUInt64BE(i + 8)), a = 16;
132
+ } else t === 0 && (t = n - i);
133
+ if (t < a || i + t > n) break;
134
+ r.push({
135
+ type: e.toString("latin1", i + 4, i + 8),
136
+ at: i,
137
+ end: i + t,
138
+ body: i + a
139
+ }), i += t;
140
+ }
141
+ return r;
142
+ }
143
+ var x = (e, t, n, r = 0) => t && b(e, t.body + r, t.end).find((e) => e.type === n);
144
+ async function S(e, t) {
145
+ let n = [], r = Buffer.alloc(16);
146
+ for (let i = 0; i + 8 <= t;) {
147
+ await e.read(r, 0, 16, i);
148
+ let a = r.readUInt32BE(0), o = 8;
149
+ if (a === 1 ? (a = Number(r.readBigUInt64BE(8)), o = 16) : a === 0 && (a = t - i), a < o || i + a > t) break;
150
+ n.push({
151
+ type: r.toString("latin1", 4, 8),
152
+ at: i,
153
+ end: i + a,
154
+ body: i + o
155
+ }), i += a;
156
+ }
157
+ return n;
158
+ }
159
+ async function C(e) {
160
+ y(e, "a file", "readVideoTrack");
161
+ let t = await o(e, "r");
162
+ try {
163
+ let r = (await t.stat()).size, i = await S(t, r), a = async (e) => {
164
+ let n = Buffer.alloc(e.end - e.at);
165
+ return await t.read(n, 0, n.length, e.at), n;
166
+ }, o = i.find((e) => e.type === "moov");
167
+ if (!o) throw n("unsupported", `${u(e)} has no moov box (not an MP4 or MOV file)`, "headless");
168
+ let s = await a(o), c = {
169
+ type: "moov",
170
+ at: 0,
171
+ end: s.length,
172
+ body: o.body - o.at
173
+ }, l = x(s, c, "mvhd"), d = l ? s.readUInt32BE(l.body + (s[l.body] === 1 ? 20 : 12)) : 1;
174
+ for (let e of b(s, c.body, c.end).filter((e) => e.type === "trak")) {
175
+ let t = x(s, e, "mdia"), n = x(s, t, "hdlr");
176
+ if (!n || s.toString("latin1", n.body + 8, n.body + 12) !== "vide") continue;
177
+ let r = x(s, e, "tkhd"), o = r ? s.readUInt32BE(r.body + (s[r.body] === 1 ? 20 : 12)) : 0, l = x(s, t, "mdhd"), u = l ? s.readUInt32BE(l.body + (s[l.body] === 1 ? 20 : 12)) : 0, f = x(s, x(s, t, "minf"), "stbl"), p = x(s, f, "stsd");
178
+ if (!u || !p) continue;
179
+ let m = b(s, p.body + 8, p.end)[0], h = m && (m.type === "avc1" || m.type === "avc3") ? x(s, m, "avcC", 78) : void 0, g = [], _ = [], v = [], y = x(s, f, "stts"), S = x(s, f, "ctts"), C = x(s, f, "stss"), w = 0;
180
+ if (y) for (let e = 0, t = s.readUInt32BE(y.body + 4); e < t; e++) {
181
+ let t = s.readUInt32BE(y.body + 8 + e * 8), n = s.readUInt32BE(y.body + 12 + e * 8);
182
+ for (let e = 0; e < t; e++, w += n) g.push(w);
183
+ }
184
+ if (S) for (let e = 0, t = s.readUInt32BE(S.body + 4); e < t; e++) {
185
+ let t = s.readUInt32BE(S.body + 8 + e * 8), n = s.readInt32BE(S.body + 12 + e * 8);
186
+ for (let e = 0; e < t; e++) _.push(n);
187
+ }
188
+ let T = C ? /* @__PURE__ */ new Set() : void 0;
189
+ if (C) for (let e = 0, t = s.readUInt32BE(C.body + 4); e < t; e++) T.add(s.readUInt32BE(C.body + 8 + e * 4));
190
+ for (let e = 0; e < g.length; e++) v.push(!T || T.has(e + 1));
191
+ let E = b(s, x(s, c, "mvex")?.body ?? 0, x(s, c, "mvex")?.end ?? 0).find((e) => e.type === "trex" && s.readUInt32BE(e.body + 4) === o), D = E ? s.readUInt32BE(E.body + 12) : 0, O = E ? s.readUInt32BE(E.body + 20) : 0;
192
+ for (let e of i.filter((e) => e.type === "moof")) {
193
+ let t = await a(e);
194
+ for (let n of b(t, e.body - e.at, t.length).filter((e) => e.type === "traf")) {
195
+ let e = x(t, n, "tfhd");
196
+ if (!e || t.readUInt32BE(e.body + 4) !== o) continue;
197
+ let r = t.readUInt32BE(e.body) & 16777215, i = e.body + 8;
198
+ r & 1 && (i += 8), r & 2 && (i += 4);
199
+ let a = r & 8 ? t.readUInt32BE(i) : D;
200
+ r & 8 && (i += 4), r & 16 && (i += 4);
201
+ let s = r & 32 ? t.readUInt32BE(i) : O, c = x(t, n, "tfdt");
202
+ c && (w = t[c.body] === 1 ? Number(t.readBigUInt64BE(c.body + 4)) : t.readUInt32BE(c.body + 4));
203
+ for (let e of b(t, n.body, n.end).filter((e) => e.type === "trun")) {
204
+ let n = t[e.body], r = t.readUInt32BE(e.body) & 16777215, i = t.readUInt32BE(e.body + 4), o = e.body + 8;
205
+ r & 1 && (o += 4);
206
+ let c = r & 4 ? t.readUInt32BE(o) : void 0;
207
+ r & 4 && (o += 4);
208
+ for (let e = 0; e < i; e++) {
209
+ let i = r & 256 ? t.readUInt32BE(o) : a;
210
+ r & 256 && (o += 4), r & 512 && (o += 4);
211
+ let l = r & 1024 ? t.readUInt32BE(o) : e === 0 && c !== void 0 ? c : s;
212
+ r & 1024 && (o += 4);
213
+ let u = r & 2048 ? n ? t.readInt32BE(o) : t.readUInt32BE(o) : 0;
214
+ r & 2048 && (o += 4), g.push(w), _.push(u), v.push(!(l & 65536)), w += i;
215
+ }
216
+ }
217
+ }
218
+ }
219
+ let k = 0, A = 0, j = x(s, x(s, e, "edts"), "elst");
220
+ if (j) {
221
+ let e = s[j.body] === 1, t = e ? 20 : 12;
222
+ for (let n = 0, r = s.readUInt32BE(j.body + 4); n < r; n++) {
223
+ let r = j.body + 8 + n * t, i = e ? Number(s.readBigUInt64BE(r)) : s.readUInt32BE(r), a = e ? Number(s.readBigInt64BE(r + 8)) : s.readInt32BE(r + 4);
224
+ if (a === -1) k += i / d;
225
+ else {
226
+ A = a;
227
+ break;
228
+ }
229
+ }
230
+ }
231
+ return {
232
+ codec: m?.type ?? "unknown",
233
+ timescale: u,
234
+ ...h ? { avcConfig: new Uint8Array(s.subarray(h.body, h.end)) } : {},
235
+ samples: g.map((e, t) => ({
236
+ time: (e + (_[t] ?? 0) - A) / u + k,
237
+ key: v[t]
238
+ }))
239
+ };
240
+ }
241
+ throw n("unsupported", `${u(e)} has no video track`, "headless");
242
+ } finally {
243
+ await t.close();
244
+ }
245
+ }
246
+ function w(t) {
247
+ if (!Number.isInteger(t) || t < 1) throw e("mp4Timescale needs a whole frame rate", "headless");
248
+ let n = t;
249
+ for (; n < 1e4;) n *= 2;
250
+ return n;
251
+ }
252
+ var T = (t) => {
253
+ let { start: n, frames: r, fps: i } = t;
254
+ if (!(n >= 0) || !Number.isFinite(n)) throw e("The span start must be 0 or more seconds", "headless");
255
+ if (!Number.isInteger(r) || r < 1) throw e("The span needs a whole number of frames, 1 or more", "headless");
256
+ if (!(i > 0) || !Number.isFinite(i)) throw e("Invalid span fps", "headless");
257
+ let a = t.timescale ?? w(i);
258
+ if (!Number.isInteger(a) || a < 1) throw e("The span timescale must be a whole number above 0", "headless");
259
+ return {
260
+ start: n,
261
+ frames: r,
262
+ fps: i,
263
+ timescale: a
264
+ };
265
+ };
266
+ async function E(e, t) {
267
+ let { ok: n, reason: r } = await D(e, T(t));
268
+ return n ? { ok: n } : {
269
+ ok: n,
270
+ reason: r
271
+ };
272
+ }
273
+ async function D(e, { start: t, frames: n, fps: r, timescale: i }) {
274
+ let a = await C(y(e, "a file", "checkSpan")), o = (e) => ({
275
+ ok: !1,
276
+ reason: e
277
+ }), s = .5 / i;
278
+ if (a.codec !== "avc1" && a.codec !== "avc3") return o(`it is ${a.codec}, not H.264`);
279
+ let c = a.samples.findIndex((e) => e.key && Math.abs(e.time - t) < s);
280
+ if (c < 0) return o(`${t} s is not on a key frame`);
281
+ let l = a.samples.slice(c, c + n);
282
+ if (l.length < n) return o("the file ends before the span does");
283
+ let u = /* @__PURE__ */ new Set();
284
+ for (let e of l) {
285
+ let a = Math.round((e.time - t) * r);
286
+ if (a < 0) return o(`a frame before ${t} s is decoded after its key frame (an open GOP)`);
287
+ if (a >= n || u.has(a)) return o(`its first ${n} frames from ${t} s are not the ${n} frames of the span: the next key frame is not at its end`);
288
+ if (Math.abs(e.time - t - a / r) >= s) return o(`a frame time is off the 1/${r} s steps by more than half a tick of ${i}`);
289
+ u.add(a);
290
+ }
291
+ return {
292
+ ok: !0,
293
+ key: l[0].time
294
+ };
295
+ }
296
+ async function O(e, t, r) {
297
+ y(t, "an output file", "copySpan");
298
+ let i = T(r), a = await D(e, i);
299
+ if (!a.ok) throw n("unsupported", `${u(e)} cannot be stream copied from ${i.start} s: ${a.reason}`, "headless");
300
+ let o = (Math.ceil(a.key * 1e6) / 1e6).toFixed(6);
301
+ await v(t, (t) => _([
302
+ "-y",
303
+ "-v",
304
+ "error",
305
+ "-nostdin",
306
+ "-ss",
307
+ o,
308
+ "-i",
309
+ e,
310
+ "-map",
311
+ "0:v:0",
312
+ "-an",
313
+ "-sn",
314
+ "-dn",
315
+ "-frames:v",
316
+ String(i.frames),
317
+ "-c",
318
+ "copy",
319
+ "-bsf:v",
320
+ m,
321
+ "-video_track_timescale",
322
+ String(i.timescale),
323
+ "-movflags",
324
+ "+faststart",
325
+ t
326
+ ], r));
327
+ }
328
+ async function k(n, r, i = {}) {
329
+ if (!Array.isArray(n) || !n.length) throw t("joinVideos needs one or more input files", "headless");
330
+ for (let e of n) y(e, "input files", "joinVideos");
331
+ y(r, "an output file", "joinVideos");
332
+ let a = i.sampleEntry ?? "auto";
333
+ if (![
334
+ "auto",
335
+ "avc1",
336
+ "avc3"
337
+ ].includes(a)) throw e("sampleEntry is 'auto', 'avc1' or 'avc3'", "headless");
338
+ i.signal?.throwIfAborted();
339
+ let o = await Promise.all(n.map((e) => C(e).then((e) => e.avcConfig, () => void 0))), s = o.every((e) => e !== void 0), m = s && o.some((e) => Buffer.compare(e, o[0]) !== 0), h = s ? a === "auto" ? m ? "avc3" : "avc1" : a : void 0, g = f(d(p(r)), `.${u(r)}.${process.pid}.ffconcat`);
340
+ await l(g, "ffconcat version 1.0\n" + n.map((e) => `file '${p(e).replace(/'/g, "'\\''")}'\n`).join(""));
341
+ try {
342
+ await v(r, (e) => _([
343
+ "-y",
344
+ "-v",
345
+ "error",
346
+ "-nostdin",
347
+ "-f",
348
+ "concat",
349
+ "-safe",
350
+ "0",
351
+ "-i",
352
+ g,
353
+ "-map",
354
+ "0",
355
+ "-c",
356
+ "copy",
357
+ ...h ? ["-tag:v", h] : [],
358
+ "-movflags",
359
+ "+faststart",
360
+ e
361
+ ], i));
362
+ } finally {
363
+ await c(g, { force: !0 });
364
+ }
365
+ return h;
366
+ }
367
+ async function A(t, n, r, i = {}) {
368
+ y(t, "a video file", "addSound"), y(n, "a sound file", "addSound"), y(r, "an output file", "addSound");
369
+ let a = /\.webm$/i.test(r), o = i.codec ?? (a ? "opus" : "aac"), s = i.bitrate ?? 192e3, c = i.offset ?? 0;
370
+ if (![
371
+ "aac",
372
+ "opus",
373
+ "copy"
374
+ ].includes(o)) throw e("addSound codec is 'aac', 'opus' or 'copy'", "headless");
375
+ if (!(s > 0) || !Number.isFinite(s)) throw e("Invalid addSound bitrate", "headless");
376
+ if (!(c >= 0) || !Number.isFinite(c)) throw e("addSound offset must be 0 or more seconds", "headless");
377
+ let l = o === "copy" ? ["-c:a", "copy"] : [
378
+ "-c:a",
379
+ o === "opus" ? "libopus" : "aac",
380
+ "-b:a",
381
+ String(Math.round(s))
382
+ ];
383
+ await v(r, (e) => _([
384
+ "-y",
385
+ "-v",
386
+ "error",
387
+ "-nostdin",
388
+ "-i",
389
+ t,
390
+ ...c ? ["-itsoffset", String(c)] : [],
391
+ "-i",
392
+ n,
393
+ "-map",
394
+ "0:v:0",
395
+ "-map",
396
+ "1:a:0",
397
+ "-c:v",
398
+ "copy",
399
+ ...l,
400
+ ...i.shortest ? ["-shortest"] : [],
401
+ ...a ? [] : ["-movflags", "+faststart"],
402
+ e
403
+ ], i));
404
+ }
405
+ //#endregion
406
+ export { m as H264_COLOUR_TAGS, A as addSound, E as checkSpan, O as copySpan, g as encoderArguments, k as joinVideos, w as mp4Timescale, C as readVideoTrack };
@@ -0,0 +1,164 @@
1
+ import type { HeadlessPixels } from './index.js';
2
+ /** Options of {@link encodeWithFFmpeg}. */
3
+ export interface FFmpegOptions {
4
+ /** File to write; an existing file is replaced. Required. */
5
+ output: string;
6
+ /** Frames per second. Default 30. */
7
+ fps?: number;
8
+ /** The program, or the program and its first arguments (`['nice', 'ffmpeg']`). Default `'ffmpeg'` on the `PATH`. */
9
+ ffmpeg?: string | readonly string[];
10
+ /** Output arguments replacing the default, which is chosen from the file name: VP9 at `-crf 32` for `.webm`, H.264 at
11
+ * `-crf 20` for anything else, both 4:2:0 BT.709. */
12
+ args?: readonly string[];
13
+ /** Sound for the file, piped to ffmpeg beside the frames: Opus for `.webm`, AAC for anything else. With `args`,
14
+ * the sound is input 1 and mapped; the codec is then yours to choose. Default none. */
15
+ audio?: FFmpegAudio;
16
+ /** Keep the frames' alpha: VP9 with an alpha plane (`yuva420p`), which ffmpeg writes beside each frame as WebM
17
+ * BlockAdditions. `.webm` files with the default arguments only. Default false. */
18
+ alpha?: boolean;
19
+ /** Video or picture files laid over the frames inside the same encode (picture in picture), in order, each on
20
+ * top of the ones before. The result goes through the same colour chain as a plain render. Default none. */
21
+ layers?: readonly FFmpegLayer[];
22
+ /** Stops ffmpeg; the promise rejects with the signal's reason, and a partial file may stay behind. Default none. */
23
+ signal?: AbortSignal;
24
+ /** Who makes the 4:2:0 picture. `'sharp'` (default): the engine (`@margoengine/headless/yuv420`), choosing each
25
+ * block's chroma and each pixel's luma so the frame a player decodes matches the render, and ffmpeg reads I420;
26
+ * slower (seconds a 1080p frame with much fine colour detail), for film delivery. `'scaler'`: ffmpeg's scaler
27
+ * filters the RGBA frames. `args`, `alpha` and `layers` always use the scaler (the layer graph works in RGB). */
28
+ chroma?: 'sharp' | 'scaler';
29
+ }
30
+ /** A file laid over the rendered frames by {@link encodeWithFFmpeg}. */
31
+ export interface FFmpegLayer {
32
+ /** A video or picture file ffmpeg reads. Its own sound is not used. */
33
+ input: string;
34
+ /** The box it fills, in pixels of the written frame. Whole numbers inside the frame. */
35
+ rect: {
36
+ x: number;
37
+ y: number;
38
+ width: number;
39
+ height: number;
40
+ };
41
+ /** Seconds into the video where it shows. Default 0. */
42
+ start?: number;
43
+ /** Seconds it shows; past the end of its file its last frame stays. Default: until its file ends (a still
44
+ * picture, one frame, needs a duration). */
45
+ duration?: number;
46
+ /** Seconds into its file where it starts. Default 0. */
47
+ in?: number;
48
+ /** Playback speed: 2 plays it twice as fast. Default 1. */
49
+ speed?: number;
50
+ /** How it fills the box: `'contain'` (all of it, the rest of the box `pad`), `'cover'` (fills the box, the edges
51
+ * cut) or `'fill'` (stretched). Default `'contain'`. */
52
+ fit?: 'contain' | 'cover' | 'fill';
53
+ /** The part of the file used, `[x, y, width, height]` in its own pixels, cut before the fit. Even whole numbers
54
+ * (4:2:0 chroma covers two by two pixels), inside the file. Default: all of it. */
55
+ crop?: readonly [number, number, number, number];
56
+ /** The colour a `'contain'` fit fills the rest of its box with: `'#rgb'`, `'#rrggbb'` or `'#rrggbbaa'`. Default
57
+ * clear, so the frames show through. */
58
+ pad?: string;
59
+ /** The matrix its YCbCr is read with, replacing the file's tag. Default: the file's tag; untagged, `'bt709'` at
60
+ * 720 lines or more and `'bt601'` below (ITU-R BT.709 is the HD matrix, BT.601 the SD one). RGB files have none. */
61
+ matrix?: FFmpegMatrix;
62
+ /** The range its YCbCr is read with, replacing the file's tag: `'tv'` (limited, 16 to 235) or `'pc'` (full).
63
+ * Default: the file's tag; untagged, `'tv'`. */
64
+ range?: 'tv' | 'pc';
65
+ }
66
+ /** A YCbCr matrix: ITU-R BT.709, BT.601 (H.273 codes 5 and 6) or BT.2020 non-constant luminance. */
67
+ export type FFmpegMatrix = 'bt709' | 'bt601' | 'bt2020';
68
+ /** What {@link probeVideo} reads of a file's first video stream. */
69
+ export interface VideoProbe {
70
+ /** ffmpeg's name of its codec, such as `'h264'`, `'vp9'`, `'png'` or `'mjpeg'`. */
71
+ codec: string;
72
+ /** ffmpeg's name of its pixel format, such as `'yuv420p'` or `'rgb24'`. */
73
+ pixelFormat: string;
74
+ width: number;
75
+ height: number;
76
+ /** True for RGB, palette and grey pixel formats, which have no YCbCr matrix. */
77
+ rgb: boolean;
78
+ /** The matrix the file is tagged with (ffmpeg's name: `'bt709'`, `'bt470bg'`, `'smpte170m'`, `'bt2020nc'` ...), or
79
+ * undefined when it carries none. */
80
+ matrix?: string;
81
+ /** The range the file is tagged with, or undefined when it carries none. */
82
+ range?: 'tv' | 'pc';
83
+ }
84
+ /** Sound for {@link encodeWithFFmpeg}. */
85
+ export interface FFmpegAudio {
86
+ /** Samples from -1 to 1, one array per channel, one or two channels of one length. */
87
+ channels: readonly Float32Array[];
88
+ /** Samples per second. */
89
+ sampleRate: number;
90
+ /** Bits per second. Default 128000. */
91
+ bitrate?: number;
92
+ }
93
+ /** Options of {@link layersOverPicture}. */
94
+ export interface LayersOverPictureOptions {
95
+ /** Frames per second of the video the picture is a frame of. Default 30. */
96
+ fps?: number;
97
+ /** The frame number the picture is: the layers are laid as frame `frame` of {@link encodeWithFFmpeg} lays them.
98
+ * Required. */
99
+ frame: number;
100
+ /** The layers, as {@link FFmpegOptions.layers}. */
101
+ layers: readonly FFmpegLayer[];
102
+ /** The program, or the program and its first arguments. Default `'ffmpeg'` on the `PATH`. */
103
+ ffmpeg?: string | readonly string[];
104
+ /** Stops ffmpeg; the promise rejects with the signal's reason. */
105
+ signal?: AbortSignal;
106
+ }
107
+ /** What {@link encodeWithFFmpeg} gives back. */
108
+ export interface FFmpegResult {
109
+ output: string;
110
+ width: number;
111
+ height: number;
112
+ frames: number;
113
+ /** Seconds of sound written, when there is sound. */
114
+ audioDuration?: number;
115
+ }
116
+ /**
117
+ * Reads the first video stream of `input` (a video or a picture) as ffmpeg reports it: codec, pixel format, size and
118
+ * the matrix and range it is tagged with. Rejects with code `'failed'` and ffmpeg's report in `cause` when ffmpeg finds
119
+ * no video stream in it.
120
+ */
121
+ export declare function probeVideo(input: string, { ffmpeg, signal }?: {
122
+ ffmpeg?: string | readonly string[];
123
+ signal?: AbortSignal;
124
+ }): Promise<VideoProbe>;
125
+ /**
126
+ * The layers with each file's matrix and range filled in where the file carries no tag of its own: BT.709 at 720
127
+ * lines or more and BT.601 below, TV range (ITU-T H.273 leaves untagged colour to the application; HD video, SMPTE
128
+ * ST 296 and ST 274, is ITU-R BT.709, SD video ITU-R BT.601). A JPEG (JFIF, ITU-T T.871) reads BT.601 at full range,
129
+ * and RGB files have no matrix. A layer's own `matrix` and `range` stay as given; a `crop` outside its file throws a
130
+ * RangeError. {@link encodeWithFFmpeg} and {@link layersOverPicture} call this; call it before
131
+ * {@link ffmpegArguments}, which reads no file.
132
+ */
133
+ export declare function resolveLayers(layers: readonly FFmpegLayer[], { ffmpeg, signal }?: {
134
+ ffmpeg?: string | readonly string[];
135
+ signal?: AbortSignal;
136
+ }): Promise<FFmpegLayer[]>;
137
+ /** The full ffmpeg argument list for a stream of RGBA frames on standard input. */
138
+ export declare function ffmpegArguments(width: number, height: number, { output, fps, args, audio, alpha, layers, chroma, }: Pick<FFmpegOptions, 'output' | 'fps' | 'args' | 'audio' | 'alpha' | 'layers' | 'chroma'>): string[];
139
+ /**
140
+ * The ffmpeg argument list of {@link layersOverPicture}: `picture` (`width` x `height`, one frame) as frame `frame` of
141
+ * a video at `fps`, with `layers` laid over it by the same graph {@link encodeWithFFmpeg} uses, written to `output` as
142
+ * one picture (PNG for a `.png` name). Reads no file: call {@link resolveLayers} first for the default colours.
143
+ */
144
+ export declare function layersPictureArguments(width: number, height: number, { picture, output, fps, frame, layers, }: {
145
+ picture: string;
146
+ output: string;
147
+ fps?: number;
148
+ frame: number;
149
+ layers: readonly FFmpegLayer[];
150
+ }): string[];
151
+ /**
152
+ * Lays `layers` over `picture`, a rendered frame `frame` of a video at `fps`, as {@link encodeWithFFmpeg} lays them on
153
+ * that frame, and writes the result to `output` (PNG for a `.png` name), so a still matches the encode. Layer files are
154
+ * read as {@link resolveLayers} reads them. A failure rejects with code `'failed'` and the end of ffmpeg's log in
155
+ * `cause`.
156
+ */
157
+ export declare function layersOverPicture(picture: string, output: string, options: LayersOverPictureOptions): Promise<void>;
158
+ /**
159
+ * Pipes frames to ffmpeg and waits for it to finish the file. Frames are all one size. A frame is written only once
160
+ * the pipe has room for it, so a slow encoder slows the render and memory stays flat. With the default H.264 arguments
161
+ * an odd width or height gains a repeated last column or row, since H.264 4:2:0 states sizes in steps of two (ITU-T
162
+ * H.264 7.4.2.1.1); the result gives the size written.
163
+ */
164
+ export declare function encodeWithFFmpeg(frames: AsyncIterable<HeadlessPixels> | Iterable<HeadlessPixels>, options: FFmpegOptions): Promise<FFmpegResult>;