OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.
TypeScript
fix: improve export stability and mobile audio compatibility
Augustus Otub2a76c4ff2a9
diff --git a/apps/web/src/services/export-presets.ts b/apps/web/src/services/export-presets.tsindex 54f10b99..45a1a0ad 100644--- a/apps/web/src/services/export-presets.ts+++ b/apps/web/src/services/export-presets.ts@@ -582,22 +582,22 @@ const WEB_PRESETS: PlatformExportPreset[] = [{id: "webm-vp9",name: "WebM VP9",- description: "Modern web format with VP9 codec",+ description: "Modern web format with VP9 codec (720p recommended)",platform: "Web",category: "web",aspectRatio: "16:9",settings: {format: "webm",codec: "vp9",- width: 1920,- height: 1080,+ width: 1280,+ height: 720,frameRate: 30,- bitrate: 4000,+ bitrate: 3000,bitrateMode: "vbr",quality: 80,keyframeInterval: 60,audioSettings: {- format: "ogg",+ format: "aac",sampleRate: 48000,bitDepth: 16,bitrate: 128,diff --git a/packages/core/src/export/export-engine.ts b/packages/core/src/export/export-engine.tsindex a925351e..6abb30a4 100644--- a/packages/core/src/export/export-engine.ts+++ b/packages/core/src/export/export-engine.ts@@ -142,10 +142,11 @@ export class ExportEngine {}}- for (const fallbackCodec of ["opus", "mp3"]) {+ for (const fallbackCodec of ["aac", "mp3", "opus"]) {if (supportedCodecs.some((c: string) =>- String(c).toLowerCase().includes(fallbackCodec),+ String(c).toLowerCase().includes(fallbackCodec) ||+ (fallbackCodec === "aac" && String(c).toLowerCase().includes("mp4a")),)) {for (const bitrate of bitrateFallbacks) {@@ -230,6 +231,29 @@ export class ExportEngine {},};+ const pixelCount = fullSettings.width * fullSettings.height;+ const isMemoryIntensiveCodec =+ fullSettings.codec === "vp9" ||+ fullSettings.codec === "av1" ||+ fullSettings.codec === "h265";+ const maxSafePixels = isMemoryIntensiveCodec ? 1920 * 1080 : 3840 * 2160;++ if (pixelCount > maxSafePixels && isMemoryIntensiveCodec) {+ console.warn(+ `[ExportEngine] ${fullSettings.codec.toUpperCase()} at ${fullSettings.width}x${fullSettings.height} may cause browser instability. Reducing to 1080p for stability.`,+ );+ const aspectRatio = fullSettings.width / fullSettings.height;+ if (aspectRatio > 1) {+ fullSettings.width = 1920;+ fullSettings.height = Math.round(1920 / aspectRatio);+ } else {+ fullSettings.height = 1920;+ fullSettings.width = Math.round(1920 * aspectRatio);+ }+ fullSettings.width = Math.round(fullSettings.width / 2) * 2;+ fullSettings.height = Math.round(fullSettings.height / 2) * 2;+ }+this.abortController = new AbortController();this.currentExport = { startTime: Date.now(), framesRendered: 0 };@@ -274,6 +298,7 @@ export class ExportEngine {AudioSample,getFirstEncodableVideoCodec,getFirstEncodableAudioCodec,+ QUALITY_MEDIUM,} = this.mediabunny!;let outputFormat;switch (fullSettings.format) {@@ -312,7 +337,7 @@ export class ExportEngine {const videoSource = new VideoSampleSource({codec: videoCodec,- bitrate: fullSettings.bitrate * 1000,+ bitrate: QUALITY_MEDIUM,keyFrameInterval:fullSettings.keyframeInterval / fullSettings.frameRate,hardwareAcceleration: "prefer-hardware",@@ -330,6 +355,12 @@ export class ExportEngine {await output.start();+ const isMemoryIntensiveCodec =+ fullSettings.codec === "vp9" ||+ fullSettings.codec === "av1" ||+ fullSettings.codec === "h265";+ const flushInterval = isMemoryIntensiveCodec ? 15 : 30;+for (let frame = 0; frame < totalFrames; frame++) {if (this.abortController.signal.aborted) {throw this.createError(@@ -371,6 +402,10 @@ export class ExportEngine {this.currentExport!.framesRendered = frame + 1;+ if ((frame + 1) % flushInterval === 0) {+ await new Promise((resolve) => setTimeout(resolve, 0));+ }+yield this.createProgress("rendering",(frame + 1) / totalFrames,@@ -846,28 +881,16 @@ export class ExportEngine {codec: "h264",},},- {- id: "web-vp9-4k",- name: "Web 4K (VP9)",- description: "4K WebM VP9 for web",- category: "web",- settings: {- ...DEFAULT_VIDEO_SETTINGS,- format: "webm",- codec: "vp9",- ...VIDEO_QUALITY_PRESETS["4k"],- },- },{id: "web-vp9",name: "Web (VP9)",- description: "WebM VP9 for web embedding",+ description: "WebM VP9 for web embedding (720p for stability)",category: "web",settings: {...DEFAULT_VIDEO_SETTINGS,format: "webm",codec: "vp9",- ...VIDEO_QUALITY_PRESETS["1080p"],+ ...VIDEO_QUALITY_PRESETS["720p"],},},{diff --git a/packages/core/src/export/types.ts b/packages/core/src/export/types.tsindex e9e3c1eb..935e5244 100644--- a/packages/core/src/export/types.ts+++ b/packages/core/src/export/types.ts@@ -114,8 +114,8 @@ export const DEFAULT_VIDEO_SETTINGS: VideoExportSettings = {width: 1920,height: 1080,frameRate: 30,- bitrate: 8000, // 8 Mbps- bitrateMode: "vbr",+ bitrate: 5000, // 5 Mbps - good quality for 1080p web video+ bitrateMode: "cbr",quality: 80,keyframeInterval: 60, // 2 seconds at 30fpsaudioSettings: {diff --git a/packages/core/src/video/video-engine.ts b/packages/core/src/video/video-engine.tsindex a8c6e704..07a79927 100644--- a/packages/core/src/video/video-engine.ts+++ b/packages/core/src/video/video-engine.ts@@ -12,6 +12,7 @@ import type { ShapeClip, EmphasisAnimation } from "../graphics/types";import { titleEngine } from "../text/title-engine";import { graphicsEngine } from "../graphics/graphics-engine";import { VideoEffectsEngine } from "./video-effects-engine";+import { getMediaEngine } from "../media/mediabunny-engine";import type {RenderedFrame,CompositeLayer,@@ -223,8 +224,33 @@ export class VideoEngine {}/**- * Decode a frame using native video element (hardware-accelerated, reliable seeking).- * Better for export than MediaBunny's CanvasSink.+ * Decode a frame using MediaBunny's WebCodecs-based decoder.+ * Much faster than video element seeking for export.+ */+ async decodeFrameWithMediaBunny(+ blob: Blob,+ time: number,+ width: number,+ _height: number,+ ): Promise<ImageBitmap | null> {+ try {+ const mediaEngine = getMediaEngine();+ if (!mediaEngine.isAvailable()) {+ await mediaEngine.initialize();+ }+ const result = await mediaEngine.getFrameAtTime(blob, time, width);+ if (result?.canvas) {+ return createImageBitmap(result.canvas);+ }+ return null;+ } catch (error) {+ console.warn("[VideoEngine] MediaBunny frame decode failed:", error);+ return null;+ }+ }++ /**+ * Decode a frame using native video element (fallback method).*/async decodeFrameWithVideoElement(mediaId: string,@@ -256,6 +282,7 @@ export class VideoEngine {const { video } = cached;video.currentTime = time;+await new Promise<void>((resolve) => {let resolved = false;const onSeeked = () => {@@ -265,7 +292,7 @@ export class VideoEngine {resolve();};video.addEventListener("seeked", onSeeked);- if (video.readyState >= 3) {+ if (video.readyState >= 2) {onSeeked();}setTimeout(() => {@@ -274,7 +301,7 @@ export class VideoEngine {video.removeEventListener("seeked", onSeeked);resolve();}- }, 5000);+ }, 3000);});const canvas = new OffscreenCanvas(width, height);@@ -399,13 +426,21 @@ export class VideoEngine {);}} else {- bitmap = await this.decodeFrameWithVideoElement(- mediaItem.id,+ bitmap = await this.decodeFrameWithMediaBunny(mediaItem.blob,clipInfo.sourceTime,settings.width,settings.height,);+ if (!bitmap) {+ bitmap = await this.decodeFrameWithVideoElement(+ mediaItem.id,+ mediaItem.blob,+ clipInfo.sourceTime,+ settings.width,+ settings.height,+ );+ }}if (bitmap) {@@ -459,15 +494,20 @@ export class VideoEngine {let processedBitmap = bitmap;if (clip.effects && clip.effects.length > 0) {- if (!this.effectsEngine) {- this.effectsEngine = new VideoEffectsEngine({- width,- height,- useGPU: false,- });- await this.effectsEngine.initialize();- }try {+ if (!this.effectsEngine) {+ this.effectsEngine = new VideoEffectsEngine({+ width,+ height,+ useGPU: true,+ preferWebGPU: false,+ });+ const initPromise = this.effectsEngine.initialize();+ const timeoutPromise = new Promise<boolean>((_, reject) =>+ setTimeout(() => reject(new Error("Effects init timeout")), 5000)+ );+ await Promise.race([initPromise, timeoutPromise]);+ }const effectsResult = await this.effectsEngine.applyEffects(bitmap,clip.effects,@@ -478,6 +518,7 @@ export class VideoEngine {`Failed to apply effects to clip ${clip.id}:`,error,);+ this.effectsEngine = null;}}