DEV Community

CompressVid
CompressVid

Posted on

MB vs MiB, Container Overhead and Headroom: Engineering a "Fits Under 20 MB" Guarantee

Disclosure: I work on CompressVid. The results and the bug below are from our own testing. Sizes are as displayed by our tool, which counts in binary megabytes (MiB).

If you build a "compress for Discord" feature, the spec sounds trivial: output must be under 20 MB. In practice, three separate things try to push you over the line. Our Discord preset is 19 MB, not 20, and this post explains why that one megabyte exists, including the time it wasn't enough.

Problem 1: which megabyte?

const MB  = 1_000_000;   // SI megabyte (what most upload limits and marketing use)
const MiB = 1_048_576;   // binary mebibyte (what many tools and OSes display)

20 * MB;   // 20,000,000 bytes
20 * MiB;  // 20,971,520 bytes (4.9% more)
19 * MiB;  // 19,922,944 bytes (still under 20,000,000)
Enter fullscreen mode Exit fullscreen mode

Our tool displays MiB. One concrete example: a result shown as 9.77 MB is 10,245,113 bytes on disk, so a file manager that uses SI units shows it as about 10.2 MB. If your preset says "20" and you mean MiB, while the platform means MB, you've already overshot by almost a megabyte before encoding starts. A 19 MiB ceiling stays under 20 MB in either interpretation. That alone justifies the preset.

Problem 2: container overhead

Your bitrate maths covers the elementary streams. The container adds:

  • the moov box: per-sample sizes, timestamps, sync-sample tables, chunk offsets. This grows with frame count, so a long 60 fps clip carries more index than a short 24 fps one;
  • ftyp, track headers and metadata;
  • audio framing (each AAC frame has its own entry).

It's typically small, a fraction of a percent to a couple of percent, but it is never zero, and it's not in your bitrate budget unless you reserve it.

Problem 3: rate control isn't exact

Even two-pass ABR lands near the target, not on it. VBV settings (-maxrate, -bufsize) allow local bursts. Single-pass modes drift more. Expect a few percent either way.

Put the three together, and a budget calculation looks like this:

function safeVideoKbps({ limitBytes, durationSec, audioKbps = 128, overhead = 0.02, drift = 0.03 }) {
  const usableBits = limitBytes * 8 * (1 - overhead - drift);
  return Math.floor(usableBits / durationSec / 1000 - audioKbps);
}

// Discord free tier, 1-minute clip, aiming at 19 MiB rather than 20 MB:
safeVideoKbps({ limitBytes: 19 * 1_048_576, durationSec: 60 }); // ≈ 2397 kbps
Enter fullscreen mode Exit fullscreen mode

The overhead and drift percentages above are illustrative assumptions, not measured constants. Tune them against your own encoder.

What our preset actually produced

Target size set to 19 MB with the '19 MB Free' Discord preset selected

Input Target Output
80.52 MB, 1:00, 1080p MP4 19 MB (Discord free) 18.64 MB in about 15 s
39.91 MB, 0:40, 1080p VP9 WebM 19 MB (Discord) 12.77 MB (H.264/AAC MP4)

The 'Fit to: 19 MB (Discord)' option selected in the WebM compressor

The first result used about 98% of its ceiling, close but safe. The second used about 67%, because the encoder didn't need the full budget for that content. Both are fine: a target size is a ceiling.

When a ceiling wasn't a ceiling: our 20.58 MB bug

During testing, an early build turned a 19 MB target into a 20.58 MB file. That's over the preset, and in MiB terms (≈21.58 million bytes) over a 20 MB limit too. It was a bug in our own target-size handling, and it has since been fixed. Whatever the exact internal cause, the lesson is the same: a budget computed without reserves for the factors above, and without a check on the final file, will eventually overshoot.

What we took away is a pattern I'd recommend to anyone shipping a size-limited export:

  1. Compute the budget from the limit minus explicit reserves (overhead + drift), not from the limit itself.
  2. Verify the muxed output size, not the estimated one.
  3. If it's over, re-encode at a reduced bitrate proportional to the overshoot, e.g. newKbps = oldKbps × (limit / actual) × 0.97, instead of failing or silently shipping it.
  4. Test with hard content (noise, confetti, fast pans). Easy footage hides overshoot because the encoder never needs its full budget.
async function encodeUnder(limitBytes, encode, kbps) {
  for (let attempt = 0; attempt < 3; attempt++) {
    const out = await encode(kbps);
    if (out.byteLength <= limitBytes) return out;
    kbps = Math.floor(kbps * (limitBytes / out.byteLength) * 0.97);
  }
  throw new Error('Could not fit under limit');
}
Enter fullscreen mode Exit fullscreen mode

Takeaways

  • Decide whether your limit is MB or MiB, and design for the stricter one.
  • Reserve bits for container overhead and rate-control drift.
  • Always verify the final file, and retry with a lower bitrate if it's over.
  • Platform limits change over time. Keep presets in config, not in code.

You can see the 19 MB preset behaviour in the Discord video compressor, which works online with nothing to install. If you've found other sources of overshoot in your own pipelines, I'd like to hear about them in the comments.

Top comments (0)