@bendyline/squisq-video-react 2.0.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/COPYING.GPL-2.0.txt +339 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE.md +39 -0
  4. package/README.md +34 -7
  5. package/dist/chunk-KQG6DZ7G.js +549 -0
  6. package/dist/chunk-VI5AIVOQ.js +1951 -0
  7. package/dist/chunk-VILZP3GL.js +856 -0
  8. package/dist/chunk-YZN7D4IC.js +300 -0
  9. package/dist/components/index.d.ts +90 -0
  10. package/dist/components/index.js +11 -0
  11. package/dist/encoder/index.d.ts +95 -0
  12. package/dist/encoder/index.js +13 -0
  13. package/dist/hooks/index.d.ts +32 -0
  14. package/dist/hooks/index.js +10 -0
  15. package/dist/index.d.ts +8 -277
  16. package/dist/index.js +12 -1503
  17. package/dist/useVideoExport-NtqZVgaz.d.ts +115 -0
  18. package/dist/workers/encode.worker.js +37 -20
  19. package/dist/workers/ffmpeg.class-worker.js +0 -1
  20. package/package.json +26 -8
  21. package/dist/chunk-6DKLHXX3.js +0 -47
  22. package/dist/chunk-6DKLHXX3.js.map +0 -1
  23. package/dist/index.js.map +0 -1
  24. package/dist/workers/encode.worker.js.map +0 -1
  25. package/dist/workers/ffmpeg.class-worker.js.map +0 -1
  26. package/src/VideoExportButton.tsx +0 -87
  27. package/src/VideoExportModal.tsx +0 -553
  28. package/src/__tests__/audioTrack.test.ts +0 -108
  29. package/src/__tests__/gifTranscode.test.ts +0 -118
  30. package/src/__tests__/mp4MuxAudio.test.ts +0 -72
  31. package/src/__tests__/useVideoExportGif.test.ts +0 -155
  32. package/src/__tests__/videoExportProps.test.tsx +0 -148
  33. package/src/__tests__/workerEncoder.test.ts +0 -143
  34. package/src/audioTrack.ts +0 -332
  35. package/src/gifTranscode.ts +0 -75
  36. package/src/hooks/useFrameCapture.ts +0 -268
  37. package/src/hooks/useVideoExport.ts +0 -647
  38. package/src/index.ts +0 -40
  39. package/src/mainThreadEncoder.ts +0 -158
  40. package/src/mp4Mux.ts +0 -123
  41. package/src/workerEncoder.ts +0 -175
  42. package/src/workers/encode.worker.ts +0 -439
  43. package/src/workers/ffmpeg.class-worker.ts +0 -11
  44. package/src/workers/workerTypes.ts +0 -89
