Skip to content

Instantly share code, notes, and snippets.

@Shamim-38
Created October 22, 2022 19:01
Show Gist options
  • Save Shamim-38/5cbf9a160ba176c0e51f6fc3a1a53822 to your computer and use it in GitHub Desktop.
Save Shamim-38/5cbf9a160ba176c0e51f6fc3a1a53822 to your computer and use it in GitHub Desktop.
To reduce the size of your mp4 you have two good choices with FFmpeg:
Experiment with Constant Rate Factor (CRF) settings to find a match between video output size and video quality
Experiment with the set bitrate of your choice and use a 2 pass encode.
Details of both below:
1. Resize using FFmpeg's Constant Rate Factor (CRF) option:
In FFmpeg the CRF settings vary from 0-51 with 0 being lossless and 51 being shockingly poor quality. The default is 23 and you need to find the 'sweet spot' for your particular video of file size and video quality with some experimentation.
A sample command-line would be:
ffmpeg -i input.mp4 \
-c:v libx264 -preset slow -crf 22 \
-c:a copy \
output.mp4
Note that in this command-line the video is re-encoded and the audio is simply copied across. Things to consider:
If the size is still too big and the video quality is still acceptable you would try -crf 24 and so on (incrementally increasing the crf integer) until you find an acceptable compromise between video quality and file size.
If the video quality is too poor you would try crf 20 and so on (incrementally decreasing the crf integer) until you find an acceptable compromise between video quality and file size.
2. Resize using FFmpeg's 'set bitrate' + '2 pass encode' options:
Another choice, which arguably will not produce as good results as the CRF example is to use the set bitrate of your choice and use a 2 pass encode. The following example of this is a single command:
ffmpeg -y -i input.mp4 \
-c:v libx264 -preset slow -b:v 1000k -pass 1 \
-c:a copy -f mp4 /dev/null && \
ffmpeg -i input.mp4 \
-c:v libx264 -preset slow -b:v 1000k -pass 2 \
-c:a copy \
output.mp4
Again in this example the video is re-encoded and the audio is simply copied over. Things to consider:
Video still too big? Decrease the bitrate...
Quality too poor? Increase the bitrate...
Keep doing this until you find an acceptable compromise between video quality and file size.
References:
FFmpeg and H.264 Encoding Guide : FFmpeg's canonical guide to encoding to H.26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment