使用ffmpeg修改视频文件的分辨率

This method is one of the bests ways to resize a video file in Linux systems (almost any distribution), and an excellent alternative to Windows and Mac users.

Changing the resolution of a video file will be one of the most common operations that we will perform when working with video files, and as such ffmpeg is able to do it perfectly. There are several reasons why we should want to change the resolution of a video file, for example:

  • To reduce the size of the video. This is possible by reducing the resolution of the video. If we take for example a video in HD (1920x1080 pixels) but we know that we will never see on a screen that supports a higher resolution than say 1024x768, we can reduce the video resolution to adapt to this new resolution, saving plenty of storage space and if used on Internet, saving bandwidth also.
  • Many times the resolution is changed on video files to standardize its format. That is, if we have several videos and we want them all in the same resolution, will have to go through this process of changing resolution.

In the development of modern websites it is quite useful to have the videos in various resolutions depending on where they appear. We can develop sites with responsive designs in which the most suitable video for the user plays. For example, if we have a video in various formats-we say 1920x1080, 1280x720, and 640x360- we can design a responsive site that makes visitors reproduce the proper video resolution depending on visitor browser saving bandwith (keep in mind that mobile users usually pay for transferred data so it is better to transfer as less traffic as possible)

In this example we will reduce the resolution of a video in HD format (1920x1080 pixels) to 640x360 (this is a fairly used configuration for aspect ratio 16:9):

ffmpeg -i video_1920.mp4 -vf scale=640:360 video_640.mp4 -hide_banner

It is only necessary to indicate the scale video filter with the new desired resolution (640:360) with -vf scale=640:360. To consider:

  • We can indicate any resolution we want, but the resulting video will always have the same aspect ratio. That is, it will not distort the images, if video is in 16:9 aspect ratio, it will keep the video in 16:9 aspect ratio. The program will adjust the resulting video so it can fit in the resolution that we have given.
  • When changing resolution, the video must go throught the encoding process another time, so the process can be slow depending on the output format and the codec you're using for the output.
  • We have not mentioned it, but most of the time it does not make sense to transform a video to a higher resolution because, there can't be any improvement in video quality.

Changing the video aspect ratio

If we want to change the look of the video, knowing that the image will appear distorted, we can use an additional filter "setdar". Imagine that in the previous case we want to change the 16:9 aspect ratio to 4:3, and therefore the video at a resolution of 4:3 aspect ratio, which in this case it will be 640x480. The ffmpeg command to make this transformation would be:

ffmpeg -i video_1920.mp4 -vf scale=640:480,setdar=4:3 video_640x480.mp4 -hide_banner

The video output that we get in this case video_640x480.mp4 changes the appearance of the original video and has distorted images a bit, but it will get the resolution we want with new look.

If on the other hand, we do not want to rely on using an aspect ratio which could be "more normal" (4:3, 16:9) or if we want to make changes to other resolutions with undefined aspect ratio and we are not afraid of possible deformations of images that will appear, we can use the "setsar" filter that will save you from having to keep those aspect ratios. In this way we could transform the previous video to a resolution of 200x400 for example, with the following command:

ffmpeg -i video_1920.mp4 -vf scale=200:400,setsar=1:1 video_200x400.mp4 -hide_banner

The result in 200x400 resolution from 1920x1080 resolution makes the video output to have a distorted appearance.

Examples of video resizing

We are going to see some examples of video resizing using ffmpeg. We have an original video with a resolution of 320x180 pixels. Here it is.

As we have seen previously we can resize the video to half its original size. We are resizing it from a 320x180 pixels resolution to a 160x90 pixels resolution with the following command:

ffmpeg -i video_320x180.mp4 -vf scale=160:90 video_180x90.mp4 -hide_banner

The video has gone from a size of 1.18MB to a 354KB video (one quarter). Here is the result:

Note that the video is smaller, but we can tell the browser to make it bigger with some loss of quality compared to the original.

Now let's change the original video aspect ratio from 16:9 to 4:3. To do this we are resizing video from 320x180 to 320x240 with the following command:

ffmpeg -i video_320x180.mp4 -vf scale=320:240,setdar=4:3 video_320x240.mp4 -hide_banner

And here is the result (you can see that images appear distorted):

And now at last we will resize the video as if it has to fit in a vertical screen, so we resize from 320x180 pixels to 180x320 pixels. Here is the command which will do the task:

ffmpeg -i video_320x180.mp4 -vf scale=180:320,setsar=1:1 video_180x320.mp4 -hide_banner

And here is the distorted result:

Note that in this videos I have also included a source in webm format for maximum compatibility. I have done this way so if your browser is not able to work directly with mp4 files you can see the results in a similar webm file.

猜你喜欢

转载自blog.csdn.net/dotphoenix/article/details/80428228