Create professional demos and 3D mockups in seconds, directly in your browser
feat: optimize video export latency and frame handling for improved performance
CristianOlivera171d75719bffa
diff --git a/hooks/useVideoExport.ts b/hooks/useVideoExport.tsindex e1198c9..e81e15d 100644--- a/hooks/useVideoExport.ts+++ b/hooks/useVideoExport.ts@@ -264,7 +264,8 @@ async function exportWithMediabunny(codec: "avc",bitrate: bitrate,bitrateMode: "variable",- latencyMode: "quality",+ latencyMode: "realtime",+ keyFrameInterval: fps * 2,fullCodecString: "avc1.640033",hardwareAcceleration: "prefer-hardware",});@@ -283,7 +284,8 @@ async function exportWithMediabunny(codec: "avc",bitrate: bitrate,bitrateMode: "variable",- latencyMode: "quality",+ latencyMode: "realtime",+ keyFrameInterval: fps * 2,fullCodecString: "avc1.640033",hardwareAcceleration: "prefer-software",});@@ -313,14 +315,18 @@ async function exportWithMediabunny(await canvasHandle.drawFrame(true, timelineTime);- await videoSource.add(outputTime, frameDuration);-+ // Seek the next frame BEFORE adding the current one to the encoder,+ // overlapping the browser's video decode with the encoder's work.const nextIndex = frameIndex + 1;+ let nextFrameReady: Promise<void> | null = null;if (nextIndex < totalFrames) {const nextContentOffset = Math.min((nextIndex / fps) * speed, duration - 0.001);video.currentTime = trimStart + nextContentOffset;+ nextFrameReady = waitForVideoFrame(video);}+ await videoSource.add(outputTime, frameDuration);+if (frameIndex % 10 === 0 || frameIndex === totalFrames - 1) {const progress = 10 + Math.round((frameIndex / totalFrames) * 80);setProgress({@@ -330,8 +336,8 @@ async function exportWithMediabunny(});}- if (nextIndex < totalFrames) {- await waitForVideoFrame(video);+ if (nextFrameReady) {+ await nextFrameReady;}}@@ -451,7 +457,8 @@ async function exportWithMediabunnyAndAudio(codec: "avc",bitrate: bitrate,bitrateMode: "variable",- latencyMode: "quality",+ latencyMode: "realtime",+ keyFrameInterval: fps * 2,fullCodecString: "avc1.640033",hardwareAcceleration: "prefer-hardware",});@@ -466,7 +473,8 @@ async function exportWithMediabunnyAndAudio(codec: "avc",bitrate: bitrate,bitrateMode: "variable",- latencyMode: "quality",+ latencyMode: "realtime",+ keyFrameInterval: fps * 2,fullCodecString: "avc1.640033",hardwareAcceleration: "prefer-software",});@@ -541,16 +549,21 @@ async function exportWithMediabunnyAndAudio(}await canvasHandle.drawFrame(true, timelineTime);- await videoSource.add(outputTime, frameDuration);+ // Seek the next frame BEFORE adding the current one to the encoder,+ // so the browser's decoder works in parallel with the encoder.+ let nextFrameReady: Promise<void> | null = null;if (!hasMultipleClips) {const nextFrame = frameIndex + 1;if (nextFrame < totalFrames) {const nextContentOffset = Math.min((nextFrame / fps) * speed, duration - 0.001);video.currentTime = trimStart + nextContentOffset;+ nextFrameReady = waitForVideoFrame(video);}}+ await videoSource.add(outputTime, frameDuration);+if (frameIndex % 10 === 0 || frameIndex === totalFrames - 1) {const progress = 5 + Math.round((frameIndex / totalFrames) * 50);setProgress({@@ -560,11 +573,8 @@ async function exportWithMediabunnyAndAudio(});}- if (!hasMultipleClips) {- const nextFrame = frameIndex + 1;- if (nextFrame < totalFrames) {- await waitForVideoFrame(video);- }+ if (nextFrameReady) {+ await nextFrameReady;}}} finally {@@ -786,21 +796,25 @@ async function exportWithMediabunnyAndAudio(const outputData = (await ffmpeg.readFile("output.mp4")) as Uint8Array;const outputBlob = new Blob([new Uint8Array(outputData)], { type: "video/mp4" });- try {- await ffmpeg.deleteFile("video.mp4");- await ffmpeg.deleteFile("output.mp4");- if (hasSourceAudio && !hasMultipleClips) await ffmpeg.deleteFile("original.mp4");- for (const { filename } of clipAudioFiles) {- await ffmpeg.deleteFile(filename).catch(() => { });- }- for (const audioTrackFile of audioTracks) {- await ffmpeg.deleteFile(audioTrackFile.filename);- }- } catch { }-+ // Start download immediately; clean up temp files in the background+ // so the user doesn't wait for file deletion to complete.downloadBlob(outputBlob, `openvid-${width}x${height}.mp4`);setProgress({ status: "complete", progress: 100, message: "Export with audio complete!" });+ (async () => {+ try {+ await ffmpeg.deleteFile("video.mp4");+ await ffmpeg.deleteFile("output.mp4");+ if (hasSourceAudio && !hasMultipleClips) await ffmpeg.deleteFile("original.mp4");+ for (const { filename } of clipAudioFiles) {+ await ffmpeg.deleteFile(filename).catch(() => { });+ }+ for (const audioTrackFile of audioTracks) {+ await ffmpeg.deleteFile(audioTrackFile.filename);+ }+ } catch { }+ })();+} catch (ffmpegError) {console.warn("FFmpeg audio processing failed, exporting video only:", ffmpegError);downloadBlob(videoBlob, `openvid-${width}x${height}.mp4`);diff --git a/lib/video.utils.ts b/lib/video.utils.tsindex 806041b..4fe41e1 100644--- a/lib/video.utils.ts+++ b/lib/video.utils.ts@@ -4,19 +4,21 @@ import { forceResolveVideoDuration } from './webm-duration.utils';export function waitForVideoFrame(video: HTMLVideoElement): Promise<void> {return new Promise((resolve) => {let resolved = false;-+const done = () => {if (!resolved) {resolved = true;resolve();}};-+if ('requestVideoFrameCallback' in HTMLVideoElement.prototype) {// eslint-disable-next-line @typescript-eslint/no-explicit-any(video as any).requestVideoFrameCallback(done);- // Long safety timeout to prevent blocking- setTimeout(done, 2000);+ // Safety timeout: 500ms is enough for local video seeks. The+ // previous 2000ms value caused unnecessary stalls when the+ // callback was missed (e.g. seeking to the same timestamp).+ setTimeout(done, 500);} else {// Fallback for browsers that do not support requestVideoFrameCallbackif (video.readyState >= 2) {@@ -58,9 +60,9 @@ export async function ensureVideoReady(video: HTMLVideoElement): Promise<void> {// Pausar y mover al iniciovideo.pause();video.currentTime = 0;-- // Brief wait for the frame to be ready- await new Promise<void>(resolve => setTimeout(resolve, 100));++ // Wait for the first frame to be ready (event-driven, not a fixed delay)+ await waitForVideoFrame(video);}export function formatTime(time: number): string {