GrepBoard/Manuals
605 manuals ·

Shrink AI Video and GIFs to Web MP4 with ffmpeg crf

·Tested on Ubuntu 24.04 · ffmpeg 7.0.2 (static build, libx264) · synthetic GIF made with Pillow·Test scope ↓·7 steps

Measure libx264 size per ffmpeg crf value, make the MP4 start playing early with faststart, and fix "width not divisible by 2" when converting a GIF to MP4.

Shrink a generated video before putting it on a page. One ffmpeg command does it. The numbers below come from a synthetic clip, not real AI footage: 1280×720, 30 fps, 5 seconds, made with testsrc2 plus light noise (5,432,873 bytes). Real footage will differ a lot with motion and texture.

[1] Change -crf and nothing else. Lower means better quality and a bigger file. Without the option libx264 uses 23. In this test a file made without -crf had exactly the same size as one made with -crf 23. Drop the audio with -an if you don't need it.

console
$ ffmpeg -i src.mp4 -c:v libx264 -crf 28 -an out.mp4
-crfBytesvs sourceSSIM
(source)5,432,873100%—
183,312,36461.0%0.9963
231,916,82735.3%0.9934
28872,38616.1%0.9849
32513,6729.5%—
36367,8926.8%0.9718

SSIM compares each output against the source (1.0 is identical).

console
$ ffmpeg -i out.mp4 -i src.mp4 -lavfi "[0:v][1:v]ssim" -f null -

Each step of 5 in crf left 46–58% of the previous size (18→23→28). Don't judge quality from one number. Play two or three candidates and pick.

[2] Add -movflags +faststart so playback can begin before the whole file arrives. When the index (moov) sits at the end of an MP4, some players must download everything first. The option runs a second pass that moves the index to the front. The official docs say this "can take a while, and will not work in various situations such as fragmented output", so it is not enabled by default.

For a file you already encoded, remux instead of re-encoding:

console
$ ffmpeg -i out.mp4 -c copy -movflags +faststart fast.mp4

Top-level box order, read from the files:

FileOrder
no optionftyp free mdat moov
+faststartftyp moov free mdat

[3] Reduce the resolution if you can. Give one side -2 and ffmpeg computes the other to keep the aspect ratio and stay even. Scaling 1280×720 to height 480 at crf 23 gave 475,052 bytes (854×480), about a quarter of the full-size file at the same crf (1,916,827).

console
$ ffmpeg -i src.mp4 -vf scale=-2:480 -c:v libx264 -crf 23 -an small.mp4

[4] Convert a GIF to MP4. The same 3-second scene (480×270, 15 fps) was 1,910,980 bytes as a GIF and 89,219 bytes as an MP4 made with this command:

console
$ ffmpeg -i in.gif -c:v libx264 -crf 23 -pix_fmt yuv420p \    -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -movflags +faststart out.mp4

Remove the options one at a time and you get the two problems below.

[5] Without -pix_fmt yuv420p, a GIF input is encoded as yuv444p. Result with a 321×201 GIF:

output
Stream #0:0: Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv444p(progressive), 321x201, 18 kb/s, 20 fps, 20 tbr, 10240 tbn (default)

The file is written, but it is a 4:4:4 profile that browsers may refuse to play (playback was not tested). For the web, always set yuv420p.

[6] Add yuv420p alone and an odd width or height stops the encoder:

output
[libx264 @ 0x3f435b40] width not divisible by 2 (321x201)[vost#0:0/libx264 @ 0x3f435700] Error while opening encoder - maybe incorrect parameters such as bit_rate, rate, width or height.Conversion failed!

4:2:0 stores one color sample per 2×2 pixels, so both sides must be even. scale=trunc(iw/2)*2:trunc(ih/2)*2 trims an odd side by one pixel. The command above turned 321×201 into 320×200 yuv420p.

[7] Check the result.

console
$ ffmpeg -i out.mp4

The Stream line should show yuv420p and even dimensions. For still images, see Shrink an AI-generated PNG to WebP and check its metadata.

#ffmpeg#mp4#gif#video