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 change the resolution of a video file, we can use the scale filter:
$ ffmpeg -i input.mp4 -vf "scale=1280:720" output.mp4
The -vf "scale=1280:720" argument tells FFmpeg to scale the video to 1280 pixels by 720 pixels.
The aspect ratio is the ratio between the width and height of a video. For example, 1280 by 720 has a 16:9 aspect ratio, and 1080 by 1080 has a ratio of 1:1. This calculator can help you determine the aspect ratio for a particular resolution.
You can instruct FFmpeg to resize a video while keeping the aspect ratio, preventing the video from being stretched out of proportion. Here's an example of how to resize a video to a width of 720 pixels:
$ ffmpeg -i input.mp4 -vf "scale=-2:720" output.mp4
The -vf "scale=scale=-2:720" argument tells FFmpeg to scale the video to 720 pixels in width. With the -2, FFmpeg calculates the height automatically and ensures it is always divisible by 2, which is required by certain video encoders.
A video can also be scaled based on its current size. As an example, to reduce a video to half its original size (0.5×):
$ ffmpeg -i input.mp4 -vf "scale=iw/2:ih/2" output.mp4
The -vf "scale=iw/2:ih/2" argument instructs FFmpeg to scale the video's width and height by half. The variables iw and ih denote the width and height of the input video, respectively.
To double the resolution of the video:
$ ffmpeg -i input.mp4 -vf "scale=iw*2:ih*2" output.mp4
Here's how you can make a video scale so that it covers the output resolution:
$ ffmpeg -i input.mp4 -vf "scale=1920:1080:force_original_aspect_ratio=increase,crop=1920:1080" output.mp4
We are using multiple filters here. First, the video is scaled to 1920 by 1080, but the original aspect ratio is kept using force_original_aspect_ratio=increase, further increasing the input video to cover the output resolution. After that, it's cropped back to 1920x1080 using crop=1920:1080. Here's the result:
Here's how you can make a video scale to fit within the output resolution. This will result in black borders.
$ ffmpeg -i input.mp4 -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:-1:-1:color=black" output.mp4
Again, we are using multiple filters. First, the video is scaled to 1920 by 1080, but the original aspect ratio is kept using force_original_aspect_ratio=decrease, scaling down the input video to fit the output resolution. The pad=1920:1080:-1:1:color=black filter ensures that the video is padded. To change the black background to another color, just change black to a different color.