Ffmpeg upscale and letterbox a video

7

5

I have a video with that is 480x360 in resolution, what I want to do is upscale this video to 1280:720 and not loos the original videos aspect ration ( letterbox the left and right side)

So I know people can achieve this task with ffmpeg but so far I am not able to do so

So can someone share a ffmpeg commend with me ??

Thank you very much for your help

Levan

Posted 2015-03-18T16:49:30.950

Reputation: 439

1Why do you want to upscale? – llogan – 2015-03-18T20:41:33.963

1I have my reasons to do so :) – Levan – 2015-03-18T23:30:59.917

Answers

17

ffmpeg -i input.mp4 -vf "scale=(iw*sar)*min(1280/(iw*sar)\,720/ih):ih*min(1280/(iw*sar)\,720/ih), pad=1280:720:(1280-iw*min(1280/iw\,720/ih))/2:(720-ih*min(1280/iw\,720/ih))/2" output.mp4

Let me break down this command for you:

ffmpeg -i input.mp4

This is the standard input method for ffmpeg. Replace "input.mp4" with the name of your input file.

-vf

Specifies that some video filters follow in the command. The video filters that are specified are applied sequentially to each frame.

scale=(iw*sar)*min(1280/(iw*sar)\,720/ih):ih*min(1280/(iw*sar)\,720/ih)

The first filter is the scale filter. The scale filter is powerful because it can perform value substitution and math. In this case, the math computes the new scaled width and height. It will scale the input image to fit the width and/or height of the desired output format without distorting the image. You don't need to make any modifications to the scale parameters because it will automatically substitute the actual values for "iw", "sar" and "ih". In your case, since your input video is 4x3, we know that the math will work out to scale to a height of 720 but a width that is less than 1280. More info: https://ffmpeg.org/ffmpeg-filters.html#scale-1

pad=1280:720:(1280-iw*min(1280/iw\,720/ih))/2:(720-ih*min(1280/iw\,720/ih))/2

The second filter is the pad filter. It has the same math/substitution features as the scale feature. So it will figure out the exact numbers for you. You don't need to change anything to make it work. The parameters tell the pad filter to make the output 720x1280 and to place the input image in the center of the frame. The pad filter will fill the output image with black anywhere that the input image doesn't cover. In the case of your input, we know that the scale filter scaled the height to 720, but the width is short of 1280. So black bars will be added to the sides to make up the difference. More info: https://ffmpeg.org/ffmpeg-filters.html#pad

output.mp4

This specifies the output file where ffmpeg will place the results. You may need to add all kinds of parameters for format, bitrate, etc. to meet your exact needs.

Brian

Posted 2015-03-18T16:49:30.950

Reputation: 331

Thank you very much for the reply but I van you explain this command a bit more, are this two different commands that I can use scale or pad or they both should be used at the same time ? and in (iw*sar) should I write something in them ? Thank you – Levan – 2015-03-19T15:54:54.307

1I edited the answer to provide more detail about the command. Bottom line: It is a single command. the only thing you should change are "input.mp4" and "output.mp4" as appropriate for your desired source and destination. – Brian – 2015-03-19T16:55:48.947

1

By the way, there is a very similar Q/A here: http://superuser.com/questions/547296/resizing-videos-with-ffmpeg-avconv-to-fit-into-static-sized-player

– Brian – 2015-03-19T17:01:45.923

Thank you very much, yes I looked at that answer before posting but I could not get it to work. Again thank you very much for the help – Levan – 2015-03-19T18:43:11.923

I had to add -aspect 16:9 aspect ratio argument force an exact 16:9 metadata flag. I realized it was a tiny bit something else after running an input file through mp4box dash splitter. – Whome – 2017-07-05T17:02:24.947