@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
package/dist/index.d.ts CHANGED
@@ -1,278 +1,9 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import { MediaProvider, Doc } from '@bendyline/squisq/schemas';
3
- import { VideoQuality, VideoOrientation, FfmpegWasmLoadConfig, RenderHtmlOptions } from '@bendyline/squisq-video';
1
+ export { VideoExportButton, VideoExportButtonProps, VideoExportModal, VideoExportModalProps, VideoExportPalette } from './components/index.js';
2
+ export { V as VideoAudioPolicy, a as VideoExportConfig, b as VideoExportResult, c as VideoExportState, d as VideoOutputFormat, u as useVideoExport } from './useVideoExport-NtqZVgaz.js';
3
+ export { FrameCaptureHandle, useFrameCapture } from './hooks/index.js';
4
+ export { EncoderConfig, MainThreadEncoder, createEncoder, supportsWebCodecs, supportsWebCodecsAac, supportsWebCodecsH264 } from './encoder/index.js';
4
5
  export { FfmpegWasmLoadConfig } from '@bendyline/squisq-video';
5
- import { CaptionMode } from '@bendyline/squisq-react';
6
-
7
- /**
8
- * useVideoExport — Main orchestration hook for browser video export.
9
- *
10
- * Coordinates frame capture (hidden iframe + html2canvas) with
11
- * main-thread WebCodecs encoding via mp4-muxer. Manages the full
12
- * lifecycle: prepare → capture + encode → download.
13
- *
14
- * Encoding runs on the main thread because frame capture via html2canvas
15
- * (~100-200ms per frame) is the bottleneck, not encoding (~1ms per frame
16
- * with hardware-accelerated WebCodecs). Worker offloading would add
17
- * complexity with minimal benefit.
18
- *
19
- * Usage:
20
- * const { state, progress, phase, startExport, cancel, downloadUrl } = useVideoExport();
21
- * <button onClick={() => startExport(doc, options)}>Export</button>
22
- */
23
-
24
- type VideoExportState = 'idle' | 'preparing' | 'capturing' | 'encoding' | 'complete' | 'error';
25
- /** Browser export container format. */
26
- type VideoOutputFormat = 'mp4' | 'gif';
27
- interface VideoExportConfig {
28
- /** Output container (default: 'mp4') */
29
- outputFormat?: VideoOutputFormat;
30
- /** Render authored animations and slide transitions (default: true for MP4, false for GIF). */
31
- animationsEnabled?: boolean;
32
- /** Encoding quality preset (default: 'normal') */
33
- quality?: VideoQuality;
34
- /** Frames per second (default: 30) */
35
- fps?: number;
36
- /** Viewport orientation (default: 'landscape') */
37
- orientation?: VideoOrientation;
38
- /** Explicit output width. GIF defaults to 960 landscape / 540 portrait. */
39
- width?: number;
40
- /** Explicit output height. GIF defaults to 540 landscape / 960 portrait. */
41
- height?: number;
42
- /**
43
- * Map of relative image paths to binary data.
44
- * Used to embed images into the render HTML.
45
- */
46
- images?: Map<string, ArrayBuffer>;
47
- /**
48
- * Map of audio segment names to binary data.
49
- * Used to embed audio into the render HTML.
50
- */
51
- audio?: Map<string, ArrayBuffer>;
52
- /** MediaProvider to resolve media URLs (alternative to passing images directly) */
53
- mediaProvider?: MediaProvider;
54
- /** Caption mode for the exported video (default: 'off') */
55
- captionMode?: CaptionMode;
56
- /** Player IIFE bundle (unused in browser export, kept for CLI/Playwright path) */
57
- playerScript?: string;
58
- /** Optional self-hosted ffmpeg.wasm core URLs for fallback/offline/CSP use. */
59
- ffmpegWasm?: FfmpegWasmLoadConfig;
60
- }
61
- interface VideoExportResult {
62
- /** Current export state */
63
- state: VideoExportState;
64
- /** 0–100 progress percentage */
65
- progress: number;
66
- /** Human-readable description of the current phase */
67
- phase: string;
68
- /** Video duration detected from the doc (seconds) */
69
- duration: number;
70
- /** Effective output format for the current or most recent export. */
71
- outputFormat: VideoOutputFormat;
72
- /** Encoder backend ('webcodecs' when WebCodecs H.264 active, 'ffmpeg-wasm' when worker fallback active, null when idle) */
73
- backend: 'webcodecs' | 'ffmpeg-wasm' | null;
74
- /** Blob download URL (populated when state === 'complete') */
75
- downloadUrl: string | null;
76
- /** File size in bytes (populated when state === 'complete') */
77
- fileSize: number;
78
- /**
79
- * Whether an audio track was muxed into the exported MP4 (populated when
80
- * state === 'complete'). False when the doc had no audio or when audio was
81
- * skipped/degraded — see {@link audioSkippedReason}.
82
- */
83
- audioIncluded: boolean;
84
- /**
85
- * Why audio is absent, when `audioIncluded` is false. `null` means the doc
86
- * simply had no audio (not a failure); a non-null string explains a genuine
87
- * capability shortfall or runtime failure. Audio problems never fail the
88
- * export — the video always completes.
89
- */
90
- audioSkippedReason: string | null;
91
- /** Error message (populated when state === 'error') */
92
- error: string | null;
93
- /** Seconds elapsed since export started */
94
- elapsed: number;
95
- /** Estimated seconds remaining (0 when idle or complete) */
96
- estimatedRemaining: number;
97
- /** Start a new export */
98
- startExport: (doc: Doc, config: VideoExportConfig) => Promise<void>;
99
- /** Cancel an in-progress export */
100
- cancel: () => void;
101
- /** Reset state back to idle (e.g., after complete or error) */
102
- reset: () => void;
103
- }
104
- declare function useVideoExport(): VideoExportResult;
105
-
106
- interface VideoExportModalProps {
107
- /** The document to export */
108
- doc: Doc;
109
- /**
110
- * Player IIFE bundle source. Unused by the browser export path (frames
111
- * are captured from a live in-page DocPlayer); only forwarded for
112
- * CLI/Playwright-style pipelines that render standalone HTML.
113
- */
114
- playerScript?: string;
115
- /** Optional media provider for resolving images/audio */
116
- mediaProvider?: MediaProvider;
117
- /** Pre-collected images map (alternative to mediaProvider) */
118
- images?: Map<string, ArrayBuffer>;
119
- /** Pre-collected audio map */
120
- audio?: Map<string, ArrayBuffer>;
121
- /**
122
- * Seeds the modal's initial format, motion, quality, FPS, orientation, and
123
- * caption selections. It is merged (as a base) into the config passed to the
124
- * export hook, so a host can share one config shape with `useVideoExport`.
125
- * The individual `images`/`audio`/`mediaProvider`/`playerScript` props still
126
- * take precedence over any matching key here.
127
- */
128
- defaultConfig?: Partial<VideoExportConfig>;
129
- /** Visual color scheme for the portaled dialog. Defaults to light. */
130
- colorScheme?: 'light' | 'dark';
131
- /** Called when the modal should close */
132
- onClose: () => void;
133
- }
134
- declare function VideoExportModal({ doc, playerScript, mediaProvider, images, audio, defaultConfig, colorScheme, onClose, }: VideoExportModalProps): react_jsx_runtime.JSX.Element;
135
-
136
- interface VideoExportButtonProps {
137
- /** The document to export */
138
- doc: Doc;
139
- /**
140
- * Player IIFE bundle source. Unused by the browser export path (frames
141
- * are captured from a live in-page DocPlayer); only forwarded for
142
- * CLI/Playwright-style pipelines that render standalone HTML.
143
- */
144
- playerScript?: string;
145
- /** Optional media provider for resolving images/audio */
146
- mediaProvider?: MediaProvider;
147
- /** Pre-collected images map */
148
- images?: Map<string, ArrayBuffer>;
149
- /** Pre-collected audio map */
150
- audio?: Map<string, ArrayBuffer>;
151
- /**
152
- * Seeds the modal's initial output format and export settings and is merged
153
- * as a base into the config passed to the export hook. Forwarded to
154
- * {@link VideoExportModal}.
155
- */
156
- defaultConfig?: Partial<VideoExportConfig>;
157
- /** Visual color scheme forwarded to the portaled modal. Defaults to light. */
158
- colorScheme?: 'light' | 'dark';
159
- /** Button label (defaults to "Export Video", or "Export GIF" for a GIF default config) */
160
- label?: string;
161
- /** Additional inline styles for the button */
162
- style?: React.CSSProperties;
163
- /** Whether the button is disabled */
164
- disabled?: boolean;
165
- }
166
- declare function VideoExportButton({ doc, playerScript, mediaProvider, images, audio, defaultConfig, colorScheme, label, style, disabled, }: VideoExportButtonProps): react_jsx_runtime.JSX.Element;
167
-
168
- /**
169
- * useFrameCapture — Hidden div + html2canvas frame capture.
170
- *
171
- * Mounts a DocPlayer in renderMode inside a hidden div (same document),
172
- * then captures individual frames by seeking the player and rendering
173
- * the DOM to a canvas via html2canvas.
174
- *
175
- * Uses React directly — no script injection, no iframes, no eval.
176
- *
177
- * Returns an ImageBitmap for each frame (transferable to a Worker).
178
- */
179
-
180
- interface FrameCaptureHandle {
181
- /** Initialize the hidden player. Returns the video duration in seconds. */
182
- init: (doc: Doc, renderOptions: Omit<RenderHtmlOptions, 'playerScript'>, captionMode?: CaptionMode) => Promise<number>;
183
- /** Capture a single frame at the given time (seconds). Returns an ImageBitmap. */
184
- captureFrame: (time: number) => Promise<ImageBitmap>;
185
- /** Clean up resources. */
186
- destroy: () => void;
187
- }
188
- /**
189
- * Hook that manages a hidden div for frame capture.
190
- */
191
- declare function useFrameCapture(): FrameCaptureHandle;
192
-
193
- /**
194
- * Main-thread WebCodecs encoder.
195
- *
196
- * Encodes video frames to MP4 using the WebCodecs API and mp4-muxer,
197
- * running directly on the main thread. This is simpler and avoids
198
- * worker module-resolution issues with bundlers. Since frame capture
199
- * via html2canvas (~100-200ms per frame) is the bottleneck — not
200
- * encoding (~1ms per frame with hardware-accelerated WebCodecs) —
201
- * worker offloading provides minimal benefit.
202
- *
203
- * Requirements: Chrome 94+ / Edge 94+ (WebCodecs support).
204
- */
205
- interface EncoderConfig {
206
- width: number;
207
- height: number;
208
- fps: number;
209
- quality: 'draft' | 'normal' | 'high';
210
- /**
211
- * When present, the underlying muxer is configured with an AAC audio track
212
- * and {@link MainThreadEncoder.addAudioChunk} becomes usable. Absent → the
213
- * encoder produces a video-only MP4 exactly as before.
214
- */
215
- audio?: {
216
- numberOfChannels: number;
217
- sampleRate: number;
218
- };
219
- }
220
- interface MainThreadEncoder {
221
- /** Encode a single frame. The bitmap is closed after encoding. */
222
- encodeFrame(bitmap: ImageBitmap, frameIndex: number): Promise<void>;
223
- /**
224
- * Hand an encoded audio chunk (from a WebCodecs `AudioEncoder`) to the muxer.
225
- * Only valid when the encoder was created with an `audio` config; otherwise a
226
- * no-op. Must be called before {@link finalize}.
227
- */
228
- addAudioChunk?(chunk: EncodedAudioChunk, meta?: EncodedAudioChunkMetadata): void;
229
- /** Flush pending frames and finalize the MP4. Returns the MP4 ArrayBuffer. */
230
- finalize(): Promise<ArrayBuffer>;
231
- /** Close the encoder without producing output (e.g., on cancel). */
232
- close(): void;
233
- }
234
- /**
235
- * Check whether the browser supports WebCodecs video encoding.
236
- */
237
- declare function supportsWebCodecs(): boolean;
238
- /**
239
- * Probe whether the WebCodecs encoder actually supports the H.264 profile
240
- * we use. The `VideoEncoder` global can exist while the specific codec is
241
- * unavailable — this is the case on Linux Chromium, which ships without
242
- * the proprietary H.264 encoder.
243
- */
244
- declare function supportsWebCodecsH264(config: EncoderConfig): Promise<boolean>;
245
- /**
246
- * Create a main-thread WebCodecs encoder.
247
- *
248
- * Throws if WebCodecs is not available.
249
- */
250
- declare function createEncoder(config: EncoderConfig): MainThreadEncoder;
251
-
252
- /**
253
- * audioTrack — In-browser audio rendering + AAC encoding for MP4 export.
254
- *
255
- * Replaces the CLI's ffmpeg `adelay`/`atrim`/`amix` graph with browser-native
256
- * primitives:
257
- *
258
- * - {@link supportsWebCodecsAac} probes whether the browser can encode AAC
259
- * (WebCodecs `AudioEncoder`).
260
- * - {@link renderAudioTimeline} mixes an {@link AudioTimelineClip} list into a
261
- * single `AudioBuffer` using an `OfflineAudioContext` (decode → schedule →
262
- * render).
263
- * - {@link encodeAacTrack} feeds that buffer through a WebCodecs
264
- * `AudioEncoder` and hands the chunks to an mp4-muxer audio track (tier 1).
265
- * - {@link audioBufferToWav} + {@link muxAudioWithFfmpegWasm} cover the
266
- * ffmpeg.wasm fallback (tier 2).
267
- * - {@link selectAudioTier} is the pure capability→tier decision shared with
268
- * `useVideoExport` (and unit-tested at the module boundary).
269
- */
270
-
271
- /**
272
- * Whether the browser can encode AAC (`mp4a.40.2`) via WebCodecs.
273
- * Returns false when `AudioEncoder` is undefined (e.g. Node/jsdom, Safari,
274
- * older Chromium) or the config is unsupported.
275
- */
276
- declare function supportsWebCodecsAac(sampleRate?: number, channels?: number): Promise<boolean>;
277
-
278
- export { type EncoderConfig, type FrameCaptureHandle, type MainThreadEncoder, VideoExportButton, type VideoExportButtonProps, type VideoExportConfig, VideoExportModal, type VideoExportModalProps, type VideoExportResult, type VideoExportState, type VideoOutputFormat, createEncoder, supportsWebCodecs, supportsWebCodecsAac, supportsWebCodecsH264, useFrameCapture, useVideoExport };
6
+ import 'react/jsx-runtime';
7
+ import '@bendyline/squisq/schemas';
8
+ import '@bendyline/squisq/markdown';
9
+ import '@bendyline/squisq-react';