I have a mp4 movie of 1:15min length, made with the Sony Alpha 6400 4k camera. It has a size of 528MB. I want to cut it using
ffmpeg. Although I only cut a few seconds, the new video was much smaller. So I tried some tests with the original uncut file and found out that the processing itself without any options produces much smaller files:
ffmpeg -i C0034.MP4 C0034_ffmpeg.MP4
The output file
C0034_ffmpeg.MP4 uses only 102MB of space. That's about 1/5 of the input file, without applying any options that could reduce the file size like less frames per second, a smaller resolution and other things.
According to the documentation:
A preset is a collection of options that will provide a certain encoding speed to compression ratio.
For my understanding, this is comparable to the
-mx=X switch of 7zip: A higher compression level means smaller files. It takes longer, but the quality of the file is unaffected.
The default of ffmpeg is medium. I changed it to
veryslow and got a 83,4 MB file, where the same file took 149 MB in
ultrafast. This is a noticeable difference of around 66 MB from the strongest to the lowest compression. But even
ultrafast is still 379 MB smaller than the raw input file, so the default
medium preset doesn't seem to explain most of the difference.
The Constant Rate Factor (CRF)
The same H264 article of ffmpeg shows the CRF. According to the documentation, it is the recommended rate control mode for most uses. It seems to be a factor that is used for ffmpeg to determinate the quality and filesize.
The range of the CRF scale is 0–51, where 0 is lossless, 23 is the default, and 51 is worst quality possible.
Since it uses 23 as default, we technically don't get a lossless output file. But it also explains, why those output files don't look worse than the input ones, although they're much smaller:
Consider 17 or 18 to be visually lossless or nearly so; it should look the same or nearly the same as the input but it isn't technically lossless.
By playing around, it seems that the CRF influences the file size much more than the presets (which seems clear, since presets only handles compressions and CRF the quality). I tried some combinations without presets (so default is used):
-crf 23~> 102 MB (default)
-crf 20~> 189 MB
-crf 18~> 285 MB
-crf 17~> 347 MB
-crf 0~> 2,64 GB
From the first tests, I assumed to found out the reason: CRF has a huge impact on the file size, even if there is no visible difference between 23 and 17 noticeable (at least not for me).
BUT I really wondered how
-crf 0 (which means lossless) is nearly 5 times larger than the original input file? From the huge difference I guessed that the output file has a better quality than the input file. How is that possible? What does
ffmpeg do to make the output file that large?