@margoengine/headless 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +768 -3
- package/dist/accumulate.d.ts +123 -0
- package/dist/accumulate.js +198 -0
- package/dist/avc-range.d.ts +15 -0
- package/dist/compare.d.ts +97 -0
- package/dist/compare.js +320 -0
- package/dist/crc32-Cc3b8qnn.js +23 -0
- package/dist/errors-CDgOgzCd.js +7 -0
- package/dist/ffmpeg-colour-Cg00HQ4H.js +15 -0
- package/dist/ffmpeg-colour.d.ts +4 -0
- package/dist/ffmpeg-files.d.ts +129 -0
- package/dist/ffmpeg-files.js +406 -0
- package/dist/ffmpeg.d.ts +159 -0
- package/dist/ffmpeg.js +372 -0
- package/dist/formats.d.ts +46 -0
- package/dist/formats.js +685 -0
- package/dist/frames.d.ts +33 -0
- package/dist/frames.js +70 -0
- package/dist/gif.d.ts +20 -0
- package/dist/gif.js +7 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.js +1 -0
- package/dist/look.d.ts +553 -0
- package/dist/look.js +1277 -0
- package/dist/null.d.ts +39 -0
- package/dist/null.js +61 -0
- package/dist/readback-HJLttuJt.js +122 -0
- package/dist/readback.d.ts +55 -0
- package/dist/src-VufTH2fo.js +154 -0
- package/dist/stage.d.ts +52 -0
- package/dist/stage.js +106 -0
- package/dist/tiles.d.ts +38 -0
- package/dist/tiles.js +79 -0
- package/dist/video-mux.d.ts +77 -0
- package/dist/video.d.ts +176 -0
- package/dist/video.js +1114 -0
- package/package.json +122 -6
package/dist/ffmpeg.js
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { n as e, r as t, t as n } from "./errors-CDgOgzCd.js";
|
|
2
|
+
import { n as r, t as i } from "./ffmpeg-colour-Cg00HQ4H.js";
|
|
3
|
+
import { spawn as a } from "node:child_process";
|
|
4
|
+
import { hexColor as o } from "@margoengine/math/color";
|
|
5
|
+
//#region src/ffmpeg.ts
|
|
6
|
+
var s = (e) => [
|
|
7
|
+
"-vf",
|
|
8
|
+
r(e),
|
|
9
|
+
...i
|
|
10
|
+
], c = [
|
|
11
|
+
"-c:v",
|
|
12
|
+
"libvpx-vp9",
|
|
13
|
+
"-crf",
|
|
14
|
+
"32",
|
|
15
|
+
"-b:v",
|
|
16
|
+
"0"
|
|
17
|
+
], l = [
|
|
18
|
+
...s("yuv420p"),
|
|
19
|
+
"-c:v",
|
|
20
|
+
"libx264",
|
|
21
|
+
"-crf",
|
|
22
|
+
"20",
|
|
23
|
+
"-movflags",
|
|
24
|
+
"+faststart"
|
|
25
|
+
], u = 3, d = 16384;
|
|
26
|
+
function f({ channels: t, sampleRate: n, bitrate: r = 128e3 }) {
|
|
27
|
+
if (!Array.isArray(t) || t.length !== 1 && t.length !== 2) throw e("Audio takes one or two channels", "headless");
|
|
28
|
+
let i = t[0].length;
|
|
29
|
+
if (!i || t.some((e) => !(e instanceof Float32Array) || e.length !== i)) throw e("Audio channels must be Float32Arrays of one length, above 0", "headless");
|
|
30
|
+
if (!Number.isInteger(n) || n < 1) throw e("Invalid audio sampleRate", "headless");
|
|
31
|
+
if (!(r > 0) || !Number.isFinite(r)) throw e("Invalid audio bitrate", "headless");
|
|
32
|
+
return i;
|
|
33
|
+
}
|
|
34
|
+
function p(e, t, n) {
|
|
35
|
+
let r = e.length, i = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer((n - t) * r * 4));
|
|
36
|
+
for (let a = t, o = 0; a < n; a++) for (let t = 0; t < r; t++, o += 4) i.setFloat32(o, e[t][a], !0);
|
|
37
|
+
return new Uint8Array(i.buffer);
|
|
38
|
+
}
|
|
39
|
+
function m(e, t, r) {
|
|
40
|
+
if (r && (t || !/\.webm$/i.test(e))) throw n("unsupported", "Alpha video needs a .webm file and the default arguments", "headless");
|
|
41
|
+
}
|
|
42
|
+
var h = (e) => String(Math.round(e * 1e6) / 1e6), g = "flags=lanczos+accurate_rnd+full_chroma_int", _ = [
|
|
43
|
+
"bt709",
|
|
44
|
+
"bt601",
|
|
45
|
+
"bt2020"
|
|
46
|
+
];
|
|
47
|
+
function v(e) {
|
|
48
|
+
let t = o(e);
|
|
49
|
+
return t && `0x${t.map((e) => Math.round(e * 255).toString(16).padStart(2, "0")).join("")}`;
|
|
50
|
+
}
|
|
51
|
+
function y(n, r, i) {
|
|
52
|
+
if (i === void 0) return [];
|
|
53
|
+
if (!Array.isArray(i)) throw t("layers must be an array", "headless");
|
|
54
|
+
return i.map((i, a) => {
|
|
55
|
+
let o = `Layer ${a}`, { input: s, rect: c, start: l = 0, duration: u, in: d = 0, speed: f = 1, fit: p = "contain", crop: m, pad: h, matrix: g, range: y } = i ?? {};
|
|
56
|
+
if (typeof s != "string" || !s) throw t(`${o} needs an input file`, "headless");
|
|
57
|
+
let { x: b, y: x, width: S, height: C } = c ?? {};
|
|
58
|
+
if (![
|
|
59
|
+
b,
|
|
60
|
+
x,
|
|
61
|
+
S,
|
|
62
|
+
C
|
|
63
|
+
].every(Number.isInteger) || b < 0 || x < 0 || S < 1 || C < 1 || b + S > n || x + C > r) throw e(`${o} rect must be whole pixels inside the ${n}x${r} frame`, "headless");
|
|
64
|
+
if (!(l >= 0) || !Number.isFinite(l)) throw e(`${o} start must be 0 or more seconds`, "headless");
|
|
65
|
+
if (u !== void 0 && (!(u > 0) || !Number.isFinite(u))) throw e(`${o} duration must be above 0 seconds`, "headless");
|
|
66
|
+
if (!(d >= 0) || !Number.isFinite(d)) throw e(`${o} in must be 0 or more seconds`, "headless");
|
|
67
|
+
if (!(f > 0) || !Number.isFinite(f)) throw e(`${o} speed must be above 0`, "headless");
|
|
68
|
+
if (![
|
|
69
|
+
"contain",
|
|
70
|
+
"cover",
|
|
71
|
+
"fill"
|
|
72
|
+
].includes(p)) throw e(`${o} fit is 'contain', 'cover' or 'fill'`, "headless");
|
|
73
|
+
if (m !== void 0 && (!Array.isArray(m) || m.length !== 4 || !m.every((e) => Number.isInteger(e) && e >= 0 && e % 2 == 0) || m[2] < 2 || m[3] < 2)) throw e(`${o} crop is [x, y, width, height] in even whole pixels, width and height from 2`, "headless");
|
|
74
|
+
let w = h === void 0 ? "0x00000000" : typeof h == "string" ? v(h) : void 0;
|
|
75
|
+
if (!w) throw e(`${o} pad is a colour '#rgb', '#rrggbb' or '#rrggbbaa'`, "headless");
|
|
76
|
+
if (g !== void 0 && !_.includes(g)) throw e(`${o} matrix is 'bt709', 'bt601' or 'bt2020'`, "headless");
|
|
77
|
+
if (y !== void 0 && y !== "tv" && y !== "pc") throw e(`${o} range is 'tv' or 'pc'`, "headless");
|
|
78
|
+
return {
|
|
79
|
+
input: s,
|
|
80
|
+
x: b,
|
|
81
|
+
y: x,
|
|
82
|
+
w: S,
|
|
83
|
+
h: C,
|
|
84
|
+
start: l,
|
|
85
|
+
duration: u,
|
|
86
|
+
from: d,
|
|
87
|
+
speed: f,
|
|
88
|
+
fit: p,
|
|
89
|
+
crop: m,
|
|
90
|
+
padding: w,
|
|
91
|
+
matrix: g,
|
|
92
|
+
range: y
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
function b(e, t, n, r, i = "0:v") {
|
|
97
|
+
let a = [], o = [], s = .5 / n;
|
|
98
|
+
return e.forEach((e, r) => {
|
|
99
|
+
a.push(...e.from ? ["-ss", h(e.from)] : [], ...e.duration ? ["-t", h(e.duration * e.speed + 1)] : [], "-i", e.input);
|
|
100
|
+
let c = `${e.matrix ? `:in_color_matrix=${e.matrix}` : ""}${e.range ? `:in_range=${e.range}` : ""}`, l = (e, t) => `scale=${e}:${t}${c}:${g},format=rgba`, u = "iw*sar/ih", d = `gt(${u},${e.w}/${e.h})`, f = `max(1,round(${e.h}*${u}))`, p = `max(1,round(${e.w}/(${u})))`, m = e.fit === "cover" ? `${l(`'if(${d},${f},${e.w})'`, `'if(${d},${e.h},${p})'`)},crop=${e.w}:${e.h}` : e.fit === "fill" ? l(e.w, e.h) : `${l(`'if(${d},${e.w},${f})'`, `'if(${d},${p},${e.h})'`)},pad=${e.w}:${e.h}:(ow-iw)/2:(oh-ih)/2:color=${e.padding}`, _ = e.crop ? `crop=${e.crop[2]}:${e.crop[3]}:${e.crop[0]}:${e.crop[1]},` : "", v = e.duration ? e.start + e.duration : void 0, y = v === void 0 ? "" : `,tpad=stop_mode=clone:stop_duration=${h(e.duration)},trim=end=${h(v)}`, b = v === void 0 ? `gte(t,${h(e.start - s)})` : `between(t,${h(e.start - s)},${h(v - s)})`;
|
|
101
|
+
o.push(`[${t + r}:v]${_}setpts=(PTS-STARTPTS)/${h(e.speed)}+${h(e.start)}/TB,fps=${h(n)}:round=near${y},${m},setsar=1[l${r}]`), o.push(`[${i}][l${r}]overlay=x=${e.x}:y=${e.y}:format=rgb:eof_action=pass:enable='${b}'[b${r + 1}]`), i = `b${r + 1}`;
|
|
102
|
+
}), o.push(`[${i}]${r}[v]`), {
|
|
103
|
+
inputs: a,
|
|
104
|
+
graph: o.join(";")
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
var x = /^(?:rgb|bgr|gbr|argb|abgr|0rgb|0bgr|x2rgb|x2bgr|pal8|gray|ya8|ya16|monow|monob)/;
|
|
108
|
+
function S(e) {
|
|
109
|
+
let t = /^\s*Stream #\d+:\d+.*?: Video: (.*)$/m.exec(e)?.[1];
|
|
110
|
+
if (!t) return;
|
|
111
|
+
let n = [], r = 0, i = "";
|
|
112
|
+
for (let e of t) e === "," && r === 0 ? (n.push(i.trim()), i = "") : (e === "(" || e === "[" ? r++ : (e === ")" || e === "]") && r > 0 && r--, i += e);
|
|
113
|
+
n.push(i.trim());
|
|
114
|
+
let a = /^\S+/.exec(n[0])?.[0] ?? "", o = /^([a-z0-9_]+)(?:\((.*)\))?$/.exec(n[1] ?? ""), s = n.map((e) => /^(\d+)x(\d+)(?:\s|$)/.exec(e)).find(Boolean);
|
|
115
|
+
if (!o || !s) return;
|
|
116
|
+
let c = o[1], l = (o[2] ?? "").split(",").map((e) => e.trim()), u = l.find((e) => e === "tv" || e === "pc"), d = l.find((e) => e.includes("/") || /^(?:bt|smpte|fcc|ycgco|gbr|ictcp|chroma|reserved)/.test(e))?.split("/")[0];
|
|
117
|
+
return {
|
|
118
|
+
codec: a,
|
|
119
|
+
pixelFormat: c,
|
|
120
|
+
width: Number(s[1]),
|
|
121
|
+
height: Number(s[2]),
|
|
122
|
+
rgb: x.test(c) || d === "gbr",
|
|
123
|
+
...d && d !== "unknown" ? { matrix: d } : {},
|
|
124
|
+
...u ? { range: u } : {}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function C(e, t, r, i = !1) {
|
|
128
|
+
r?.throwIfAborted();
|
|
129
|
+
let [o, ...s] = typeof e == "string" ? [e] : e;
|
|
130
|
+
return new Promise((e, c) => {
|
|
131
|
+
let l = "", u = a(o, [...s, ...t], {
|
|
132
|
+
stdio: [
|
|
133
|
+
"ignore",
|
|
134
|
+
"ignore",
|
|
135
|
+
"pipe"
|
|
136
|
+
],
|
|
137
|
+
windowsHide: !0
|
|
138
|
+
}), d = () => u.kill();
|
|
139
|
+
r?.addEventListener("abort", d, { once: !0 }), u.stderr.setEncoding("utf8").on("data", (e) => l += e), u.once("error", (e) => {
|
|
140
|
+
r?.removeEventListener("abort", d), c(e.code === "ENOENT" ? Object.assign(n("unsupported", "ffmpeg was not found", "headless"), { cause: e }) : e);
|
|
141
|
+
}), u.once("close", (t) => {
|
|
142
|
+
r?.removeEventListener("abort", d), r?.aborted ? c(r.reason) : e(i && t === 0 ? void 0 : l);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
async function w(e, { ffmpeg: r = "ffmpeg", signal: i } = {}) {
|
|
147
|
+
if (typeof e != "string" || !e) throw t("probeVideo needs an input file", "headless");
|
|
148
|
+
let a = await C(r, [
|
|
149
|
+
"-hide_banner",
|
|
150
|
+
"-nostdin",
|
|
151
|
+
"-i",
|
|
152
|
+
e
|
|
153
|
+
], i), o = S(a);
|
|
154
|
+
if (!o) throw Object.assign(n("failed", `ffmpeg found no video stream in ${e}`, "headless"), { cause: a.slice(-4096) });
|
|
155
|
+
return o;
|
|
156
|
+
}
|
|
157
|
+
async function T(n, { ffmpeg: r = "ffmpeg", signal: i } = {}) {
|
|
158
|
+
if (!Array.isArray(n)) throw t("layers must be an array", "headless");
|
|
159
|
+
let a = /* @__PURE__ */ new Map();
|
|
160
|
+
return Promise.all(n.map(async (n, o) => {
|
|
161
|
+
if (typeof n?.input != "string" || !n.input) throw t(`Layer ${o} needs an input file`, "headless");
|
|
162
|
+
if (n.matrix && n.range && !n.crop) return { ...n };
|
|
163
|
+
a.has(n.input) || a.set(n.input, w(n.input, {
|
|
164
|
+
ffmpeg: r,
|
|
165
|
+
signal: i
|
|
166
|
+
}));
|
|
167
|
+
let s = await a.get(n.input), [c, l, u, d] = n.crop ?? [
|
|
168
|
+
0,
|
|
169
|
+
0,
|
|
170
|
+
s.width,
|
|
171
|
+
s.height
|
|
172
|
+
];
|
|
173
|
+
if (c + u > s.width || l + d > s.height) throw e(`Layer ${o} crop must lie inside its ${s.width}x${s.height} file`, "headless");
|
|
174
|
+
if (s.rgb) return { ...n };
|
|
175
|
+
let f = s.codec === "mjpeg" || s.pixelFormat.startsWith("yuvj"), p = f || s.height < 720 ? "bt601" : "bt709";
|
|
176
|
+
return {
|
|
177
|
+
...n,
|
|
178
|
+
...n.matrix || s.matrix ? {} : { matrix: p },
|
|
179
|
+
...n.range || s.range ? {} : { range: f ? "pc" : "tv" }
|
|
180
|
+
};
|
|
181
|
+
}));
|
|
182
|
+
}
|
|
183
|
+
function E(e, t, { output: n, fps: r = 30, args: i, audio: a, alpha: o, layers: d }) {
|
|
184
|
+
m(n, i, o);
|
|
185
|
+
let f = y(e, t, d), p = /\.webm$/i.test(n), h = a ? {
|
|
186
|
+
input: [
|
|
187
|
+
"-f",
|
|
188
|
+
"f32le",
|
|
189
|
+
"-ar",
|
|
190
|
+
String(a.sampleRate),
|
|
191
|
+
"-ac",
|
|
192
|
+
String(a.channels.length),
|
|
193
|
+
"-i",
|
|
194
|
+
`pipe:${u}`
|
|
195
|
+
],
|
|
196
|
+
map: [
|
|
197
|
+
"-map",
|
|
198
|
+
"0:v:0",
|
|
199
|
+
"-map",
|
|
200
|
+
"1:a:0"
|
|
201
|
+
],
|
|
202
|
+
codec: [
|
|
203
|
+
...p ? ["-c:a", "libopus"] : ["-c:a", "aac"],
|
|
204
|
+
"-b:a",
|
|
205
|
+
String(Math.round(a.bitrate ?? 128e3))
|
|
206
|
+
]
|
|
207
|
+
} : void 0, g = [
|
|
208
|
+
"-y",
|
|
209
|
+
"-f",
|
|
210
|
+
"rawvideo",
|
|
211
|
+
"-pix_fmt",
|
|
212
|
+
"rgba",
|
|
213
|
+
"-s",
|
|
214
|
+
`${e}x${t}`,
|
|
215
|
+
"-r",
|
|
216
|
+
String(r),
|
|
217
|
+
"-i",
|
|
218
|
+
"pipe:0"
|
|
219
|
+
], _ = i ?? [...p ? [...s(o ? "yuva420p" : "yuv420p"), ...c] : l, ...h?.codec ?? []];
|
|
220
|
+
if (!f.length) return [
|
|
221
|
+
...g,
|
|
222
|
+
...h ? [...h.input, ...h.map] : ["-an"],
|
|
223
|
+
..._,
|
|
224
|
+
n
|
|
225
|
+
];
|
|
226
|
+
let v = _.indexOf("-vf"), { inputs: x, graph: S } = b(f, h ? 2 : 1, r, v < 0 ? "null" : _[v + 1]);
|
|
227
|
+
return [
|
|
228
|
+
...g,
|
|
229
|
+
...h?.input ?? [],
|
|
230
|
+
...x,
|
|
231
|
+
"-filter_complex",
|
|
232
|
+
S,
|
|
233
|
+
"-map",
|
|
234
|
+
"[v]",
|
|
235
|
+
...h ? ["-map", "1:a:0"] : ["-an"],
|
|
236
|
+
...v < 0 ? _ : [..._.slice(0, v), ..._.slice(v + 2)],
|
|
237
|
+
n
|
|
238
|
+
];
|
|
239
|
+
}
|
|
240
|
+
function D(n, r, { picture: i, output: a, fps: o = 30, frame: s, layers: c }) {
|
|
241
|
+
if (typeof i != "string" || !i) throw t("layersOverPicture needs a picture file", "headless");
|
|
242
|
+
if (typeof a != "string" || !a) throw t("layersOverPicture needs an output file", "headless");
|
|
243
|
+
if (!(o > 0) || !Number.isFinite(o)) throw e("Invalid fps", "headless");
|
|
244
|
+
if (!Number.isInteger(s) || s < 0) throw e("frame must be a whole number from 0", "headless");
|
|
245
|
+
let { inputs: l, graph: u } = b(y(n, r, c), 1, o, "format=rgb24", "p");
|
|
246
|
+
return [
|
|
247
|
+
"-y",
|
|
248
|
+
"-framerate",
|
|
249
|
+
String(o),
|
|
250
|
+
"-i",
|
|
251
|
+
i,
|
|
252
|
+
...l,
|
|
253
|
+
"-filter_complex",
|
|
254
|
+
`[0:v]trim=end_frame=1,setpts=PTS-STARTPTS+${s},format=rgb24[p];${u}`,
|
|
255
|
+
"-map",
|
|
256
|
+
"[v]",
|
|
257
|
+
"-frames:v",
|
|
258
|
+
"1",
|
|
259
|
+
"-update",
|
|
260
|
+
"1",
|
|
261
|
+
a
|
|
262
|
+
];
|
|
263
|
+
}
|
|
264
|
+
async function O(e, t, r) {
|
|
265
|
+
let { fps: i = 30, frame: a, layers: o, ffmpeg: s = "ffmpeg", signal: c } = r ?? {}, l = await w(e, {
|
|
266
|
+
ffmpeg: s,
|
|
267
|
+
signal: c
|
|
268
|
+
}), u = await C(s, [
|
|
269
|
+
"-hide_banner",
|
|
270
|
+
"-nostdin",
|
|
271
|
+
"-v",
|
|
272
|
+
"error",
|
|
273
|
+
...D(l.width, l.height, {
|
|
274
|
+
picture: e,
|
|
275
|
+
output: t,
|
|
276
|
+
fps: i,
|
|
277
|
+
frame: a,
|
|
278
|
+
layers: await T(o, {
|
|
279
|
+
ffmpeg: s,
|
|
280
|
+
signal: c
|
|
281
|
+
})
|
|
282
|
+
})
|
|
283
|
+
], c, !0);
|
|
284
|
+
if (u !== void 0) throw Object.assign(n("failed", "ffmpeg failed", "headless"), { cause: u.slice(-4096) });
|
|
285
|
+
}
|
|
286
|
+
function k(e, t, n, r, i) {
|
|
287
|
+
let a = new Uint8Array(r * i * 4);
|
|
288
|
+
for (let o = 0; o < i; o++) {
|
|
289
|
+
let i = Math.min(o, n - 1) * t * 4, s = o * r * 4;
|
|
290
|
+
a.set(e.subarray(i, i + t * 4), s);
|
|
291
|
+
for (let e = t; e < r; e++) a.copyWithin(s + e * 4, s + (t - 1) * 4, s + t * 4);
|
|
292
|
+
}
|
|
293
|
+
return a;
|
|
294
|
+
}
|
|
295
|
+
async function A(r, i) {
|
|
296
|
+
let { output: o, fps: s = 30, ffmpeg: c = "ffmpeg", signal: l, audio: d } = i;
|
|
297
|
+
if (typeof o != "string" || !o) throw t("encodeWithFFmpeg needs an output file", "headless");
|
|
298
|
+
if (!(s > 0) || !Number.isFinite(s)) throw e("Invalid fps", "headless");
|
|
299
|
+
let p = d === void 0 ? 0 : f(d);
|
|
300
|
+
m(o, i.args, i.alpha), l?.throwIfAborted();
|
|
301
|
+
let h = i.layers === void 0 ? void 0 : await T(i.layers, {
|
|
302
|
+
ffmpeg: c,
|
|
303
|
+
signal: l
|
|
304
|
+
}), g = {
|
|
305
|
+
...i,
|
|
306
|
+
layers: h
|
|
307
|
+
}, [_, ...v] = typeof c == "string" ? [c] : c, y, b, x = !1, S = () => {}, C = 0, w = 0, D = 0, O = 0, A = 0, M = "", N, P = () => y?.kill();
|
|
308
|
+
l?.addEventListener("abort", P, { once: !0 });
|
|
309
|
+
try {
|
|
310
|
+
for await (let t of r) {
|
|
311
|
+
if (l?.throwIfAborted(), !y) {
|
|
312
|
+
({width: C, height: w} = t);
|
|
313
|
+
let e = !i.args && !/\.webm$/i.test(o);
|
|
314
|
+
[D, O] = e ? [C + (C & 1), w + (w & 1)] : [C, w];
|
|
315
|
+
let r = a(_, [...v, ...E(D, O, g)], {
|
|
316
|
+
stdio: d ? [
|
|
317
|
+
"pipe",
|
|
318
|
+
"ignore",
|
|
319
|
+
"pipe",
|
|
320
|
+
"pipe"
|
|
321
|
+
] : [
|
|
322
|
+
"pipe",
|
|
323
|
+
"ignore",
|
|
324
|
+
"pipe"
|
|
325
|
+
],
|
|
326
|
+
windowsHide: !0
|
|
327
|
+
});
|
|
328
|
+
y = r, r.stderr.setEncoding("utf8").on("data", (e) => {
|
|
329
|
+
M = (M + e).slice(-4096);
|
|
330
|
+
}), r.stdin.on("error", () => {}), b = new Promise((e, t) => {
|
|
331
|
+
r.once("error", (e) => {
|
|
332
|
+
x = !0, S(), t(e.code === "ENOENT" ? Object.assign(n("unsupported", "ffmpeg was not found", "headless"), { cause: e }) : e);
|
|
333
|
+
}), r.once("close", (r) => {
|
|
334
|
+
x = !0, S(), r === 0 ? e() : t(Object.assign(n("failed", "ffmpeg failed", "headless"), { cause: M }));
|
|
335
|
+
});
|
|
336
|
+
}), b.catch(() => {}), d && (N = j(r.stdio[u], d.channels, p, () => x));
|
|
337
|
+
} else if (t.width !== C || t.height !== w) throw e("Video frames must all be one size", "headless");
|
|
338
|
+
if (t.data.length !== C * w * 4) throw e("Frame data does not match its size", "headless");
|
|
339
|
+
if (x) break;
|
|
340
|
+
let r = D === C && O === w ? t.data : k(t.data, C, w, D, O);
|
|
341
|
+
if (!y.stdin.write(r)) {
|
|
342
|
+
let e = y.stdin, t = () => S();
|
|
343
|
+
e.once("drain", t), await new Promise((e) => S = e), e.off("drain", t);
|
|
344
|
+
}
|
|
345
|
+
A++;
|
|
346
|
+
}
|
|
347
|
+
if (!y) throw n("state", "No frames to encode", "headless");
|
|
348
|
+
return y.stdin.end(), await N, await b, {
|
|
349
|
+
output: o,
|
|
350
|
+
width: D,
|
|
351
|
+
height: O,
|
|
352
|
+
frames: A,
|
|
353
|
+
...d ? { audioDuration: p / d.sampleRate } : {}
|
|
354
|
+
};
|
|
355
|
+
} catch (e) {
|
|
356
|
+
throw l?.throwIfAborted(), e;
|
|
357
|
+
} finally {
|
|
358
|
+
l?.removeEventListener("abort", P), y && !x && y.kill();
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
async function j(e, t, n, r) {
|
|
362
|
+
e.on("error", () => {});
|
|
363
|
+
for (let i = 0; i < n && !r(); i += d) e.write(p(t, i, Math.min(n, i + d))) || await new Promise((t) => {
|
|
364
|
+
let n = () => {
|
|
365
|
+
e.off("drain", n), e.off("close", n), t();
|
|
366
|
+
};
|
|
367
|
+
e.on("drain", n), e.on("close", n);
|
|
368
|
+
});
|
|
369
|
+
e.end();
|
|
370
|
+
}
|
|
371
|
+
//#endregion
|
|
372
|
+
export { A as encodeWithFFmpeg, E as ffmpegArguments, O as layersOverPicture, D as layersPictureArguments, w as probeVideo, T as resolveLayers };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Engine } from '@margoengine/core';
|
|
2
|
+
import type { HeadlessPixels, HeadlessReadOptions } from './index.js';
|
|
3
|
+
/** Options of {@link encodeJPEG}. */
|
|
4
|
+
export interface JPEGOptions {
|
|
5
|
+
/** 1 to 100. Default 90. */
|
|
6
|
+
quality?: number;
|
|
7
|
+
/** Colour resolution: `'420'` halves it each way, `'444'` keeps it all, for sharp coloured edges. Default `'420'`. */
|
|
8
|
+
subsampling?: '420' | '444';
|
|
9
|
+
/** JPEG has no alpha, so transparent pixels are laid over this RGB colour, 0 to 255. Default white. */
|
|
10
|
+
background?: readonly [number, number, number];
|
|
11
|
+
}
|
|
12
|
+
/** RGBA pixels as a baseline JPEG file. */
|
|
13
|
+
export declare function encodeJPEG(pixels: HeadlessPixels, options?: JPEGOptions): Uint8Array;
|
|
14
|
+
/** Options of {@link encodeWebP}. */
|
|
15
|
+
export interface WebPOptions {
|
|
16
|
+
/** Unset: lossless, the same pixels back, anywhere. Set, 0 to 1: lossy, through the runtime's own encoder, which needs
|
|
17
|
+
* `OffscreenCanvas` and WebP support, so a browser or worker. Default unset. */
|
|
18
|
+
quality?: number;
|
|
19
|
+
}
|
|
20
|
+
/** RGBA pixels as a WebP file: lossless, or lossy where the runtime has an encoder. Images are at most 16,384 wide. */
|
|
21
|
+
export declare function encodeWebP(pixels: HeadlessPixels, { quality }?: WebPOptions): Promise<Uint8Array>;
|
|
22
|
+
/** Float RGBA pixels: linear light, straight or premultiplied as their source, top row first. */
|
|
23
|
+
export interface HDRPixels {
|
|
24
|
+
width: number;
|
|
25
|
+
height: number;
|
|
26
|
+
data: Float32Array;
|
|
27
|
+
}
|
|
28
|
+
/** Options of {@link encodeEXR}. */
|
|
29
|
+
export interface EXROptions {
|
|
30
|
+
/** Bits per sample in the file: `'half'` is what compositors expect, `'float'` keeps every bit. Default `'half'`. */
|
|
31
|
+
type?: 'half' | 'float';
|
|
32
|
+
/** Stops the encode; the promise rejects with the signal's reason. Default none. */
|
|
33
|
+
signal?: AbortSignal;
|
|
34
|
+
}
|
|
35
|
+
/** The sRGB transfer undone: 8-bit display pixels as linear light, for an EXR of a tone-mapped frame. Alpha stays linear. */
|
|
36
|
+
export declare function linearPixels({ width, height, data }: HeadlessPixels): HDRPixels;
|
|
37
|
+
/**
|
|
38
|
+
* Float RGBA pixels as an uncompressed OpenEXR file, written by `@margoengine/environment/exr-write`, an optional peer
|
|
39
|
+
* loaded on first use. Rejects with `Install @margoengine/environment` when it is missing.
|
|
40
|
+
*/
|
|
41
|
+
export declare function encodeEXR({ width, height, data }: HDRPixels, { type, signal }?: EXROptions): Promise<Uint8Array>;
|
|
42
|
+
/**
|
|
43
|
+
* Reads a float texture to {@link HDRPixels}: `rgba16float` or `rgba32float`, with `COPY_SRC` usage, such as
|
|
44
|
+
* `stack.sceneTexture` from `@margoengine/post/stack`, which holds the frame in linear light before tone mapping.
|
|
45
|
+
*/
|
|
46
|
+
export declare function readTexture(engine: Engine, texture: GPUTexture, { signal }?: HeadlessReadOptions): Promise<HDRPixels>;
|