Hitting an exact file size limit without a server: 8 MB in the browser
Upload forms say "max 8 MB". The file is 41 MB. Most compressors will happily make it smaller — few let you say this small, and almost none tell you what that costs in bitrate before you commit.
I build CutUtils, a set of browser-only media tools. This is the arithmetic behind the "compress to a target size" mode, and the three decisions that mattered.
The only equation that matters
For a fixed duration and frame rate, target size splits into video bitrate and audio bitrate:
video_bitrate = (target_bytes - audio_bytes) * 8 / duration_seconds / 1000 Eight megabytes is 8 × 1024 × 1024 bytes ≈ 8,388,608 bytes. At 128 kbps audio and a 90-second clip, audio eats 1,440,000 bytes, leaving ~6.9 MB for video — about 615 kbps at that duration. That number is the whole game: at 615 kbps a 1080p talking-head clip is fine, a 1080p screen recording of code is not.
So before encoding, the tool computes the budget and picks a resolution the budget can actually carry:
| Duration | Budget after audio | Suggested resolution |
|---|---|---|
| 30 s | ~1,900 kbps | 1080p |
| 90 s | ~615 kbps | 720p |
| 5 min | ~180 kbps | 480p |
Without that step you get a 1080p file at 180 kbps, which is not compression, it is destruction with extra steps.
Two-pass, or one very careful pass
A single pass with a target bitrate overshoots on high-motion content and undershoots on static content, because the encoder's rate control is chasing a buffer, not your byte count. We do a short analysis pass over sampled frames (not the whole file) to estimate motion, then a single encode with a bitrate ceiling and maxrate/bufsize tied to the budget.
The honest limitation: on very short clips the container overhead dominates. A 6-second clip at 8 MB is ~10 Mbps — no constraint at all — while a 12-minute clip at 8 MB is ~90 kbps, below what most encoders will produce sensibly. We refuse the second case rather than shipping a potato.
Why this runs in the browser at all
Because the alternative is uploading 41 MB to a stranger's server to get 8 MB back. Two consequences:
- No upload time, no queue, no retention policy to trust. The file is decoded and re-encoded in a WebAssembly build of FFmpeg inside a worker.
- Memory is the constraint. A 41 MB 1080p source decodes to well over a gigabyte of raw frames if you are careless, so the pipeline streams frames through the worker instead of holding the whole thing.
The failure mode people hit is memory, not CPU: on a low-RAM laptop the worker dies mid-encode and the tab reloads. We cap the input at 2 GB and warn before starting when navigator.deviceMemory looks tight.
What I would tell someone building this
- Compute the target bitrate before the user picks a quality preset. The preset is the output of the math, not an input.
- Pick resolution from the budget, not from the source. Downscaling to 720p at 615 kbps looks better than 1080p at 615 kbps, every time.
- Fail loudly on impossible targets. A warning that says "this will be 90 kbps, the result will be unusable" is a feature.
The tool itself is at cututils.com — 29 tools, all local, no account. Numbers above are from our own test files, not benchmarks of anyone else's encoder.