@glissade/cli 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 +202 -0
- package/dist/audioMix.js +96 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +72 -0
- package/dist/encoders.js +66 -0
- package/dist/index.d.ts +102 -0
- package/dist/index.js +5 -0
- package/dist/render.js +159 -0
- package/dist/rolldown-runtime.js +13 -0
- package/dist/videoSource.js +127 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/dist/audioMix.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime.js";
|
|
2
|
+
import { dirname, isAbsolute, resolve } from "node:path";
|
|
3
|
+
import "@glissade/core";
|
|
4
|
+
//#region src/audioMix.ts
|
|
5
|
+
/**
|
|
6
|
+
* Audio mixing on the CLI path (DESIGN.md §5.3): the timeline's AudioClip
|
|
7
|
+
* metadata compiles to an FFmpeg filter graph (atrim/atempo/volume/adelay/
|
|
8
|
+
* amix) applied at mux time. Offsets are time arithmetic on clip metadata —
|
|
9
|
+
* never a clock; A/V reconcile only through fps and sampleRate.
|
|
10
|
+
*/
|
|
11
|
+
var audioMix_exports = /* @__PURE__ */ __exportAll({
|
|
12
|
+
AudioMixError: () => AudioMixError,
|
|
13
|
+
atempoChain: () => atempoChain,
|
|
14
|
+
gainExpression: () => gainExpression,
|
|
15
|
+
planAudioMix: () => planAudioMix,
|
|
16
|
+
resolveAssetPath: () => resolveAssetPath
|
|
17
|
+
});
|
|
18
|
+
var AudioMixError = class extends Error {
|
|
19
|
+
constructor(detail) {
|
|
20
|
+
super(`audio mix: ${detail}`);
|
|
21
|
+
this.name = "AudioMixError";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
/** Resolve a clip asset URL relative to the scene module's directory. */
|
|
25
|
+
function resolveAssetPath(url, modulePath) {
|
|
26
|
+
if (/^[a-z]+:\/\//i.test(url)) throw new AudioMixError(`remote audio assets are not supported on the CLI path yet: ${url}`);
|
|
27
|
+
return isAbsolute(url) ? url : resolve(dirname(resolve(modulePath)), url);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Piecewise-linear FFmpeg volume expression from gain-track keys. Eases are
|
|
31
|
+
* approximated linearly with a warning at the call site; keys outside the
|
|
32
|
+
* clip hold their boundary value.
|
|
33
|
+
*/
|
|
34
|
+
function gainExpression(keys) {
|
|
35
|
+
if (keys.length === 0) throw new AudioMixError("gain track has no keys");
|
|
36
|
+
if (keys.length === 1) return String(Number(keys[0].value));
|
|
37
|
+
let expr = String(Number(keys[keys.length - 1].value));
|
|
38
|
+
for (let i = keys.length - 1; i >= 1; i--) {
|
|
39
|
+
const a = keys[i - 1];
|
|
40
|
+
const b = keys[i];
|
|
41
|
+
const va = Number(a.value);
|
|
42
|
+
const seg = `${va}+(${Number(b.value) - va})*(t-${a.t})/(${b.t - a.t})`;
|
|
43
|
+
expr = `if(lt(t,${b.t}),${seg},${expr})`;
|
|
44
|
+
}
|
|
45
|
+
return `if(lt(t,${keys[0].t}),${Number(keys[0].value)},${expr})`;
|
|
46
|
+
}
|
|
47
|
+
/** atempo only accepts [0.5, 2]; chain filters for rates outside. */
|
|
48
|
+
function atempoChain(rate) {
|
|
49
|
+
if (!(rate > 0)) throw new AudioMixError(`playbackRate must be > 0, got ${rate}`);
|
|
50
|
+
const chain = [];
|
|
51
|
+
let r = rate;
|
|
52
|
+
while (r > 2) {
|
|
53
|
+
chain.push("atempo=2");
|
|
54
|
+
r /= 2;
|
|
55
|
+
}
|
|
56
|
+
while (r < .5) {
|
|
57
|
+
chain.push("atempo=0.5");
|
|
58
|
+
r *= 2;
|
|
59
|
+
}
|
|
60
|
+
if (Math.abs(r - 1) > 1e-9 || chain.length === 0) chain.push(`atempo=${r}`);
|
|
61
|
+
return chain;
|
|
62
|
+
}
|
|
63
|
+
/** Build the FFmpeg mix plan for clips that intersect [0, duration]. */
|
|
64
|
+
function planAudioMix(clips, modulePath, duration) {
|
|
65
|
+
const active = clips.filter((c) => c.at < duration);
|
|
66
|
+
if (active.length === 0) return null;
|
|
67
|
+
const inputs = [];
|
|
68
|
+
const chains = [];
|
|
69
|
+
let hasEasedGain = false;
|
|
70
|
+
active.forEach((clip, i) => {
|
|
71
|
+
inputs.push(resolveAssetPath(clip.asset.url, modulePath));
|
|
72
|
+
const steps = [];
|
|
73
|
+
if (clip.trim) steps.push(`atrim=start=${clip.trim.start}:end=${clip.trim.end}`);
|
|
74
|
+
steps.push("asetpts=PTS-STARTPTS");
|
|
75
|
+
if (clip.playbackRate !== void 0 && clip.playbackRate !== 1) steps.push(...atempoChain(clip.playbackRate));
|
|
76
|
+
if (clip.gain) {
|
|
77
|
+
if (clip.gain.keys.some((k) => k.ease !== void 0)) hasEasedGain = true;
|
|
78
|
+
steps.push(`volume='${gainExpression(clip.gain.keys)}':eval=frame`);
|
|
79
|
+
}
|
|
80
|
+
if (clip.at > 0) {
|
|
81
|
+
const ms = Math.round(clip.at * 1e3);
|
|
82
|
+
steps.push(`adelay=${ms}:all=1`);
|
|
83
|
+
}
|
|
84
|
+
steps.push("apad");
|
|
85
|
+
chains.push(`[${i + 1}:a]${steps.join(",")}[a${i}]`);
|
|
86
|
+
});
|
|
87
|
+
const mixInputs = active.map((_, i) => `[a${i}]`).join("");
|
|
88
|
+
const mix = active.length === 1 ? `${mixInputs}atrim=end=${duration}[aout]` : `${mixInputs}amix=inputs=${active.length}:normalize=0,atrim=end=${duration}[aout]`;
|
|
89
|
+
return {
|
|
90
|
+
inputs,
|
|
91
|
+
filterComplex: [...chains, mix].join(";"),
|
|
92
|
+
hasEasedGain
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { planAudioMix as a, gainExpression as i, atempoChain as n, resolveAssetPath as o, audioMix_exports as r, AudioMixError as t };
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { i as render } from "./render.js";
|
|
3
|
+
//#region src/cli.ts
|
|
4
|
+
/**
|
|
5
|
+
* gs — the glissade CLI (DESIGN.md §5.7).
|
|
6
|
+
* gs render <scene-module> [--out <dir|file.mp4|file.webm>] [--fps N] [--range a..b]
|
|
7
|
+
*/
|
|
8
|
+
function fail(msg) {
|
|
9
|
+
console.error(`gs: ${msg}`);
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
function parseArgs(argv) {
|
|
13
|
+
const positional = [];
|
|
14
|
+
const flags = /* @__PURE__ */ new Map();
|
|
15
|
+
for (let i = 0; i < argv.length; i++) {
|
|
16
|
+
const a = argv[i];
|
|
17
|
+
if (a.startsWith("--")) {
|
|
18
|
+
const eq = a.indexOf("=");
|
|
19
|
+
if (eq >= 0) flags.set(a.slice(2, eq), a.slice(eq + 1));
|
|
20
|
+
else flags.set(a.slice(2), argv[++i] ?? "");
|
|
21
|
+
} else positional.push(a);
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
positional,
|
|
25
|
+
flags
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const USAGE = `usage:
|
|
29
|
+
gs render <scene-module> [options]
|
|
30
|
+
|
|
31
|
+
options:
|
|
32
|
+
--out <path> output directory for a PNG sequence, or .mp4/.webm (needs ffmpeg). default: ./out
|
|
33
|
+
--fps <n> frames per second (default: timeline fps, else 60)
|
|
34
|
+
--range <a..b> seconds to render (default: 0..duration)
|
|
35
|
+
`;
|
|
36
|
+
async function main() {
|
|
37
|
+
const [command, ...rest] = process.argv.slice(2);
|
|
38
|
+
if (command !== "render") {
|
|
39
|
+
console.error(USAGE);
|
|
40
|
+
process.exit(command === void 0 || command === "help" || command === "--help" ? 0 : 1);
|
|
41
|
+
}
|
|
42
|
+
const { positional, flags } = parseArgs(rest);
|
|
43
|
+
const modulePath = positional[0];
|
|
44
|
+
if (!modulePath) fail(`missing <scene-module>\n${USAGE}`);
|
|
45
|
+
let range;
|
|
46
|
+
const rangeFlag = flags.get("range");
|
|
47
|
+
if (rangeFlag) {
|
|
48
|
+
const m = /^([\d.]+)\.\.([\d.]+)$/.exec(rangeFlag);
|
|
49
|
+
if (!m) fail(`--range must be 'a..b' in seconds, got '${rangeFlag}'`);
|
|
50
|
+
range = [parseFloat(m[1]), parseFloat(m[2])];
|
|
51
|
+
}
|
|
52
|
+
const fpsFlag = flags.get("fps");
|
|
53
|
+
const started = performance.now();
|
|
54
|
+
try {
|
|
55
|
+
const result = await render({
|
|
56
|
+
modulePath,
|
|
57
|
+
out: flags.get("out") ?? "out",
|
|
58
|
+
...fpsFlag ? { fps: parseInt(fpsFlag, 10) } : {},
|
|
59
|
+
...range ? { range } : {},
|
|
60
|
+
onProgress: (n, total) => {
|
|
61
|
+
if (n % 30 === 0 || n === total) process.stderr.write(`\rrendering ${n}/${total} frames`);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
const secs = ((performance.now() - started) / 1e3).toFixed(2);
|
|
65
|
+
process.stderr.write(`\rrendered ${result.frames} frames in ${secs}s → ${result.out}\n`);
|
|
66
|
+
} catch (err) {
|
|
67
|
+
fail(err instanceof Error ? err.message : String(err));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
main();
|
|
71
|
+
//#endregion
|
|
72
|
+
export {};
|
package/dist/encoders.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime.js";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
//#region src/encoders.ts
|
|
4
|
+
/**
|
|
5
|
+
* FFmpeg encoder detection (§5.2 feature-detection extended to the CLI path):
|
|
6
|
+
* distro "free" FFmpeg builds ship without libx264, so the encoder is picked
|
|
7
|
+
* from what the installed binary actually offers, in preference order, with
|
|
8
|
+
* an actionable error when a container has no usable encoder at all.
|
|
9
|
+
*/
|
|
10
|
+
var encoders_exports = /* @__PURE__ */ __exportAll({
|
|
11
|
+
NoEncoderError: () => NoEncoderError,
|
|
12
|
+
availableEncoders: () => availableEncoders,
|
|
13
|
+
parseEncoderList: () => parseEncoderList,
|
|
14
|
+
pickEncoder: () => pickEncoder
|
|
15
|
+
});
|
|
16
|
+
var NoEncoderError = class extends Error {
|
|
17
|
+
constructor(container, wanted) {
|
|
18
|
+
super(`your ffmpeg has none of the ${container} encoders ${wanted.join(", ")}. Install a full ffmpeg build (with libx264/libvpx), or render a different container (--out file.webm) or a PNG sequence (--out <directory>).`);
|
|
19
|
+
this.name = "NoEncoderError";
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const VIDEO_PREFERENCE = {
|
|
23
|
+
mp4: [
|
|
24
|
+
"libx264",
|
|
25
|
+
"libopenh264",
|
|
26
|
+
"mpeg4"
|
|
27
|
+
],
|
|
28
|
+
webm: ["libvpx-vp9", "libvpx"]
|
|
29
|
+
};
|
|
30
|
+
const AUDIO_PREFERENCE = {
|
|
31
|
+
mp4: ["aac"],
|
|
32
|
+
webm: [
|
|
33
|
+
"libopus",
|
|
34
|
+
"libvorbis",
|
|
35
|
+
"vorbis"
|
|
36
|
+
]
|
|
37
|
+
};
|
|
38
|
+
let cachedEncoders = null;
|
|
39
|
+
/** Parse `ffmpeg -encoders` output into the set of encoder names. */
|
|
40
|
+
function parseEncoderList(output) {
|
|
41
|
+
const names = /* @__PURE__ */ new Set();
|
|
42
|
+
for (const line of output.split("\n")) {
|
|
43
|
+
const m = /^ [VAS][\w.]{5} (\S+)/.exec(line);
|
|
44
|
+
if (m) names.add(m[1]);
|
|
45
|
+
}
|
|
46
|
+
return names;
|
|
47
|
+
}
|
|
48
|
+
function availableEncoders() {
|
|
49
|
+
if (cachedEncoders) return cachedEncoders;
|
|
50
|
+
const res = spawnSync("ffmpeg", ["-hide_banner", "-encoders"], { encoding: "utf8" });
|
|
51
|
+
cachedEncoders = res.status === 0 ? parseEncoderList(res.stdout) : /* @__PURE__ */ new Set();
|
|
52
|
+
return cachedEncoders;
|
|
53
|
+
}
|
|
54
|
+
function pickEncoder(kind, container, encoders = availableEncoders()) {
|
|
55
|
+
const preference = (kind === "video" ? VIDEO_PREFERENCE : AUDIO_PREFERENCE)[container];
|
|
56
|
+
for (const name of preference) if (encoders.has(name)) {
|
|
57
|
+
if (kind === "video" && name !== preference[0]) return {
|
|
58
|
+
name,
|
|
59
|
+
note: name === "mpeg4" ? `ffmpeg lacks ${preference.slice(0, -1).join("/")}; falling back to mpeg4 (larger files, older codec)` : `ffmpeg lacks ${preference[0]}; using ${name}`
|
|
60
|
+
};
|
|
61
|
+
return { name };
|
|
62
|
+
}
|
|
63
|
+
throw new NoEncoderError(container, preference);
|
|
64
|
+
}
|
|
65
|
+
//#endregion
|
|
66
|
+
export { pickEncoder as a, parseEncoderList as i, availableEncoders as n, encoders_exports as r, NoEncoderError as t };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { SceneModule, VideoFrameSource } from "@glissade/scene";
|
|
2
|
+
import { Image } from "@napi-rs/canvas";
|
|
3
|
+
import { AudioClip, Key } from "@glissade/core";
|
|
4
|
+
|
|
5
|
+
//#region src/render.d.ts
|
|
6
|
+
|
|
7
|
+
interface RenderOptions {
|
|
8
|
+
modulePath: string;
|
|
9
|
+
out: string;
|
|
10
|
+
fps?: number;
|
|
11
|
+
/** seconds; defaults to [0, duration] */
|
|
12
|
+
range?: [number, number];
|
|
13
|
+
onProgress?: (frame: number, total: number) => void;
|
|
14
|
+
}
|
|
15
|
+
declare class SceneModuleError extends Error {
|
|
16
|
+
constructor(modulePath: string, detail: string);
|
|
17
|
+
}
|
|
18
|
+
declare function loadSceneModule(modulePath: string): Promise<SceneModule>;
|
|
19
|
+
declare function ffmpegAvailable(): boolean;
|
|
20
|
+
declare function render(opts: RenderOptions): Promise<{
|
|
21
|
+
frames: number;
|
|
22
|
+
out: string;
|
|
23
|
+
}>;
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/videoSource.d.ts
|
|
26
|
+
declare class VideoProbeError extends Error {
|
|
27
|
+
constructor(path: string, detail: string);
|
|
28
|
+
}
|
|
29
|
+
interface VideoInfo {
|
|
30
|
+
fps: number;
|
|
31
|
+
duration: number;
|
|
32
|
+
width: number;
|
|
33
|
+
height: number;
|
|
34
|
+
}
|
|
35
|
+
declare function probeVideo(path: string): VideoInfo;
|
|
36
|
+
/**
|
|
37
|
+
* v1 strategy: warm() extracts the full requested media range once at the
|
|
38
|
+
* source's own fps; getFrameSync indexes the extracted sequence. Disk-bound,
|
|
39
|
+
* trivially correct; the streaming-pipe variant is a later optimization.
|
|
40
|
+
*/
|
|
41
|
+
declare class FfmpegVideoFrameSource implements VideoFrameSource {
|
|
42
|
+
readonly fps: number;
|
|
43
|
+
readonly duration: number;
|
|
44
|
+
private readonly path;
|
|
45
|
+
private framesDir;
|
|
46
|
+
private warmedFrom;
|
|
47
|
+
private warmedTo;
|
|
48
|
+
private readonly cache;
|
|
49
|
+
constructor(path: string, info?: VideoInfo);
|
|
50
|
+
private frameIndex;
|
|
51
|
+
warm(fromT: number, toT: number): Promise<void>;
|
|
52
|
+
getFrameSync(mediaT: number): Image;
|
|
53
|
+
close(): void;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/audioMix.d.ts
|
|
57
|
+
declare class AudioMixError extends Error {
|
|
58
|
+
constructor(detail: string);
|
|
59
|
+
}
|
|
60
|
+
/** Resolve a clip asset URL relative to the scene module's directory. */
|
|
61
|
+
declare function resolveAssetPath(url: string, modulePath: string): string;
|
|
62
|
+
/**
|
|
63
|
+
* Piecewise-linear FFmpeg volume expression from gain-track keys. Eases are
|
|
64
|
+
* approximated linearly with a warning at the call site; keys outside the
|
|
65
|
+
* clip hold their boundary value.
|
|
66
|
+
*/
|
|
67
|
+
declare function gainExpression(keys: Key[]): string;
|
|
68
|
+
/** atempo only accepts [0.5, 2]; chain filters for rates outside. */
|
|
69
|
+
declare function atempoChain(rate: number): string[];
|
|
70
|
+
interface AudioMixPlan {
|
|
71
|
+
/** extra `-i` inputs, in order, starting at input index 1 (0 = video) */
|
|
72
|
+
inputs: string[];
|
|
73
|
+
/** -filter_complex value producing [aout] */
|
|
74
|
+
filterComplex: string;
|
|
75
|
+
hasEasedGain: boolean;
|
|
76
|
+
}
|
|
77
|
+
/** Build the FFmpeg mix plan for clips that intersect [0, duration]. */
|
|
78
|
+
declare function planAudioMix(clips: AudioClip[], modulePath: string, duration: number): AudioMixPlan | null;
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/encoders.d.ts
|
|
81
|
+
/**
|
|
82
|
+
* FFmpeg encoder detection (§5.2 feature-detection extended to the CLI path):
|
|
83
|
+
* distro "free" FFmpeg builds ship without libx264, so the encoder is picked
|
|
84
|
+
* from what the installed binary actually offers, in preference order, with
|
|
85
|
+
* an actionable error when a container has no usable encoder at all.
|
|
86
|
+
*/
|
|
87
|
+
declare class NoEncoderError extends Error {
|
|
88
|
+
constructor(container: string, wanted: string[]);
|
|
89
|
+
}
|
|
90
|
+
/** Parse `ffmpeg -encoders` output into the set of encoder names. */
|
|
91
|
+
declare function parseEncoderList(output: string): Set<string>;
|
|
92
|
+
declare function availableEncoders(): Set<string>;
|
|
93
|
+
/** Test seam: override/clear the cached encoder set. */
|
|
94
|
+
|
|
95
|
+
interface EncoderChoice {
|
|
96
|
+
name: string;
|
|
97
|
+
/** Non-preferred pick worth telling the user about. */
|
|
98
|
+
note?: string;
|
|
99
|
+
}
|
|
100
|
+
declare function pickEncoder(kind: 'video' | 'audio', container: 'mp4' | 'webm', encoders?: Set<string>): EncoderChoice;
|
|
101
|
+
//#endregion
|
|
102
|
+
export { AudioMixError, type AudioMixPlan, type EncoderChoice, FfmpegVideoFrameSource, NoEncoderError, type RenderOptions, SceneModuleError, type VideoInfo, VideoProbeError, atempoChain, availableEncoders, ffmpegAvailable, gainExpression, loadSceneModule, parseEncoderList, pickEncoder, planAudioMix, probeVideo, render, resolveAssetPath };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { i as render, n as ffmpegAvailable, r as loadSceneModule, t as SceneModuleError } from "./render.js";
|
|
2
|
+
import { n as VideoProbeError, r as probeVideo, t as FfmpegVideoFrameSource } from "./videoSource.js";
|
|
3
|
+
import { a as planAudioMix, i as gainExpression, n as atempoChain, o as resolveAssetPath, t as AudioMixError } from "./audioMix.js";
|
|
4
|
+
import { a as pickEncoder, i as parseEncoderList, n as availableEncoders, t as NoEncoderError } from "./encoders.js";
|
|
5
|
+
export { AudioMixError, FfmpegVideoFrameSource, NoEncoderError, SceneModuleError, VideoProbeError, atempoChain, availableEncoders, ffmpegAvailable, gainExpression, loadSceneModule, parseEncoderList, pickEncoder, planAudioMix, probeVideo, render, resolveAssetPath };
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
5
|
+
import { pathToFileURL } from "node:url";
|
|
6
|
+
import { createJiti } from "jiti";
|
|
7
|
+
import { evaluate } from "@glissade/scene";
|
|
8
|
+
import { SkiaBackend } from "@glissade/backend-skia";
|
|
9
|
+
//#region src/render.ts
|
|
10
|
+
/**
|
|
11
|
+
* gs render (DESIGN.md §5.1d, §5.7): load a scene module, evaluate each frame,
|
|
12
|
+
* rasterize on Skia, write a PNG sequence — and mux to mp4/webm via FFmpeg
|
|
13
|
+
* when requested and available. No browser anywhere.
|
|
14
|
+
*/
|
|
15
|
+
var SceneModuleError = class extends Error {
|
|
16
|
+
constructor(modulePath, detail) {
|
|
17
|
+
super(`${modulePath}: ${detail}\nA scene module default-exports { createScene(): Scene, timeline: Timeline } (SceneModule).`);
|
|
18
|
+
this.name = "SceneModuleError";
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
async function loadSceneModule(modulePath) {
|
|
22
|
+
const abs = isAbsolute(modulePath) ? modulePath : resolve(process.cwd(), modulePath);
|
|
23
|
+
const loaded = await createJiti(pathToFileURL(process.cwd() + "/").href).import(pathToFileURL(abs).href, { default: true });
|
|
24
|
+
if (typeof loaded?.createScene !== "function" || loaded?.timeline === void 0) throw new SceneModuleError(modulePath, "default export is not a SceneModule");
|
|
25
|
+
return loaded;
|
|
26
|
+
}
|
|
27
|
+
function ffmpegAvailable() {
|
|
28
|
+
return spawnSync("ffmpeg", ["-version"], { stdio: "ignore" }).status === 0;
|
|
29
|
+
}
|
|
30
|
+
async function render(opts) {
|
|
31
|
+
const mod = await loadSceneModule(opts.modulePath);
|
|
32
|
+
const scene = mod.createScene();
|
|
33
|
+
const doc = mod.timeline;
|
|
34
|
+
const fps = opts.fps ?? doc.fps ?? 60;
|
|
35
|
+
const { compileTimeline } = await import("@glissade/core");
|
|
36
|
+
const compiled = compileTimeline(doc);
|
|
37
|
+
const duration = compiled.duration;
|
|
38
|
+
const [from, to] = opts.range ?? [0, duration];
|
|
39
|
+
const firstFrame = Math.round(from * fps);
|
|
40
|
+
const lastFrame = Math.max(firstFrame, Math.ceil(to * fps) - 1);
|
|
41
|
+
const total = lastFrame - firstFrame + 1;
|
|
42
|
+
const isVideo = /\.(mp4|webm)$/i.test(opts.out);
|
|
43
|
+
if (isVideo && !ffmpegAvailable()) throw new Error(`'${opts.out}' needs FFmpeg on PATH and none was found. Render a PNG sequence instead (--out <directory>) or install ffmpeg.`);
|
|
44
|
+
const framesDir = isVideo ? mkdtempSync(join(tmpdir(), "glissade-frames-")) : resolve(opts.out);
|
|
45
|
+
mkdirSync(framesDir, { recursive: true });
|
|
46
|
+
const backend = new SkiaBackend(scene.size.w, scene.size.h);
|
|
47
|
+
scene.setTextMeasurer(backend);
|
|
48
|
+
if ([...scene.nodes.values()].some((n) => n.constructor.isLayoutNode === true)) {
|
|
49
|
+
const { loadYogaLayoutEngine } = await import("@glissade/scene/layout");
|
|
50
|
+
await loadYogaLayoutEngine();
|
|
51
|
+
}
|
|
52
|
+
const videoSources = [];
|
|
53
|
+
for (const [assetId, ref] of Object.entries(doc.assets ?? {})) {
|
|
54
|
+
const { resolveAssetPath } = await import("./audioMix.js").then((n) => n.r);
|
|
55
|
+
if (ref.kind === "font") {
|
|
56
|
+
const { GlobalFonts } = await import("@napi-rs/canvas");
|
|
57
|
+
GlobalFonts.registerFromPath(resolveAssetPath(ref.url, opts.modulePath), assetId);
|
|
58
|
+
} else if (ref.kind === "image") {
|
|
59
|
+
const { loadImage } = await import("@napi-rs/canvas");
|
|
60
|
+
backend.setImageAsset(assetId, await loadImage(resolveAssetPath(ref.url, opts.modulePath)));
|
|
61
|
+
} else if (ref.kind === "video") {
|
|
62
|
+
if (!ffmpegAvailable()) throw new Error(`video asset '${assetId}' needs FFmpeg on PATH for frame extraction (§5.4)`);
|
|
63
|
+
const { FfmpegVideoFrameSource } = await import("./videoSource.js").then((n) => n.i);
|
|
64
|
+
const source = new FfmpegVideoFrameSource(resolveAssetPath(ref.url, opts.modulePath));
|
|
65
|
+
await source.warm(0, source.duration);
|
|
66
|
+
backend.setVideoAsset(assetId, source);
|
|
67
|
+
videoSources.push(source);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
for (let f = firstFrame; f <= lastFrame; f++) {
|
|
71
|
+
backend.render(evaluate(scene, doc, f / fps));
|
|
72
|
+
writeFileSync(join(framesDir, `frame-${String(f).padStart(5, "0")}.png`), backend.encodePng());
|
|
73
|
+
opts.onProgress?.(f - firstFrame + 1, total);
|
|
74
|
+
}
|
|
75
|
+
backend.dispose();
|
|
76
|
+
for (const source of videoSources) source.close();
|
|
77
|
+
if (!isVideo) {
|
|
78
|
+
if (compiled.audio.length > 0) process.stderr.write("note: PNG-sequence output ignores timeline audio; render to .mp4/.webm to mix it\n");
|
|
79
|
+
return {
|
|
80
|
+
frames: total,
|
|
81
|
+
out: framesDir
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const outAbs = resolve(opts.out);
|
|
85
|
+
mkdirSync(dirname(outAbs), { recursive: true });
|
|
86
|
+
const isWebm = /\.webm$/i.test(outAbs);
|
|
87
|
+
const container = isWebm ? "webm" : "mp4";
|
|
88
|
+
const { pickEncoder } = await import("./encoders.js").then((n) => n.r);
|
|
89
|
+
const videoEnc = pickEncoder("video", container);
|
|
90
|
+
if (videoEnc.note) process.stderr.write(`note: ${videoEnc.note}\n`);
|
|
91
|
+
const codec = [
|
|
92
|
+
"-c:v",
|
|
93
|
+
videoEnc.name,
|
|
94
|
+
...{
|
|
95
|
+
"libx264": ["-crf", "18"],
|
|
96
|
+
"libvpx-vp9": [
|
|
97
|
+
"-b:v",
|
|
98
|
+
"0",
|
|
99
|
+
"-crf",
|
|
100
|
+
"32"
|
|
101
|
+
],
|
|
102
|
+
"libvpx": ["-b:v", "2M"],
|
|
103
|
+
"libopenh264": ["-b:v", "4M"],
|
|
104
|
+
"mpeg4": ["-q:v", "3"]
|
|
105
|
+
}[videoEnc.name] ?? [],
|
|
106
|
+
...isWebm ? [] : [
|
|
107
|
+
"-pix_fmt",
|
|
108
|
+
"yuv420p",
|
|
109
|
+
"-movflags",
|
|
110
|
+
"+faststart"
|
|
111
|
+
]
|
|
112
|
+
];
|
|
113
|
+
const { planAudioMix } = await import("./audioMix.js").then((n) => n.r);
|
|
114
|
+
const mix = planAudioMix(compiled.audio, opts.modulePath, duration);
|
|
115
|
+
if (mix?.hasEasedGain) process.stderr.write("note: eased gain keys are approximated linearly in the FFmpeg mix\n");
|
|
116
|
+
const audioInputs = mix ? mix.inputs.flatMap((p) => ["-i", p]) : [];
|
|
117
|
+
const audioEnc = mix ? pickEncoder("audio", container) : null;
|
|
118
|
+
const audioArgs = mix && audioEnc ? [
|
|
119
|
+
"-filter_complex",
|
|
120
|
+
mix.filterComplex,
|
|
121
|
+
"-map",
|
|
122
|
+
"0:v",
|
|
123
|
+
"-map",
|
|
124
|
+
"[aout]",
|
|
125
|
+
"-c:a",
|
|
126
|
+
audioEnc.name,
|
|
127
|
+
...container === "mp4" ? ["-b:a", "192k"] : []
|
|
128
|
+
] : [];
|
|
129
|
+
const result = spawnSync("ffmpeg", [
|
|
130
|
+
"-y",
|
|
131
|
+
"-framerate",
|
|
132
|
+
String(fps),
|
|
133
|
+
"-start_number",
|
|
134
|
+
String(firstFrame),
|
|
135
|
+
"-i",
|
|
136
|
+
join(framesDir, "frame-%05d.png"),
|
|
137
|
+
...audioInputs,
|
|
138
|
+
...audioArgs,
|
|
139
|
+
...codec,
|
|
140
|
+
"-t",
|
|
141
|
+
String(duration),
|
|
142
|
+
outAbs
|
|
143
|
+
], { stdio: [
|
|
144
|
+
"ignore",
|
|
145
|
+
"ignore",
|
|
146
|
+
"pipe"
|
|
147
|
+
] });
|
|
148
|
+
rmSync(framesDir, {
|
|
149
|
+
recursive: true,
|
|
150
|
+
force: true
|
|
151
|
+
});
|
|
152
|
+
if (result.status !== 0) throw new Error(`ffmpeg failed (exit ${result.status}):\n${result.stderr?.toString().slice(-2e3)}`);
|
|
153
|
+
return {
|
|
154
|
+
frames: total,
|
|
155
|
+
out: outAbs
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
159
|
+
export { render as i, ffmpegAvailable as n, loadSceneModule as r, SceneModuleError as t };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { __exportAll as t };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime.js";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { existsSync, mkdtempSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { ColdAssetError } from "@glissade/scene";
|
|
7
|
+
import { loadImage } from "@napi-rs/canvas";
|
|
8
|
+
//#region src/videoSource.ts
|
|
9
|
+
/**
|
|
10
|
+
* CLI video decoding (DESIGN.md §5.4): FFmpeg frame extraction, not
|
|
11
|
+
* WebCodecs-in-Node. Frames are pre-extracted to a temp dir on warm() and
|
|
12
|
+
* served synchronously to SkiaBackend behind the VideoFrameSource seam —
|
|
13
|
+
* swappable if Node WebCodecs ever lands.
|
|
14
|
+
*/
|
|
15
|
+
var videoSource_exports = /* @__PURE__ */ __exportAll({
|
|
16
|
+
FfmpegVideoFrameSource: () => FfmpegVideoFrameSource,
|
|
17
|
+
VideoProbeError: () => VideoProbeError,
|
|
18
|
+
probeVideo: () => probeVideo
|
|
19
|
+
});
|
|
20
|
+
var VideoProbeError = class extends Error {
|
|
21
|
+
constructor(path, detail) {
|
|
22
|
+
super(`ffprobe failed for '${path}': ${detail}`);
|
|
23
|
+
this.name = "VideoProbeError";
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
function probeVideo(path) {
|
|
27
|
+
const res = spawnSync("ffprobe", [
|
|
28
|
+
"-v",
|
|
29
|
+
"error",
|
|
30
|
+
"-select_streams",
|
|
31
|
+
"v:0",
|
|
32
|
+
"-show_entries",
|
|
33
|
+
"stream=r_frame_rate,width,height:format=duration",
|
|
34
|
+
"-of",
|
|
35
|
+
"json",
|
|
36
|
+
path
|
|
37
|
+
]);
|
|
38
|
+
if (res.status !== 0) throw new VideoProbeError(path, res.stderr?.toString() ?? "unknown");
|
|
39
|
+
const info = JSON.parse(res.stdout.toString());
|
|
40
|
+
const stream = info.streams[0];
|
|
41
|
+
if (!stream) throw new VideoProbeError(path, "no video stream");
|
|
42
|
+
const [num, den] = stream.r_frame_rate.split("/").map(Number);
|
|
43
|
+
return {
|
|
44
|
+
fps: num / (den || 1),
|
|
45
|
+
duration: parseFloat(info.format.duration),
|
|
46
|
+
width: stream.width,
|
|
47
|
+
height: stream.height
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* v1 strategy: warm() extracts the full requested media range once at the
|
|
52
|
+
* source's own fps; getFrameSync indexes the extracted sequence. Disk-bound,
|
|
53
|
+
* trivially correct; the streaming-pipe variant is a later optimization.
|
|
54
|
+
*/
|
|
55
|
+
var FfmpegVideoFrameSource = class {
|
|
56
|
+
fps;
|
|
57
|
+
duration;
|
|
58
|
+
path;
|
|
59
|
+
framesDir = null;
|
|
60
|
+
warmedFrom = 0;
|
|
61
|
+
warmedTo = -1;
|
|
62
|
+
cache = /* @__PURE__ */ new Map();
|
|
63
|
+
constructor(path, info) {
|
|
64
|
+
this.path = path;
|
|
65
|
+
const probed = info ?? probeVideo(path);
|
|
66
|
+
this.fps = probed.fps;
|
|
67
|
+
this.duration = probed.duration;
|
|
68
|
+
}
|
|
69
|
+
frameIndex(mediaT) {
|
|
70
|
+
return Math.max(0, Math.floor(mediaT * this.fps + 1e-9));
|
|
71
|
+
}
|
|
72
|
+
async warm(fromT, toT) {
|
|
73
|
+
const from = this.frameIndex(Math.max(0, fromT));
|
|
74
|
+
const to = Math.min(this.frameIndex(Math.min(toT, this.duration)), Math.ceil(this.duration * this.fps) - 1);
|
|
75
|
+
if (this.framesDir && from >= this.warmedFrom && to <= this.warmedTo) return;
|
|
76
|
+
if (this.framesDir) rmSync(this.framesDir, {
|
|
77
|
+
recursive: true,
|
|
78
|
+
force: true
|
|
79
|
+
});
|
|
80
|
+
this.cache.clear();
|
|
81
|
+
this.framesDir = mkdtempSync(join(tmpdir(), "glissade-vsrc-"));
|
|
82
|
+
const startT = from / this.fps;
|
|
83
|
+
const res = spawnSync("ffmpeg", [
|
|
84
|
+
"-y",
|
|
85
|
+
"-ss",
|
|
86
|
+
String(startT),
|
|
87
|
+
"-i",
|
|
88
|
+
this.path,
|
|
89
|
+
"-vf",
|
|
90
|
+
`fps=${this.fps}`,
|
|
91
|
+
"-start_number",
|
|
92
|
+
String(from),
|
|
93
|
+
"-frames:v",
|
|
94
|
+
String(to - from + 1),
|
|
95
|
+
join(this.framesDir, "f-%06d.png")
|
|
96
|
+
], { stdio: [
|
|
97
|
+
"ignore",
|
|
98
|
+
"ignore",
|
|
99
|
+
"pipe"
|
|
100
|
+
] });
|
|
101
|
+
if (res.status !== 0) throw new Error(`ffmpeg frame extraction failed: ${res.stderr?.toString().slice(-1e3)}`);
|
|
102
|
+
for (let idx = from; idx <= to; idx++) {
|
|
103
|
+
const file = join(this.framesDir, `f-${String(idx).padStart(6, "0")}.png`);
|
|
104
|
+
if (!existsSync(file)) break;
|
|
105
|
+
this.cache.set(idx, await loadImage(file));
|
|
106
|
+
}
|
|
107
|
+
this.warmedFrom = from;
|
|
108
|
+
this.warmedTo = to;
|
|
109
|
+
}
|
|
110
|
+
getFrameSync(mediaT) {
|
|
111
|
+
let idx = Math.min(this.frameIndex(mediaT), this.warmedTo);
|
|
112
|
+
while (idx > this.warmedFrom && !this.cache.has(idx)) idx--;
|
|
113
|
+
const hit = this.cache.get(idx);
|
|
114
|
+
if (!hit) throw new ColdAssetError(this.path, `frame ${idx} (mediaT=${mediaT}) outside warmed range`);
|
|
115
|
+
return hit;
|
|
116
|
+
}
|
|
117
|
+
close() {
|
|
118
|
+
if (this.framesDir) rmSync(this.framesDir, {
|
|
119
|
+
recursive: true,
|
|
120
|
+
force: true
|
|
121
|
+
});
|
|
122
|
+
this.framesDir = null;
|
|
123
|
+
this.cache.clear();
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
//#endregion
|
|
127
|
+
export { videoSource_exports as i, VideoProbeError as n, probeVideo as r, FfmpegVideoFrameSource as t };
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@glissade/cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "glissade CLI: headless rendering via backend-skia (+ FFmpeg mux).",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"gs": "./dist/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@napi-rs/canvas": "^0.1.65",
|
|
21
|
+
"jiti": "^2.4.2",
|
|
22
|
+
"@glissade/backend-skia": "0.1.0",
|
|
23
|
+
"@glissade/core": "0.1.0",
|
|
24
|
+
"@glissade/scene": "0.1.0"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/tyevco/glissade.git",
|
|
29
|
+
"directory": "packages/cli"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsdown",
|
|
33
|
+
"typecheck": "tsc --noEmit"
|
|
34
|
+
}
|
|
35
|
+
}
|