(FFmpeg) How to use filter_complex without losing video quality?

This is one of the most asked questions on google about FFMPEG and for a good reason, by default automatic compression is added depending on the filter. This is usually done to perform the filter quicker as the higher the quality, the longer the command completion time. 

Compression can be avoided using recommended codecs and setting quality. It is recommended that the H.264 aka libx264 encoder is used with most video formats. Libx264 is used entirely throughout this book. This assumes that your FFMPEG installation is configured with –enable-libx264.

The H.264 codec can be set as seen in the example below:

 $ ffmpeg -i input.mp4 -c:v libx264 output.mp4 

Another useful command is setting the constant rate factor (-crf) a rate control mode. The setting for a lossless output is 0, the lower the number the higher the quality but larger the file size. The default value is 23 while 51 is the worst quality. According to the documentation, values between 17-28 are “virtually lossless” visually but technically not. 

$ ffmpeg -i input.mp4 -c:v libx264 -crf 18 output.mp4

In addition to the previous two, selecting a preset can also increase quality but will increase file size. Setting the preset will determine which encoding speed and compression ratio. The slower the preset the longer the computation time. The presets are as follows:

  • ultrafast 
  • superfast 
  • veryfast 
  • faster 
  • fast 
  • medium – default 
  • slow 
  • slower 
  • veryslow

For the highest video quality with all 3 settings are as follows:

 $ ffmpeg -i input.mp4 -c:v libx264 -crf 0 -preset veryslow output.mp4 

Love FFmpeg? Grab a copy of FFmpeg For Beginners on Kindle or Paperback to learn over 120 ways to master FFmpeg!

buy now on amazon

Trackbacks & Pingbacks 1

  1. From (FFmpeg) How to Compress MP4 and Reduce File Size? - John Riselvato on 16 Apr 2020 at 5:00 pm

    […] ratios. The slower the preset the better compression. Compression was discussed in question 16, “How to Use -filter_complex Without Losing Video Quality?”, the same suggestions […]