今日已更新 98 条资讯 | 累计 29551 条内容
关于我们

Compressing Video to a Target File Size: The Bitrate Math in TypeScript

Noah Chen 2026年08月03日 17:39 4 次阅读 来源:Dev.to

A practical calculator for turning an upload limit into a video bitrate, with enough margin for audio and container overhead. “Make this video smaller” is an open-ended request. “Make this three-minute video fit under 10 MB” is an engineering constraint. The second version sounds more precise, but a quality slider alone cannot solve it. A quality setting tells an encoder how aggressively to preserve detail. It does not directly tell us how many bytes the final file may contain. If the destination has a hard upload limit, the useful starting point is a bit budget. This article builds that calculation in TypeScript, then looks at the assumptions that make the answer less exact than the formula first appears. File Size Is Bitrate Multiplied by Time A video file contains several streams plus a container. For a simple MP4, the largest pieces are usually: the video stream; the audio stream; container metadata and indexing overhead. If we ignore overhead for a moment, the relationship is straightforward: file size in bits = total bitrate in bits per second × duration in seconds Rearranging it gives us the total bitrate available for a target size: total bitrate = target size in bits / duration in seconds That total must cover both video and audio. The approximate video budget is therefore: video bitrate = total bitrate - audio bitrate - overhead allowance The result is not a promise. It is a budget that an encoder can aim at. Be Explicit About MB and MiB Before writing code, decide what “10 MB” means. Storage vendors and many web services use decimal megabytes: 1 MB = 1,000,000 bytes Operating systems and developer tools often display binary mebibytes: 1 MiB = 1,048,576 bytes The difference is about 4.9%. That is large enough to turn a file that looks safe locally into a rejected upload. For a hard external limit, I prefer to calculate with decimal MB and keep an additional safety margin. For an internal tool where the unit is clearly MiB, I make that choice explicit in th

本文内容来源于互联网,版权归原作者所有
查看原文