fix: optimize FFmpeg encoding to reduce memory usage

- 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 <noreply@anthropic.com>
This commit is contained in:
2025-11-17 10:54:03 +01:00
parent fc02b2d9af
commit 1d9f10fd32
2 changed files with 17 additions and 5 deletions

View File

@@ -120,12 +120,24 @@ function buildFFmpegArgs(
// Format-specific settings // Format-specific settings
switch (outputFormat) { switch (outputFormat) {
case 'webm': case 'webm':
if (!options.videoCodec) args.push('-c:v', 'libvpx-vp9'); // Use VP8 by default (less memory-intensive than VP9)
if (!options.audioCodec) args.push('-c:a', 'libopus'); 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; break;
case 'mp4': case 'mp4':
if (!options.videoCodec) args.push('-c:v', 'libx264'); if (!options.videoCodec) args.push('-c:v', 'libx264');
if (!options.audioCodec) args.push('-c:a', 'aac'); 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; break;
case 'mp3': case 'mp3':
if (!options.audioCodec) args.push('-c:a', 'libmp3lame'); if (!options.audioCodec) args.push('-c:a', 'libmp3lame');

View File

@@ -218,14 +218,14 @@ export const FORMAT_PRESETS: FormatPreset[] = [
{ {
id: 'web-video', id: 'web-video',
name: 'Web Video', name: 'Web Video',
description: 'Optimize video for web playback', description: 'Optimize video for web playback (VP8 for better compatibility)',
category: 'video', category: 'video',
sourceFormats: ['mp4', 'avi', 'mov', 'mkv'], sourceFormats: ['mp4', 'avi', 'mov', 'mkv'],
targetFormat: 'webm', targetFormat: 'webm',
options: { options: {
videoCodec: 'libvpx-vp9', videoCodec: 'libvpx',
videoBitrate: '1M', videoBitrate: '1M',
audioCodec: 'libopus', audioCodec: 'libvorbis',
audioBitrate: '128k', audioBitrate: '128k',
}, },
}, },