27

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?

2 Answers 2

41

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:

1
  • great answer. I knocked the -crf down to 10 to get a better quality video with less compression. play with the number to get an output that suits you.
    – AutoBaker
    Dec 23, 2021 at 8:48
1

There is no such thing as 'lossless' if you want the size to be small. Lossless video means keeping the original bitstream, which does not reduce the size. Re-encoding is the only way to reduce the size of a video bitstream, which is a lossy process, and you're more likely to increase the size just to preserve the original quality and not add additional artifacts. True lossless (uncompressed) raw video is massive.

If your goal is a smaller file without compromising quality, simply re-encode the video with the lowest bit-rate that is visually acceptable. Try different codecs (x265 is more efficient than x264 etc, but less compatible). FFmpeg is a powerful tool for messing with encoder settings.

Another option is compressing the audio without touching the video, as some audio streams can be larger than needed (256kb/s AAC can go down to 128 or 96 depending on how important it is).

2
  • 5
    Just a note: you can totally have lossless compression. Just don't expect the file size reduction to be as small.
    – DaVince
    Nov 17, 2020 at 15:49
  • Of course you can have losless compression, just think of a ZIP file, which is compressing data lossless. And the same is true for video - it might be that the recording device was not compressing it (enough), due to the requirement for (near) real-time writing the video stream during recording; so later re-compression with more computing resources/time available might produce a smaller, lossless file Aug 24, 2022 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .