0

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.

The presets

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?

0

Not all encoders are equal

x264 is a highly efficient H.264 encoder, meaning it can output a high quality at lower bitrates. x264 uses fancy "psychovisual" tricks to fool the eye to save bits which the less efficient encoders may lack.

H.264 encoders on cameras and phones must make more compromises due to other priorities (fewer resources, minimum required encoding speed, battery life, etc). So even though they are making the same format, they can be highly inefficient which means they can't match x264 at the same bitrate.

The default settings for x264 are good, so the general user often doesn't see a quality difference although the file size might be significantly reduced.

Lossless is huge

Setting -crf 0 uses a special compressed lossless mode. Most of the tricks to reduce file size can't be used in lossless mode.

Input file size should not be considered when dealing with lossless. The input is fully decoded into uncompressed raw video: the basic building blocks of a video. Raw video is lossless, but is the biggest file size of all because it is not compressed. The lossless encoder then takes the raw video and can compress it somewhat, but still keep it lossless. Like ZIP but designed for video. Smaller than raw video, but not as small as your typical lossy H.264 because it can't use all of the fancy tricks. Otherwise it would no longer be lossless.

It is not possible to create an output that has a better quality than the input. It can only be less than or equal to in quality. No matter the format or encoder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.