@bendyline/squisq-video-react 1.0.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.
@@ -0,0 +1,176 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { Doc, MediaProvider } from '@bendyline/squisq/schemas';
3
+ import { VideoQuality, VideoOrientation, RenderHtmlOptions } from '@bendyline/squisq-video';
4
+ import { CaptionMode } from '@bendyline/squisq-react';
5
+
6
+ interface VideoExportModalProps {
7
+ /** The document to export */
8
+ doc: Doc;
9
+ /** Player IIFE bundle source */
10
+ playerScript: string;
11
+ /** Optional media provider for resolving images/audio */
12
+ mediaProvider?: MediaProvider;
13
+ /** Pre-collected images map (alternative to mediaProvider) */
14
+ images?: Map<string, ArrayBuffer>;
15
+ /** Pre-collected audio map */
16
+ audio?: Map<string, ArrayBuffer>;
17
+ /** Called when the modal should close */
18
+ onClose: () => void;
19
+ }
20
+ declare function VideoExportModal({ doc, playerScript, mediaProvider, images, audio, onClose, }: VideoExportModalProps): react_jsx_runtime.JSX.Element;
21
+
22
+ interface VideoExportButtonProps {
23
+ /** The document to export */
24
+ doc: Doc;
25
+ /** Player IIFE bundle source */
26
+ playerScript: string;
27
+ /** Optional media provider for resolving images/audio */
28
+ mediaProvider?: MediaProvider;
29
+ /** Pre-collected images map */
30
+ images?: Map<string, ArrayBuffer>;
31
+ /** Pre-collected audio map */
32
+ audio?: Map<string, ArrayBuffer>;
33
+ /** Button label (default: "Export Video") */
34
+ label?: string;
35
+ /** Additional inline styles for the button */
36
+ style?: React.CSSProperties;
37
+ /** Whether the button is disabled */
38
+ disabled?: boolean;
39
+ }
40
+ declare function VideoExportButton({ doc, playerScript, mediaProvider, images, audio, label, style, disabled, }: VideoExportButtonProps): react_jsx_runtime.JSX.Element;
41
+
42
+ /**
43
+ * useVideoExport — Main orchestration hook for browser video export.
44
+ *
45
+ * Coordinates frame capture (hidden iframe + html2canvas) with
46
+ * main-thread WebCodecs encoding via mp4-muxer. Manages the full
47
+ * lifecycle: prepare → capture + encode → download.
48
+ *
49
+ * Encoding runs on the main thread because frame capture via html2canvas
50
+ * (~100-200ms per frame) is the bottleneck, not encoding (~1ms per frame
51
+ * with hardware-accelerated WebCodecs). Worker offloading would add
52
+ * complexity with minimal benefit.
53
+ *
54
+ * Usage:
55
+ * const { state, progress, phase, startExport, cancel, downloadUrl } = useVideoExport();
56
+ * <button onClick={() => startExport(doc, options)}>Export</button>
57
+ */
58
+
59
+ type VideoExportState = 'idle' | 'preparing' | 'capturing' | 'encoding' | 'complete' | 'error';
60
+ interface VideoExportConfig {
61
+ /** Encoding quality preset (default: 'normal') */
62
+ quality?: VideoQuality;
63
+ /** Frames per second (default: 30) */
64
+ fps?: number;
65
+ /** Viewport orientation (default: 'landscape') */
66
+ orientation?: VideoOrientation;
67
+ /**
68
+ * Map of relative image paths to binary data.
69
+ * Used to embed images into the render HTML.
70
+ */
71
+ images?: Map<string, ArrayBuffer>;
72
+ /**
73
+ * Map of audio segment names to binary data.
74
+ * Used to embed audio into the render HTML.
75
+ */
76
+ audio?: Map<string, ArrayBuffer>;
77
+ /** MediaProvider to resolve media URLs (alternative to passing images directly) */
78
+ mediaProvider?: MediaProvider;
79
+ /** Caption mode for the exported video (default: 'off') */
80
+ captionMode?: CaptionMode;
81
+ /** Player IIFE bundle (unused in browser export, kept for CLI/Playwright path) */
82
+ playerScript?: string;
83
+ }
84
+ interface VideoExportResult {
85
+ /** Current export state */
86
+ state: VideoExportState;
87
+ /** 0–100 progress percentage */
88
+ progress: number;
89
+ /** Human-readable description of the current phase */
90
+ phase: string;
91
+ /** Video duration detected from the doc (seconds) */
92
+ duration: number;
93
+ /** Encoder backend ('webcodecs' when active, null when idle) */
94
+ backend: 'webcodecs' | null;
95
+ /** Blob download URL (populated when state === 'complete') */
96
+ downloadUrl: string | null;
97
+ /** File size in bytes (populated when state === 'complete') */
98
+ fileSize: number;
99
+ /** Error message (populated when state === 'error') */
100
+ error: string | null;
101
+ /** Seconds elapsed since export started */
102
+ elapsed: number;
103
+ /** Estimated seconds remaining (0 when idle or complete) */
104
+ estimatedRemaining: number;
105
+ /** Start a new export */
106
+ startExport: (doc: Doc, config: VideoExportConfig) => Promise<void>;
107
+ /** Cancel an in-progress export */
108
+ cancel: () => void;
109
+ /** Reset state back to idle (e.g., after complete or error) */
110
+ reset: () => void;
111
+ }
112
+ declare function useVideoExport(): VideoExportResult;
113
+
114
+ /**
115
+ * useFrameCapture — Hidden div + html2canvas frame capture.
116
+ *
117
+ * Mounts a DocPlayer in renderMode inside a hidden div (same document),
118
+ * then captures individual frames by seeking the player and rendering
119
+ * the DOM to a canvas via html2canvas.
120
+ *
121
+ * Uses React directly — no script injection, no iframes, no eval.
122
+ *
123
+ * Returns an ImageBitmap for each frame (transferable to a Worker).
124
+ */
125
+
126
+ interface FrameCaptureHandle {
127
+ /** Initialize the hidden player. Returns the video duration in seconds. */
128
+ init: (doc: Doc, renderOptions: Omit<RenderHtmlOptions, 'playerScript'>, captionMode?: CaptionMode) => Promise<number>;
129
+ /** Capture a single frame at the given time (seconds). Returns an ImageBitmap. */
130
+ captureFrame: (time: number) => Promise<ImageBitmap>;
131
+ /** Clean up resources. */
132
+ destroy: () => void;
133
+ }
134
+ /**
135
+ * Hook that manages a hidden div for frame capture.
136
+ */
137
+ declare function useFrameCapture(): FrameCaptureHandle;
138
+
139
+ /**
140
+ * Main-thread WebCodecs encoder.
141
+ *
142
+ * Encodes video frames to MP4 using the WebCodecs API and mp4-muxer,
143
+ * running directly on the main thread. This is simpler and avoids
144
+ * worker module-resolution issues with bundlers. Since frame capture
145
+ * via html2canvas (~100-200ms per frame) is the bottleneck — not
146
+ * encoding (~1ms per frame with hardware-accelerated WebCodecs) —
147
+ * worker offloading provides minimal benefit.
148
+ *
149
+ * Requirements: Chrome 94+ / Edge 94+ (WebCodecs support).
150
+ */
151
+ interface EncoderConfig {
152
+ width: number;
153
+ height: number;
154
+ fps: number;
155
+ quality: 'draft' | 'normal' | 'high';
156
+ }
157
+ interface MainThreadEncoder {
158
+ /** Encode a single frame. The bitmap is closed after encoding. */
159
+ encodeFrame(bitmap: ImageBitmap, frameIndex: number): void;
160
+ /** Flush pending frames and finalize the MP4. Returns the MP4 ArrayBuffer. */
161
+ finalize(): Promise<ArrayBuffer>;
162
+ /** Close the encoder without producing output (e.g., on cancel). */
163
+ close(): void;
164
+ }
165
+ /**
166
+ * Check whether the browser supports WebCodecs video encoding.
167
+ */
168
+ declare function supportsWebCodecs(): boolean;
169
+ /**
170
+ * Create a main-thread WebCodecs encoder.
171
+ *
172
+ * Throws if WebCodecs is not available.
173
+ */
174
+ declare function createEncoder(config: EncoderConfig): MainThreadEncoder;
175
+
176
+ export { type FrameCaptureHandle, type MainThreadEncoder, VideoExportButton, type VideoExportButtonProps, type VideoExportConfig, VideoExportModal, type VideoExportModalProps, type VideoExportResult, type VideoExportState, createEncoder, supportsWebCodecs, useFrameCapture, useVideoExport };