Is there a way to set the maximum final size for video converting using ffmpeg (or any other CLI based video converter)?
Like if I have an 100mb video and want to convert it to a 10mb video with the highest quality possible, considering the final format isn't important.
Going from 100mb to 10mb is a slightly unrealistic 90% drop in size but I will give an example of reducing to 50mb creating an H.264 video, which is a more reasonable 50% reduction in size as well as reducing to 40mb using HEVC.
I am using the following sample file:
You can use this sample file to confirm the following results that I have given and perhaps experiment a little further yourself. MediaInfo reveals the following for this downloaded file:
The arrows of course are my own! You then perhaps have 2 really good choices:
1. Re-Encode to H.264 (50mb)
H.264 is widely accepted now and would be an excellent choice for your output video file. The formula to calculate the output bitrate for the desired 50mb would then be:
To accomplish this use the following FFmpeg 2 pass command:
The resulting file size is 49.4MiB with quite reasonable, but not amazing, viewing quality. Pretty good for a 50% reduction in file size and a huge drop in video bitrate actually!
2. Re-encode to HEVC (40mb)
Another choice (as suggested by emk2203) is to re-encode the existing HEVC stream with a lower bitrate, this time aiming for 40mb as HEVC claims better quality at a lower bitrate.
This time the formula would be:
and the FFmpeg 2 pass command line is:
And this provides a 40mb file with quite reasonable quality.
In conclusion:
From these example you can experiment further by decreasing the required MiB in the formula and observing the subsequent viewing quality. Have Fun!!
References:
Yes. Choose a video codec (x264 is a good universal one) and choose the resolution, leave most settings at default, but do a 2-pass conversion with a set average bitrate.
Quick lesson in video compression: video complexity changes throughout the video, so if you compress to the same visual quality level the whole way through, actual bitrate is going to fluctuate up and down. If you compress to a fixed bitrate, the quality is going to be bad in bits of high complexity because the bitrate is too low, and/or the bitrate used will be too high for the parts of low complexity. So as much as you can you let it aim for a constant quality and let the bitrate fluctuate as it may. But if the compressor is aiming for a particular file size it doesn't know whether the video complexity is going to go up or down in the parts it hasn't converted yet, so that's why people use a 2 pass conversion when aiming for a particular file size.
You need to specify the output as a bitrate (audio and video, plus the container has a slight overhead), so you need a calculator to convert from filesize and duration to a bitrate.