FFmpeg is a free and open-source video editing tool capable of trimming, cropping, concatenating, muxing, and transcoding almost any type of media file you throw at it.
It's also a very robust solution for implementing video automation, as we use it extensively in our own video editing API. For this tutorial we'll use FFmpeg 5.1.2, but any recent version will do.
To shrink the size of a video file, it can be re-encoded with a higher CRF:
$ ffmpeg -i input.mp4 -crf 28 -preset veryslow -c:a copy output.mp4
If you want to compress the audio track as well, you can do it like this:
$ ffmpeg -i input.mp4 -crf 28 -preset veryslow -c:a aac -b:a 128k output.mp4
With -c:a aac -b:a 128k, the audio is re-encoded as AAC with a bitrate of 128 kbit/s.
If you want to reduce the size of a video even further, you can also reduce its resolution. Here's how to convert the video to 720p:
$ ffmpeg -i input.mp4 -vf "scale=-2:720" -crf 28 -preset veryslow -c:a copy output.mp4
Video shot at a high frame rate, like 60 frames per second, can also be compressed by reducing the frame rate:
$ ffmpeg -i input.mp4 -vf "fps=25" -crf 28 -preset veryslow -c:a copy output.mp4
All the above can be done in a single pass by doing the following:
$ ffmpeg -i input.mp4 -vf "fps=25, scale=-2:720" -crf 28 -preset veryslow -c:a aac -b:a 128k output.mp4