@twick/browser-render 0.15.6 → 0.15.8
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 +63 -25
- package/dist/index.d.mts +225 -0
- package/dist/index.d.ts +225 -0
- package/dist/index.js +747 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +708 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +15 -8
- package/public/audio-worker.js +1 -4
- package/public/mp4-wasm.wasm +0 -0
- package/AUDIO_IMPLEMENTATION.md +0 -217
- package/package.json.bak +0 -53
- package/src/audio/audio-processor.ts +0 -239
- package/src/audio/audio-video-muxer.ts +0 -79
- package/src/browser-renderer.ts +0 -495
- package/src/hooks/use-browser-renderer.ts +0 -218
- package/src/index.ts +0 -20
- package/src/mp4-wasm.d.ts +0 -4
- package/tsconfig.json +0 -23
- package/tsup.config.ts +0 -19
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/browser-renderer.ts","../src/audio/video-audio-extractor.ts","../src/audio/audio-processor.ts","../src/audio/audio-video-muxer.ts","../src/hooks/use-browser-renderer.ts"],"sourcesContent":["import { Renderer, Vector2 } from \"@twick/core\";\nimport type { Project, RendererSettings } from \"@twick/core\";\nimport defaultProject from \"@twick/visualizer/dist/project.js\";\nimport { BrowserAudioProcessor, getAssetPlacement, type AssetInfo } from './audio/audio-processor';\nimport { muxAudioVideo } from './audio/audio-video-muxer';\n\n/**\n * Browser-native video exporter using WebCodecs API\n * This exporter downloads the video directly in the browser without any server interaction\n */\nclass BrowserWasmExporter {\n public static readonly id = '@twick/core/wasm';\n public static readonly displayName = 'Browser Video (Wasm)';\n\n private encoder: any;\n private videoBlob: Blob | null = null;\n private onProgressCallback?: (progress: number) => void;\n private currentFrame: number = 0;\n private fps: number = 30;\n\n public static async create(settings: RendererSettings) {\n return new BrowserWasmExporter(settings);\n }\n\n public constructor(\n private readonly settings: RendererSettings,\n ) {\n this.fps = settings.fps || 30;\n }\n\n public async start(): Promise<void> {\n try {\n // Import mp4-wasm\n const loadMp4Module = (await import('mp4-wasm')).default;\n \n // Try multiple locations to fetch the WASM file\n const possiblePaths = [\n // Vite dev server virtual path\n '/@mp4-wasm',\n // Common bundled asset paths (Vite uses hashed names)\n '/assets/mp4-wasm.wasm',\n '/assets/mp4-YBRi_559.wasm', // Known Vite hash\n '/mp4-wasm.wasm',\n // Node modules path (for dev)\n '/node_modules/mp4-wasm/dist/mp4-wasm.wasm',\n ];\n \n let buffer: ArrayBuffer | null = null;\n let successPath = '';\n \n for (const path of possiblePaths) {\n try {\n const resp = await fetch(path);\n if (resp.ok) {\n const contentType = resp.headers.get('content-type');\n // Make sure we got a WASM file, not HTML\n if (contentType && contentType.includes('html')) {\n continue;\n }\n buffer = await resp.arrayBuffer();\n successPath = path;\n break;\n }\n } catch (e) {\n continue;\n }\n }\n \n if (!buffer) {\n throw new Error(\n 'Could not load WASM file from any location. ' +\n 'Please copy mp4-wasm.wasm to your public directory or configure Vite to serve it.'\n );\n }\n \n const mp4 = await loadMp4Module({ wasmBinary: buffer });\n\n this.encoder = mp4.createWebCodecsEncoder({\n width: this.settings.size.x,\n height: this.settings.size.y,\n fps: this.fps,\n });\n } catch (error) {\n throw error;\n }\n }\n\n public async handleFrame(canvas: HTMLCanvasElement, frameNumber?: number): Promise<void> {\n const frameIndex = frameNumber !== undefined ? frameNumber : this.currentFrame;\n const timestampMicroseconds = Math.round((frameIndex / this.fps) * 1_000_000);\n \n const frame = new VideoFrame(canvas, { \n timestamp: timestampMicroseconds,\n duration: Math.round((1 / this.fps) * 1_000_000)\n });\n \n await this.encoder.addFrame(frame);\n frame.close();\n \n if (frameNumber === undefined) {\n this.currentFrame++;\n }\n }\n\n public async stop(): Promise<void> {\n const buf = await this.encoder.end();\n this.videoBlob = new Blob([buf], { type: 'video/mp4' });\n }\n\n public async generateAudio(\n assets: AssetInfo[][],\n startFrame: number,\n endFrame: number,\n ): Promise<ArrayBuffer | null> {\n try {\n const processor = new BrowserAudioProcessor();\n const assetPlacements = getAssetPlacement(assets);\n\n if (assetPlacements.length === 0) {\n return null;\n }\n\n const processedBuffers: AudioBuffer[] = [];\n for (const asset of assetPlacements) {\n if (asset.volume > 0 && asset.playbackRate > 0) {\n try {\n const buffer = await processor.processAudioAsset(\n asset,\n this.settings.fps || 30,\n endFrame - startFrame\n );\n processedBuffers.push(buffer);\n } catch {\n // Continue with other audio assets\n }\n }\n }\n\n if (processedBuffers.length === 0) {\n return null;\n }\n\n const mixedBuffer = processor.mixAudioBuffers(processedBuffers);\n const wavData = processor.audioBufferToWav(mixedBuffer);\n\n await processor.close();\n return wavData;\n } catch {\n return null;\n }\n }\n\n public async mergeMedia(): Promise<void> {\n // In browser, we don't need to merge separately\n // The video is already created with audio in the encoder\n }\n\n public async downloadVideos(assets: any[][]): Promise<void> {\n // Browser doesn't need to download source videos\n // They're already accessible via URLs\n }\n\n public getVideoBlob(): Blob | null {\n return this.videoBlob;\n }\n\n public setProgressCallback(callback: (progress: number) => void): void {\n this.onProgressCallback = callback;\n }\n}\n\n/**\n * Browser rendering configuration\n */\nexport interface BrowserRenderConfig {\n /** \n * Custom Project object\n * If not provided, defaults to @twick/visualizer project\n * \n * Note: Must be an imported Project object, not a string path.\n * String paths only work in Node.js environments (server renderer).\n * \n * Example:\n * ```typescript\n * import myProject from './my-custom-project';\n * \n * await renderTwickVideoInBrowser({\n * projectFile: myProject,\n * variables: { input: {...} }\n * });\n * ```\n */\n projectFile?: Project;\n /** Input variables containing project configuration */\n variables: {\n input: any;\n playerId?: string;\n [key: string]: any;\n };\n /** Render settings */\n settings?: {\n width?: number;\n height?: number;\n fps?: number;\n quality?: 'low' | 'medium' | 'high';\n range?: [number, number]; // [start, end] in seconds\n includeAudio?: boolean; // Enable audio processing\n downloadAudioSeparately?: boolean; // Download audio.wav separately\n onAudioReady?: (audioBlob: Blob) => void; // Callback when audio is ready\n onProgress?: (progress: number) => void;\n onComplete?: (videoBlob: Blob) => void;\n onError?: (error: Error) => void;\n };\n}\n\n/**\n * Renders a Twick video directly in the browser without requiring a server.\n * Uses WebCodecs API for encoding video frames into MP4 format.\n * \n * This function uses the same signature as the server renderer for consistency.\n *\n * @param config - Configuration object containing variables and settings\n * @param config.projectFile - Optional project file path or Project object (defaults to visualizer project)\n * @param config.variables - Variables containing input configuration (tracks, elements, etc.)\n * @param config.settings - Optional render settings (width, height, fps, etc.)\n * @returns Promise resolving to a Blob containing the rendered video\n * \n * @example\n * ```js\n * import { renderTwickVideoInBrowser } from '@twick/browser-render';\n * \n * // Using default visualizer project\n * const videoBlob = await renderTwickVideoInBrowser({\n * variables: {\n * input: {\n * properties: { width: 1920, height: 1080, fps: 30 },\n * tracks: [\n * // Your tracks configuration\n * ]\n * }\n * },\n * settings: {\n * width: 1920,\n * height: 1080,\n * fps: 30,\n * quality: 'high',\n * onProgress: (progress) => console.log(`Rendering: ${progress * 100}%`),\n * }\n * });\n * \n * // Using custom project\n * import myProject from './my-custom-project';\n * const videoBlob = await renderTwickVideoInBrowser({\n * projectFile: myProject, // Must be an imported Project object\n * variables: { input: {...} },\n * settings: {...}\n * });\n * \n * // Download the video\n * const url = URL.createObjectURL(videoBlob);\n * const a = document.createElement('a');\n * a.href = url;\n * a.download = 'video.mp4';\n * a.click();\n * URL.revokeObjectURL(url);\n * ```\n */\nexport const renderTwickVideoInBrowser = async (\n config: BrowserRenderConfig\n): Promise<Blob> => {\n // Save original methods to restore later\n const originalVideoPlay = HTMLVideoElement.prototype.play;\n const originalAudioPlay = HTMLAudioElement.prototype.play;\n const originalCreateElement = document.createElement.bind(document);\n \n // Override play methods to force muting\n HTMLVideoElement.prototype.play = function() {\n this.muted = true;\n this.volume = 0;\n return originalVideoPlay.call(this);\n };\n \n HTMLAudioElement.prototype.play = function() {\n this.muted = true;\n this.volume = 0;\n return originalAudioPlay.call(this);\n };\n \n // Override createElement to mute video/audio on creation\n document.createElement = function(tagName: string, options?: any) {\n const element = originalCreateElement(tagName, options);\n if (tagName.toLowerCase() === 'video' || tagName.toLowerCase() === 'audio') {\n (element as any).muted = true;\n (element as any).volume = 0;\n }\n return element;\n } as any;\n\n try {\n const { projectFile, variables, settings = {} } = config;\n\n if (!variables || !variables.input) {\n throw new Error('Invalid configuration. \"variables.input\" is required.');\n }\n\n const width = settings.width || variables.input.properties?.width || 1920;\n const height = settings.height || variables.input.properties?.height || 1080;\n const fps = settings.fps || variables.input.properties?.fps || 30;\n\n const project: Project = !projectFile ? defaultProject : (projectFile as Project);\n project.variables = variables as any;\n\n // Create renderer settings\n const renderSettings: RendererSettings = {\n name: 'browser-render',\n exporter: {\n name: '@twick/core/wasm',\n },\n size: new Vector2(width, height),\n resolutionScale: 1,\n colorSpace: 'srgb',\n fps: fps,\n range: settings.range || [0, Infinity],\n background: variables.input.backgroundColor || '#000000',\n ...(settings.quality && {\n quality: settings.quality,\n }),\n };\n\n const renderer = new Renderer(project);\n const exporter = await BrowserWasmExporter.create(renderSettings);\n await exporter.start();\n \n if (settings.onProgress) {\n exporter.setProgressCallback(settings.onProgress);\n }\n\n await renderer['reloadScenes'](renderSettings);\n (renderer as any).stage.configure(renderSettings);\n (renderer as any).playback.fps = renderSettings.fps;\n \n // Set playback state to Rendering (critical for video elements)\n // PlaybackState: Playing = 0, Rendering = 1, Paused = 2, Presenting = 3\n (renderer as any).playback.state = 1;\n \n const totalFrames = await renderer.getNumberOfFrames(renderSettings);\n\n if (totalFrames === 0 || !isFinite(totalFrames)) {\n throw new Error(\n 'Cannot render: Video has zero duration. ' +\n 'Please ensure your project has valid content with non-zero duration. ' +\n 'Check that all video elements have valid sources and are properly loaded.'\n );\n }\n \n const videoElements: any[] = [];\n if (variables.input.tracks) {\n variables.input.tracks.forEach((track: any) => {\n if (track.elements) {\n track.elements.forEach((el: any) => {\n if (el.type === 'video') videoElements.push(el);\n });\n }\n });\n }\n\n if (videoElements.length > 0) {\n for (const videoEl of videoElements) {\n const src = videoEl.props?.src;\n if (!src || src === 'undefined') continue;\n const preloadVideo = document.createElement('video');\n preloadVideo.crossOrigin = 'anonymous';\n preloadVideo.preload = 'metadata';\n preloadVideo.src = src;\n await new Promise<void>((resolve, reject) => {\n const timeout = setTimeout(\n () => reject(new Error(`Timeout loading video metadata: ${src.substring(0, 80)}`)),\n 30000\n );\n preloadVideo.addEventListener('loadedmetadata', () => {\n clearTimeout(timeout);\n resolve();\n }, { once: true });\n preloadVideo.addEventListener('error', () => {\n clearTimeout(timeout);\n const err = preloadVideo.error;\n reject(new Error(`Failed to load video: ${err?.message || 'Unknown error'}`));\n }, { once: true });\n });\n }\n }\n\n await (renderer as any).playback.recalculate();\n await (renderer as any).playback.reset();\n await (renderer as any).playback.seek(0);\n\n const mediaAssets: AssetInfo[][] = [];\n \n for (let frame = 0; frame < totalFrames; frame++) {\n if (frame > 0) {\n await (renderer as any).playback.progress();\n }\n await (renderer as any).stage.render(\n (renderer as any).playback.currentScene,\n (renderer as any).playback.previousScene,\n );\n const currentAssets = (renderer as any).playback.currentScene.getMediaAssets?.() || [];\n mediaAssets.push(currentAssets);\n const canvas = (renderer as any).stage.finalBuffer;\n await exporter.handleFrame(canvas, frame);\n if (settings.onProgress) settings.onProgress(frame / totalFrames);\n }\n\n await exporter.stop();\n \n let audioData: ArrayBuffer | null = null;\n if (settings.includeAudio && mediaAssets.length > 0) {\n audioData = await exporter.generateAudio(mediaAssets, 0, totalFrames);\n }\n\n let finalBlob = exporter.getVideoBlob();\n if (!finalBlob) {\n throw new Error('Failed to create video blob');\n }\n\n if (audioData && settings.includeAudio) {\n try {\n finalBlob = await muxAudioVideo({\n videoBlob: finalBlob,\n audioBuffer: audioData,\n });\n } catch {\n const audioBlob = new Blob([audioData], { type: 'audio/wav' });\n const audioUrl = URL.createObjectURL(audioBlob);\n const a = document.createElement('a');\n a.href = audioUrl;\n a.download = 'audio.wav';\n a.click();\n URL.revokeObjectURL(audioUrl);\n }\n }\n\n if (settings.onComplete) {\n settings.onComplete(finalBlob);\n }\n\n return finalBlob;\n } catch (error) {\n if (config.settings?.onError) {\n config.settings.onError(error as Error);\n }\n throw error;\n } finally {\n // Restore original methods\n HTMLVideoElement.prototype.play = originalVideoPlay;\n HTMLAudioElement.prototype.play = originalAudioPlay;\n document.createElement = originalCreateElement as any;\n }\n};\n\n/**\n * Helper function to download a video blob as a file\n * \n * @param videoBlob - The video blob to download\n * @param filename - The desired filename (default: 'video.mp4')\n * \n * @example\n * ```js\n * const blob = await renderTwickVideoInBrowser(projectData);\n * downloadVideoBlob(blob, 'my-video.mp4');\n * ```\n */\nexport const downloadVideoBlob = (videoBlob: Blob, filename: string = 'video.mp4'): void => {\n const url = URL.createObjectURL(videoBlob);\n const a = document.createElement('a');\n a.href = url;\n a.download = filename;\n a.style.display = 'none';\n document.body.appendChild(a);\n a.click();\n document.body.removeChild(a);\n \n // Clean up the object URL after a delay\n setTimeout(() => URL.revokeObjectURL(url), 1000);\n};\n\nexport default renderTwickVideoInBrowser;\n","/**\n * Alternative audio extraction using MediaElementAudioSourceNode\n * This captures audio directly from a playing video element without decoding\n * Works with ANY audio codec the browser can play!\n */\n\nexport class VideoElementAudioExtractor {\n private audioContext: AudioContext;\n private video: HTMLVideoElement;\n private destination: MediaStreamAudioDestinationNode | null = null;\n private mediaRecorder: MediaRecorder | null = null;\n private audioChunks: Blob[] = [];\n \n constructor(videoSrc: string, sampleRate: number = 48000) {\n this.audioContext = new AudioContext({ sampleRate });\n this.video = document.createElement('video');\n this.video.crossOrigin = 'anonymous';\n this.video.src = videoSrc;\n this.video.muted = true; // Mute playback but audio will still be captured\n }\n \n async initialize(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.video.addEventListener('loadedmetadata', () => resolve(), { once: true });\n \n this.video.addEventListener('error', (e) => {\n reject(new Error(`Failed to load video for audio extraction: ${e}`));\n }, { once: true });\n });\n }\n \n /**\n * Extract audio by playing the video and capturing audio output\n */\n async extractAudio(\n startTime: number,\n duration: number,\n playbackRate: number = 1.0\n ): Promise<AudioBuffer> {\n // Create audio source from video element\n const source = this.audioContext.createMediaElementSource(this.video);\n \n // Create destination for recording\n this.destination = this.audioContext.createMediaStreamDestination();\n source.connect(this.destination);\n \n // Create MediaRecorder to capture audio\n this.audioChunks = [];\n this.mediaRecorder = new MediaRecorder(this.destination.stream, {\n mimeType: 'audio/webm',\n });\n \n this.mediaRecorder.ondataavailable = (event) => {\n if (event.data.size > 0) {\n this.audioChunks.push(event.data);\n }\n };\n \n // Set up video playback\n this.video.currentTime = startTime;\n this.video.playbackRate = playbackRate;\n \n // Wait for seek to complete\n await new Promise<void>((resolve) => {\n this.video.addEventListener('seeked', () => resolve(), { once: true });\n });\n \n // Start recording and playing\n return new Promise((resolve, reject) => {\n const recordingTimeout = setTimeout(() => {\n reject(new Error('Audio extraction timeout'));\n }, (duration / playbackRate + 5) * 1000); // Add 5s buffer\n \n this.mediaRecorder!.start();\n this.video.play();\n \n // Stop recording after duration\n setTimeout(async () => {\n clearTimeout(recordingTimeout);\n this.video.pause();\n this.mediaRecorder!.stop();\n \n // Wait for final data\n await new Promise<void>((res) => {\n this.mediaRecorder!.addEventListener('stop', () => res(), { once: true });\n });\n \n // Convert recorded audio to AudioBuffer\n try {\n const audioBlob = new Blob(this.audioChunks, { type: 'audio/webm' });\n const arrayBuffer = await audioBlob.arrayBuffer();\n const audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer);\n resolve(audioBuffer);\n } catch (err) {\n reject(new Error(`Failed to decode recorded audio: ${err}`));\n }\n }, (duration / playbackRate) * 1000);\n });\n }\n \n async close(): Promise<void> {\n if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {\n this.mediaRecorder.stop();\n }\n this.video.pause();\n this.video.src = '';\n if (this.audioContext.state !== 'closed') {\n await this.audioContext.close();\n }\n }\n}\n\n/**\n * Extract audio from video using MediaElementAudioSourceNode\n * This method works with any audio codec the browser can play\n */\nexport async function extractAudioFromVideo(\n videoSrc: string,\n startTime: number,\n duration: number,\n playbackRate: number = 1.0,\n sampleRate: number = 48000\n): Promise<AudioBuffer> {\n const extractor = new VideoElementAudioExtractor(videoSrc, sampleRate);\n \n try {\n await extractor.initialize();\n const audioBuffer = await extractor.extractAudio(startTime, duration, playbackRate);\n return audioBuffer;\n } finally {\n await extractor.close();\n }\n}\n","/**\n * Browser-based audio processing using Web Audio API\n * Mirrors the server's FFmpeg audio generation logic\n */\n\nimport { extractAudioFromVideo } from './video-audio-extractor';\n\nexport interface MediaAsset {\n key: string;\n src: string;\n type: 'video' | 'audio';\n startInVideo: number;\n endInVideo: number;\n duration: number;\n playbackRate: number;\n volume: number;\n trimLeftInSeconds: number;\n durationInSeconds: number;\n}\n\nexport interface AssetInfo {\n key: string;\n src: string;\n type: 'video' | 'audio';\n currentTime: number;\n playbackRate: number;\n volume: number;\n}\n\n/**\n * Get asset placement from frames (similar to server's getAssetPlacement)\n */\nexport function getAssetPlacement(frames: AssetInfo[][]): MediaAsset[] {\n const assets: MediaAsset[] = [];\n const assetTimeMap = new Map<string, { start: number; end: number }>();\n\n for (let frame = 0; frame < frames.length; frame++) {\n for (const asset of frames[frame]) {\n if (!assetTimeMap.has(asset.key)) {\n assetTimeMap.set(asset.key, {\n start: asset.currentTime,\n end: asset.currentTime,\n });\n assets.push({\n key: asset.key,\n src: asset.src,\n type: asset.type,\n startInVideo: frame,\n endInVideo: frame,\n duration: 0,\n durationInSeconds: 0,\n playbackRate: asset.playbackRate,\n volume: asset.volume,\n trimLeftInSeconds: asset.currentTime,\n });\n } else {\n const timeInfo = assetTimeMap.get(asset.key);\n if (timeInfo) {\n timeInfo.end = asset.currentTime;\n }\n const existingAsset = assets.find(a => a.key === asset.key);\n if (existingAsset) {\n existingAsset.endInVideo = frame;\n }\n }\n }\n }\n\n // Calculate durations\n assets.forEach(asset => {\n const timeInfo = assetTimeMap.get(asset.key);\n if (timeInfo) {\n asset.durationInSeconds = (timeInfo.end - timeInfo.start) / asset.playbackRate;\n }\n asset.duration = asset.endInVideo - asset.startInVideo + 1;\n });\n\n return assets;\n}\n\n/**\n * Audio processor using Web Audio API\n */\nexport class BrowserAudioProcessor {\n private audioContext: AudioContext;\n\n constructor(private sampleRate: number = 48000) {\n this.audioContext = new AudioContext({ sampleRate });\n }\n\n /**\n * Fetch and decode audio from a media source\n * Falls back to video element extraction if decodeAudioData fails\n */\n async fetchAndDecodeAudio(src: string): Promise<AudioBuffer> {\n try {\n const response = await fetch(src);\n const arrayBuffer = await response.arrayBuffer();\n return await this.audioContext.decodeAudioData(arrayBuffer);\n } catch (err) {\n try {\n return await extractAudioFromVideo(\n src,\n 0,\n 999999,\n 1.0,\n this.sampleRate\n );\n } catch (fallbackErr) {\n throw new Error(`Failed to extract audio: ${err}. Fallback also failed: ${fallbackErr}`);\n }\n }\n }\n\n /**\n * Process audio asset with playback rate, volume, and timing\n */\n async processAudioAsset(\n asset: MediaAsset,\n fps: number,\n totalFrames: number\n ): Promise<AudioBuffer> {\n const audioBuffer = await this.fetchAndDecodeAudio(asset.src);\n \n const duration = totalFrames / fps;\n const outputLength = Math.ceil(duration * this.sampleRate);\n const outputBuffer = this.audioContext.createBuffer(\n 2, // stereo\n outputLength,\n this.sampleRate\n );\n\n // Calculate timing\n const startTime = asset.startInVideo / fps;\n const trimLeft = asset.trimLeftInSeconds / asset.playbackRate;\n const trimRight = trimLeft + asset.durationInSeconds;\n\n // Process each channel\n for (let channel = 0; channel < 2; channel++) {\n const inputData = audioBuffer.getChannelData(Math.min(channel, audioBuffer.numberOfChannels - 1));\n const outputData = outputBuffer.getChannelData(channel);\n\n // Calculate sample positions\n const startSample = Math.floor(startTime * this.sampleRate);\n const trimLeftSample = Math.floor(trimLeft * this.sampleRate);\n const trimRightSample = Math.floor(trimRight * this.sampleRate);\n\n // Copy and process samples\n for (let i = 0; i < outputData.length; i++) {\n const outputTime = i / this.sampleRate;\n const assetTime = outputTime - startTime;\n \n if (assetTime < 0 || assetTime >= asset.durationInSeconds) {\n outputData[i] = 0; // Silence\n } else {\n // Apply playback rate\n const inputSample = Math.floor((trimLeftSample + assetTime * asset.playbackRate * this.sampleRate));\n if (inputSample >= 0 && inputSample < inputData.length) {\n outputData[i] = inputData[inputSample] * asset.volume;\n } else {\n outputData[i] = 0;\n }\n }\n }\n }\n\n return outputBuffer;\n }\n\n /**\n * Mix multiple audio buffers\n */\n mixAudioBuffers(buffers: AudioBuffer[]): AudioBuffer {\n if (buffers.length === 0) {\n return this.audioContext.createBuffer(2, 1, this.sampleRate);\n }\n\n const maxLength = Math.max(...buffers.map(b => b.length));\n const mixedBuffer = this.audioContext.createBuffer(2, maxLength, this.sampleRate);\n\n for (let channel = 0; channel < 2; channel++) {\n const mixedData = mixedBuffer.getChannelData(channel);\n \n buffers.forEach(buffer => {\n const channelData = buffer.getChannelData(Math.min(channel, buffer.numberOfChannels - 1));\n for (let i = 0; i < channelData.length; i++) {\n mixedData[i] = (mixedData[i] || 0) + channelData[i] / buffers.length;\n }\n });\n }\n\n return mixedBuffer;\n }\n\n /**\n * Convert AudioBuffer to WAV format\n */\n audioBufferToWav(buffer: AudioBuffer): ArrayBuffer {\n const numberOfChannels = buffer.numberOfChannels;\n const sampleRate = buffer.sampleRate;\n const format = 1; // PCM\n const bitDepth = 16;\n\n const bytesPerSample = bitDepth / 8;\n const blockAlign = numberOfChannels * bytesPerSample;\n\n const data = new Float32Array(buffer.length * numberOfChannels);\n for (let channel = 0; channel < numberOfChannels; channel++) {\n const channelData = buffer.getChannelData(channel);\n for (let i = 0; i < buffer.length; i++) {\n data[i * numberOfChannels + channel] = channelData[i];\n }\n }\n\n const dataLength = data.length * bytesPerSample;\n const headerLength = 44;\n const wav = new ArrayBuffer(headerLength + dataLength);\n const view = new DataView(wav);\n\n // Write WAV header\n const writeString = (offset: number, string: string) => {\n for (let i = 0; i < string.length; i++) {\n view.setUint8(offset + i, string.charCodeAt(i));\n }\n };\n\n writeString(0, 'RIFF');\n view.setUint32(4, 36 + dataLength, true);\n writeString(8, 'WAVE');\n writeString(12, 'fmt ');\n view.setUint32(16, 16, true); // fmt chunk size\n view.setUint16(20, format, true);\n view.setUint16(22, numberOfChannels, true);\n view.setUint32(24, sampleRate, true);\n view.setUint32(28, sampleRate * blockAlign, true);\n view.setUint16(32, blockAlign, true);\n view.setUint16(34, bitDepth, true);\n writeString(36, 'data');\n view.setUint32(40, dataLength, true);\n\n // Write audio data\n const volume = 0.8;\n let offset = 44;\n for (let i = 0; i < data.length; i++) {\n const sample = Math.max(-1, Math.min(1, data[i]));\n view.setInt16(offset, sample < 0 ? sample * 0x8000 : sample * 0x7FFF, true);\n offset += 2;\n }\n\n return wav;\n }\n\n async close() {\n await this.audioContext.close();\n }\n}\n","'use client';\n\n/**\n * Browser-based audio/video muxing using FFmpeg.wasm (main thread)\n * Compatible with Next.js 15\n *\n * FFmpeg core files must be served from the app's public folder, e.g.:\n * twick-web/public/ffmpeg/ffmpeg-core.js, ffmpeg-core.wasm\n */\n\nexport interface MuxerOptions {\n videoBlob: Blob;\n audioBuffer: ArrayBuffer;\n}\n\n/** Base URL for FFmpeg assets (twick-web public/ffmpeg). Use same-origin URLs directly; toBlobURL causes \"Cannot find module 'blob:...'\" in some environments. */\nfunction getFFmpegBaseURL(): string {\n if (typeof window !== 'undefined') {\n return `${window.location.origin}/ffmpeg`;\n }\n return '/ffmpeg';\n}\n\nexport async function muxAudioVideo(\n options: MuxerOptions\n): Promise<Blob> {\n try {\n const { FFmpeg } = await import('@ffmpeg/ffmpeg');\n const { fetchFile } = await import('@ffmpeg/util');\n\n const ffmpeg = new FFmpeg();\n\n const base = getFFmpegBaseURL();\n const coreURL = `${base}/ffmpeg-core.js`;\n const wasmURL = `${base}/ffmpeg-core.wasm`;\n\n // Load from same-origin public folder (twick-web/public/ffmpeg). Do NOT use toBlobURL —\n // it produces blob: URLs that can trigger \"Cannot find module 'blob:...'\".\n await ffmpeg.load({\n coreURL,\n wasmURL,\n });\n\n // Write inputs\n await ffmpeg.writeFile(\n 'video.mp4',\n await fetchFile(options.videoBlob)\n );\n\n await ffmpeg.writeFile(\n 'audio.wav',\n new Uint8Array(options.audioBuffer)\n );\n\n await ffmpeg.exec([\n '-i', 'video.mp4',\n '-i', 'audio.wav',\n '-c:v', 'copy',\n '-c:a', 'aac',\n '-b:a', '192k',\n '-shortest',\n 'output.mp4',\n ]);\n\n const data = await ffmpeg.readFile('output.mp4');\n\n const uint8 =\n typeof data === 'string'\n ? new TextEncoder().encode(data)\n : new Uint8Array(data);\n\n return new Blob([uint8], { type: 'video/mp4' });\n\n } catch {\n return options.videoBlob;\n }\n}\n","import { useState, useCallback } from 'react';\nimport { renderTwickVideoInBrowser, downloadVideoBlob } from '../browser-renderer';\nimport type { BrowserRenderConfig } from '../browser-renderer';\n\nexport interface UseBrowserRendererOptions {\n /** \n * Custom Project object\n * If not provided, defaults to @twick/visualizer project\n * \n * Note: Must be an imported Project object, not a string path.\n * String paths only work in Node.js (server renderer).\n * \n * Example:\n * ```typescript\n * import myProject from './my-custom-project';\n * useBrowserRenderer({ projectFile: myProject })\n * ```\n */\n projectFile?: any;\n /** Video width in pixels */\n width?: number;\n /** Video height in pixels */\n height?: number;\n /** Frames per second */\n fps?: number;\n /** Render quality */\n quality?: 'low' | 'medium' | 'high';\n /** Time range to render [start, end] in seconds */\n range?: [number, number];\n /** Include audio in rendered video (experimental) */\n includeAudio?: boolean;\n /** Download audio separately as WAV file */\n downloadAudioSeparately?: boolean;\n /** Callback when audio is ready */\n onAudioReady?: (audioBlob: Blob) => void;\n /** Automatically download the video when rendering completes */\n autoDownload?: boolean;\n /** Default filename for downloads */\n downloadFilename?: string;\n}\n\nexport interface UseBrowserRendererReturn {\n /** Start rendering the video */\n render: (variables: BrowserRenderConfig['variables']) => Promise<Blob | null>;\n /** Current rendering progress (0-1) */\n progress: number;\n /** Whether rendering is in progress */\n isRendering: boolean;\n /** Error if rendering failed */\n error: Error | null;\n /** The rendered video blob (available after rendering completes) */\n videoBlob: Blob | null;\n /** Download the rendered video */\n download: (filename?: string) => void;\n /** Reset the renderer state */\n reset: () => void;\n}\n\n/**\n * React hook for rendering Twick videos in the browser\n * \n * Uses the same pattern as the server renderer for consistency.\n * \n * @param options - Rendering options\n * @returns Renderer state and control functions\n * \n * @example\n * ```tsx\n * import { useBrowserRenderer } from '@twick/browser-render';\n * \n * // Using default visualizer project\n * function MyComponent() {\n * const { render, progress, isRendering, videoBlob, download } = useBrowserRenderer({\n * width: 1920,\n * height: 1080,\n * fps: 30,\n * autoDownload: true,\n * });\n * \n * const handleRender = async () => {\n * await render({\n * input: {\n * properties: { width: 1920, height: 1080, fps: 30 },\n * tracks: [\n * // Your tracks configuration\n * ]\n * }\n * });\n * };\n * \n * return (\n * <div>\n * <button onClick={handleRender} disabled={isRendering}>\n * {isRendering ? `Rendering... ${(progress * 100).toFixed(0)}%` : 'Render Video'}\n * </button>\n * {videoBlob && !autoDownload && (\n * <button onClick={() => download('my-video.mp4')}>Download</button>\n * )}\n * </div>\n * );\n * }\n * \n * // Using custom project (must import it first)\n * import myProject from './my-project';\n * \n * function CustomProjectComponent() {\n * const { render } = useBrowserRenderer({\n * projectFile: myProject, // Pass the imported project object\n * width: 1920,\n * height: 1080,\n * });\n * \n * // ... rest of component\n * }\n * ```\n */\nexport const useBrowserRenderer = (options: UseBrowserRendererOptions = {}): UseBrowserRendererReturn => {\n const [progress, setProgress] = useState(0);\n const [isRendering, setIsRendering] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n const [videoBlob, setVideoBlob] = useState<Blob | null>(null);\n\n const reset = useCallback(() => {\n setProgress(0);\n setIsRendering(false);\n setError(null);\n setVideoBlob(null);\n }, []);\n\n const download = useCallback((filename?: string) => {\n if (!videoBlob) {\n setError(new Error('No video available to download. Please render the video first.'));\n return;\n }\n try {\n downloadVideoBlob(videoBlob, filename || options.downloadFilename || 'video.mp4');\n } catch (err) {\n setError(err instanceof Error ? err : new Error('Failed to download video'));\n }\n }, [videoBlob, options.downloadFilename]);\n\n const render = useCallback(async (variables: BrowserRenderConfig['variables']): Promise<Blob | null> => {\n reset();\n setIsRendering(true);\n\n try {\n const { projectFile, width, height, fps, quality, range, includeAudio, downloadAudioSeparately, onAudioReady, autoDownload, downloadFilename, ...restOptions } = options;\n \n const blob = await renderTwickVideoInBrowser({\n projectFile,\n variables,\n settings: {\n width,\n height,\n includeAudio,\n downloadAudioSeparately,\n onAudioReady,\n fps,\n quality,\n range,\n ...restOptions,\n onProgress: (p) => {\n setProgress(p);\n },\n onComplete: (blob) => {\n setVideoBlob(blob);\n if (autoDownload) {\n try {\n downloadVideoBlob(blob, downloadFilename || 'video.mp4');\n } catch (downloadErr) {\n setError(downloadErr instanceof Error ? downloadErr : new Error('Failed to auto-download video'));\n }\n }\n },\n onError: (err) => {\n setError(err);\n },\n },\n });\n\n if (!blob) {\n throw new Error('Rendering failed: No video blob was generated');\n }\n\n setVideoBlob(blob);\n setProgress(1);\n return blob;\n } catch (err) {\n setError(err instanceof Error ? err : new Error(String(err)));\n return null;\n } finally {\n setIsRendering(false);\n }\n }, [options, reset]);\n\n return {\n render,\n progress,\n isRendering,\n error,\n videoBlob,\n download,\n reset,\n };\n};\n\nexport default useBrowserRenderer;\n"],"mappings":";AAAA,SAAS,UAAU,eAAe;AAElC,OAAO,oBAAoB;;;ACIpB,IAAM,6BAAN,MAAiC;AAAA,EAC9B;AAAA,EACA;AAAA,EACA,cAAsD;AAAA,EACtD,gBAAsC;AAAA,EACtC,cAAsB,CAAC;AAAA,EAE/B,YAAY,UAAkB,aAAqB,MAAO;AACxD,SAAK,eAAe,IAAI,aAAa,EAAE,WAAW,CAAC;AACnD,SAAK,QAAQ,SAAS,cAAc,OAAO;AAC3C,SAAK,MAAM,cAAc;AACzB,SAAK,MAAM,MAAM;AACjB,SAAK,MAAM,QAAQ;AAAA,EACrB;AAAA,EAEA,MAAM,aAA4B;AAChC,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,WAAK,MAAM,iBAAiB,kBAAkB,MAAM,QAAQ,GAAG,EAAE,MAAM,KAAK,CAAC;AAE7E,WAAK,MAAM,iBAAiB,SAAS,CAAC,MAAM;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version": "0.15.
|
|
3
|
+
"version": "0.15.8",
|
|
4
4
|
"license": "https://github.com/ncounterspecialist/twick/blob/main/LICENSE.md",
|
|
5
5
|
"description": "Browser-native video rendering for Twick using WebCodecs API",
|
|
6
|
-
"main": "./dist/index.
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
7
|
"module": "./dist/index.mjs",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"import": "./dist/index.mjs",
|
|
13
|
-
"require": "./dist/index.
|
|
13
|
+
"require": "./dist/index.js"
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"public"
|
|
19
|
+
],
|
|
16
20
|
"scripts": {
|
|
17
|
-
"build": "tsup",
|
|
21
|
+
"build": "tsup && node scripts/copy-wasm.js",
|
|
18
22
|
"dev": "tsup --watch",
|
|
19
23
|
"test:browser": "tsx src/test-browser-render.ts",
|
|
20
|
-
"clean": "rimraf dist"
|
|
24
|
+
"clean": "rimraf dist",
|
|
25
|
+
"docs": "typedoc"
|
|
21
26
|
},
|
|
22
27
|
"publishConfig": {
|
|
23
28
|
"access": "public"
|
|
24
29
|
},
|
|
25
30
|
"dependencies": {
|
|
26
|
-
"@twick/core": "^0.15.
|
|
27
|
-
"@twick/visualizer": "0.15.
|
|
31
|
+
"@twick/core": "^0.15.8",
|
|
32
|
+
"@twick/visualizer": "0.15.8",
|
|
28
33
|
"mp4-wasm": "^1.0.6",
|
|
29
34
|
"mp4box": "^0.5.2",
|
|
30
35
|
"@ffmpeg/ffmpeg": "^0.12.10",
|
|
@@ -45,7 +50,9 @@
|
|
|
45
50
|
"rimraf": "^5.0.5",
|
|
46
51
|
"tsup": "^8.0.0",
|
|
47
52
|
"tsx": "^4.7.0",
|
|
48
|
-
"typescript": "5.4.2"
|
|
53
|
+
"typescript": "5.4.2",
|
|
54
|
+
"typedoc": "^0.25.8",
|
|
55
|
+
"typedoc-plugin-markdown": "^3.17.1"
|
|
49
56
|
},
|
|
50
57
|
"engines": {
|
|
51
58
|
"node": ">=20.0.0"
|
package/public/audio-worker.js
CHANGED
|
@@ -6,13 +6,11 @@
|
|
|
6
6
|
const CACHE_NAME = 'twick-audio-cache-v1';
|
|
7
7
|
const AUDIO_CACHE = 'audio-assets';
|
|
8
8
|
|
|
9
|
-
self.addEventListener('install', (
|
|
10
|
-
console.log('Audio Service Worker installed');
|
|
9
|
+
self.addEventListener('install', () => {
|
|
11
10
|
self.skipWaiting();
|
|
12
11
|
});
|
|
13
12
|
|
|
14
13
|
self.addEventListener('activate', (event) => {
|
|
15
|
-
console.log('Audio Service Worker activated');
|
|
16
14
|
event.waitUntil(self.clients.claim());
|
|
17
15
|
});
|
|
18
16
|
|
|
@@ -80,7 +78,6 @@ async function extractAudio(url) {
|
|
|
80
78
|
size: blob.size
|
|
81
79
|
};
|
|
82
80
|
} catch (error) {
|
|
83
|
-
console.error('Failed to extract audio:', error);
|
|
84
81
|
throw error;
|
|
85
82
|
}
|
|
86
83
|
}
|
|
Binary file
|
package/AUDIO_IMPLEMENTATION.md
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
# Audio Implementation Guide
|
|
2
|
-
|
|
3
|
-
## Overview
|
|
4
|
-
|
|
5
|
-
Browser-based audio processing for Twick video rendering, matching the server's FFmpeg implementation.
|
|
6
|
-
|
|
7
|
-
## Architecture
|
|
8
|
-
|
|
9
|
-
### 1. **Audio Processor** (`src/audio/audio-processor.ts`)
|
|
10
|
-
- Mirrors server's `generate-audio.ts` logic
|
|
11
|
-
- Uses Web Audio API instead of FFmpeg
|
|
12
|
-
- Handles:
|
|
13
|
-
- Asset tracking across frames
|
|
14
|
-
- Audio extraction from video/audio elements
|
|
15
|
-
- Playback rate adjustment
|
|
16
|
-
- Volume control
|
|
17
|
-
- Audio trimming
|
|
18
|
-
- Multi-track mixing
|
|
19
|
-
|
|
20
|
-
### 2. **Service Worker** (`public/audio-worker.js`)
|
|
21
|
-
- Caches media assets for offline processing
|
|
22
|
-
- Handles background audio extraction
|
|
23
|
-
- Reduces network requests during rendering
|
|
24
|
-
|
|
25
|
-
### 3. **Audio/Video Muxer** (`src/audio/audio-video-muxer.ts`)
|
|
26
|
-
- Combines video and audio tracks
|
|
27
|
-
- Two approaches:
|
|
28
|
-
- **mp4box.js**: Lightweight, browser-native
|
|
29
|
-
- **FFmpeg.wasm**: More robust, ~30MB bundle
|
|
30
|
-
|
|
31
|
-
## Implementation Steps
|
|
32
|
-
|
|
33
|
-
### Step 1: Install Dependencies
|
|
34
|
-
|
|
35
|
-
```bash
|
|
36
|
-
# For mp4box.js approach (recommended)
|
|
37
|
-
npm install mp4box
|
|
38
|
-
|
|
39
|
-
# OR for FFmpeg.wasm approach (more features, larger)
|
|
40
|
-
npm install @ffmpeg/ffmpeg @ffmpeg/util
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
### Step 2: Register Service Worker
|
|
44
|
-
|
|
45
|
-
```typescript
|
|
46
|
-
// In your app's initialization
|
|
47
|
-
if ('serviceWorker' in navigator) {
|
|
48
|
-
navigator.serviceWorker.register('/audio-worker.js')
|
|
49
|
-
.then(reg => console.log('Audio worker registered'))
|
|
50
|
-
.catch(err => console.error('Audio worker failed:', err));
|
|
51
|
-
}
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### Step 3: Enable Audio in Browser Renderer
|
|
55
|
-
|
|
56
|
-
Update `browser-renderer.ts`:
|
|
57
|
-
|
|
58
|
-
```typescript
|
|
59
|
-
import { BrowserAudioProcessor, getAssetPlacement } from './audio/audio-processor';
|
|
60
|
-
import { muxAudioVideo } from './audio/audio-video-muxer';
|
|
61
|
-
|
|
62
|
-
// In BrowserWasmExporter.generateAudio():
|
|
63
|
-
public async generateAudio(
|
|
64
|
-
assets: any[][],
|
|
65
|
-
startFrame: number,
|
|
66
|
-
endFrame: number,
|
|
67
|
-
): Promise<ArrayBuffer | null> {
|
|
68
|
-
const processor = new BrowserAudioProcessor();
|
|
69
|
-
const assetPlacements = getAssetPlacement(assets);
|
|
70
|
-
|
|
71
|
-
const processedBuffers: AudioBuffer[] = [];
|
|
72
|
-
for (const asset of assetPlacements) {
|
|
73
|
-
if (asset.volume > 0 && asset.playbackRate > 0) {
|
|
74
|
-
const buffer = await processor.processAudioAsset(
|
|
75
|
-
asset,
|
|
76
|
-
this.settings.fps || 30,
|
|
77
|
-
endFrame - startFrame
|
|
78
|
-
);
|
|
79
|
-
processedBuffers.push(buffer);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const mixedBuffer = processor.mixAudioBuffers(processedBuffers);
|
|
84
|
-
const wavData = processor.audioBufferToWav(mixedBuffer);
|
|
85
|
-
|
|
86
|
-
await processor.close();
|
|
87
|
-
return wavData;
|
|
88
|
-
}
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### Step 4: Collect Audio Assets During Rendering
|
|
92
|
-
|
|
93
|
-
In `renderTwickVideoInBrowser()`:
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
// Track media assets for each frame
|
|
97
|
-
const mediaAssets: AssetInfo[][] = [];
|
|
98
|
-
|
|
99
|
-
for (let frame = 0; frame < totalFrames; frame++) {
|
|
100
|
-
// ... existing rendering code ...
|
|
101
|
-
|
|
102
|
-
// Collect media assets from current scene
|
|
103
|
-
const currentAssets = (renderer as any).playback.currentScene.getMediaAssets?.() || [];
|
|
104
|
-
mediaAssets.push(currentAssets);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Generate audio after video rendering
|
|
108
|
-
const audioData = await exporter.generateAudio(mediaAssets, 0, totalFrames);
|
|
109
|
-
|
|
110
|
-
// Mux audio and video
|
|
111
|
-
if (audioData) {
|
|
112
|
-
const finalBlob = await muxAudioVideo({
|
|
113
|
-
videoBlob,
|
|
114
|
-
audioBuffer: audioData,
|
|
115
|
-
fps,
|
|
116
|
-
width,
|
|
117
|
-
height
|
|
118
|
-
});
|
|
119
|
-
return finalBlob;
|
|
120
|
-
}
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
## API Parity with Server
|
|
124
|
-
|
|
125
|
-
| Feature | Server (FFmpeg) | Browser (Web Audio) | Status |
|
|
126
|
-
|---------|-----------------|---------------------|--------|
|
|
127
|
-
| Asset Tracking | `getAssetPlacement()` | `getAssetPlacement()` | ✅ Ready |
|
|
128
|
-
| Audio Extraction | FFmpeg decode | `decodeAudioData()` | ✅ Ready |
|
|
129
|
-
| Playback Rate | `atempo` filter | Sample interpolation | ✅ Ready |
|
|
130
|
-
| Volume | `volume` filter | Gain multiplication | ✅ Ready |
|
|
131
|
-
| Trimming | `atrim` filter | Sample slicing | ✅ Ready |
|
|
132
|
-
| Mixing | `amix` filter | Buffer mixing | ✅ Ready |
|
|
133
|
-
| WAV Encoding | FFmpeg encode | Manual WAV encoding | ✅ Ready |
|
|
134
|
-
| Muxing | FFmpeg merge | mp4box.js / FFmpeg.wasm | ⚠️ Needs library |
|
|
135
|
-
|
|
136
|
-
## Performance Considerations
|
|
137
|
-
|
|
138
|
-
### Memory Usage
|
|
139
|
-
- Web Audio API decodes entire audio files into memory
|
|
140
|
-
- Large video files can cause memory issues
|
|
141
|
-
- Consider chunked processing for long videos
|
|
142
|
-
|
|
143
|
-
### Processing Time
|
|
144
|
-
- Audio processing adds 20-50% to render time
|
|
145
|
-
- Service worker caching helps with repeated renders
|
|
146
|
-
- Consider showing separate progress for video/audio
|
|
147
|
-
|
|
148
|
-
### Browser Limits
|
|
149
|
-
- Chrome: ~2GB audio buffer limit
|
|
150
|
-
- Safari: Stricter memory limits
|
|
151
|
-
- Firefox: Better memory management but slower
|
|
152
|
-
|
|
153
|
-
## Example Usage
|
|
154
|
-
|
|
155
|
-
```typescript
|
|
156
|
-
import { useBrowserRenderer } from '@twick/browser-render';
|
|
157
|
-
|
|
158
|
-
function VideoRenderer() {
|
|
159
|
-
const { render, progress } = useBrowserRenderer({
|
|
160
|
-
width: 1920,
|
|
161
|
-
height: 1080,
|
|
162
|
-
fps: 30,
|
|
163
|
-
includeAudio: true, // Enable audio processing
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
const handleRender = async () => {
|
|
167
|
-
const videoBlob = await render({
|
|
168
|
-
input: {
|
|
169
|
-
properties: { width: 1920, height: 1080, fps: 30 },
|
|
170
|
-
tracks: [
|
|
171
|
-
{
|
|
172
|
-
type: 'element',
|
|
173
|
-
elements: [
|
|
174
|
-
{
|
|
175
|
-
type: 'video',
|
|
176
|
-
src: 'https://example.com/video.mp4',
|
|
177
|
-
// Audio will be automatically extracted and included
|
|
178
|
-
}
|
|
179
|
-
]
|
|
180
|
-
}
|
|
181
|
-
]
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
return <button onClick={handleRender}>Render with Audio</button>;
|
|
187
|
-
}
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
## Troubleshooting
|
|
191
|
-
|
|
192
|
-
### No Audio in Output
|
|
193
|
-
1. Check if `includeAudio: true` is set
|
|
194
|
-
2. Verify service worker is registered
|
|
195
|
-
3. Check browser console for Web Audio API errors
|
|
196
|
-
4. Ensure video sources have audio tracks
|
|
197
|
-
|
|
198
|
-
### Memory Errors
|
|
199
|
-
1. Reduce video quality/resolution
|
|
200
|
-
2. Process shorter segments
|
|
201
|
-
3. Clear service worker cache
|
|
202
|
-
4. Use FFmpeg.wasm with streaming
|
|
203
|
-
|
|
204
|
-
### Performance Issues
|
|
205
|
-
1. Use service worker caching
|
|
206
|
-
2. Reduce number of audio tracks
|
|
207
|
-
3. Lower audio sample rate (default: 48kHz)
|
|
208
|
-
4. Consider server-side rendering for production
|
|
209
|
-
|
|
210
|
-
## Future Enhancements
|
|
211
|
-
|
|
212
|
-
- [ ] Streaming audio processing (chunks)
|
|
213
|
-
- [ ] Web Workers for parallel processing
|
|
214
|
-
- [ ] Real-time audio preview
|
|
215
|
-
- [ ] Audio effects (reverb, EQ, etc.)
|
|
216
|
-
- [ ] WASM-based audio processing for better performance
|
|
217
|
-
- [ ] Support for more audio formats
|
package/package.json.bak
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@twick/browser-render",
|
|
3
|
-
"version": "0.15.6",
|
|
4
|
-
"license": "https://github.com/ncounterspecialist/twick/blob/main/LICENSE.md",
|
|
5
|
-
"description": "Browser-native video rendering for Twick using WebCodecs API",
|
|
6
|
-
"main": "./dist/index.cjs",
|
|
7
|
-
"module": "./dist/index.mjs",
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
9
|
-
"exports": {
|
|
10
|
-
".": {
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
12
|
-
"import": "./dist/index.mjs",
|
|
13
|
-
"require": "./dist/index.cjs"
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
"scripts": {
|
|
17
|
-
"build": "tsup",
|
|
18
|
-
"dev": "tsup --watch",
|
|
19
|
-
"test:browser": "tsx src/test-browser-render.ts",
|
|
20
|
-
"clean": "rimraf dist"
|
|
21
|
-
},
|
|
22
|
-
"publishConfig": {
|
|
23
|
-
"access": "public"
|
|
24
|
-
},
|
|
25
|
-
"dependencies": {
|
|
26
|
-
"@twick/core": "^0.15.6",
|
|
27
|
-
"@twick/visualizer": "0.15.6",
|
|
28
|
-
"mp4-wasm": "^1.0.6",
|
|
29
|
-
"mp4box": "^0.5.2",
|
|
30
|
-
"@ffmpeg/ffmpeg": "^0.12.10",
|
|
31
|
-
"@ffmpeg/util": "^0.12.1",
|
|
32
|
-
"@ffmpeg/core": "^0.12.6"
|
|
33
|
-
},
|
|
34
|
-
"peerDependencies": {
|
|
35
|
-
"react": ">=17.0.0"
|
|
36
|
-
},
|
|
37
|
-
"peerDependenciesMeta": {
|
|
38
|
-
"react": {
|
|
39
|
-
"optional": true
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
"devDependencies": {
|
|
43
|
-
"@types/node": "^20.10.0",
|
|
44
|
-
"@types/react": "^18.2.0",
|
|
45
|
-
"rimraf": "^5.0.5",
|
|
46
|
-
"tsup": "^8.0.0",
|
|
47
|
-
"tsx": "^4.7.0",
|
|
48
|
-
"typescript": "5.4.2"
|
|
49
|
-
},
|
|
50
|
-
"engines": {
|
|
51
|
-
"node": ">=20.0.0"
|
|
52
|
-
}
|
|
53
|
-
}
|