<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muhammad Afan</title>
    <description>The latest articles on DEV Community by Muhammad Afan (@muhammad_afan).</description>
    <link>https://dev.to/muhammad_afan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4208734%2F671372ad-ec9e-46ef-953e-5b0d9728cf3f.jpeg</url>
      <title>DEV Community: Muhammad Afan</title>
      <link>https://dev.to/muhammad_afan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/muhammad_afan"/>
    <language>en</language>
    <item>
      <title>I replaced FFmpeg.wasm with WebCodecs in my browser video tools. Compression went from 97 s to 5.6 s</title>
      <dc:creator>Muhammad Afan</dc:creator>
      <pubDate>Tue, 27 Oct 2026 14:03:09 +0000</pubDate>
      <link>https://dev.to/muhammad_afan/i-replaced-ffmpegwasm-with-webcodecs-in-my-browser-video-tools-compression-went-from-97-s-to-56-s-32em</link>
      <guid>https://dev.to/muhammad_afan/i-replaced-ffmpegwasm-with-webcodecs-in-my-browser-video-tools-compression-went-from-97-s-to-56-s-32em</guid>
      <description>&lt;p&gt;I run &lt;a href="https://dashutils.com" rel="noopener noreferrer"&gt;DashUtils&lt;/a&gt;, a site with 392 free file tools: image and PDF converters, a video compressor, an MP3 cutter and so on. The rule for every tool is simple. Nothing gets uploaded; the work happens in your browser.&lt;/p&gt;

&lt;p&gt;Until last week, every video and audio tool ran on FFmpeg.wasm. It handles every format you can think of, but it has costs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A 31 MB engine&lt;/strong&gt; (about 10 MB compressed) has to download before the first job starts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One CPU thread.&lt;/strong&gt; The multi-threaded build needs &lt;code&gt;SharedArrayBuffer&lt;/code&gt;, which means cross-origin isolation, which fights with third-party embeds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No hardware acceleration.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I tried my own MP4-to-GIF tool and closed the tab before it finished. That was the push I needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  The switch: WebCodecs, through Mediabunny
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebCodecs_API" rel="noopener noreferrer"&gt;WebCodecs&lt;/a&gt; gives JavaScript the browser's own video and audio encoders and decoders, using the GPU where the platform allows. On its own it only handles raw frames, so I used &lt;a href="https://mediabunny.dev" rel="noopener noreferrer"&gt;Mediabunny&lt;/a&gt; (MPL-2.0) for reading and writing MP4, MOV, WebM, MKV, MP3, WAV, OGG and FLAC. Its core is about 140 KB compressed, and it's only loaded when someone opens a file.&lt;/p&gt;

&lt;p&gt;The core of a conversion is short:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ALL_FORMATS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;BlobSource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;BufferTarget&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Conversion&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Mp4OutputFormat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;QUALITY_MEDIUM&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mediabunny&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BlobSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="na"&gt;formats&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ALL_FORMATS&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BufferTarget&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Mp4OutputFormat&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;fastStart&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;in-memory&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;conversion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Conversion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;video&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;codec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;avc&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;720&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;QUALITY_MEDIUM&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;audio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;codec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aac&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// A track we can't encode here gets discarded, not converted. Treat that as "not supported".&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;conversion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isValid&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;conversion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;discardedTracks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reason&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;discarded_by_user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;runWithFFmpeg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;conversion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mp4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Blob&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;video/mp4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;FFmpeg.wasm didn't go away. It's the fallback for AVI, FLV and WMV, which browsers can't read, and for the speed and volume filters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Five things that quietly made it slow
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. A silent fallback
&lt;/h3&gt;

&lt;p&gt;The first benchmark looked great everywhere except compression, which still took 97 seconds. The reason: &lt;strong&gt;Chrome on Linux can't encode AAC audio&lt;/strong&gt;. Mediabunny discarded the audio track, my check saw that, and the job fell back to FFmpeg without telling anyone.&lt;/p&gt;

