ffmpeg: re-encode a video, keeping settings similar
Solution 1:
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:
- FFmpeg Wiki: H.264 Video
Solution 2:
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).