Thursday, November 30, 2023

Reducing a video's size with FFMPEG

 The following command uses the H.265 codec that compresses more for the same quality:

ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4

Other options for further compression are the following:

To scale to half size:

ffmpeg -i input.mkv -vf "scale=trunc(iw/4)*2:trunc(ih/4)*2" -c:v libx265 -crf 28 half_the_frame_size.mkv

One-third size:

ffmpeg -i input.mkv -vf "scale=trunc(iw/6)*2:trunc(ih/6)*2" -c:v libx265 -crf 28 a_third_the_frame_size.mkv

One-quarter size:

ffmpeg -i input.mkv -vf "scale=trunc(iw/8)*2:trunc(ih/8)*2" -c:v libx265 -crf 28 a_fourth_the_frame_size.mkv

One-fifth size:

ffmpeg -i input.mkv -vf "scale=trunc(iw/10)*2:trunc(ih/10)*2" -c:v libx265 -crf 28 a_fifth_the_frame_size.mkv

In these examples, the size is divided by twice of the reduction value and multiplied by two to ensure the pixel size is a multiple of two, which is required for some codecs, including H265. Changing the resolution always requires re-encoding.