DEV Community

hao jia
hao jia

Posted on

Why a 2-second sample overestimates a static video's size by 84%+

Before a big batch, it is tempting to compress a couple of seconds of the video first, see how big that comes out, and scale it up to the full length. For a static lecture recording, that guess came out at least 84% too large in this run, no matter which two seconds were picked. What explains it fits in one number: the two-second sample was 94.5% keyframe by bytes, while the full file was about half.

All numbers below come from one Apple-silicon Mac, a single run per configuration. The clip in question is a 90-second, 1280×720, 30 fps, silent slide-recording of a lecture. Three clips in the wider test are synthetic or stitched together, and 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). They were all encoded with x264, preset medium, CRF 23, and real long-form footage may behave differently.

The command

This reads every packet of an encoded file, sorts them into keyframes and everything else, and sums the bytes in each group. full.mp4 is the whole clip compressed once; sample_14.mp4 is a two-second cut taken from the 14-second mark and compressed with the same settings.

$ for f in full.mp4 sample_14.mp4; do ffprobe -v error -select_streams v:0 -show_entries packet=flags,size -of csv=p=0 $f | awk -F, -v f=$f '{if($2~/K/){k++;kb+=$1}else{p++;pb+=$1}} END{printf "%s keyframes=%d bytes=%d  others=%d bytes=%d\n", f,k,kb,p,pb}'; done
full.mp4 keyframes=11 bytes=883013  others=2689 bytes=903720
sample_14.mp4 keyframes=1 bytes=70634  others=59 bytes=4093
Enter fullscreen mode Exit fullscreen mode

What was measured

In the sample, one keyframe took 70,634 bytes out of 74,727 bytes of packet data, which is 94.5%. The other 59 frames added up to 4,093 bytes. In the full file, 11 keyframes took 883,013 bytes, and 2,689 other frames took 903,720. Against the 1,819,679-byte file, that is 48.5%.

Linear scaling of the sample estimated the 1,819,679-byte clip at 3,360,870 bytes when the sample came from the very start (+84.7%), and 3,556,530 with three samples from 14, 44 and 74 seconds (+95.4%). Across all 89 two-second windows of the clip, the smallest sample was 74,589 bytes, while the whole file averages 40,437 bytes per two seconds. So every window overshoots. Even in 200 random draws of ten samples, the best was +90.5%, and a CRF 28 run leaned the same way (+116% to +162%).

The same method behaved differently on other clips. A clip of constant noisy motion landed within about 3% (−0.9% to −3.8%). The cut-heavy clip, with a different source every one to two seconds, ranged from −99.0% to +344.4% for a single sample.

What is inference

Every sample is its own encode, so it has to open with a keyframe. The full file only pays that cost once per keyframe interval, and its keyframes arrived roughly every 8.3 seconds, while a two-second sample always carries one. I did not test this by changing the keyframe interval, so treat it as the likely reason, not a proven one.

The other half, also inference: in the sample the 59 non-keyframes added only 4,093 bytes, so it is nearly all keyframe, while in the full file the 2,689 non-keyframes carry about as many bytes as the keyframes do (903,720 against 883,013), probably the page turns and scrolling. A busy clip hides the keyframe overhead behind large inter frames, which is probably why the motion clip came out so close.

Where the estimate stops working

An estimate by scaling is only about as good as the clip is uniform. Static content overshoots because of the keyframes. Cut-heavy content swings with whichever scene the sample hit. Four seconds of black at the start sent a single start sample to −87.5% on the static clip and −99.9% on the motion clip. Adding more samples helps with the swings, but ten samples cost 24% to 41% of the full encode time in this test.

If you want a quick look without scripting, ImgIng ( https://imging.ai/ ) has a short trial in its video compressor. It is available for H.264 SDR output to MP4 and takes up to three segments of at most two seconds each, with positions chosen by the page. In this run they landed at roughly 8%, 48% and 88% of the clip length. It shows the sample size and time, and the page itself says the samples carry no audio, cannot be used to work out the full size, and do not prove quality is intact.

What to check before trusting a sample

Run the command above on your own sample and your own full encode. If the sample's keyframe share is far above the full file's, a plain scale-up will overshoot. If the clip has a black intro or constant scene cuts, pick samples from the middle and spread them out. For the final number, encode the whole file once.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

The 94.5% number is the whole story in one figure — one keyframe took 70,634 of the sample's 74,727 bytes, so the sample is mostly paying the cost of starting an encode, and then you scale that cost up to the full 90 seconds. No wonder the 1,819,679-byte clip came out estimated at 3,360,870.

Your keyframe-share check is the practical takeaway, but it leaves one open question: where's the cutoff? A sample at 94.5% keyframe is obviously untrustworthy, but at what share would you trust the estimate — and is the fix for a static clip to sample longer, so the keyframe cost amortizes, rather than to sample a different two seconds? You note the overshoot held no matter which two seconds were picked, which suggests length, not position, is the lever.