Skip to content

Investigate a WebCodecs conversion pipeline (native encode, no Wasm SIMD requirement) #78

Closed

Description

@liladas

Summary

Investigate replacing the ffmpeg.wasm conversion path with a native browser pipeline: render CDG frames to a canvas in JS, encode with WebCodecs VideoEncoder, and mux to MP4. This would be faster for everyone on modern hardware and would fix most of the devices that cannot convert at all today, without shipping a second wasm binary.

Follow-up to #77.

Why

Two problems point at the same solution.

1. Some devices cannot convert at all. @ffmpeg/core 0.12.x ships a SIMD-enabled wasm. Browsers on a CPU without SSE4.1 disable Wasm SIMD, so the core fails to compile before it reads any user file. PostHog, all history:

Browser successful sessions load-stage failure sessions
Chrome 109 (Win) 0 8
Firefox 115 (Win) 0 3
Chrome 150 (Win) 59 5 (2 SIMD-confirmed)
Brave 7 1

Of 136 conversion sessions, 16 never succeeded. #77 shipped an honest error message and surfaced the local ffmpeg command, but those users still cannot convert in the browser.

2. The conversion is slow for everyone else, because H.264 encoding runs in wasm with a -preset veryfast concession, on top of a ~31MB core (9.8MB gzipped) that every first-time user downloads.

A WebCodecs pipeline addresses both: the encode happens in the browser's own native, often hardware-accelerated encoder. No wasm engine is involved, so the SIMD gate does not apply, and there is no multi-megabyte core to fetch.

Proposed pipeline

Stage Today Proposed
CDG decode ffmpeg cdgraphics decoder (wasm) cdgraphics 7.0.0 (JS, renders to canvas)
Video encode libx264 in wasm native VideoEncoder (H.264)
Audio ffmpeg AAC (wasm) AudioEncoder, or remux the source MP3
Mux ffmpeg mediabunny 1.52.2

Both libraries are current: cdgraphics (Aug 2025) is the renderer used by KaraokeEternal; mediabunny (Jul 2026) is actively developed. By contrast @ffmpeg/core last released Jan 2025, and ffmpegwasm/ffmpeg.wasm#238 ("does simd feature support?") has been open and unanswered since 2021, so a non-SIMD core is not coming from upstream.

Support

VideoEncoder, per MDN browser-compat-data:

Browser Minimum
Chrome / Edge 94
Safari 16.4
Firefox 130

Chrome 109 has it, which covers 8 of the 11 provably-dead sessions. ffmpeg.wasm must remain as the fallback for Firefox 115, Safari <16.4, and any device where VideoEncoder.isConfigSupported() rejects the H.264 config. This is an added path, not a replacement.

Selection order at runtime:

  1. VideoEncoder present and the H.264 config is supported → native pipeline.
  2. Otherwise hasWasmSimd() → existing ffmpeg.wasm pipeline.
  3. Otherwise the error message and local ffmpeg command from Tell SIMD-less devices why the converter cannot run (2.3.7) #77.

Open questions

  • Render fidelity. The JS cdgraphics renderer must match ffmpeg's cdgraphics decoder. CDG has enough edge cases (XOR fills, scrolling/border presets, transparency, palette swaps mid-song) that this needs frame-level comparison on real rips, not spot checks.
  • Frame timing. CDG is 300 packets/sec; output is 30fps. Today ffmpeg owns that resampling. We would own it, and drift over a 3+ minute song would be audible as lip-sync error.
  • The tpad behavior from Hold the last CDG frame instead of ending the video at the graphics end (2.3.4) #69 (hold the last CDG frame when the graphics end before the audio) has to be reimplemented, since it is currently an ffmpeg filter.
  • Audio. Remuxing the source MP3 into MP4 avoids re-encoding entirely but is less compatible than AAC; AudioEncoder support needs its own check.
  • Does it actually help the target hardware? Chrome 150 on a pre-SSE4.1 CPU would fall back to software H.264 encoding. Native and scalar-safe, so it should work, but "should" needs measuring.
  • Quality parity against the current libx264 -preset veryfast output at each resolution.

Suggested phases

  1. Spike. Prove cdgraphics + VideoEncoder + mediabunny produces a correct MP4 from TRKD1502.zip (the file from Tell SIMD-less devices why the converter cannot run (2.3.7) #77, a known-good 187s CDG + 188s MP3). Compare frames and A/V sync against the current output. Measure encode time. Stop here if fidelity or sync does not hold.
  2. Capability detection + routing, with the ffmpeg.wasm path intact behind it, and an analytics property recording which pipeline ran so the split is visible in PostHog.
  3. Roll out behind the existing feedback loop, watching conversion_failed by pipeline before considering ffmpeg.wasm demotion.

Non-goals

  • Removing ffmpeg.wasm. It stays as the fallback.
  • A custom non-SIMD ffmpeg build. This issue is the alternative to that: it avoids a second ~10MB binary and a bespoke emscripten toolchain, and unlike a non-SIMD core it makes things faster rather than slower.
  • Server-side conversion, which would contradict PRIVACY.md.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions