Alex Balgavy

Just some stuff about me.

Here's my dotfiles repository.

Check out my blog.

My keys: PGP, SSH

My crypto wallets (BTC, XMR)

ffmpeg how to use filter_complex without losing video quality

Written by John Riselvato - original link

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:

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