@@ -0,0 +1,856 @@
1
+ import {
2
+ EXPORT_AUDIO_CHANNELS,
3
+ EXPORT_AUDIO_SAMPLE_RATE,
4
+ audioBufferToWav,
5
+ createEncoder,
6
+ encodeAacTrack,
7
+ muxAudioWithFfmpegWasm,
8
+ renderAudioTimeline,
9
+ selectAudioTier,
10
+ supportsWebCodecs,
11
+ supportsWebCodecsAac,
12
+ supportsWebCodecsH264
13
+ } from "./chunk-YZN7D4IC.js";
14
+
15
+ // src/hooks/useFrameCapture.ts
16
+ import { createElement } from "react";
17
+ import { createRoot } from "react-dom/client";
18
+ import { useRef, useCallback, useMemo } from "react";
19
+ import { DocPlayer, MediaContext } from "@bendyline/squisq-react";
20
+ import html2canvas from "html2canvas";
21
+ var MIME_MAP = {
22
+ jpg: "image/jpeg",
23
+ jpeg: "image/jpeg",
24
+ png: "image/png",
25
+ gif: "image/gif",
26
+ webp: "image/webp",
27
+ svg: "image/svg+xml",
28
+ bmp: "image/bmp",
29
+ avif: "image/avif"
30
+ };
31
+ function createInlineProvider(images) {
32
+ const blobUrls = /* @__PURE__ */ new Map();
33
+ const mimeTypes = /* @__PURE__ */ new Map();
34
+ for (const [path, buffer] of images) {
35
+ const ext = path.split(".").pop()?.toLowerCase() ?? "";
36
+ const mime = MIME_MAP[ext] ?? "application/octet-stream";
37
+ blobUrls.set(path, URL.createObjectURL(new Blob([buffer], { type: mime })));
38
+ mimeTypes.set(path, mime);
39
+ }
40
+ return {
41
+ async resolveUrl(relativePath) {
42
+ return blobUrls.get(relativePath) ?? relativePath;
43
+ },
44
+ async listMedia() {
45
+ return [...blobUrls.keys()].map((name) => ({
46
+ name,
47
+ mimeType: mimeTypes.get(name) ?? "application/octet-stream",
48
+ size: images.get(name)?.byteLength ?? 0
49
+ }));
50
+ },
51
+ async addMedia() {
52
+ throw new Error("Read-only");
53
+ },
54
+ async removeMedia() {
55
+ throw new Error("Read-only");
56
+ },
57
+ dispose() {
58
+ blobUrls.forEach((url) => URL.revokeObjectURL(url));
59
+ blobUrls.clear();
60
+ }
61
+ };
62
+ }
63
+ function useFrameCapture() {
64
+ const containerRef = useRef(null);
65
+ const rootRef = useRef(null);
66
+ const renderAPIRef = useRef(null);
67
+ const mediaProviderRef = useRef(null);
68
+ const dimensionsRef = useRef({ width: 1920, height: 1080 });
69
+ const init = useCallback(
70
+ async (doc, renderOptions, captionMode) => {
71
+ if (rootRef.current || containerRef.current || mediaProviderRef.current) {
72
+ const oldRoot = rootRef.current;
73
+ const oldContainer = containerRef.current;
74
+ const oldMediaProvider = mediaProviderRef.current;
75
+ rootRef.current = null;
76
+ containerRef.current = null;
77
+ renderAPIRef.current = null;
78
+ mediaProviderRef.current = null;
79
+ await new Promise((resolve) => {
80
+ setTimeout(() => {
81
+ if (oldRoot) oldRoot.unmount();
82
+ if (oldContainer) oldContainer.remove();
83
+ oldMediaProvider?.dispose();
84
+ resolve();
85
+ }, 0);
86
+ });
87
+ }
88
+ const width = renderOptions.width ?? 1920;
89
+ const height = renderOptions.height ?? 1080;
90
+ const animationsEnabled = renderOptions.animationsEnabled ?? true;
91
+ dimensionsRef.current = { width, height };
92
+ const container = document.createElement("div");
93
+ container.style.cssText = `position:fixed;left:0;top:0;width:${width}px;height:${height}px;opacity:0;pointer-events:none;z-index:-1;overflow:hidden;`;
94
+ document.body.appendChild(container);
95
+ containerRef.current = container;
96
+ const renderRoot = document.createElement("div");
97
+ renderRoot.id = "squisq-capture-root";
98
+ renderRoot.style.cssText = `width:${width}px;height:${height}px;`;
99
+ container.appendChild(renderRoot);
100
+ const mediaProvider = renderOptions.images ? createInlineProvider(renderOptions.images) : null;
101
+ mediaProviderRef.current = mediaProvider;
102
+ const root = createRoot(renderRoot);
103
+ rootRef.current = root;
104
+ const captionsEnabled = captionMode !== void 0 && captionMode !== "off";
105
+ const captionStyle = captionMode === "social" ? "social" : "standard";
106
+ let resolveRenderAPI;
107
+ const renderAPIReady = new Promise((resolve) => {
108
+ resolveRenderAPI = resolve;
109
+ });
110
+ const playerElement = createElement(DocPlayer, {
111
+ doc,
112
+ basePath: ".",
113
+ renderMode: true,
114
+ animationsEnabled,
115
+ showControls: false,
116
+ autoPlay: false,
117
+ forceViewport: { width, height, name: "export" },
118
+ captionsEnabled,
119
+ captionStyle,
120
+ onRenderAPIReady: (api) => {
121
+ if (containerRef.current !== container) return;
122
+ renderAPIRef.current = api;
123
+ if (api) resolveRenderAPI(api);
124
+ }
125
+ });
126
+ await new Promise((resolve) => setTimeout(resolve, 0));
127
+ if (mediaProvider) {
128
+ root.render(createElement(MediaContext.Provider, { value: mediaProvider }, playerElement));
129
+ } else {
130
+ root.render(playerElement);
131
+ }
132
+ return new Promise((resolve, reject) => {
133
+ const timeout = setTimeout(() => {
134
+ const api = renderAPIRef.current;
135
+ const hasSeek = typeof api?.seekTo === "function";
136
+ const hasDur = typeof api?.getDuration === "function";
137
+ const rootEl = containerRef.current?.querySelector("#squisq-capture-root");
138
+ const hasPlayer = rootEl ? rootEl.querySelector(".doc-player") !== null : false;
139
+ reject(
140
+ new Error(
141
+ `Render API did not initialize within 15s. seekTo=${hasSeek}, getDuration=${hasDur}, player=${hasPlayer}, root=${!!rootEl}`
142
+ )
143
+ );
144
+ }, 15e3);
145
+ void renderAPIReady.then((api) => {
146
+ clearTimeout(timeout);
147
+ resolve(api.getDuration());
148
+ });
149
+ });
150
+ },
151
+ []
152
+ );
153
+ const captureFrame = useCallback(async (time) => {
154
+ const container = containerRef.current;
155
+ const api = renderAPIRef.current;
156
+ if (!container || !api) {
157
+ throw new Error("Frame capture not initialized \u2014 call init() first");
158
+ }
159
+ const { width, height } = dimensionsRef.current;
160
+ await api.seekTo(time);
161
+ await new Promise(
162
+ (resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve()))
163
+ );
164
+ const root = container.querySelector("#squisq-capture-root");
165
+ if (!root) {
166
+ throw new Error("Capture root element not found");
167
+ }
168
+ const canvas = await html2canvas(root, {
169
+ width,
170
+ height,
171
+ scale: 1,
172
+ useCORS: true,
173
+ allowTaint: true,
174
+ backgroundColor: "#000000",
175
+ logging: false
176
+ });
177
+ const bitmap = await createImageBitmap(canvas);
178
+ return bitmap;
179
+ }, []);
180
+ const destroy = useCallback(() => {
181
+ if (rootRef.current) {
182
+ rootRef.current.unmount();
183
+ rootRef.current = null;
184
+ }
185
+ if (containerRef.current) {
186
+ containerRef.current.remove();
187
+ containerRef.current = null;
188
+ }
189
+ mediaProviderRef.current?.dispose();
190
+ mediaProviderRef.current = null;
191
+ renderAPIRef.current = null;
192
+ }, []);
193
+ return useMemo(() => ({ init, captureFrame, destroy }), [init, captureFrame, destroy]);
194
+ }
195
+
196
+ // src/hooks/useVideoExport.ts
197
+ import { useState, useRef as useRef2, useCallback as useCallback2, useEffect } from "react";
198
+ import {
199
+ DEFAULT_INTERACTIVE_RESOURCE_POLICY,
200
+ fetchResourceBytes
201
+ } from "@bendyline/squisq/markdown";
202
+ import {
203
+ resolveDimensions,
204
+ computeAudioTimeline,
205
+ resolveFfmpegWasmLoad as resolveFfmpegWasmLoad2,
206
+ QUALITY_PRESETS
207
+ } from "@bendyline/squisq-video";
208
+
209
+ // src/workerEncoder.ts
210
+ import { validateVideoExportOptions } from "@bendyline/squisq-video";
211
+ function createWorkerEncoder(config) {
212
+ validateVideoExportOptions(config);
213
+ const worker = new Worker(new URL("./workers/encode.worker.js", import.meta.url), {
214
+ type: "module"
215
+ });
216
+ let state = "open";
217
+ let fatalError = null;
218
+ let finalizeResolve = null;
219
+ let finalizeReject = null;
220
+ let readyResolve = null;
221
+ let readyReject = null;
222
+ let readySettled = false;
223
+ const frameWaiters = /* @__PURE__ */ new Map();
224
+ const ready = new Promise((resolve, reject) => {
225
+ readyResolve = resolve;
226
+ readyReject = reject;
227
+ });
228
+ const frameDuration = 1e6 / config.fps;
229
+ function post(msg, transfer) {
230
+ worker.postMessage(msg, transfer ?? []);
231
+ }
232
+ const currentState = () => state;
233
+ worker.onmessage = (event) => {
234
+ const msg = event.data;
235
+ switch (msg.type) {
236
+ case "capabilities":
237
+ readySettled = true;
238
+ readyResolve?.(msg.backend);
239
+ readyResolve = readyReject = null;
240
+ break;
241
+ case "frame-complete": {
242
+ const waiter = frameWaiters.get(msg.frameIndex);
243
+ waiter?.resolve();
244
+ frameWaiters.delete(msg.frameIndex);
245
+ break;
246
+ }
247
+ case "complete":
248
+ state = "closed";
249
+ finalizeResolve?.(msg.data);
250
+ finalizeResolve = finalizeReject = null;
251
+ worker.terminate();
252
+ break;
253
+ case "error": {
254
+ const err = new Error(msg.message);
255
+ fatalError = err;
256
+ state = "closed";
257
+ readySettled = true;
258
+ readyReject?.(err);
259
+ finalizeReject?.(err);
260
+ for (const waiter of frameWaiters.values()) waiter.reject(err);
261
+ frameWaiters.clear();
262
+ readyResolve = readyReject = null;
263
+ finalizeResolve = finalizeReject = null;
264
+ worker.terminate();
265
+ break;
266
+ }
267
+ }
268
+ };
269
+ worker.onerror = (event) => {
270
+ const err = new Error(event.message || "Worker error");
271
+ fatalError = err;
272
+ state = "closed";
273
+ readySettled = true;
274
+ readyReject?.(err);
275
+ finalizeReject?.(err);
276
+ for (const waiter of frameWaiters.values()) waiter.reject(err);
277
+ frameWaiters.clear();
278
+ readyResolve = readyReject = null;
279
+ finalizeResolve = finalizeReject = null;
280
+ worker.terminate();
281
+ };
282
+ post({
283
+ type: "init",
284
+ width: config.width,
285
+ height: config.height,
286
+ fps: config.fps,
287
+ quality: config.quality,
288
+ ...config.totalFrames !== void 0 ? { totalFrames: config.totalFrames } : {},
289
+ ...config.ffmpegWasm ? { ffmpegWasm: config.ffmpegWasm } : {}
290
+ });
291
+ return {
292
+ ready,
293
+ encodeFrame(bitmap, frameIndex) {
294
+ if (state !== "open" || fatalError) {
295
+ bitmap.close();
296
+ return Promise.reject(fatalError ?? new Error("Encoder is not accepting frames"));
297
+ }
298
+ if (frameWaiters.has(frameIndex)) {
299
+ bitmap.close();
300
+ return Promise.reject(new Error(`Frame ${frameIndex} was submitted more than once`));
301
+ }
302
+ let resolveFrame;
303
+ let rejectFrame;
304
+ const promise = new Promise((resolve, reject) => {
305
+ resolveFrame = resolve;
306
+ rejectFrame = reject;
307
+ });
308
+ frameWaiters.set(frameIndex, { promise, resolve: resolveFrame, reject: rejectFrame });
309
+ const timestamp = Math.round(frameIndex * frameDuration);
310
+ post({ type: "frame", bitmap, frameIndex, timestamp }, [bitmap]);
311
+ return promise;
312
+ },
313
+ async finalize() {
314
+ if (state !== "open") throw new Error("Encoder already closed or finalizing");
315
+ if (fatalError) throw fatalError;
316
+ state = "finalizing";
317
+ await Promise.all(Array.from(frameWaiters.values(), (waiter) => waiter.promise));
318
+ if (currentState() === "closed") {
319
+ throw fatalError ?? new Error("Encoder closed during finalization");
320
+ }
321
+ return new Promise((resolve, reject) => {
322
+ finalizeResolve = resolve;
323
+ finalizeReject = reject;
324
+ post({ type: "finalize" });
325
+ });
326
+ },
327
+ close() {
328
+ if (state === "closed") return;
329
+ state = "closed";
330
+ const err = new Error("Encoder closed");
331
+ if (!readySettled) {
332
+ readySettled = true;
333
+ readyReject?.(err);
334
+ }
335
+ finalizeReject?.(err);
336
+ for (const waiter of frameWaiters.values()) waiter.reject(err);
337
+ frameWaiters.clear();
338
+ readyResolve = readyReject = null;
339
+ finalizeResolve = finalizeReject = null;
340
+ post({ type: "cancel" });
341
+ worker.terminate();
342
+ }
343
+ };
344
+ }
345
+
346
+ // src/gifTranscode.ts
347
+ import {
348
+ ffmpegGifOutputArgs,
349
+ resolveFfmpegWasmLoad
350
+ } from "@bendyline/squisq-video";
351
+ function buildGifFfmpegArgs(options) {
352
+ return ["-y", "-i", "video.mp4", ...ffmpegGifOutputArgs(options), "out.gif"];
353
+ }
354
+ async function transcodeMp4ToGifWithFfmpegWasm(videoMp4, options, loadConfig, signal) {
355
+ if (videoMp4.byteLength === 0) {
356
+ throw new Error("Cannot create an animated GIF from an empty MP4.");
357
+ }
358
+ if (signal?.aborted) {
359
+ throw new DOMException("Animated GIF export was cancelled.", "AbortError");
360
+ }
361
+ const load = resolveFfmpegWasmLoad(loadConfig, "Animated GIF export", {
362
+ classWorkerURL: new URL("./workers/ffmpeg.class-worker.js", import.meta.url).href
363
+ });
364
+ const args = buildGifFfmpegArgs(options);
365
+ const { FFmpeg } = await import("@ffmpeg/ffmpeg");
366
+ const ffmpeg = new FFmpeg();
367
+ let terminated = false;
368
+ const terminate = () => {
369
+ if (terminated) return;
370
+ terminated = true;
371
+ ffmpeg.terminate();
372
+ };
373
+ const handleAbort = () => terminate();
374
+ signal?.addEventListener("abort", handleAbort, { once: true });
375
+ try {
376
+ await ffmpeg.load(load);
377
+ if (signal?.aborted) {
378
+ throw new DOMException("Animated GIF export was cancelled.", "AbortError");
379
+ }
380
+ await ffmpeg.writeFile("video.mp4", videoMp4);
381
+ if (signal?.aborted) {
382
+ throw new DOMException("Animated GIF export was cancelled.", "AbortError");
383
+ }
384
+ const exitCode = await ffmpeg.exec(args);
385
+ if (signal?.aborted) {
386
+ throw new DOMException("Animated GIF export was cancelled.", "AbortError");
387
+ }
388
+ if (exitCode !== 0) {
389
+ throw new Error(`ffmpeg.wasm GIF transcode failed with exit code ${exitCode}`);
390
+ }
391
+ const data = await ffmpeg.readFile("out.gif");
392
+ return data instanceof Uint8Array ? data : new TextEncoder().encode(data);
393
+ } finally {
394
+ signal?.removeEventListener("abort", handleAbort);
395
+ terminate();
396
+ }
397
+ }
398
+
399
+ // src/hooks/useVideoExport.ts
400
+ var MAX_EXPORT_MEDIA_FILES = 256;
401
+ var MAX_EXPORT_MEDIA_FILE_BYTES = 64 * 1024 * 1024;
402
+ var MAX_EXPORT_MEDIA_TOTAL_BYTES = 256 * 1024 * 1024;
403
+ function toArrayBuffer(bytes) {
404
+ return bytes.slice().buffer;
405
+ }
406
+ function collectDocumentMediaReferences(doc) {
407
+ const references = /* @__PURE__ */ new Set();
408
+ const seen = /* @__PURE__ */ new WeakSet();
409
+ const visit = (value) => {
410
+ if (typeof value === "string") {
411
+ references.add(value);
412
+ if (value.startsWith("./")) references.add(value.slice(2));
413
+ return;
414
+ }
415
+ if (!value || typeof value !== "object" || seen.has(value)) return;
416
+ seen.add(value);
417
+ if (Array.isArray(value)) {
418
+ value.forEach(visit);
419
+ return;
420
+ }
421
+ Object.values(value).forEach(visit);
422
+ };
423
+ visit(doc);
424
+ return references;
425
+ }
426
+ async function resolveAudioBuffers(clips, sources) {
427
+ const srcs = new Set(clips.map((c) => c.src));
428
+ const out = /* @__PURE__ */ new Map();
429
+ for (const src of srcs) {
430
+ let data = sources.audio?.get(src) ?? sources.images?.get(src);
431
+ if (!data && sources.mediaProvider) {
432
+ try {
433
+ const url = await sources.mediaProvider.resolveUrl(src);
434
+ const resource = await fetchResourceBytes(url, {
435
+ policy: sources.resourcePolicy
436
+ });
437
+ data = toArrayBuffer(resource.bytes);
438
+ } catch {
439
+ }
440
+ }
441
+ if (data) out.set(src, data);
442
+ }
443
+ return out;
444
+ }
445
+ function useVideoExport() {
446
+ const [state, setState] = useState("idle");
447
+ const [progress, setProgress] = useState(0);
448
+ const [phase, setPhase] = useState("");
449
+ const [duration, setDuration] = useState(0);
450
+ const [outputFormat, setOutputFormat] = useState("mp4");
451
+ const [backend, setBackend] = useState(null);
452
+ const [downloadUrl, setDownloadUrl] = useState(null);
453
+ const [fileSize, setFileSize] = useState(0);
454
+ const [audioIncluded, setAudioIncluded] = useState(false);
455
+ const [audioSkippedReason, setAudioSkippedReason] = useState(null);
456
+ const [error, setError] = useState(null);
457
+ const [elapsed, setElapsed] = useState(0);
458
+ const [estimatedRemaining, setEstimatedRemaining] = useState(0);
459
+ const encoderRef = useRef2(null);
460
+ const gifAbortRef = useRef2(null);
461
+ const cancelledRef = useRef2(false);
462
+ const downloadUrlRef = useRef2(null);
463
+ const startTimeRef = useRef2(0);
464
+ const elapsedTimerRef = useRef2(null);
465
+ const frameCapture = useFrameCapture();
466
+ useEffect(() => {
467
+ return () => {
468
+ if (elapsedTimerRef.current) clearInterval(elapsedTimerRef.current);
469
+ if (downloadUrlRef.current) {
470
+ URL.revokeObjectURL(downloadUrlRef.current);
471
+ }
472
+ if (encoderRef.current) {
473
+ encoderRef.current.close();
474
+ }
475
+ gifAbortRef.current?.abort();
476
+ frameCapture.destroy();
477
+ };
478
+ }, [frameCapture]);
479
+ const reset = useCallback2(() => {
480
+ if (downloadUrlRef.current) {
481
+ URL.revokeObjectURL(downloadUrlRef.current);
482
+ downloadUrlRef.current = null;
483
+ }
484
+ if (encoderRef.current) {
485
+ encoderRef.current.close();
486
+ encoderRef.current = null;
487
+ }
488
+ gifAbortRef.current?.abort();
489
+ gifAbortRef.current = null;
490
+ frameCapture.destroy();
491
+ setState("idle");
492
+ setProgress(0);
493
+ setPhase("");
494
+ setDuration(0);
495
+ setOutputFormat("mp4");
496
+ setBackend(null);
497
+ setDownloadUrl(null);
498
+ setFileSize(0);
499
+ setAudioIncluded(false);
500
+ setAudioSkippedReason(null);
501
+ setError(null);
502
+ setElapsed(0);
503
+ setEstimatedRemaining(0);
504
+ if (elapsedTimerRef.current) clearInterval(elapsedTimerRef.current);
505
+ cancelledRef.current = false;
506
+ }, [frameCapture]);
507
+ const cancel = useCallback2(() => {
508
+ cancelledRef.current = true;
509
+ if (elapsedTimerRef.current) clearInterval(elapsedTimerRef.current);
510
+ if (encoderRef.current) {
511
+ encoderRef.current.close();
512
+ encoderRef.current = null;
513
+ }
514
+ gifAbortRef.current?.abort();
515
+ gifAbortRef.current = null;
516
+ frameCapture.destroy();
517
+ setState("idle");
518
+ setProgress(0);
519
+ setPhase("Cancelled");
520
+ }, [frameCapture]);
521
+ const startExport = useCallback2(
522
+ async (doc, config) => {
523
+ cancelledRef.current = false;
524
+ if (downloadUrlRef.current) {
525
+ URL.revokeObjectURL(downloadUrlRef.current);
526
+ downloadUrlRef.current = null;
527
+ }
528
+ setDownloadUrl(null);
529
+ setFileSize(0);
530
+ setAudioIncluded(false);
531
+ setAudioSkippedReason(null);
532
+ setError(null);
533
+ const quality = config.quality ?? "normal";
534
+ const effectiveOutputFormat = config.outputFormat ?? "mp4";
535
+ const fps = config.fps ?? (effectiveOutputFormat === "gif" ? 10 : 30);
536
+ const orientation = config.orientation ?? "landscape";
537
+ const animationsEnabled = config.animationsEnabled ?? effectiveOutputFormat === "mp4";
538
+ const audioPolicy = config.audioPolicy ?? "require";
539
+ setOutputFormat(effectiveOutputFormat);
540
+ try {
541
+ const gifDefaults = orientation === "portrait" ? { width: 540, height: 960 } : { width: 960, height: 540 };
542
+ const { width, height } = resolveDimensions({
543
+ orientation,
544
+ fps,
545
+ quality,
546
+ ...config.width !== void 0 ? { width: config.width } : effectiveOutputFormat === "gif" ? { width: gifDefaults.width } : {},
547
+ ...config.height !== void 0 ? { height: config.height } : effectiveOutputFormat === "gif" ? { height: gifDefaults.height } : {}
548
+ });
549
+ const webCodecsAvailable = supportsWebCodecs();
550
+ const sharedArrayBufferAvailable = typeof SharedArrayBuffer !== "undefined";
551
+ if (effectiveOutputFormat === "gif" && !sharedArrayBufferAvailable) {
552
+ throw new Error(
553
+ "Animated GIF export requires ffmpeg.wasm and SharedArrayBuffer (Cross-Origin-Isolation headers)."
554
+ );
555
+ }
556
+ if (effectiveOutputFormat === "gif") {
557
+ resolveFfmpegWasmLoad2(config.ffmpegWasm, "Animated GIF export");
558
+ }
559
+ if (!webCodecsAvailable && !sharedArrayBufferAvailable) {
560
+ throw new Error(
561
+ "No video encoder available. WebCodecs requires Chrome 94+ / Edge 94+, and the ffmpeg.wasm fallback requires SharedArrayBuffer (Cross-Origin-Isolation headers)."
562
+ );
563
+ }
564
+ setState("preparing");
565
+ setPhase("Loading document\u2026");
566
+ setProgress(0);
567
+ setElapsed(0);
568
+ setEstimatedRemaining(0);
569
+ startTimeRef.current = performance.now();
570
+ if (elapsedTimerRef.current) clearInterval(elapsedTimerRef.current);
571
+ elapsedTimerRef.current = setInterval(() => {
572
+ setElapsed(Math.floor((performance.now() - startTimeRef.current) / 1e3));
573
+ }, 1e3);
574
+ let images = config.images;
575
+ if (!images && config.mediaProvider) {
576
+ images = /* @__PURE__ */ new Map();
577
+ const entries = await config.mediaProvider.listMedia();
578
+ const references = collectDocumentMediaReferences(doc);
579
+ const neededEntries = entries.filter(
580
+ (entry) => references.has(entry.name) || references.has(`./${entry.name}`)
581
+ );
582
+ if (neededEntries.length > MAX_EXPORT_MEDIA_FILES) {
583
+ throw new Error(
584
+ `Document references ${neededEntries.length} media files; browser export supports at most ${MAX_EXPORT_MEDIA_FILES}.`
585
+ );
586
+ }
587
+ let totalMediaBytes = 0;
588
+ for (const entry of neededEntries) {
589
+ if (cancelledRef.current) return;
590
+ if (entry.size > MAX_EXPORT_MEDIA_FILE_BYTES) {
591
+ throw new Error(`Media file "${entry.name}" is too large for browser video export.`);
592
+ }
593
+ const url2 = await config.mediaProvider.resolveUrl(entry.name);
594
+ const resource = await fetchResourceBytes(url2, {
595
+ policy: {
596
+ ...DEFAULT_INTERACTIVE_RESOURCE_POLICY,
597
+ ...config.resourcePolicy,
598
+ maxBytes: Math.min(
599
+ config.resourcePolicy?.maxBytes ?? MAX_EXPORT_MEDIA_FILE_BYTES,
600
+ MAX_EXPORT_MEDIA_FILE_BYTES
601
+ )
602
+ }
603
+ });
604
+ const data = toArrayBuffer(resource.bytes);
605
+ totalMediaBytes += data.byteLength;
606
+ if (totalMediaBytes > MAX_EXPORT_MEDIA_TOTAL_BYTES) {
607
+ throw new Error("Referenced media exceeds the browser video export memory limit.");
608
+ }
609
+ images.set(entry.name, data);
610
+ }
611
+ }
612
+ const docDuration = await frameCapture.init(
613
+ doc,
614
+ { images, audio: config.audio, width, height, animationsEnabled },
615
+ config.captionMode
616
+ );
617
+ if (cancelledRef.current) return;
618
+ setDuration(docDuration);
619
+ if (docDuration <= 0) {
620
+ throw new Error("Document has zero duration \u2014 nothing to export");
621
+ }
622
+ const totalFrames = Math.ceil(docDuration * fps);
623
+ setPhase("Starting encoder\u2026");
624
+ setProgress(5);
625
+ const canUseWebCodecs = webCodecsAvailable && await supportsWebCodecsH264({ width, height, fps, quality });
626
+ const audioBitrate = (QUALITY_PRESETS[quality] ?? QUALITY_PRESETS.normal).audioBitrate;
627
+ const timeline = effectiveOutputFormat === "mp4" && audioPolicy !== "omit" ? computeAudioTimeline(doc, 0) : [];
628
+ const aacSupported = timeline.length > 0 ? await supportsWebCodecsAac(EXPORT_AUDIO_SAMPLE_RATE, EXPORT_AUDIO_CHANNELS) : false;
629
+ const tierDecision = selectAudioTier({
630
+ hasClips: timeline.length > 0,
631
+ aacSupported,
632
+ sharedArrayBufferAvailable,
633
+ canUseMainThreadWebCodecs: canUseWebCodecs
634
+ });
635
+ let renderedAudio = null;
636
+ let audioIncludedLocal = false;
637
+ let audioReasonLocal = tierDecision.reason;
638
+ if (timeline.length > 0 && tierDecision.tier === 3 && audioPolicy === "require") {
639
+ throw new Error(tierDecision.reason ?? "This browser cannot include the document audio.");
640
+ }
641
+ if (tierDecision.tier === 1 || tierDecision.tier === 2) {
642
+ setPhase("Preparing audio\u2026");
643
+ try {
644
+ const buffers = await resolveAudioBuffers(timeline, {
645
+ audio: config.audio,
646
+ images,
647
+ mediaProvider: config.mediaProvider,
648
+ resourcePolicy: config.resourcePolicy
649
+ });
650
+ const missingSources = [...new Set(timeline.map((clip) => clip.src))].filter(
651
+ (src) => !buffers.has(src)
652
+ );
653
+ if (missingSources.length > 0) {
654
+ audioReasonLocal = `Audio files could not be loaded: ${missingSources.join(", ")}`;
655
+ if (audioPolicy === "require") throw new Error(audioReasonLocal);
656
+ }
657
+ if (buffers.size === 0) {
658
+ audioReasonLocal ?? (audioReasonLocal = "Audio files for this document could not be loaded.");
659
+ } else {
660
+ const totalAudioDur = timeline.reduce(
661
+ (max, c) => Math.max(max, c.startSec + c.durationSec),
662
+ docDuration
663
+ );
664
+ renderedAudio = await renderAudioTimeline(
665
+ timeline,
666
+ buffers,
667
+ totalAudioDur,
668
+ EXPORT_AUDIO_SAMPLE_RATE
669
+ );
670
+ }
671
+ } catch (audioErr) {
672
+ renderedAudio = null;
673
+ audioReasonLocal = `Audio could not be prepared: ${audioErr instanceof Error ? audioErr.message : String(audioErr)}`;
674
+ if (audioPolicy === "require") throw new Error(audioReasonLocal);
675
+ }
676
+ }
677
+ const useInlineAudio = renderedAudio !== null && tierDecision.tier === 1;
678
+ const useFfmpegAudio = renderedAudio !== null && tierDecision.tier === 2;
679
+ if (cancelledRef.current) return;
680
+ let encoder;
681
+ if (canUseWebCodecs) {
682
+ encoder = createEncoder({
683
+ width,
684
+ height,
685
+ fps,
686
+ quality,
687
+ ...useInlineAudio && renderedAudio ? {
688
+ audio: {
689
+ numberOfChannels: renderedAudio.numberOfChannels,
690
+ sampleRate: renderedAudio.sampleRate
691
+ }
692
+ } : {}
693
+ });
694
+ setBackend("webcodecs");
695
+ } else if (sharedArrayBufferAvailable) {
696
+ const workerEncoder = createWorkerEncoder({
697
+ width,
698
+ height,
699
+ fps,
700
+ quality,
701
+ totalFrames,
702
+ ffmpegWasm: config.ffmpegWasm
703
+ });
704
+ encoder = workerEncoder;
705
+ const selectedBackend = await workerEncoder.ready;
706
+ setBackend(selectedBackend);
707
+ setPhase(
708
+ selectedBackend === "ffmpeg-wasm" ? "Starting encoder (ffmpeg.wasm)\u2026" : "Starting encoder\u2026"
709
+ );
710
+ } else {
711
+ throw new Error(
712
+ "WebCodecs H.264 is unavailable in this browser and the ffmpeg.wasm fallback requires SharedArrayBuffer (Cross-Origin-Isolation headers)."
713
+ );
714
+ }
715
+ encoderRef.current = encoder;
716
+ if (cancelledRef.current) return;
717
+ setState("capturing");
718
+ const captureStartTime = performance.now();
719
+ const UI_UPDATE_INTERVAL = 10;
720
+ for (let i = 0; i < totalFrames; i++) {
721
+ if (cancelledRef.current) return;
722
+ const time = i / fps;
723
+ if (i % UI_UPDATE_INTERVAL === 0 || i === totalFrames - 1) {
724
+ const captureProgress = Math.round(i / totalFrames * 90);
725
+ setProgress(5 + captureProgress);
726
+ setPhase(`Capturing frame ${i + 1}/${totalFrames} (${time.toFixed(1)}s)`);
727
+ if (i > 0) {
728
+ const elapsedCapture = (performance.now() - captureStartTime) / 1e3;
729
+ const avgPerFrame = elapsedCapture / i;
730
+ const remaining = Math.round(avgPerFrame * (totalFrames - i));
731
+ setEstimatedRemaining(remaining);
732
+ }
733
+ }
734
+ const bitmap = await frameCapture.captureFrame(time);
735
+ if (cancelledRef.current) {
736
+ bitmap.close();
737
+ return;
738
+ }
739
+ await encoder.encodeFrame(bitmap, i);
740
+ }
741
+ if (cancelledRef.current) return;
742
+ if (useInlineAudio && renderedAudio && encoder.addAudioChunk) {
743
+ setState("encoding");
744
+ setPhase("Encoding audio\u2026");
745
+ try {
746
+ await encodeAacTrack(
747
+ renderedAudio,
748
+ { addAudioChunk: encoder.addAudioChunk.bind(encoder) },
749
+ audioBitrate
750
+ );
751
+ audioIncludedLocal = true;
752
+ } catch (audioErr) {
753
+ audioIncludedLocal = false;
754
+ audioReasonLocal = `Audio encoding failed: ${audioErr instanceof Error ? audioErr.message : String(audioErr)}`;
755
+ if (audioPolicy === "require") throw new Error(audioReasonLocal);
756
+ }
757
+ }
758
+ setState("encoding");
759
+ setPhase(effectiveOutputFormat === "gif" ? "Finalizing GIF frames\u2026" : "Finalizing video\u2026");
760
+ setProgress(95);
761
+ let outputBytes = await encoder.finalize();
762
+ encoderRef.current = null;
763
+ if (cancelledRef.current) return;
764
+ if (effectiveOutputFormat === "gif") {
765
+ setPhase("Generating GIF palette\u2026");
766
+ const videoOnly = outputBytes instanceof Uint8Array ? outputBytes : new Uint8Array(outputBytes);
767
+ const gifAbort = new AbortController();
768
+ gifAbortRef.current = gifAbort;
769
+ try {
770
+ outputBytes = await transcodeMp4ToGifWithFfmpegWasm(
771
+ videoOnly,
772
+ { width, height, loop: 0 },
773
+ config.ffmpegWasm,
774
+ gifAbort.signal
775
+ );
776
+ } finally {
777
+ if (gifAbortRef.current === gifAbort) gifAbortRef.current = null;
778
+ }
779
+ } else if (useFfmpegAudio && renderedAudio) {
780
+ setPhase("Muxing audio\u2026");
781
+ try {
782
+ const wav = audioBufferToWav(renderedAudio);
783
+ const videoOnly = outputBytes instanceof Uint8Array ? outputBytes : new Uint8Array(outputBytes);
784
+ outputBytes = await muxAudioWithFfmpegWasm(
785
+ videoOnly,
786
+ wav,
787
+ audioBitrate,
788
+ config.ffmpegWasm
789
+ );
790
+ audioIncludedLocal = true;
791
+ } catch (audioErr) {
792
+ audioIncludedLocal = false;
793
+ audioReasonLocal = `Audio muxing failed: ${audioErr instanceof Error ? audioErr.message : String(audioErr)}`;
794
+ if (audioPolicy === "require") throw new Error(audioReasonLocal);
795
+ }
796
+ }
797
+ if (cancelledRef.current) return;
798
+ const finalBytes = outputBytes instanceof Uint8Array ? outputBytes.slice() : new Uint8Array(outputBytes);
799
+ const mimeType = effectiveOutputFormat === "gif" ? "image/gif" : "video/mp4";
800
+ const blob = new Blob([finalBytes], { type: mimeType });
801
+ const url = URL.createObjectURL(blob);
802
+ downloadUrlRef.current = url;
803
+ setDownloadUrl(url);
804
+ setFileSize(finalBytes.byteLength);
805
+ setAudioIncluded(audioIncludedLocal);
806
+ setAudioSkippedReason(
807
+ effectiveOutputFormat === "gif" || audioIncludedLocal ? null : audioReasonLocal
808
+ );
809
+ setState("complete");
810
+ setProgress(100);
811
+ setPhase("Export complete");
812
+ setEstimatedRemaining(0);
813
+ if (elapsedTimerRef.current) clearInterval(elapsedTimerRef.current);
814
+ frameCapture.destroy();
815
+ } catch (err) {
816
+ if (elapsedTimerRef.current) clearInterval(elapsedTimerRef.current);
817
+ if (cancelledRef.current) return;
818
+ const message = err instanceof Error ? err.message : String(err);
819
+ setState("error");
820
+ setError(message);
821
+ setPhase("Export failed");
822
+ if (encoderRef.current) {
823
+ encoderRef.current.close();
824
+ encoderRef.current = null;
825
+ }
826
+ gifAbortRef.current?.abort();
827
+ gifAbortRef.current = null;
828
+ frameCapture.destroy();
829
+ }
830
+ },
831
+ [frameCapture]
832
+ );
833
+ return {
834
+ state,
835
+ progress,
836
+ phase,
837
+ duration,
838
+ outputFormat,
839
+ backend,
840
+ downloadUrl,
841
+ fileSize,
842
+ audioIncluded,
843
+ audioSkippedReason,
844
+ error,
845
+ elapsed,
846
+ estimatedRemaining,
847
+ startExport,
848
+ cancel,
849
+ reset
850
+ };
851
+ }
852
+
853
+ export {
854
+ useFrameCapture,
855
+ useVideoExport
856
+ };