@algorisys/tinyfly 0.70.1 → 0.74.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/README.md +32 -8
- package/bin/tinyfly.mjs +95 -2
- package/lib/addons/adapters.js +31 -12
- package/lib/addons/characters.js +204 -0
- package/lib/addons/export.js +476 -440
- package/lib/browser/tinyfly.iife.js +4 -4
- package/lib/browser/tinyfly.js +1352 -1115
- package/lib/browser/tinyfly.umd.js +4 -4
- package/lib/embed/tinyfly-embed.iife.js +1 -1
- package/lib/embed/tinyfly-embed.js +1 -1
- package/lib/engine/tinyfly-engine.js +313 -277
- package/lib/engine/tinyfly-engine.umd.cjs +2 -2
- package/lib/headless/headless.js +947 -0
- package/lib/types/adapters/canvas/canvas-adapter.d.ts +32 -2
- package/lib/types/browser/index.d.ts +3 -1
- package/lib/types/characters/index.d.ts +5 -0
- package/lib/types/characters/stick-figure.d.ts +162 -0
- package/lib/types/engine/authoring/index.d.ts +1 -0
- package/lib/types/engine/authoring/narration.d.ts +80 -0
- package/lib/types/engine/export/captions.d.ts +31 -0
- package/lib/types/engine/export/index.d.ts +1 -0
- package/lib/types/headless/ffmpeg-args.d.ts +28 -0
- package/lib/types/headless/frame-renderer.d.ts +45 -0
- package/lib/types/headless/index.d.ts +12 -0
- package/lib/types/headless/narration-audio.d.ts +13 -0
- package/lib/types/headless/node-audio.d.ts +46 -0
- package/lib/types/headless/node-video.d.ts +48 -0
- package/lib/types/headless/scene-captions.d.ts +4 -0
- package/lib/types/headless/video-scene.d.ts +57 -0
- package/package.json +19 -4
|
@@ -0,0 +1,947 @@
|
|
|
1
|
+
import { deserializeTimeline, planNarration } from "@algorisys/tinyfly";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
4
|
+
import { resolve, join, dirname } from "node:path";
|
|
5
|
+
const DEFAULT_FPS = 30;
|
|
6
|
+
const DEFAULT_GLOW_COLOR = "#ffffff";
|
|
7
|
+
const DEFAULT_SHADOW_COLOR = "rgba(0, 0, 0, 0.5)";
|
|
8
|
+
function composeFilter(v) {
|
|
9
|
+
const parts = [];
|
|
10
|
+
if (v.blur !== void 0) {
|
|
11
|
+
parts.push(`blur(${Math.max(0, v.blur)}px)`);
|
|
12
|
+
}
|
|
13
|
+
if (v.brightness !== void 0) {
|
|
14
|
+
parts.push(`brightness(${Math.max(0, v.brightness)})`);
|
|
15
|
+
}
|
|
16
|
+
if (v.glow !== void 0) {
|
|
17
|
+
parts.push(`drop-shadow(0 0 ${Math.max(0, v.glow)}px ${v.glowColor ?? DEFAULT_GLOW_COLOR})`);
|
|
18
|
+
}
|
|
19
|
+
if (v.shadowX !== void 0 || v.shadowY !== void 0 || v.shadowBlur !== void 0) {
|
|
20
|
+
const x = v.shadowX ?? 0;
|
|
21
|
+
const y = v.shadowY ?? 0;
|
|
22
|
+
const blur = Math.max(0, v.shadowBlur ?? 0);
|
|
23
|
+
parts.push(`drop-shadow(${x}px ${y}px ${blur}px ${v.shadowColor ?? DEFAULT_SHADOW_COLOR})`);
|
|
24
|
+
}
|
|
25
|
+
return parts.length > 0 ? parts.join(" ") : null;
|
|
26
|
+
}
|
|
27
|
+
function shineStops(progress, baseColor, options = {}) {
|
|
28
|
+
const highlight = options.highlight ?? "#ffffff";
|
|
29
|
+
const halfWidth = options.halfWidth ?? 0.12;
|
|
30
|
+
const center = -0.2 + progress * 1.4;
|
|
31
|
+
const stops = [{ offset: 0, color: baseColor }];
|
|
32
|
+
const band = [
|
|
33
|
+
{ offset: center - halfWidth, color: baseColor },
|
|
34
|
+
{ offset: center, color: highlight },
|
|
35
|
+
{ offset: center + halfWidth, color: baseColor }
|
|
36
|
+
];
|
|
37
|
+
for (const stop of band) {
|
|
38
|
+
if (stop.offset > 0 && stop.offset < 1) stops.push(stop);
|
|
39
|
+
}
|
|
40
|
+
stops.push({ offset: 1, color: baseColor });
|
|
41
|
+
stops.sort((a, b) => a.offset - b.offset);
|
|
42
|
+
return stops;
|
|
43
|
+
}
|
|
44
|
+
class CanvasAdapter {
|
|
45
|
+
targets = /* @__PURE__ */ new Map();
|
|
46
|
+
targetOrder = [];
|
|
47
|
+
// Store animation offsets separately - these are applied via ctx.translate() during rendering
|
|
48
|
+
// This ensures that entire elements (including line x2/y2) move together like CSS transforms
|
|
49
|
+
animationOffsets = /* @__PURE__ */ new Map();
|
|
50
|
+
// Track alias IDs that point to the same target (for lookup only, not rendering)
|
|
51
|
+
aliases = /* @__PURE__ */ new Map();
|
|
52
|
+
// Time of the last applied state, passed to custom draw functions
|
|
53
|
+
time = 0;
|
|
54
|
+
/**
|
|
55
|
+
* Register a canvas drawing target.
|
|
56
|
+
* @param id - The ID to register the target under
|
|
57
|
+
* @param target - The canvas target to register
|
|
58
|
+
* @param aliasFor - If provided, this ID is an alias for another target (won't render separately)
|
|
59
|
+
*/
|
|
60
|
+
registerTarget(id, target, aliasFor) {
|
|
61
|
+
if (aliasFor) {
|
|
62
|
+
this.aliases.set(id, aliasFor);
|
|
63
|
+
this.targets.set(id, target);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!this.targets.has(id)) {
|
|
67
|
+
this.targetOrder.push(id);
|
|
68
|
+
}
|
|
69
|
+
this.targets.set(id, target.type === "custom" ? { ...target, props: { ...target.props } } : { ...target });
|
|
70
|
+
this.animationOffsets.set(id, { x: 0, y: 0 });
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Unregister a target by its ID.
|
|
74
|
+
*/
|
|
75
|
+
unregisterTarget(id) {
|
|
76
|
+
this.targets.delete(id);
|
|
77
|
+
this.animationOffsets.delete(id);
|
|
78
|
+
this.aliases.delete(id);
|
|
79
|
+
this.targetOrder = this.targetOrder.filter((tid) => tid !== id);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get a registered target.
|
|
83
|
+
*/
|
|
84
|
+
getTarget(id) {
|
|
85
|
+
return this.targets.get(id);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Clear all registered targets.
|
|
89
|
+
*/
|
|
90
|
+
clearTargets() {
|
|
91
|
+
this.targets.clear();
|
|
92
|
+
this.animationOffsets.clear();
|
|
93
|
+
this.aliases.clear();
|
|
94
|
+
this.targetOrder = [];
|
|
95
|
+
}
|
|
96
|
+
// Map animation property names to canvas target property names
|
|
97
|
+
static PROPERTY_MAP = {
|
|
98
|
+
fill: "fillStyle",
|
|
99
|
+
stroke: "strokeStyle",
|
|
100
|
+
strokeWidth: "lineWidth",
|
|
101
|
+
motionPathRotate: "rotate"
|
|
102
|
+
};
|
|
103
|
+
// Properties that are position offsets (applied via ctx.translate)
|
|
104
|
+
static OFFSET_PROPERTIES = /* @__PURE__ */ new Set(["x", "y", "motionPathX", "motionPathY"]);
|
|
105
|
+
/**
|
|
106
|
+
* Apply animation state to all registered targets.
|
|
107
|
+
* Position properties (x, y) are treated as offsets applied via ctx.translate().
|
|
108
|
+
* This matches how DOM/CSS transforms work (entire element moves together).
|
|
109
|
+
*/
|
|
110
|
+
applyState(state) {
|
|
111
|
+
this.time = state.currentTime;
|
|
112
|
+
for (const [targetId, properties] of state.values) {
|
|
113
|
+
const target = this.targets.get(targetId);
|
|
114
|
+
if (!target) continue;
|
|
115
|
+
const primaryId = this.aliases.get(targetId) ?? targetId;
|
|
116
|
+
const offsets = this.animationOffsets.get(primaryId) ?? { x: 0, y: 0 };
|
|
117
|
+
for (const [property, value] of properties) {
|
|
118
|
+
if (CanvasAdapter.OFFSET_PROPERTIES.has(property) && typeof value === "number") {
|
|
119
|
+
if (property === "x" || property === "motionPathX") {
|
|
120
|
+
offsets.x = value;
|
|
121
|
+
} else if (property === "y" || property === "motionPathY") {
|
|
122
|
+
offsets.y = value;
|
|
123
|
+
}
|
|
124
|
+
} else if (target.type === "custom" && target.props && property in target.props) {
|
|
125
|
+
target.props[property] = value;
|
|
126
|
+
} else {
|
|
127
|
+
const targetProperty = CanvasAdapter.PROPERTY_MAP[property] ?? property;
|
|
128
|
+
target[targetProperty] = value;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
this.animationOffsets.set(primaryId, offsets);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get animation offset for a target (for testing).
|
|
136
|
+
*/
|
|
137
|
+
getAnimationOffset(id) {
|
|
138
|
+
return this.animationOffsets.get(id);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Render all targets to the provided canvas context.
|
|
142
|
+
* Call this in your animation loop after applyState().
|
|
143
|
+
*/
|
|
144
|
+
render(ctx) {
|
|
145
|
+
for (const id of this.targetOrder) {
|
|
146
|
+
const target = this.targets.get(id);
|
|
147
|
+
if (!target) continue;
|
|
148
|
+
const offset = this.animationOffsets.get(id) ?? { x: 0, y: 0 };
|
|
149
|
+
this.renderTarget(ctx, target, offset);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Render a single target.
|
|
154
|
+
*/
|
|
155
|
+
renderTarget(ctx, target, offset) {
|
|
156
|
+
ctx.save();
|
|
157
|
+
if (offset.x !== 0 || offset.y !== 0) {
|
|
158
|
+
ctx.translate(offset.x, offset.y);
|
|
159
|
+
}
|
|
160
|
+
if (target.opacity !== void 0) {
|
|
161
|
+
ctx.globalAlpha = target.opacity;
|
|
162
|
+
}
|
|
163
|
+
const hasTransform = target.rotate !== void 0 || target.rotateX !== void 0 || target.rotateY !== void 0 || target.scale !== void 0 || target.scaleX !== void 0 || target.scaleY !== void 0 || target.skewX !== void 0 || target.skewY !== void 0;
|
|
164
|
+
if (hasTransform) {
|
|
165
|
+
const centerX = this.getPivotX(target);
|
|
166
|
+
const centerY = this.getPivotY(target);
|
|
167
|
+
ctx.translate(centerX, centerY);
|
|
168
|
+
if (target.rotate !== void 0) {
|
|
169
|
+
ctx.rotate(target.rotate * Math.PI / 180);
|
|
170
|
+
}
|
|
171
|
+
let effectiveScaleX = target.scaleX ?? 1;
|
|
172
|
+
let effectiveScaleY = target.scaleY ?? 1;
|
|
173
|
+
if (target.rotateX !== void 0) {
|
|
174
|
+
effectiveScaleY *= Math.cos(target.rotateX * Math.PI / 180);
|
|
175
|
+
}
|
|
176
|
+
if (target.rotateY !== void 0) {
|
|
177
|
+
effectiveScaleX *= Math.cos(target.rotateY * Math.PI / 180);
|
|
178
|
+
}
|
|
179
|
+
if (target.scale !== void 0) {
|
|
180
|
+
ctx.scale(target.scale, target.scale);
|
|
181
|
+
} else if (effectiveScaleX !== 1 || effectiveScaleY !== 1) {
|
|
182
|
+
ctx.scale(effectiveScaleX, effectiveScaleY);
|
|
183
|
+
}
|
|
184
|
+
if (target.skewX !== void 0 || target.skewY !== void 0) {
|
|
185
|
+
const skewXRad = (target.skewX ?? 0) * Math.PI / 180;
|
|
186
|
+
const skewYRad = (target.skewY ?? 0) * Math.PI / 180;
|
|
187
|
+
ctx.transform(1, Math.tan(skewYRad), Math.tan(skewXRad), 1, 0, 0);
|
|
188
|
+
}
|
|
189
|
+
ctx.translate(-centerX, -centerY);
|
|
190
|
+
}
|
|
191
|
+
if (target.fillStyle) {
|
|
192
|
+
ctx.fillStyle = this.resolveFillStyle(ctx, target);
|
|
193
|
+
}
|
|
194
|
+
if (target.strokeStyle) {
|
|
195
|
+
ctx.strokeStyle = target.strokeStyle;
|
|
196
|
+
}
|
|
197
|
+
if (target.lineWidth !== void 0) {
|
|
198
|
+
ctx.lineWidth = target.lineWidth;
|
|
199
|
+
}
|
|
200
|
+
const composedFilter = composeFilter(target);
|
|
201
|
+
if (composedFilter) {
|
|
202
|
+
ctx.filter = composedFilter;
|
|
203
|
+
}
|
|
204
|
+
if (target.clipTop !== void 0 || target.clipRight !== void 0 || target.clipBottom !== void 0 || target.clipLeft !== void 0) {
|
|
205
|
+
const bounds = this.getTargetBounds(target);
|
|
206
|
+
const l = bounds.x + (target.clipLeft ?? 0) / 100 * bounds.width;
|
|
207
|
+
const r = bounds.x + bounds.width - (target.clipRight ?? 0) / 100 * bounds.width;
|
|
208
|
+
const t = bounds.y + (target.clipTop ?? 0) / 100 * bounds.height;
|
|
209
|
+
const b = bounds.y + bounds.height - (target.clipBottom ?? 0) / 100 * bounds.height;
|
|
210
|
+
ctx.beginPath();
|
|
211
|
+
ctx.rect(l, t, Math.max(0, r - l), Math.max(0, b - t));
|
|
212
|
+
ctx.clip();
|
|
213
|
+
}
|
|
214
|
+
switch (target.type) {
|
|
215
|
+
case "rect":
|
|
216
|
+
this.renderRect(ctx, target);
|
|
217
|
+
break;
|
|
218
|
+
case "circle":
|
|
219
|
+
this.renderCircle(ctx, target);
|
|
220
|
+
break;
|
|
221
|
+
case "text":
|
|
222
|
+
this.renderText(ctx, target);
|
|
223
|
+
break;
|
|
224
|
+
case "line":
|
|
225
|
+
this.renderLine(ctx, target);
|
|
226
|
+
break;
|
|
227
|
+
case "path":
|
|
228
|
+
this.renderPath(ctx, target);
|
|
229
|
+
break;
|
|
230
|
+
case "image":
|
|
231
|
+
this.renderImage(ctx, target);
|
|
232
|
+
break;
|
|
233
|
+
case "custom":
|
|
234
|
+
this.renderCustom(ctx, target);
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
ctx.restore();
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Resolve a fill style to a canvas-compatible value.
|
|
241
|
+
*/
|
|
242
|
+
resolveFillStyle(ctx, target) {
|
|
243
|
+
const fill = target.fillStyle;
|
|
244
|
+
if (!fill) return "transparent";
|
|
245
|
+
if (typeof fill === "string") return fill;
|
|
246
|
+
const bounds = this.getTargetBounds(target);
|
|
247
|
+
if (fill.type === "linear") {
|
|
248
|
+
return this.createLinearGradient(ctx, fill, bounds);
|
|
249
|
+
} else {
|
|
250
|
+
return this.createRadialGradient(ctx, fill, bounds);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Create a canvas linear gradient from a LinearGradient definition.
|
|
255
|
+
*/
|
|
256
|
+
createLinearGradient(ctx, gradient, bounds) {
|
|
257
|
+
const angleRad = (gradient.angle - 90) * (Math.PI / 180);
|
|
258
|
+
const centerX = bounds.x + bounds.width / 2;
|
|
259
|
+
const centerY = bounds.y + bounds.height / 2;
|
|
260
|
+
const length = Math.max(bounds.width, bounds.height);
|
|
261
|
+
const x1 = centerX - Math.cos(angleRad) * length / 2;
|
|
262
|
+
const y1 = centerY - Math.sin(angleRad) * length / 2;
|
|
263
|
+
const x2 = centerX + Math.cos(angleRad) * length / 2;
|
|
264
|
+
const y2 = centerY + Math.sin(angleRad) * length / 2;
|
|
265
|
+
const canvasGradient = ctx.createLinearGradient(x1, y1, x2, y2);
|
|
266
|
+
for (const stop of gradient.stops) {
|
|
267
|
+
canvasGradient.addColorStop(stop.offset, stop.color);
|
|
268
|
+
}
|
|
269
|
+
return canvasGradient;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Create a canvas radial gradient from a RadialGradient definition.
|
|
273
|
+
*/
|
|
274
|
+
createRadialGradient(ctx, gradient, bounds) {
|
|
275
|
+
const centerX = bounds.x + bounds.width * gradient.centerX;
|
|
276
|
+
const centerY = bounds.y + bounds.height * gradient.centerY;
|
|
277
|
+
const radius = Math.max(bounds.width, bounds.height) * gradient.radius;
|
|
278
|
+
const canvasGradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, radius);
|
|
279
|
+
for (const stop of gradient.stops) {
|
|
280
|
+
canvasGradient.addColorStop(stop.offset, stop.color);
|
|
281
|
+
}
|
|
282
|
+
return canvasGradient;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Get target bounding box for gradient calculations.
|
|
286
|
+
*/
|
|
287
|
+
getTargetBounds(target) {
|
|
288
|
+
switch (target.type) {
|
|
289
|
+
case "rect":
|
|
290
|
+
case "image":
|
|
291
|
+
case "custom":
|
|
292
|
+
return { x: target.x, y: target.y, width: target.width, height: target.height };
|
|
293
|
+
case "circle":
|
|
294
|
+
return {
|
|
295
|
+
x: target.x - target.radius,
|
|
296
|
+
y: target.y - target.radius,
|
|
297
|
+
width: target.radius * 2,
|
|
298
|
+
height: target.radius * 2
|
|
299
|
+
};
|
|
300
|
+
case "text":
|
|
301
|
+
return { x: target.x, y: target.y - (target.fontSize ?? 16), width: 100, height: target.fontSize ?? 16 };
|
|
302
|
+
case "line":
|
|
303
|
+
return {
|
|
304
|
+
x: Math.min(target.x, target.x2),
|
|
305
|
+
y: Math.min(target.y, target.y2),
|
|
306
|
+
width: Math.abs(target.x2 - target.x),
|
|
307
|
+
height: Math.abs(target.y2 - target.y)
|
|
308
|
+
};
|
|
309
|
+
case "path":
|
|
310
|
+
return { x: target.x, y: target.y, width: 100, height: 100 };
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Render a rectangle.
|
|
315
|
+
*/
|
|
316
|
+
renderRect(ctx, target) {
|
|
317
|
+
ctx.beginPath();
|
|
318
|
+
if (target.borderRadius && target.borderRadius > 0) {
|
|
319
|
+
this.roundRect(ctx, target.x, target.y, target.width, target.height, target.borderRadius);
|
|
320
|
+
} else {
|
|
321
|
+
ctx.rect(target.x, target.y, target.width, target.height);
|
|
322
|
+
}
|
|
323
|
+
if (target.fillStyle) {
|
|
324
|
+
ctx.fill();
|
|
325
|
+
}
|
|
326
|
+
if (target.strokeStyle) {
|
|
327
|
+
ctx.stroke();
|
|
328
|
+
}
|
|
329
|
+
ctx.closePath();
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Draw a rounded rectangle path.
|
|
333
|
+
*/
|
|
334
|
+
roundRect(ctx, x, y, width, height, radius) {
|
|
335
|
+
const r = Math.min(radius, width / 2, height / 2);
|
|
336
|
+
ctx.moveTo(x + r, y);
|
|
337
|
+
ctx.lineTo(x + width - r, y);
|
|
338
|
+
ctx.arcTo(x + width, y, x + width, y + r, r);
|
|
339
|
+
ctx.lineTo(x + width, y + height - r);
|
|
340
|
+
ctx.arcTo(x + width, y + height, x + width - r, y + height, r);
|
|
341
|
+
ctx.lineTo(x + r, y + height);
|
|
342
|
+
ctx.arcTo(x, y + height, x, y + height - r, r);
|
|
343
|
+
ctx.lineTo(x, y + r);
|
|
344
|
+
ctx.arcTo(x, y, x + r, y, r);
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Render a circle.
|
|
348
|
+
*/
|
|
349
|
+
renderCircle(ctx, target) {
|
|
350
|
+
ctx.beginPath();
|
|
351
|
+
ctx.arc(target.x, target.y, target.radius, 0, Math.PI * 2);
|
|
352
|
+
if (target.fillStyle) {
|
|
353
|
+
ctx.fill();
|
|
354
|
+
}
|
|
355
|
+
if (target.strokeStyle) {
|
|
356
|
+
ctx.stroke();
|
|
357
|
+
}
|
|
358
|
+
ctx.closePath();
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Render text.
|
|
362
|
+
*/
|
|
363
|
+
renderText(ctx, target) {
|
|
364
|
+
const fontSize = target.fontSize ?? 16;
|
|
365
|
+
const fontFamily = target.fontFamily ?? "sans-serif";
|
|
366
|
+
const fontWeight = target.fontWeight ?? 400;
|
|
367
|
+
ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
|
|
368
|
+
ctx.textAlign = target.textAlign ?? "left";
|
|
369
|
+
ctx.textBaseline = target.textBaseline ?? "top";
|
|
370
|
+
if (target.fillStyle) {
|
|
371
|
+
if (target.shine !== void 0 && typeof target.fillStyle === "string") {
|
|
372
|
+
const width = ctx.measureText(target.text).width;
|
|
373
|
+
const align = target.textAlign ?? "left";
|
|
374
|
+
const left = align === "center" ? target.x - width / 2 : align === "right" ? target.x - width : target.x;
|
|
375
|
+
const gradient = ctx.createLinearGradient(left, target.y, left + width, target.y);
|
|
376
|
+
for (const stop of shineStops(target.shine, target.fillStyle)) {
|
|
377
|
+
gradient.addColorStop(stop.offset, stop.color);
|
|
378
|
+
}
|
|
379
|
+
ctx.fillStyle = gradient;
|
|
380
|
+
}
|
|
381
|
+
ctx.fillText(target.text, target.x, target.y);
|
|
382
|
+
}
|
|
383
|
+
if (target.strokeStyle) {
|
|
384
|
+
ctx.strokeText(target.text, target.x, target.y);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Render a line.
|
|
389
|
+
*/
|
|
390
|
+
renderLine(ctx, target) {
|
|
391
|
+
if (target.lineCap) {
|
|
392
|
+
ctx.lineCap = target.lineCap;
|
|
393
|
+
}
|
|
394
|
+
ctx.beginPath();
|
|
395
|
+
ctx.moveTo(target.x, target.y);
|
|
396
|
+
ctx.lineTo(target.x2, target.y2);
|
|
397
|
+
if (target.strokeStyle) {
|
|
398
|
+
ctx.stroke();
|
|
399
|
+
}
|
|
400
|
+
ctx.closePath();
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Render an SVG path.
|
|
404
|
+
*/
|
|
405
|
+
renderPath(ctx, target) {
|
|
406
|
+
if (target.lineCap) {
|
|
407
|
+
ctx.lineCap = target.lineCap;
|
|
408
|
+
}
|
|
409
|
+
if (target.lineJoin) {
|
|
410
|
+
ctx.lineJoin = target.lineJoin;
|
|
411
|
+
}
|
|
412
|
+
const path = new Path2D(target.d);
|
|
413
|
+
ctx.translate(target.x, target.y);
|
|
414
|
+
if (target.fillStyle) {
|
|
415
|
+
ctx.fill(path);
|
|
416
|
+
}
|
|
417
|
+
if (target.strokeStyle) {
|
|
418
|
+
ctx.stroke(path);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Render an image.
|
|
423
|
+
*/
|
|
424
|
+
renderImage(ctx, target) {
|
|
425
|
+
if (!target.image) return;
|
|
426
|
+
if (target.opacity !== void 0) {
|
|
427
|
+
ctx.globalAlpha = target.opacity;
|
|
428
|
+
}
|
|
429
|
+
const { x, y, width: bw, height: bh } = target;
|
|
430
|
+
const fit = target.objectFit ?? "fill";
|
|
431
|
+
const radius = target.borderRadius ?? 0;
|
|
432
|
+
const src = target.image;
|
|
433
|
+
const iw = src.naturalWidth || src.videoWidth || src.width || bw;
|
|
434
|
+
const ih = src.naturalHeight || src.videoHeight || src.height || bh;
|
|
435
|
+
let dx = x, dy = y, dw = bw, dh = bh;
|
|
436
|
+
if (fit !== "fill" && iw > 0 && ih > 0) {
|
|
437
|
+
const scale = fit === "cover" ? Math.max(bw / iw, bh / ih) : Math.min(bw / iw, bh / ih);
|
|
438
|
+
dw = iw * scale;
|
|
439
|
+
dh = ih * scale;
|
|
440
|
+
dx = x + (bw - dw) / 2;
|
|
441
|
+
dy = y + (bh - dh) / 2;
|
|
442
|
+
}
|
|
443
|
+
ctx.save();
|
|
444
|
+
ctx.beginPath();
|
|
445
|
+
if (radius > 0 && typeof ctx.roundRect === "function") {
|
|
446
|
+
ctx.roundRect(x, y, bw, bh, radius);
|
|
447
|
+
} else {
|
|
448
|
+
ctx.rect(x, y, bw, bh);
|
|
449
|
+
}
|
|
450
|
+
ctx.clip();
|
|
451
|
+
ctx.drawImage(target.image, dx, dy, dw, dh);
|
|
452
|
+
ctx.restore();
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Render a custom target by calling its draw function in local coordinates.
|
|
456
|
+
*/
|
|
457
|
+
renderCustom(ctx, target) {
|
|
458
|
+
ctx.translate(target.x, target.y);
|
|
459
|
+
target.draw(ctx, target, this.time);
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Transform pivot X: the target's centre, shifted by `originX` across its
|
|
463
|
+
* own width. `originX: 0` pivots on the left edge, 100 on the right.
|
|
464
|
+
*/
|
|
465
|
+
getPivotX(target) {
|
|
466
|
+
const centre = this.getCenterX(target);
|
|
467
|
+
if (target.originX === void 0) return centre;
|
|
468
|
+
return centre + (target.originX - 50) / 100 * this.getBoundsWidth(target);
|
|
469
|
+
}
|
|
470
|
+
/** Transform pivot Y — see `getPivotX`. */
|
|
471
|
+
getPivotY(target) {
|
|
472
|
+
const centre = this.getCenterY(target);
|
|
473
|
+
if (target.originY === void 0) return centre;
|
|
474
|
+
return centre + (target.originY - 50) / 100 * this.getBoundsHeight(target);
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Bounding-box width used to resolve `originX`. Circles use their diameter;
|
|
478
|
+
* text is approximated from its font size, since measuring it would need the
|
|
479
|
+
* context and the pivot only has to be stable, not typographically exact.
|
|
480
|
+
*/
|
|
481
|
+
getBoundsWidth(target) {
|
|
482
|
+
switch (target.type) {
|
|
483
|
+
case "rect":
|
|
484
|
+
case "image":
|
|
485
|
+
case "custom":
|
|
486
|
+
return target.width;
|
|
487
|
+
case "circle":
|
|
488
|
+
return target.radius * 2;
|
|
489
|
+
case "text":
|
|
490
|
+
return (target.text?.length ?? 0) * (target.fontSize ?? 16) * 0.6;
|
|
491
|
+
case "line":
|
|
492
|
+
return Math.abs(target.x2 - target.x);
|
|
493
|
+
case "path":
|
|
494
|
+
return 100;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
/** Bounding-box height used to resolve `originY`. See `getBoundsWidth`. */
|
|
498
|
+
getBoundsHeight(target) {
|
|
499
|
+
switch (target.type) {
|
|
500
|
+
case "rect":
|
|
501
|
+
case "image":
|
|
502
|
+
case "custom":
|
|
503
|
+
return target.height;
|
|
504
|
+
case "circle":
|
|
505
|
+
return target.radius * 2;
|
|
506
|
+
case "text":
|
|
507
|
+
return target.fontSize ?? 16;
|
|
508
|
+
case "line":
|
|
509
|
+
return Math.abs(target.y2 - target.y);
|
|
510
|
+
case "path":
|
|
511
|
+
return 100;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Get the center X coordinate of a target.
|
|
516
|
+
*/
|
|
517
|
+
getCenterX(target) {
|
|
518
|
+
switch (target.type) {
|
|
519
|
+
case "rect":
|
|
520
|
+
case "image":
|
|
521
|
+
case "custom":
|
|
522
|
+
return target.x + target.width / 2;
|
|
523
|
+
case "circle":
|
|
524
|
+
return target.x;
|
|
525
|
+
case "text":
|
|
526
|
+
return target.x;
|
|
527
|
+
case "line":
|
|
528
|
+
return (target.x + target.x2) / 2;
|
|
529
|
+
case "path":
|
|
530
|
+
return target.x + 50;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Get the center Y coordinate of a target.
|
|
535
|
+
*/
|
|
536
|
+
getCenterY(target) {
|
|
537
|
+
switch (target.type) {
|
|
538
|
+
case "rect":
|
|
539
|
+
case "image":
|
|
540
|
+
case "custom":
|
|
541
|
+
return target.y + target.height / 2;
|
|
542
|
+
case "circle":
|
|
543
|
+
return target.y;
|
|
544
|
+
case "text":
|
|
545
|
+
return target.y + (target.fontSize ?? 16) / 2;
|
|
546
|
+
case "line":
|
|
547
|
+
return (target.y + target.y2) / 2;
|
|
548
|
+
case "path":
|
|
549
|
+
return target.y + 50;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Load an image and return a promise that resolves to the image.
|
|
554
|
+
* Useful for creating ImageTarget objects.
|
|
555
|
+
*/
|
|
556
|
+
static loadImage(src) {
|
|
557
|
+
return new Promise((resolve2, reject) => {
|
|
558
|
+
const img = new Image();
|
|
559
|
+
img.onload = () => resolve2(img);
|
|
560
|
+
img.onerror = reject;
|
|
561
|
+
img.src = src;
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
class FrameRenderer {
|
|
566
|
+
scene;
|
|
567
|
+
fps;
|
|
568
|
+
/** Length in ms */
|
|
569
|
+
duration;
|
|
570
|
+
/** Output size is the scene size times this */
|
|
571
|
+
scale;
|
|
572
|
+
timeline;
|
|
573
|
+
adapter = new CanvasAdapter();
|
|
574
|
+
constructor(scene, options = {}) {
|
|
575
|
+
this.scene = scene;
|
|
576
|
+
this.fps = scene.fps ?? DEFAULT_FPS;
|
|
577
|
+
this.scale = options.scale ?? 1;
|
|
578
|
+
this.timeline = scene.timeline ? deserializeTimeline(scene.timeline) : void 0;
|
|
579
|
+
this.duration = scene.duration ?? this.timeline?.duration ?? 0;
|
|
580
|
+
if (!(this.fps > 0)) throw new Error(`video: fps must be positive (got ${scene.fps})`);
|
|
581
|
+
if (!(this.duration > 0)) {
|
|
582
|
+
throw new Error("video: the scene needs a duration (set `duration`, or give it a timeline with tracks)");
|
|
583
|
+
}
|
|
584
|
+
for (const [id, target] of Object.entries(scene.targets ?? {})) {
|
|
585
|
+
this.adapter.registerTarget(id, target);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
/** Output width in pixels */
|
|
589
|
+
get width() {
|
|
590
|
+
return Math.round(this.scene.width * this.scale);
|
|
591
|
+
}
|
|
592
|
+
/** Output height in pixels */
|
|
593
|
+
get height() {
|
|
594
|
+
return Math.round(this.scene.height * this.scale);
|
|
595
|
+
}
|
|
596
|
+
/** Frames needed to cover the whole duration */
|
|
597
|
+
get frameCount() {
|
|
598
|
+
return frameCount(this.duration, this.fps);
|
|
599
|
+
}
|
|
600
|
+
/** Time in ms of a frame */
|
|
601
|
+
frameTime(index) {
|
|
602
|
+
return index * 1e3 / this.fps;
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Stills worth checking: the middle of each caption (the moment a line is
|
|
606
|
+
* being spoken), else the middle of each marker's step (from the marker to
|
|
607
|
+
* the next one, or the end), else the middle of the video.
|
|
608
|
+
*/
|
|
609
|
+
stillTimes() {
|
|
610
|
+
const captions = this.scene.captions ?? [];
|
|
611
|
+
if (captions.length > 0) {
|
|
612
|
+
return captions.map((cue, index) => ({ id: cue.id ?? `line-${index}`, time: (cue.start + cue.end) / 2 }));
|
|
613
|
+
}
|
|
614
|
+
const markers = [...this.scene.timeline?.config.markers ?? []].sort((a, b) => a.time - b.time);
|
|
615
|
+
if (markers.length === 0) return [{ id: "middle", time: this.duration / 2 }];
|
|
616
|
+
return markers.map((marker, index) => {
|
|
617
|
+
const end = index + 1 < markers.length ? markers[index + 1].time : this.duration;
|
|
618
|
+
return { id: marker.id, time: (marker.time + end) / 2 };
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
/** Clear the context and draw the scene at `time` ms. */
|
|
622
|
+
render(ctx, time, index = 0) {
|
|
623
|
+
const state = this.timeline?.getStateAtTime(time);
|
|
624
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
625
|
+
ctx.clearRect(0, 0, this.width, this.height);
|
|
626
|
+
ctx.save();
|
|
627
|
+
ctx.scale(this.scale, this.scale);
|
|
628
|
+
const frame = { index, time, state, width: this.scene.width, height: this.scene.height };
|
|
629
|
+
const background = this.scene.background ?? "#ffffff";
|
|
630
|
+
if (typeof background === "function") {
|
|
631
|
+
this.drawIsolated(ctx, background, frame);
|
|
632
|
+
} else if (background !== "transparent") {
|
|
633
|
+
ctx.fillStyle = background;
|
|
634
|
+
ctx.fillRect(0, 0, this.scene.width, this.scene.height);
|
|
635
|
+
}
|
|
636
|
+
if (state) this.adapter.applyState(state);
|
|
637
|
+
this.adapter.render(ctx);
|
|
638
|
+
if (this.scene.draw) this.drawIsolated(ctx, this.scene.draw, frame);
|
|
639
|
+
ctx.restore();
|
|
640
|
+
}
|
|
641
|
+
/** Run scene code without letting its context changes leak into the rest. */
|
|
642
|
+
drawIsolated(ctx, draw, frame) {
|
|
643
|
+
ctx.save();
|
|
644
|
+
draw(ctx, frame);
|
|
645
|
+
ctx.restore();
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
function frameCount(duration, fps) {
|
|
649
|
+
return Math.max(1, Math.ceil(duration * fps / 1e3 - 1e-6));
|
|
650
|
+
}
|
|
651
|
+
function ffmpegArgs(options) {
|
|
652
|
+
const { width, height, fps, output, audio } = options;
|
|
653
|
+
const args = [
|
|
654
|
+
"-y",
|
|
655
|
+
"-loglevel",
|
|
656
|
+
"error",
|
|
657
|
+
"-f",
|
|
658
|
+
"rawvideo",
|
|
659
|
+
"-pix_fmt",
|
|
660
|
+
"rgba",
|
|
661
|
+
"-s",
|
|
662
|
+
`${width}x${height}`,
|
|
663
|
+
"-r",
|
|
664
|
+
String(fps),
|
|
665
|
+
"-i",
|
|
666
|
+
"-"
|
|
667
|
+
];
|
|
668
|
+
if (audio) args.push("-i", audio);
|
|
669
|
+
args.push(
|
|
670
|
+
"-c:v",
|
|
671
|
+
"libx264",
|
|
672
|
+
"-preset",
|
|
673
|
+
options.preset ?? "medium",
|
|
674
|
+
"-crf",
|
|
675
|
+
String(options.crf ?? 20),
|
|
676
|
+
// yuv420p needs even dimensions; players reject anything else.
|
|
677
|
+
"-vf",
|
|
678
|
+
"pad=ceil(iw/2)*2:ceil(ih/2)*2",
|
|
679
|
+
"-pix_fmt",
|
|
680
|
+
"yuv420p"
|
|
681
|
+
);
|
|
682
|
+
if (audio) args.push("-c:a", "aac", "-b:a", "192k", "-shortest");
|
|
683
|
+
args.push("-movflags", "+faststart", output);
|
|
684
|
+
return args;
|
|
685
|
+
}
|
|
686
|
+
function ffmpegDecodeArgs(input, sampleRate) {
|
|
687
|
+
return ["-v", "error", "-i", input, "-f", "f32le", "-ac", "1", "-ar", String(sampleRate), "-"];
|
|
688
|
+
}
|
|
689
|
+
function ffmpegStartError(ffmpeg, error) {
|
|
690
|
+
return error.code === "ENOENT" ? new Error(`tinyfly: "${ffmpeg}" was not found; install ffmpeg or pass its path`) : error;
|
|
691
|
+
}
|
|
692
|
+
function ffmpegExitError(code, stderr) {
|
|
693
|
+
return new Error(`tinyfly: ffmpeg exited with code ${code}${stderr.trim() ? `:
|
|
694
|
+
${stderr.trim()}` : ""}`);
|
|
695
|
+
}
|
|
696
|
+
function formatTime(ms, separator) {
|
|
697
|
+
const total = Math.max(0, Math.round(ms));
|
|
698
|
+
const hours = Math.floor(total / 36e5);
|
|
699
|
+
const minutes = Math.floor(total % 36e5 / 6e4);
|
|
700
|
+
const seconds = Math.floor(total % 6e4 / 1e3);
|
|
701
|
+
const millis = total % 1e3;
|
|
702
|
+
const pad = (value, width = 2) => String(value).padStart(width, "0");
|
|
703
|
+
return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}${separator}${pad(millis, 3)}`;
|
|
704
|
+
}
|
|
705
|
+
function cueText(text) {
|
|
706
|
+
return text.replace(/\r\n?/g, "\n").replace(/\n{2,}/g, "\n").trim();
|
|
707
|
+
}
|
|
708
|
+
function toSRT(cues) {
|
|
709
|
+
return cues.map((cue, index) => `${index + 1}
|
|
710
|
+
${formatTime(cue.start, ",")} --> ${formatTime(cue.end, ",")}
|
|
711
|
+
${cueText(cue.text)}
|
|
712
|
+
`).join("\n");
|
|
713
|
+
}
|
|
714
|
+
function toWebVTT(cues) {
|
|
715
|
+
const body = cues.map((cue) => `${formatTime(cue.start, ".")} --> ${formatTime(cue.end, ".")}
|
|
716
|
+
${cueText(cue.text)}
|
|
717
|
+
`).join("\n");
|
|
718
|
+
return `WEBVTT
|
|
719
|
+
|
|
720
|
+
${body}`;
|
|
721
|
+
}
|
|
722
|
+
function captionCuesFromTimeline(definition, options = {}) {
|
|
723
|
+
const markers = [...definition.config.markers ?? []].sort((a, b) => a.time - b.time);
|
|
724
|
+
const translations = options.language ? definition.captions?.[options.language] : void 0;
|
|
725
|
+
const duration = deserializeTimeline(definition).duration;
|
|
726
|
+
const cues = [];
|
|
727
|
+
markers.forEach((marker, index) => {
|
|
728
|
+
const text = translations?.[marker.id] ?? marker.label;
|
|
729
|
+
if (!text) return;
|
|
730
|
+
const end = index + 1 < markers.length ? markers[index + 1].time : duration;
|
|
731
|
+
if (end > marker.time) cues.push({ start: marker.time, end, text });
|
|
732
|
+
});
|
|
733
|
+
return cues;
|
|
734
|
+
}
|
|
735
|
+
function sceneCaptions(scene, language) {
|
|
736
|
+
if (scene.captions) return scene.captions;
|
|
737
|
+
if (scene.timeline) return captionCuesFromTimeline(scene.timeline, { language });
|
|
738
|
+
return [];
|
|
739
|
+
}
|
|
740
|
+
let canvasModule;
|
|
741
|
+
function loadCanvas() {
|
|
742
|
+
canvasModule ??= import("@napi-rs/canvas").then(
|
|
743
|
+
(mod) => {
|
|
744
|
+
const canvas = mod;
|
|
745
|
+
const scope = globalThis;
|
|
746
|
+
if (scope.Path2D === void 0) scope.Path2D = canvas.Path2D;
|
|
747
|
+
return canvas;
|
|
748
|
+
},
|
|
749
|
+
() => {
|
|
750
|
+
canvasModule = void 0;
|
|
751
|
+
throw new Error("tinyfly: rendering in Node needs the optional package @napi-rs/canvas (npm install @napi-rs/canvas)");
|
|
752
|
+
}
|
|
753
|
+
);
|
|
754
|
+
return canvasModule;
|
|
755
|
+
}
|
|
756
|
+
async function prepare(scene, options) {
|
|
757
|
+
const canvasLib = await loadCanvas();
|
|
758
|
+
const baseDir = options.baseDir ?? process.cwd();
|
|
759
|
+
for (const [family, file] of Object.entries({ ...scene.fonts, ...options.fonts })) {
|
|
760
|
+
canvasLib.GlobalFonts.registerFromPath(resolve(baseDir, file), family);
|
|
761
|
+
}
|
|
762
|
+
const renderer = new FrameRenderer(options.fps ? { ...scene, fps: options.fps } : scene, { scale: options.scale });
|
|
763
|
+
const canvas = canvasLib.createCanvas(renderer.width, renderer.height);
|
|
764
|
+
const ctx = canvas.getContext("2d");
|
|
765
|
+
return { renderer, canvas, ctx, baseDir };
|
|
766
|
+
}
|
|
767
|
+
async function renderVideo(scene, options) {
|
|
768
|
+
const { renderer, ctx, baseDir } = await prepare(scene, options);
|
|
769
|
+
const audioFile = options.audio === false ? void 0 : options.audio ?? scene.audio;
|
|
770
|
+
const output = resolve(options.output);
|
|
771
|
+
const encoder = startEncoder(
|
|
772
|
+
options.ffmpeg ?? "ffmpeg",
|
|
773
|
+
ffmpegArgs({
|
|
774
|
+
width: renderer.width,
|
|
775
|
+
height: renderer.height,
|
|
776
|
+
fps: renderer.fps,
|
|
777
|
+
output,
|
|
778
|
+
audio: audioFile ? resolve(baseDir, audioFile) : void 0,
|
|
779
|
+
crf: options.crf,
|
|
780
|
+
preset: options.preset
|
|
781
|
+
})
|
|
782
|
+
);
|
|
783
|
+
const total = renderer.frameCount;
|
|
784
|
+
try {
|
|
785
|
+
for (let index = 0; index < total; index++) {
|
|
786
|
+
renderer.render(ctx, renderer.frameTime(index), index);
|
|
787
|
+
const pixels = ctx.getImageData(0, 0, renderer.width, renderer.height).data;
|
|
788
|
+
await encoder.write(new Uint8Array(pixels.buffer, pixels.byteOffset, pixels.byteLength));
|
|
789
|
+
options.onProgress?.(index + 1, total);
|
|
790
|
+
}
|
|
791
|
+
} catch (error) {
|
|
792
|
+
encoder.abort();
|
|
793
|
+
throw error;
|
|
794
|
+
}
|
|
795
|
+
await encoder.finish();
|
|
796
|
+
return { output, frames: total, duration: renderer.duration };
|
|
797
|
+
}
|
|
798
|
+
async function renderStills(scene, options) {
|
|
799
|
+
const { renderer, canvas, ctx } = await prepare(scene, options);
|
|
800
|
+
const dir = resolve(options.dir);
|
|
801
|
+
await mkdir(dir, { recursive: true });
|
|
802
|
+
const times = options.times ?? renderer.stillTimes();
|
|
803
|
+
const width = String(times.length).length;
|
|
804
|
+
const files = [];
|
|
805
|
+
for (const [index, { id, time }] of times.entries()) {
|
|
806
|
+
renderer.render(ctx, time);
|
|
807
|
+
const file = join(dir, `${String(index).padStart(width, "0")}-${safeName(id)}.png`);
|
|
808
|
+
await writeFile(file, await canvas.encode("png"));
|
|
809
|
+
files.push(file);
|
|
810
|
+
}
|
|
811
|
+
return files;
|
|
812
|
+
}
|
|
813
|
+
function safeName(id) {
|
|
814
|
+
return id.replace(/[^a-zA-Z0-9_-]+/g, "-") || "frame";
|
|
815
|
+
}
|
|
816
|
+
function startEncoder(ffmpeg, args) {
|
|
817
|
+
const child = spawn(ffmpeg, args, { stdio: ["pipe", "ignore", "pipe"] });
|
|
818
|
+
let stderr = "";
|
|
819
|
+
child.stderr.on("data", (chunk) => {
|
|
820
|
+
stderr += chunk.toString();
|
|
821
|
+
});
|
|
822
|
+
child.stdin.on("error", () => {
|
|
823
|
+
});
|
|
824
|
+
const done = new Promise((resolveDone, rejectDone) => {
|
|
825
|
+
child.on("error", (error) => rejectDone(ffmpegStartError(ffmpeg, error)));
|
|
826
|
+
child.on("close", (code) => code === 0 ? resolveDone() : rejectDone(ffmpegExitError(code, stderr)));
|
|
827
|
+
});
|
|
828
|
+
const exitedEarly = done.then(() => {
|
|
829
|
+
throw new Error("tinyfly: ffmpeg stopped before all frames were written");
|
|
830
|
+
});
|
|
831
|
+
exitedEarly.catch(() => {
|
|
832
|
+
});
|
|
833
|
+
return {
|
|
834
|
+
write(frame) {
|
|
835
|
+
if (child.stdin.write(frame)) return Promise.resolve();
|
|
836
|
+
const drained = new Promise((resolveDrain) => child.stdin.once("drain", () => resolveDrain()));
|
|
837
|
+
return Promise.race([drained, exitedEarly]);
|
|
838
|
+
},
|
|
839
|
+
finish() {
|
|
840
|
+
child.stdin.end();
|
|
841
|
+
return done;
|
|
842
|
+
},
|
|
843
|
+
abort() {
|
|
844
|
+
child.kill();
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
function assembleNarration(plan, clips, sampleRate) {
|
|
849
|
+
if (clips.length !== plan.cues.length) {
|
|
850
|
+
throw new Error(`narration audio: ${clips.length} clip(s) for ${plan.cues.length} line(s)`);
|
|
851
|
+
}
|
|
852
|
+
const total = Math.round(plan.duration * sampleRate / 1e3);
|
|
853
|
+
const out = new Float32Array(total);
|
|
854
|
+
plan.cues.forEach((cue, index) => {
|
|
855
|
+
const start = Math.round(cue.start * sampleRate / 1e3);
|
|
856
|
+
const clip = clips[index];
|
|
857
|
+
out.set(clip.subarray(0, Math.max(0, Math.min(clip.length, total - start))), start);
|
|
858
|
+
});
|
|
859
|
+
return out;
|
|
860
|
+
}
|
|
861
|
+
function encodeWav(samples, sampleRate) {
|
|
862
|
+
const bytesPerSample = 2;
|
|
863
|
+
const dataSize = samples.length * bytesPerSample;
|
|
864
|
+
const bytes = new Uint8Array(44 + dataSize);
|
|
865
|
+
const view = new DataView(bytes.buffer);
|
|
866
|
+
const ascii = (offset, text) => {
|
|
867
|
+
for (let i = 0; i < text.length; i++) view.setUint8(offset + i, text.charCodeAt(i));
|
|
868
|
+
};
|
|
869
|
+
ascii(0, "RIFF");
|
|
870
|
+
view.setUint32(4, 36 + dataSize, true);
|
|
871
|
+
ascii(8, "WAVE");
|
|
872
|
+
ascii(12, "fmt ");
|
|
873
|
+
view.setUint32(16, 16, true);
|
|
874
|
+
view.setUint16(20, 1, true);
|
|
875
|
+
view.setUint16(22, 1, true);
|
|
876
|
+
view.setUint32(24, sampleRate, true);
|
|
877
|
+
view.setUint32(28, sampleRate * bytesPerSample, true);
|
|
878
|
+
view.setUint16(32, bytesPerSample, true);
|
|
879
|
+
view.setUint16(34, 16, true);
|
|
880
|
+
ascii(36, "data");
|
|
881
|
+
view.setUint32(40, dataSize, true);
|
|
882
|
+
for (let i = 0; i < samples.length; i++) {
|
|
883
|
+
const clamped = Math.max(-1, Math.min(1, samples[i]));
|
|
884
|
+
view.setInt16(44 + i * bytesPerSample, Math.round(clamped * 32767), true);
|
|
885
|
+
}
|
|
886
|
+
return bytes;
|
|
887
|
+
}
|
|
888
|
+
async function voiceNarration(scenes, options) {
|
|
889
|
+
const baseDir = options.baseDir ?? process.cwd();
|
|
890
|
+
const sampleRate = options.sampleRate ?? 48e3;
|
|
891
|
+
const ffmpeg = options.ffmpeg ?? "ffmpeg";
|
|
892
|
+
const clips = [];
|
|
893
|
+
const timedScenes = [];
|
|
894
|
+
for (const scene of scenes) {
|
|
895
|
+
const lines = [];
|
|
896
|
+
for (const { audio, ...line } of scene.lines) {
|
|
897
|
+
const samples = await decodeAudio(resolve(baseDir, audio), { sampleRate, ffmpeg });
|
|
898
|
+
clips.push(samples);
|
|
899
|
+
lines.push({ ...line, duration: samples.length * 1e3 / sampleRate });
|
|
900
|
+
}
|
|
901
|
+
timedScenes.push({ id: scene.id, lines, tail: scene.tail });
|
|
902
|
+
}
|
|
903
|
+
const plan = planNarration(timedScenes, options);
|
|
904
|
+
const output = resolve(baseDir, options.output);
|
|
905
|
+
await mkdir(dirname(output), { recursive: true });
|
|
906
|
+
await writeFile(output, encodeWav(assembleNarration(plan, clips, sampleRate), sampleRate));
|
|
907
|
+
return { plan, audio: output };
|
|
908
|
+
}
|
|
909
|
+
function decodeAudio(file, options = {}) {
|
|
910
|
+
const ffmpeg = options.ffmpeg ?? "ffmpeg";
|
|
911
|
+
const child = spawn(ffmpeg, ffmpegDecodeArgs(file, options.sampleRate ?? 48e3), { stdio: ["ignore", "pipe", "pipe"] });
|
|
912
|
+
const chunks = [];
|
|
913
|
+
let stderr = "";
|
|
914
|
+
child.stdout.on("data", (chunk) => chunks.push(chunk));
|
|
915
|
+
child.stderr.on("data", (chunk) => {
|
|
916
|
+
stderr += chunk.toString();
|
|
917
|
+
});
|
|
918
|
+
return new Promise((resolveSamples, reject) => {
|
|
919
|
+
child.on("error", (error) => reject(ffmpegStartError(ffmpeg, error)));
|
|
920
|
+
child.on("close", (code) => {
|
|
921
|
+
if (code !== 0) return reject(ffmpegExitError(code, stderr));
|
|
922
|
+
const bytes = Buffer.concat(chunks);
|
|
923
|
+
const samples = new Float32Array(Math.floor(bytes.length / 4));
|
|
924
|
+
new Uint8Array(samples.buffer).set(bytes.subarray(0, samples.length * 4));
|
|
925
|
+
resolveSamples(samples);
|
|
926
|
+
});
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
export {
|
|
930
|
+
DEFAULT_FPS,
|
|
931
|
+
FrameRenderer,
|
|
932
|
+
assembleNarration,
|
|
933
|
+
captionCuesFromTimeline,
|
|
934
|
+
decodeAudio,
|
|
935
|
+
encodeWav,
|
|
936
|
+
ffmpegArgs,
|
|
937
|
+
ffmpegDecodeArgs,
|
|
938
|
+
ffmpegExitError,
|
|
939
|
+
ffmpegStartError,
|
|
940
|
+
frameCount,
|
|
941
|
+
renderStills,
|
|
942
|
+
renderVideo,
|
|
943
|
+
sceneCaptions,
|
|
944
|
+
toSRT,
|
|
945
|
+
toWebVTT,
|
|
946
|
+
voiceNarration
|
|
947
|
+
};
|