Skip to content

Latest commit

 

History

History
45 lines (34 loc) · 3.09 KB

webcodecs-fast-path.md

File metadata and controls

45 lines (34 loc) · 3.09 KB

WebCodecs + mediabunny media fast path

The built-in ffmpeg tool normally instantiates the single-threaded FFmpeg WASM core (~31 MB). For the most common jobs Co-do now skips that cold start. It runs the job through the browser's hardware-accelerated WebCodecs encoders/decoders using mediabunny (mediabunny@1.65.2, loaded with a lazy dynamic import()). If the request isn't covered, it falls back to FFmpeg automatically.

flowchart TD
  Call["ffmpeg tool call<br/>inputPath · args · outputPath · engine"] --> Validate["Validate args + read input"]
  Validate --> Plan["planFastPath(args, in, out)"]
  Plan -->|unsupported flag / container| FF["FFmpeg WASM (unchanged)"]
  Plan -->|supported| Select["selectMediaEngine(plan, detectWebCodecsSupport(), engine)"]
  Select -->|needs codecs but WebCodecs missing| FF
  Select -->|engine = ffmpeg| FF
  Select -->|webcodecs| Run["executeFastPath → mediabunny Conversion<br/>prefer-hardware"]
  Run -->|success| Out["Output bytes<br/>stderr: [fast-path] … FFmpeg WASM not loaded"]
  Run -->|invalid / track dropped / error| FF
Loading

What takes the fast path

Input containers Output containers
.mp4 .m4v .mov .webm .mkv .mp4 .m4v .mov .webm .mkv

Supported arguments:

  • Trim: -ss, -to, -t (seconds, MM:SS, HH:MM:SS.ms, 2s/500ms)
  • Remux / stream copy: -c copy, -codec copy, -c:v copy, -c:a copy (copy-only jobs use mediabunny's forced copy mode and need no WebCodecs at all)
  • Video transcode: -c:v libx264|h264|libx265|hevc|libvpx|vp8|libvpx-vp9|vp9|libaom-av1|libsvtav1|av1, -b:v, -crf (mapped to a quality level), -r, -vf scale=W:H (with -1/-2 aspect preservation)
  • Audio transcode: -c:a aac|libopus|opus, -b:a
  • Track selection: -an, -vn
  • Ignored (harmless): -y, -movflags +faststart, -preset, -tune, -hide_banner, -loglevel

Anything else is reported as unsupported with a reason and goes to FFmpeg. That includes other -vf filters, -filter_complex, -map, GIF/MP3/WAV output, subtitles and so on.

Safety nets

  • If mediabunny reports the conversion as invalid, or would drop a track for any reason other than an explicit -an/-vn, the fast path aborts and FFmpeg runs instead. This means you never silently lose audio.
  • The fast path never touches the file system itself. inputPath/outputPath handling, permissions and result caching are the same as before (src/wasm-tools/manager.ts).
  • engine: "ffmpeg" always forces FFmpeg. engine: "webcodecs" prefers the fast path and explains in stderr why it had to fall back.

Code map

  • src/wasm-tools/adapters/webcodecs-fast-path.ts: the pure planner (parseTimecode, parseBitrate, parseScaleFilter, planFastPath), detectWebCodecsSupport, selectMediaEngine and executeFastPath.
  • src/wasm-tools/adapters/ffmpeg-adapter.ts: tries the fast path before ensureInitialized() loads FFmpeg WASM.
  • tests/unit/webcodecs-fast-path.test.ts: planner accept/reject matrix, engine selection, a mocked mediabunny conversion, and adapter fallback.