Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFMPEG sensible defaults

Tags:

ffmpeg

I'm using ffmpeg to watermark videos with a PNG file using vfilters like so:

ffmpeg -i 26.wmv -vcodec libx264 -acodec copy -vf "movie=logo.png [watermark]; [in][watermark] overlay=10:10 [out]" 26_w.mkv

However, the input files are all of different quality/bitrates, and I want the output files to be of similar quality/bitrate to the input files. How would I achieve this? Also, I know almost nothing about ffmpeg, so are there any options which would be sensible to set to give a good quality:filesize ratio?

like image 620
Cameron Martin Avatar asked Dec 02 '22 23:12

Cameron Martin


1 Answers

Usually wanting the output to be the "same quality" as the input is an assumed thing that people will always want. Unfortunately, this is not possible when using a lossy encoder, and even lossless encoders may not provide the same quality due to colorspace conversion, chroma subsampling, and other issues. However, you can achieve visually lossless (or nearly so) outputs when using a lossy encoder; meaning that it may look as if the output is the same quality to your eyes, but technically it is not. Also, attempting to use the same bitrate and other parameters as the input will most likely not achieve what you want.

Example:

ffmpeg -i input -codec:v libx264 -preset medium -crf 24 -codec:a copy output.mkv

The two option for you to adjust are -crf and -preset. CRF (constant rate factor) is your quality level. A lower value is a higher quality. The preset is a collection of options that will give a particular encoding speed vs compression tradeoff. A slower preset will encode slower, but will achieve higher compression (compression is quality per filesize). The basic usage is:

  1. Use the highest crf value that still gives you the quality you want.
  2. Use the slowest preset you have patience for (see x264 --help for a preset list and ignore the placebo preset as it is a joke).
  3. Use these settings for the rest of your videos.

Other notes:

  • You do not have to encode the whole video to test quality. You can use the -ss and -t options to select a random section to encode, such as -ss 30 -t 60 which will skip the first 30 seconds and create a 60 second output.

  • In this example the audio is stream copied instead of re-encoded.

  • Remember that every encoder is different, and what works for x264 will not apply to other encoders.

  • Add -pix_fmt yuv420p if the output does not play in dumb players like QuickTime.

Also see:

  • FFmpeg and x264 Encoding Guide
  • FFmpeg and AAC Encoding Guide
like image 51
llogan Avatar answered Dec 18 '22 11:12

llogan