FFMPEG sensible defaults

11,868

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:

Share:
11,868
Cameron Martin
Author by

Cameron Martin

I am me

Updated on June 14, 2022

Comments

  • Cameron Martin
    Cameron Martin about 2 years

    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?

  • Cameron Martin
    Cameron Martin over 12 years
    I don't want a the output file to have a constant bitrate though, I want the output file to be of the same 'quality' (however that is measured) as the input file. And the same resolution, of course.
  • Mike
    Mike over 12 years
    Have a look at the link i gave you. You will find this there ‘-s[:stream_specifier] size (input/output,per-stream)’ Set frame size. The format is ‘wxh’ (default - same as source). The following abbreviations are recognized: ... Everything you need as parameters is there and there are also good examples.
  • Cameron Martin
    Cameron Martin over 12 years
    Oh okay, how about setting the quality of the output stream the same as the quality of the input stream? Would I have to find out the bitrate of the source, then put this as the bitrate of the output? Or is there a way to make ffmpeg automatically do that?
  • Mike
    Mike over 12 years
    as i said earlier the parameter wxh will take the same dimensions as the original movie and pass it to the converted one.
  • Cameron Martin
    Cameron Martin over 12 years
    I'm not looking to set the frame size to the same as source, I'm looking to set the bitrate to the same as source.
  • Mike
    Mike over 12 years
    I cannot think right now at a specific way to do this because ff mepg lets you specify the bitrate by yourself using -b 1200 for example or it calculates for itself the default bitrate for the type of file you output if you do not specify a bitrate explicitly. To set a type of output file you can use code like this -target vcd file.mpg and possible options are: vcd, svcd, dvd, dv, pal-vcd or ntsc-svcd
  • Cameron Martin
    Cameron Martin over 12 years
    Hmm, well basically I've got a wide range of input videos and I'd like a way to programmatically convert these into files where the quality of the output file reflects the quality of the input file. Does the -qscale flag adjust the bitrate to make the output file a desired quality?
  • Cameron Martin
    Cameron Martin over 12 years
    Does this mean you want a higher crf value for lower quality input videos?
  • llogan
    llogan over 12 years
    CRF determines the quality of your output. To keep things simple, ignore your input and keep decreasing (or increasing) your CRF value until you reach an acceptable quality level for your output. A good starting value is 24.
  • Mike
    Mike over 12 years
    -qscale is for constant quality and variable bitrate and has ranges from 1 (high q) to 31 (lowest q). Also this is a parameter and does not read by itself the data from your input file so that iti could automatically set itself. I don't think however that bitrate is what you need since for example if you convert DVD format to a flv you will definitely will not have the same bitrate and the same quality because video conversion has loss of quality in order to save space.
  • Mike
    Mike over 12 years
    You could make an application where you can set your own quality factor at file upload for example or do as youtube does: make 2 or 3 copies of the same movie in different qualities and see after that which fits best. Of course this comes with a high cost of resources both CPU and space.