Resize a video in Linux through ffmpeg

In this article, we cover how to resize a video in Linux through ffmpeg. At times we need to optimize our videos for various devices like Desktops, smartphones, TVs, projectors, etc. There are numerous ways to get that done.

Before we proceed, we thought it would be important to discuss it now. It is important to maintain both quality and aspect ratio while resizing a video. Otherwise, it hampers the video quality. And, user experience gets affected.

Also, it is not advisable to resize the video to an extent where the dimensions of the final video are higher than the original video.

The command-line tool which we cover is ffmpeg. Since it won’t be possible for us to cover the installation steps required for the package ffmpeg for various distributions. So, we cover only Ubuntu here.

Install ffmpeg in Ubuntu

The package is available through the standard Ubuntu repository. So, first, update the repository to get the latest version of the package. Hence, open a terminal and issue the following:

sudo apt update

Next, to install ffmpeg:

sudo apt install ffmpeg

Resize a video in Linux through ffmpeg

A scale filter in ffmpeg is used to resize the video. Use the following syntax:

ffmpeg -i <video-input> -vf scale=Width:Height <video-output>

For instance, we want to resize our video file to 1280×720 dimensions. Input video file: input.mp4, Output video file we wish to generate: output.mp4

Then, the command will come out to be:

ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4

In the above example, we have provided the dimensions of the video. If we wish to keep the aspect ratio then, only provide one of the dimensions (either width or height) and enter -1 for the other. Let’s see how it’s done:

ffmpeg -i input.mp4 -vf scale=1280:-1 output.mp4

It considers the width and calculates the aspect ratio accordingly.

Lastly, if we wish not to provide a specific width and height and instead take the original dimensions just as a reference then, use the variables iw (input width) and ih (input height).

ffmpeg -i input.mp4 -vf scale=iw/4:-ih/4 output.mp4

The dimensions are reduced to a fourth of the original video.

In conclusion, we have covered here how to resize a video in Linux through ffmpeg.

Similar Posts