Weighting 2-second video samples by source bytes: 31.1% to 8.0% error on one clip, worse on a static one and a black-intro one

2026-12-01来源 dev.toEnglish

Compressing a few seconds of a video first and scaling the result up to the full length is a tempting way to guess the final file size. Plain linear scaling is unreliable, so the next idea is to weight each sample by how heavy its stretch of the original file was. In this run that fix helped one clip a lot, made another worse, and gave a wild number on a clip with a black intro. It is a conditional trick, not a general one.

Everything here is from one Apple-silicon Mac, with one run per configuration unless a number says "200 runs". The three 90-second clips are synthetic or stitched: a static lecture, a clip of constant noisy motion, and a cut-heavy clip that changes source every one to two seconds. A few seconds of the cut-heavy one come from Wikimedia Commons (Israel Police, CC BY-SA 3.0; Forza1000, CC BY-SA 4.0), recut and re-encoded. All clips are 1280×720, 30 fps, no audio track, compressed with x264 preset medium at CRF 23. Real footage may behave differently.

The weighting rule

Linear scaling takes the average sample size, divides by two seconds, and multiplies by the clip length. The weighted version reads the packet sizes of the original file and compares each sampled two-second window with the whole: estimate = total source bytes × (sum of sample output bytes ÷ sum of source bytes in those windows). It needs no extra encoding, only a read of the source.

It worked on the cut-heavy clip

The mean absolute error over 200 random sample placements, linear against weighted:

Clip 1 sample 3 samples 10 samples
Cut-heavy, linear 105.0% 57.4% 31.1%
Cut-heavy, weighted 76.5% 18.1% 8.0%
Constant motion, linear / weighted 3.1% / 3.0% 2.9% / 2.9% 2.9% / 2.9%
Static lecture, linear / weighted 125.9% / 197.7% 128.0% / 171.1% 129.4% / 141.4%

On the cut-heavy clip, ten samples bring the error down to 8.0%. Even so, with one or two samples the weighted estimate can be far off, and the worst single run landed at +3112.6%. The constant-motion clip was already near 3% either way, so weighting added nothing there.

It made the static lecture worse

The static clip is the interesting failure. Linear scaling already overestimated it by at least 84% whatever was sampled, because every two-second sample restarts at a keyframe: one sample of 74,727 bytes held a 70,634-byte keyframe, while the whole clip averaged only 40,437 bytes per two seconds. Here is the three-sample case, weighted and linear side by side:

$ python3 estimate.py input.mp4 14,44,74 sample_14.mp4 sample_44.mp4 sample_74.mp4 --real full.mp4 duration 90.0 s, samples 3, sample bytes [76297, 80057, 80748] linear estimate 3,556,530 B weighted estimate 5,763,790 B real size 1,819,679 B linear error +95.4% weighted error +216.7% 

Weighting multiplies a sample that is already inflated by a ratio taken from the source file, and for this clip that ratio points the wrong way. The original here was a high-quality master whose per-window bytes did not line up with where the compressed output spends its bits. That explanation is an inference; the master's byte distribution was not traced any further.

A black intro broke it

The constant-motion clip with four seconds of black added at the start, taking only the first sample: linear scaling gave −99.9%, which is bad but at least bounded. The weighted estimate came out at +621.3%. The black window has almost no bytes in the source, so the denominator collapses and the ratio is blown up. With linear scaling, even sampling brought this clip back to +3.2% at three samples; the weighted estimate with even sampling was not run on it.

When to trust it

The pattern across the three clips is that weighting helps only when the source file's bitrate tracks how hard each stretch is to compress. The cut-heavy clip fits that, and the static lecture and the black intro do not. That condition is an inference from three clips, and real camera footage and other encoders were not tested.

Cost matters too. In these runs, three even samples took 8.3% to 13.9% of the full encode time, and ten took 23.8% to 40.8%, so ten samples cover about 22% of the footage but cost 24% to 41% of the time. A workable rule for a batch: look at the per-second bitrate of one file first, use a few spread-out samples, and treat the result as a rough range. Where the exact size matters, such as an upload limit, run the full encode on that file.

For reference, ImgIng https://imging.ai/ has a short-clip trial in its desktop-browser video compressor, available when the output is H.264 SDR in MP4. It encodes up to three segments of at most two seconds, picked by the page, and shows the sample size and time. The page itself says the sample has no audio and cannot be used to infer the full file's size, and it does not change the bitrate automatically.

原文:https://www.a8q9.cn/en/article/weighting-2-second-video-samples-by-source-bytes-31-1-to-8-0-error-on-one-clip-w-b5c654.html

相关文章