Exporting 60fps Audio-Synced 1080p MP4s in Pure Browser JS with WebCodecs (
Most browser-based audio visualizers stop at a live <canvas> preview or delegate video rendering to a slow server-side FFmpeg queue. When we built SoundFlick—a browser-native release studio for indie musicians and AI mus
Most browser-based audio visualizers stop at a live <canvas> preview or delegate video rendering to a slow server-side FFmpeg queue. When we built SoundFlick—a browser-native release studio for indie musicians and AI music creators—we wanted the entire pipeline from MP3 decoding to 1080p MP4 export to run deterministically inside the user's tab.
Here are three hard engineering problems we hit when pairing WebGL1, Web Audio, and WebCodecs, and how we solved them.
1. Real-Time MediaRecorder Drifts; Deterministic Frame Stepping Does Not
Recording a live <canvas> stream with MediaRecorder ties frame timestamps to main-thread requestAnimationFrame jitter. If a tab briefly stutters or encodes slightly slower than real time, audio and video drift apart and keyframe spacing breaks.
Instead, SoundFlick renders deterministically frame by frame:
- Decode the full audio track with
AudioContext.decodeAudioData()and slice the selected 15s/30s chorus hook. - Step a fixed virtual clock
t = frameIndex / fps(60fpspreview, deterministic export clock). - Feed each stepped frame (
VideoFrame) intoVideoEncoder(avc1H.264) and feed PCM chunks intoAudioEncoder(mp4a.40.2AAC). - Mux encoded chunks directly in memory via a zero-copy
Blob-part MP4 muxer withfaststart(moovbeforemdat) so exported MP4s play immediately on TikTok, Reels, YouTube Shorts, and iOS Photos.
Across our automated Chrome + ffprobe test suite, measured audio/video duration drift stays within <= 40ms (typically ~2.7ms on 15s 1080p exports).
2. Guarding Against Silent Exports and Mobile Sample-Rate Edge Cases
Two real-world audio traps surfaced right after launch:
-
Zero-chunk silent MP4s: If an encoder configuration fails silently or audio chunks aren't flushed before muxer finalization, the user downloads a valid-looking MP4 with no sound. We added a strict guard (
audioChunks.length === 0aborts export with a clear diagnostic error instead of producing a silent file). -
Mobile sample-rate mismatches: Mobile Safari and Chrome instances frequently report hardware sample rates (
44100 Hzvs48000 Hz) that differ from naive encoder defaults, requiring explicit fallback resampling before passingAudioDatatoAudioEncoder.
3. Keeping Landing Pages Fast (0 KB Studio JS on Entry)
A full WebGL shader + WebCodecs studio bundle is heavier than a marketing page should ship on initial load. Instead of hydrating the whole studio on / or /ai-lyric-video-maker, SoundFlick decouples them:
-
/and AI Lyric Video Maker ship lightweight pre-rendered preview loops and an in-memory handoff dropzone (0 KBStudio engine JS on the landing route). - Dropping an MP3/WAV or pasting lyrics hands the
Fileobject and text straight into/studioin memory with zero double-upload.
Try It in Chrome
You can load a built-in demo track or drop your own MP3 at soundflick.com with no signup required and inspect the exported 1080p MP4 locally with ffprobe.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.