From 1d9f10fd32ef30741c4c386b71f3be90c9271d43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Kr=C3=BCger?= Date: Mon, 17 Nov 2025 10:54:03 +0100 Subject: [PATCH] fix: optimize FFmpeg encoding to reduce memory usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Switch from VP9 to VP8 codec for WebM (less memory-intensive) - Add realtime encoding deadline and fast CPU preset - Add ultrafast preset for x264 encoding - Set explicit bitrates to control memory usage - Use libvorbis instead of libopus for better compatibility This fixes "memory access out of bounds" errors during video conversion in browser environments with limited WASM memory. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- lib/converters/ffmpegService.ts | 16 ++++++++++++++-- lib/utils/formatMappings.ts | 6 +++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/converters/ffmpegService.ts b/lib/converters/ffmpegService.ts index 4bc7b78..e949f68 100644 --- a/lib/converters/ffmpegService.ts +++ b/lib/converters/ffmpegService.ts @@ -120,12 +120,24 @@ function buildFFmpegArgs( // Format-specific settings switch (outputFormat) { case 'webm': - if (!options.videoCodec) args.push('-c:v', 'libvpx-vp9'); - if (!options.audioCodec) args.push('-c:a', 'libopus'); + // Use VP8 by default (less memory-intensive than VP9) + if (!options.videoCodec) args.push('-c:v', 'libvpx'); + if (!options.audioCodec) args.push('-c:a', 'libvorbis'); + // Optimize for faster encoding and lower memory usage + args.push('-deadline', 'realtime'); + args.push('-cpu-used', '8'); + // Set quality/bitrate if not specified + if (!options.videoBitrate) args.push('-b:v', '1M'); + if (!options.audioBitrate) args.push('-b:a', '128k'); break; case 'mp4': if (!options.videoCodec) args.push('-c:v', 'libx264'); if (!options.audioCodec) args.push('-c:a', 'aac'); + // Use faster preset for browser encoding + args.push('-preset', 'ultrafast'); + args.push('-tune', 'zerolatency'); + if (!options.videoBitrate) args.push('-b:v', '1M'); + if (!options.audioBitrate) args.push('-b:a', '128k'); break; case 'mp3': if (!options.audioCodec) args.push('-c:a', 'libmp3lame'); diff --git a/lib/utils/formatMappings.ts b/lib/utils/formatMappings.ts index a842552..9106b73 100644 --- a/lib/utils/formatMappings.ts +++ b/lib/utils/formatMappings.ts @@ -218,14 +218,14 @@ export const FORMAT_PRESETS: FormatPreset[] = [ { id: 'web-video', name: 'Web Video', - description: 'Optimize video for web playback', + description: 'Optimize video for web playback (VP8 for better compatibility)', category: 'video', sourceFormats: ['mp4', 'avi', 'mov', 'mkv'], targetFormat: 'webm', options: { - videoCodec: 'libvpx-vp9', + videoCodec: 'libvpx', videoBitrate: '1M', - audioCodec: 'libopus', + audioCodec: 'libvorbis', audioBitrate: '128k', }, },