<?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: liuliuball</title>
    <description>The latest articles on DEV Community by liuliuball (@xie233).</description>
    <link>https://dev.to/xie233</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%2F4267998%2Fb31f0254-33c8-4ffe-a67f-40dad70f5990.jpg</url>
      <title>DEV Community: liuliuball</title>
      <link>https://dev.to/xie233</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xie233"/>
    <language>en</language>
    <item>
      <title>Compressing Video to an Exact MB Target in the Browser with FFmpeg.wasm</title>
      <dc:creator>liuliuball</dc:creator>
      <pubDate>Fri, 27 Nov 2026 03:27:20 +0000</pubDate>
      <link>https://dev.to/xie233/compressing-video-to-an-exact-mb-target-in-the-browser-with-ffmpegwasm-5e4c</link>
      <guid>https://dev.to/xie233/compressing-video-to-an-exact-mb-target-in-the-browser-with-ffmpegwasm-5e4c</guid>
      <description>&lt;p&gt;If you have ever tried to compress a video to fit under a 25 MB email attachment limit or a 10 MB chat limit, you have probably noticed that FFmpeg's &lt;code&gt;-crf&lt;/code&gt; flag cannot guarantee a file size.&lt;/p&gt;

&lt;p&gt;When we built the local &lt;a href="https://videoediting.tips/en/video-compressor" rel="noopener noreferrer"&gt;video compressor on VideoEditingTips&lt;/a&gt; using FFmpeg.wasm, our goal was simple: let users pick 10 MB, 25 MB, or a custom MB limit, process the file completely inside the browser (no server upload), and verify the real output byte count.&lt;/p&gt;

&lt;p&gt;Here are the three practical details that made it reliable.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Budget for container and audio overhead first
&lt;/h2&gt;

&lt;p&gt;A naive &lt;code&gt;targetBytes * 8 / duration&lt;/code&gt; calculation almost always overshoots because of MP4 atom overhead (&lt;code&gt;moov&lt;/code&gt;/&lt;code&gt;mdat&lt;/code&gt;), audio bitrate, and x264 rate-control variance. We reserve 6% + 8 KB for container overhead and step down audio bitrate (&lt;code&gt;128k&lt;/code&gt; -&amp;gt; &lt;code&gt;64k&lt;/code&gt; -&amp;gt; &lt;code&gt;32k&lt;/code&gt;) when the total budget is tight:&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;targetBytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;targetMB&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1_000_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;totalKbps&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="n"&gt;targetBytes&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.94&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;8192&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;duration&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;audioKbps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;totalKbps&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;512&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;128&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;totalKbps&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;256&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;videoKbps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;totalKbps&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;audioKbps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Constrain x264 with &lt;code&gt;maxrate&lt;/code&gt; and &lt;code&gt;bufsize&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Running single-pass ABR in WebAssembly is much faster than 2-pass, so we pair &lt;code&gt;-b:v&lt;/code&gt; with &lt;code&gt;-maxrate&lt;/code&gt; and a 2x &lt;code&gt;-bufsize&lt;/code&gt;, plus &lt;code&gt;-movflags +faststart&lt;/code&gt; and even-dimension scaling (&lt;code&gt;scale=trunc(iw/2)*2:trunc(ih/2)*2&lt;/code&gt;) so odd-resolution screen recordings do not fail &lt;code&gt;yuv420p&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Measure actual bytes and retry from the original input
&lt;/h2&gt;

&lt;p&gt;Even with VBV constraints, high-motion clips can still land a few percent over target. Instead of silently returning an oversized file, we check &lt;code&gt;data.byteLength &amp;lt;= targetBytes&lt;/code&gt; and retry up to 3 times — always encoding from the original input rather than re-encoding a lossy intermediate.&lt;/p&gt;

&lt;p&gt;We extracted this bitrate-budgeting and retry helper into a small MIT package on GitHub: &lt;a href="https://github.com/xie233/ffmpeg-target-size" rel="noopener noreferrer"&gt;xie233/ffmpeg-target-size&lt;/a&gt;. If you want to test the full flow locally on your own clips (no uploads, no watermark), try the live &lt;a href="https://videoediting.tips/en/video-compressor" rel="noopener noreferrer"&gt;browser video compressor&lt;/a&gt; or browse our &lt;a href="https://videoediting.tips/en/ffmpeg" rel="noopener noreferrer"&gt;copy-paste FFmpeg command recipes&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webassembly</category>
      <category>ffmpeg</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
