<?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: mengyuxuan</title>
    <description>The latest articles on DEV Community by mengyuxuan (@mengyuxuan).</description>
    <link>https://dev.to/mengyuxuan</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%2F4041464%2Fa7983a53-ea41-4f63-b0d1-4adfab57e3c1.jpg</url>
      <title>DEV Community: mengyuxuan</title>
      <link>https://dev.to/mengyuxuan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mengyuxuan"/>
    <language>en</language>
    <item>
      <title>Three things that broke when I moved video compression into the browser</title>
      <dc:creator>mengyuxuan</dc:creator>
      <pubDate>Wed, 19 Aug 2026 02:41:10 +0000</pubDate>
      <link>https://dev.to/mengyuxuan/three-things-that-broke-when-i-moved-video-compression-into-the-browser-41l8</link>
      <guid>https://dev.to/mengyuxuan/three-things-that-broke-when-i-moved-video-compression-into-the-browser-41l8</guid>
      <description>&lt;p&gt;I run &lt;a href="https://videocompress.dev/" rel="noopener noreferrer"&gt;a video compressor that works entirely in the browser&lt;/a&gt;.&lt;br&gt;
No upload, no server, nothing leaves the machine. Two of the three bugs below&lt;br&gt;
only showed up when I stopped reading code and started timing things, so I want&lt;br&gt;
to write them down while the numbers are still in front of me.&lt;/p&gt;

&lt;p&gt;The pipeline has three paths. If the file already meets the target, copy the&lt;br&gt;
encoded samples and encode nothing. If the browser can decode the container,&lt;br&gt;
transcode with WebCodecs. Otherwise fall back to ffmpeg.wasm, which is roughly&lt;br&gt;
an order of magnitude slower. mediabunny handles the demux and remux.&lt;/p&gt;

&lt;h2&gt;
  
  
  AVI to Matroska with -c copy produces a 1151 byte file
&lt;/h2&gt;

&lt;p&gt;The fast path needs mediabunny to be able to read the container. It cannot read&lt;br&gt;
AVI, but plenty of AVI files carry H.264 inside, so the plan was to rewrap&lt;br&gt;
losslessly and stay on the fast path:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ffmpeg -i in.avi -map 0:v:0 -map 0:a? -c copy \
       -avoid_negative_ts make_zero out.mkv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That fails. Not slowly, not subtly:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[matroska] Timestamps are unset in a packet for stream 0
