FFmpeg Video – Re-encode Keeping Similar Settings

.mp4ffmpegvideo

I'm dealing with some mp4 videos that were encoded on mobile phones and are pretty huge.

I think ffmpeg can do better. I'm trying to figure out what flags to use to preserve fps, resolution, and keep the appearance the same (lossless or near lossless).

What flags should I use?

Best Answer

Preserve settings

ffmpeg will automatically attempt to use many of the same parameters when encoding including: frame rate, width, height, pixel format, audio channel layout, audio sample rate, etc. So you usually don't have to do anything special.

Some settings may change if there are format or encoder restrictions.

Preserve quality

For H.264 video using the encoder libx264 use:

  • -crf 18
  • the slowest preset you have patience for

These options will output a lossy video, but it -crf 18 provides enough bits that it will likely be visually lossless or nearly so. If the output is still too big the general recommendation is to use the highest -crf value that still provides an acceptable quality.

You can change it to -crf 0 for true lossless, but the resulting output will be a huge file size–probably even bigger than the original.

Example:

ffmpeg -i input.mp4 -c:v libx264 -crf 18 -preset slow -c:a copy output.mp4

This example stream copies the audio instead of re-encoding it since the majority of the file size comes from video.

Development is very active, so make sure to use a recent build of ffmpeg. See the FFmpeg Download page for links to binaries.

Also see:

Related Question