&lt;p&gt;The fix was to check &lt;code&gt;canEncodeAudio("aac")&lt;/code&gt; and, only when it's missing, register &lt;a href="https://www.npmjs.com/package/@mediabunny/aac-encoder" rel="noopener noreferrer"&gt;&lt;code&gt;@mediabunny/aac-encoder&lt;/code&gt;&lt;/a&gt;, a WebAssembly encoder of about 260 KB compressed. After that, compression took 5.6 seconds. Lesson: log which engine actually ran, not which one you expected.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. The progress bar was the bottleneck
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;onProgress&lt;/code&gt; fires for every frame and every audio chunk. I passed it straight into React state, so converting 30 seconds of MP3 to WAV re-rendered the page about 1,150 times. The conversion itself took 0.43 seconds; the page took 5.1. Throttling progress to ten updates a second brought it to 0.62 seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Browser encoders have a bitrate floor
&lt;/h3&gt;

&lt;p&gt;To fit a video under Discord's limit, you work backwards: bitrate = size ÷ duration. At 1080p, Chrome's software H.264 encoder simply ignored targets below about 2 Mbps. A "10 MB" export came out at 14.7 MB, even after re-encoding at lower bitrates.&lt;/p&gt;

&lt;p&gt;Now the compressor picks the resolution from the budget (roughly 0.04 bits per pixel per frame), checks the result, and re-encodes smaller only if it's still over. A 60-second 1080p clip lands at 9.3 MB, scaled to 720p, in 21 seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Reading pixels back from the GPU
&lt;/h3&gt;

&lt;p&gt;GIFs need raw pixels for colour quantisation. &lt;code&gt;getImageData()&lt;/code&gt; on the canvas the decoder drew into cost about 45 ms per frame, because it forces a GPU-to-CPU copy. &lt;code&gt;createImageBitmap(frame, { resizeWidth, resizeHeight })&lt;/code&gt; followed by a &lt;code&gt;willReadFrequently&lt;/code&gt; canvas was about three times faster. Moving the quantisation (&lt;a href="https://github.com/mattdesl/gifenc" rel="noopener noreferrer"&gt;gifenc&lt;/a&gt;) into a pool of workers, one per core, took a 72-frame portrait GIF from 15.2 to 3.5 seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Asking for a bitrate turned copies into encodes
&lt;/h3&gt;

&lt;p&gt;Trimming a clip should only copy data. But I always passed an audio bitrate, and that made Mediabunny re-encode the audio. Setting the bitrate only when the audio really is being transcoded took a trim from 5.5 seconds to 0.31.&lt;/p&gt;

&lt;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3jbeiiey5frltxxcftdt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3jbeiiey5frltxxcftdt.png" alt="Before and after: in-browser video jobs with FFmpeg.wasm vs WebCodecs" width="800" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All measured in the browser on a 2016 laptop (Intel i7-6820HQ, Linux, no hardware video encoder). "Before" is FFmpeg.wasm for compression and my first WebCodecs version for the others.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Job&lt;/th&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Compress a 20 s 1080p clip&lt;/td&gt;
&lt;td&gt;97.1 s&lt;/td&gt;
&lt;td&gt;5.6 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;72-frame portrait GIF&lt;/td&gt;
&lt;td&gt;15.2 s&lt;/td&gt;
&lt;td&gt;3.5 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Trim a 20 s clip&lt;/td&gt;
&lt;td&gt;5.5 s&lt;/td&gt;
&lt;td&gt;0.31 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;30 s MP3 to WAV&lt;/td&gt;
&lt;td&gt;5.1 s&lt;/td&gt;
&lt;td&gt;0.62 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Opening a file went from "download 31 MB first" to about a tenth of a second.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell anyone trying this
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Detect support per codec, not per API.&lt;/strong&gt; &lt;code&gt;VideoEncoder&lt;/code&gt; existing tells you nothing about AAC or HEVC.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the old engine as a fallback&lt;/strong&gt;, and make the switch visible in your logs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test on your slowest supported setup.&lt;/strong&gt; Every problem above was invisible on a fast Mac.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Throttle anything that fires per frame&lt;/strong&gt; before it reaches your UI.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to try it, the &lt;a href="https://dashutils.com/tools/video-compressor" rel="noopener noreferrer"&gt;video compressor&lt;/a&gt; and &lt;a href="https://dashutils.com/tools/mp4-to-gif" rel="noopener noreferrer"&gt;MP4 to GIF&lt;/a&gt; are the best demos. I'd love to hear how they run on your machine.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>performance</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
