FFMPEG sensible defaults
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:
- Use the highest crf value that still gives you the quality you want.
- 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). - 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:
Comments
-
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 over 12 yearsI 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 over 12 yearsHave 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 over 12 yearsOh 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 over 12 yearsas i said earlier the parameter
wxh
will take the same dimensions as the original movie and pass it to the converted one. -
Cameron Martin over 12 yearsI'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 over 12 yearsI 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 over 12 yearsHmm, 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 over 12 yearsDoes this mean you want a higher crf value for lower quality input videos?
-
llogan over 12 yearsCRF 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 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 over 12 yearsYou 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.