ffmpeg - how to apply filters without losing quali

2019-01-09 16:09发布

问题:

Here is a simple request, it has an input, output, and two watermarks. From what I gathered I can't apply -codec copy because I'm using a filter.

ffmpeg -i input.mp4 -i wm-bl.png -i wm-br.png -filter_complex "overlay=x=0:y=H-h,overlay=x=W-w:y=H-h" output.mp4

This does the trick, as far as watermarking is concerned, but the output is compressed into half the original file size.

Is it possible to watermark without losing video quality?

回答1:

You must re-encode to perform any filtering

Therefore any attempt at stream copying while filtering will be ignored. This is why -codec copy does nothing for you.

You can re-encode to a lossless format

Although you must re-encode for filtering it does not mean that you have to lose quality. You can use a lossless encoder:

  • -codec:v libx264 -crf 0 -preset veryslow
  • -codec:v huffyuv
  • -codec:v ffv1
  • -codec:v ffvhuff

The outputs file size can be huge (they are lossless).

You may experience some loss if chroma subsampling occurs.

Lossy, but looks lossless

Alternatively you can use proper encoding settings that look "visually lossless", but technically are not truly lossless. Example for creating H.264 video that will likely look visually lossless (depends on input and the viewer since quality is subjective):

ffmpeg -i input -codec:v libx264 -crf 18 -preset slow -pix_fmt yuv420p out.mp4

To generalize, -crf controls quality: range is a log scale of 0-51, 0 is lossless, ~18 is often considered visually lossless, and 23 is default. -preset controls encoding speed: ultrafast, superfast, veryfast, faster, fast, medium (the default), slow, slower, veryslow.

-pix_fmt yuv420p will allow the encoder to use a chroma sub-sampling scheme that is compatible with troglodyte players like QuickTime. It may or may not be required depending on your input, your ffmpeg version, and how you're going to use the output but for general use it does not hurt to include it when using libx264.

Also see

  • FFmpeg and x264 Encoding Guide


标签: ffmpeg