if you are targeting a specific output file size, itβs best to use H.264 and two-pass encoding .
There is a great example here, but it's too big for copy-paste: https://trac.ffmpeg.org/wiki/Encode/H.264
You calculate the target bitrate using bitrate = file size / duration and run ffmpeg two times: one pass analyzes the media, and the second performs the actual encoding:
ffmpeg -y -i input -c:v libx264 -preset medium -b:v 555k -pass 1 -c:a libfdk_aac -b:a 128k -f mp4 /dev/null && \ ffmpeg -i input -c:v libx264 -preset medium -b:v 555k -pass 2 -c:a libfdk_aac -b:a 128k output.mp4
Edit: H.265 (HEVC) is even better when compressed (in some cases, it is 50% of the H.264 size), but support is not widespread so far, so stick with H.264.
source share