[matroska] Can't write packet with unknown timestamp
[out#0/matroska] Error muxing a packet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;AVI has no per-packet timestamps. It stores a fixed frame rate plus an index and&lt;br&gt;
lets the demuxer work them out. Matroska requires timestamps on every block, so&lt;br&gt;
the copy has nothing to write. What comes out is a 1151 byte header with zero&lt;br&gt;
clusters, which is a valid Matroska file containing no media.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-avoid_negative_ts&lt;/code&gt; does not help, and it took me longer than it should have to&lt;br&gt;
see why: the timestamps are not negative, they are absent. Two different&lt;br&gt;
problems that produce similar looking errors.&lt;/p&gt;

&lt;p&gt;The fix is one flag on the input side:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ffmpeg -fflags +genpts -i in.avi -map 0:v:0 -map 0:a? -c copy \
       -avoid_negative_ts make_zero out.mkv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;+genpts&lt;/code&gt; synthesises presentation timestamps from the stream's frame rate.&lt;br&gt;
Output went from 1151 bytes to 55.8 MB on a 20 second 1080p30 clip, all 600&lt;br&gt;
frames decoding cleanly.&lt;/p&gt;

&lt;p&gt;Then I measured it against a control. Same geometry, same duration, same target,&lt;br&gt;
but Xvid instead of H.264 so it takes the ffmpeg path:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;H.264 AVI, rewrap then WebCodecs   7.5 s    53.3 MB to 12.3 MB
Xvid AVI, ffmpeg.wasm             53.4 s    48.2 MB to 12.5 MB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Output sizes within 2 percent, wall clock 7x apart. Both numbers come from&lt;br&gt;
headless Chromium with software encoding, so a real machine with a hardware&lt;br&gt;
encoder should do better than this.&lt;/p&gt;

&lt;p&gt;One more thing worth doing: ffmpeg.wasm resolves with the exit code instead of&lt;br&gt;
rejecting, and a muxer that dies mid run still leaves a readable file behind. A&lt;br&gt;
truncated remux looks exactly like a complete one unless you check.&lt;/p&gt;

&lt;h2&gt;
  
  
  Every AAC track defeats mediabunny's copy path, by design
&lt;/h2&gt;

&lt;p&gt;The passthrough case is supposed to touch nothing. Give mediabunny an empty&lt;br&gt;
video config and it copies encoded samples straight through, since&lt;br&gt;
&lt;code&gt;forceTranscode&lt;/code&gt; defaults to false.&lt;/p&gt;

&lt;p&gt;It worked for video and not for audio. A 3.3 MB source came back at 3.6 MB, so&lt;br&gt;
I parsed the output boxes:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source   moov 33,948   mdat 3,432,287
output   moov 17,879   mdat 3,788,908
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The moov got smaller. The growth was all in mdat. Per track:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;avc1   900 samples   3,071,520 bytes
mp4a  1295 samples     717,380 bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Video was byte identical to the source. Audio was double: 717 KB against the&lt;br&gt;
source's 360 KB, about 191 kbps from a 96 kbps original.&lt;/p&gt;

&lt;p&gt;The reason is in mediabunny's copy conditions. The fast path requires, among&lt;br&gt;
other things, &lt;code&gt;!needsTrimming&lt;/code&gt;, where &lt;code&gt;needsTrimming&lt;/code&gt; is&lt;br&gt;
&lt;code&gt;firstTimestamp &amp;lt; startTimestamp&lt;/code&gt;. I asked the library what it saw:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;video codec: avc   firstTimestamp: 0
audio codec: aac   firstTimestamp: -0.023219954648526078
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Negative. And 0.0232 seconds at 44100 Hz is 1024 samples, which is exactly one&lt;br&gt;
AAC frame. That is encoder priming delay, and every AAC track produced by any&lt;br&gt;
normal encoder has it. So the audio copy path is not occasionally unavailable,&lt;br&gt;
it is never available.&lt;/p&gt;

&lt;p&gt;You cannot fix this from the outside. Passing a bitrate to keep the size down&lt;br&gt;
forces a transcode by itself, because &lt;code&gt;!trackOptions.bitrate&lt;/code&gt; is one of the&lt;br&gt;
copy conditions. Passing nothing lets the library re-encode at its own default.&lt;/p&gt;

&lt;p&gt;I went with a floor instead: if a passthrough would return more bytes than it&lt;br&gt;
received, and the source is already MP4, hand back the source untouched.&lt;br&gt;
Verified byte exact, 3,466,275 in and 3,466,275 out where it used to be&lt;br&gt;
3,806,815. For a file that was already close to optimal, "nothing needed doing"&lt;br&gt;
is a more honest answer than a 10 percent larger file.&lt;/p&gt;

&lt;h2&gt;
  
  
  The multithreaded ffmpeg core costs you your ad revenue
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;@ffmpeg/core-mt&lt;/code&gt; is 4 to 8 times faster than the single threaded core. It needs&lt;br&gt;
SharedArrayBuffer, which needs cross origin isolation, which means sending&lt;br&gt;
COOP and COEP headers.&lt;/p&gt;

&lt;p&gt;COEP: require-corp breaks third party embeds that do not opt in. On a site&lt;br&gt;
funded by AdSense that is not a technical tradeoff, it is a revenue decision.&lt;br&gt;
I stayed single threaded and put the effort into not reaching for ffmpeg at all,&lt;br&gt;
which is what the rewrap path above is for.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would take away from this
&lt;/h2&gt;

&lt;p&gt;Both real bugs were invisible to code review and to unit tests. The AVI failure&lt;br&gt;
had a passing test suite and a plausible looking implementation sitting on top&lt;br&gt;
of it. What found it was dragging an actual AVI file into an actual browser and&lt;br&gt;
noticing that a 54 MB input had produced 1151 bytes.&lt;/p&gt;

&lt;p&gt;If you want to try it on something awkward, the two cases I get asked about most&lt;br&gt;
have their own pages: &lt;a href="https://videocompress.dev/compress-mp4-for-discord" rel="noopener noreferrer"&gt;compress video for Discord&lt;/a&gt;&lt;br&gt;
if you are fighting the 10 MB free tier limit, and&lt;br&gt;
&lt;a href="https://videocompress.dev/compress-video-to-10mb" rel="noopener noreferrer"&gt;compress video to 10 MB&lt;/a&gt; if the&lt;br&gt;
cap is the whole problem. Both run on your own machine.&lt;/p&gt;

</description>
      <category>video</category>
      <category>compress</category>
      <category>compressor</category>
    </item>
    <item>
      <title>I built a video compressor with zero backend — your files never leave the tab</title>
      <dc:creator>mengyuxuan</dc:creator>
      <pubDate>Wed, 22 Jul 2026 08:07:50 +0000</pubDate>
      <link>https://dev.to/mengyuxuan/i-built-a-video-compressor-with-zero-backend-your-files-never-leave-the-tab-13b6</link>
      <guid>https://dev.to/mengyuxuan/i-built-a-video-compressor-with-zero-backend-your-files-never-leave-the-tab-13b6</guid>
      <description>&lt;p&gt;Drop a 200 MB screen recording into most "free video compressor" sites and here's what actually happens: the file uploads to someone's server, gets transcoded there, and you download the result. Your video sat on a stranger's disk. The site paid for bandwidth and CPU, so it pays that back with aggressive ads or a paywall at 3 files a day.&lt;/p&gt;

&lt;p&gt;I wanted the opposite: the compression runs &lt;strong&gt;in your browser tab&lt;/strong&gt;, the file never leaves your machine, and because there's no server doing the work, the whole thing is genuinely free to run.&lt;/p&gt;

&lt;p&gt;That's &lt;a href="https://videocompress.dev" rel="noopener noreferrer"&gt;videocompress.dev&lt;/a&gt;. This post is how it works under the hood.&lt;/p&gt;

&lt;h2&gt;
  
  
  The core constraint: no upload, ever
&lt;/h2&gt;

&lt;p&gt;The entire architecture falls out of one rule — &lt;strong&gt;the video bytes stay client-side&lt;/strong&gt;. That immediately kills the "rent a beefy transcoding server" design and forces the work into the browser. Two APIs can do it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;WebCodecs&lt;/strong&gt; — a low-level browser API that talks to the OS's hardware video encoder/decoder. Fast, uses almost no CPU, native-quality H.264.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ffmpeg.wasm&lt;/strong&gt; — the full FFmpeg compiled to WebAssembly. Works literally everywhere, but it's a software encoder running in a WASM sandbox, so it's slower.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;WebCodecs is the better experience when it's available. It isn't always. So the app probes the browser and picks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Picking an engine at runtime
&lt;/h2&gt;

&lt;p&gt;The selection logic is deliberately boring — a pure function that's trivial to unit test:&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;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;CompressionEngine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webcodecs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ffmpeg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;EngineCaps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;hasWebCodecsApi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// VideoEncoder + VideoDecoder exist&lt;/span&gt;
  &lt;span class="nl"&gt;inputReadable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// we can actually demux + decode this file&lt;/span&gt;
  &lt;span class="nl"&gt;canEncodeTarget&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// the target H.264 config is encodable here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;decideEngine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;caps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;EngineCaps&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;CompressionEngine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;caps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasWebCodecsApi&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;caps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inputReadable&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;caps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;canEncodeTarget&lt;/span&gt;
    &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webcodecs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ffmpeg&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important detail is the third capability. Having the WebCodecs &lt;em&gt;API&lt;/em&gt; isn't enough — the browser also has to be able to demux the specific container you dropped in and encode the target config. So before committing, the app asks &lt;a href="https://www.npmjs.com/package/mediabunny" rel="noopener noreferrer"&gt;&lt;code&gt;mediabunny&lt;/code&gt;&lt;/a&gt; whether it can read the input's primary video track and whether &lt;code&gt;VideoEncoder.isConfigSupported()&lt;/code&gt; accepts the output config.&lt;/p&gt;

&lt;p&gt;Any &lt;code&gt;false&lt;/code&gt;, anything unprobed, anything that throws → fall back to ffmpeg.wasm. WebCodecs is the fast path you &lt;em&gt;earn&lt;/em&gt; by passing every check; ffmpeg is the floor that always catches you. Users on the happy path get hardware speed; everyone else still gets a working compressor. Nobody gets a broken page.&lt;/p&gt;

&lt;h2&gt;
  
  
  The actual "compression": it's bitrate math
&lt;/h2&gt;

&lt;p&gt;"Compress this to 25 MB for email" sounds like a quality knob, but under the hood it's arithmetic. A video file's size is roughly &lt;code&gt;bitrate × duration&lt;/code&gt;. If you know the target size and the duration, you can solve for the bitrate:&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;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AUDIO_KBPS&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;TARGET_SIZE_SAFETY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.94&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// aim slightly under so we don't overshoot&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;computeTargetVideoKbps&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;targetSizeMB&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;durationSec&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;removeAudio&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;span class="nl"&gt;targetSizeMB&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;durationSec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;removeAudio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}):&lt;/span&gt; &lt;span class="kr"&gt;number&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;audioKbps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;removeAudio&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AUDIO_KBPS&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;safeTargetMB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;targetSizeMB&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;TARGET_SIZE_SAFETY&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;videoKbps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;safeTargetMB&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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;durationSec&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;audioKbps&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&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;max&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;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;videoKbps&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;100&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;p&gt;Two things I learned the hard way and baked in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The 6% safety margin (&lt;code&gt;0.94&lt;/code&gt;).&lt;/strong&gt; Encoders don't hit a bitrate target exactly; they hover around it. If you aim for exactly 25 MB you'll ship 26 and blow the email limit. Aiming for ~23.5 lands under it reliably.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Subtract the audio budget first.&lt;/strong&gt; The size cap is for the &lt;em&gt;whole file&lt;/em&gt;. Spend 128 kbps on audio and the video has to fit in what's left — otherwise your "target size" mode quietly ignores the audio track and overshoots.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The other mode is quality-based, which maps straight to H.264's CRF (constant rate factor):&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="nx"&gt;QUALITY_CRF&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;high&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;balanced&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;small&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lower CRF = better quality + bigger file. 23 is a genuinely good default for most footage; 28 is where you go when "smaller" matters more than "pristine."&lt;/p&gt;

&lt;h2&gt;
  
  
  Why "no server" changes the whole economics
&lt;/h2&gt;

&lt;p&gt;This is the part I think other devs will appreciate. Because the transcode happens in the tab:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hosting is basically free.&lt;/strong&gt; The app is 100% static — it's just HTML, JS, and a WASM blob on a CDN. No transcoding boxes, no per-video cost, no scaling worries. A traffic spike costs bandwidth on static assets, nothing more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Privacy is architectural, not a promise.&lt;/strong&gt; Plenty of sites &lt;em&gt;say&lt;/em&gt; "we delete your files." Here there's nothing to delete because nothing was ever sent. Open DevTools → Network tab and compress a file: zero upload requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It works offline&lt;/strong&gt; once the page and core are cached.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The one gotcha: &lt;code&gt;@ffmpeg/core-mt&lt;/code&gt; (the multithreaded build) needs &lt;code&gt;SharedArrayBuffer&lt;/code&gt;, which needs cross-origin isolation (COOP/COEP headers), which breaks a lot of third-party embeds. I chose the &lt;strong&gt;single-thread&lt;/strong&gt; &lt;code&gt;@ffmpeg/core&lt;/code&gt; on purpose — slightly slower, but no header gymnastics and full compatibility with everything else on the page. Know your tradeoff before you reach for the MT build.&lt;/p&gt;

&lt;h2&gt;
  
  
  The stack
&lt;/h2&gt;

&lt;p&gt;Nothing exotic, deliberately:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js 16&lt;/strong&gt; (App Router) + &lt;strong&gt;React 19&lt;/strong&gt; + &lt;strong&gt;TypeScript&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tailwind CSS 4&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@ffmpeg/ffmpeg&lt;/code&gt; 0.12&lt;/strong&gt; for the fallback path, &lt;strong&gt;&lt;code&gt;mediabunny&lt;/code&gt;&lt;/strong&gt; for WebCodecs demux/probing&lt;/li&gt;
&lt;li&gt;Static export — every page is SSG, deployable to Vercel / Cloudflare Pages / Netlify free tiers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The compression parameter functions (the bitrate/CRF math above) are pure and covered by &lt;code&gt;node --test&lt;/code&gt; unit tests. When the thing that matters is "did we compute the right bitrate," you want that logic testable without spinning up a browser or a real encoder.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it / read it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The tool: &lt;strong&gt;&lt;a href="https://videocompress.dev" rel="noopener noreferrer"&gt;videocompress.dev&lt;/a&gt;&lt;/strong&gt; — drop a file in, pick a target size or quality, download. No account, no upload, no watermark.&lt;/li&gt;
&lt;li&gt;If you're building something similar, the WebCodecs-with-ffmpeg-fallback pattern and the bitrate math are the two pieces worth stealing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you end up shipping in-browser media processing, I'd genuinely like to hear how you handled the engine-selection edge cases — the "API exists but can't encode &lt;em&gt;this&lt;/em&gt;" gap is where most of the real bugs live. Drop a comment.&lt;/p&gt;

</description>
      <category>compress</category>
      <category>video</category>
      <category>free</category>
    </item>
  </channel>
</rss>
