13

So I'm using ffmpeg to convert a video to 1920*1080 px, I found two ways to do so, the first one would be to stretch the video to 1920*1080, but then it looks kinda stretched. I used this command for this:

./ffmpeg_darwin -i SRC -vf scale=1920:1080,setdar=16:9 DEST

The other option is the same without setdar but this just adapts the resolution to the one it started from (1728*1080).

I would like to fill the 192 pixels of the width with a black border. Is there some kind of option to do so? Or is there maybe another command line that could achieve this?

Thanks for your help :)

Share a link to this question
CC BY-SA 3.0
2
  • 1
    This is out of SO's scope, which is about writing code. Yet I wonder what you are trying to achieve. All video players I know of allow you to choose between automatically adding bands or stretching to fit when the ratio of the output surface and video stream do not match. Apart from making your video file a bit heavier and annoying people with a narrower resolution (who will have both your black bands and their player-added ones on top/bottom), I don't see the point.
    – spectras
    Oct 10, 2017 at 16:13
  • Thanks for your time ! The point is for people to be able to export a recorded video in 1920*1080 and edit them right after it was downloaded. It seems that if the format isn't exactly 1920*1080 they have to convert it afterwards. So the point is to give them less work after the recorded video was downloaded. Oct 11, 2017 at 8:02

4 Answers 4

26

Use

-vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1"

The scale will ensure that its output fits within 1920x1080. The pad then fills that out.

Share a link to this answer
CC BY-SA 4.0
5
  • 3
    Enclose expressions in single quotes.
    – Gyan
    Jan 16, 2020 at 6:31
  • 1
    searched for a long time for this, works great, thank you
    – Poopy Doop
    Apr 25, 2020 at 22:07
  • Just for self-containment: ffmpeg -i SRC -vf 'scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1' DEST
    – rayryeng
    Nov 11, 2020 at 18:11
  • @rayryeng running this on Windows 10 in command line gives error No such filter: 'scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1'. You must replace quotes with double quotes.
    – izogfif
    Jan 9, 2021 at 18:05
  • @izogfif thanks. I had to use single quotes on Mac OS. Pity that you have to use different quotes on different OSes.
    – rayryeng
    Jan 9, 2021 at 22:05
8

Add border to all side of video with you set your padding that you want

here in input one video and add padding=20 all side left,right,top and bottom

"-i",path1,"-filter_complex","[0]pad=w=20+iw:h=20+ih:x=10:y=10:color=red; output

[0]pad=w=20+iw:h=20+ih:x=10:y=10:color=red

  1. Here, w=20+iw means your video width + 20 because you want add border so we need to add padding 20 for 10 right side pad and 10 left side pad
  2. and same as in height h=20+ih so +20 to video height for 10 for top pad and 10 for bottom pad

  3. x=10:y=10 is use for if x=0,y=0 so border is not show at left and top side and show border at right and bottom side of 20;

  4. if we want to add border 20 so width + 40 and height + 40 and x,y = 20
  5. color=red is used for border color

enter image description here

Share a link to this answer
CC BY-SA 4.0
1
  • 2
    Really helpfull, thanks! Used it, to place a 1024x1024 picture into a 2048x2048 frame: ffmpeg -i input.mp4 -filter_complex "[0]pad=w=1024+iw:h=1024+ih:x=512:y=512:color=black" fillout.mp4
    – BjoernL.
    Mar 25, 2020 at 12:24
0

Thanks to everyone. Adapted your answers to a batch file placed in directory with the files I wanted to convert.

setlocal enabledelayedexpansion

GOTO :EndComment
  For low resolution videos that look worse when the display automatically
  makes them full screen.  This script upscales the video using horizontal 
  and vertical padding (black bars).       
:EndComment

set ffmpegExe="C:\Utilities\ffmpeg\bin\ffmpeg.exe"
set "oldScale=848:480"
set "newScale=1920:1080"


for %%f in (*.mp4) do (
  set str=%%f
  %ffmpegExe% -i "%%f" -vf "scale=%oldScale%:force_original_aspect_ratio=decrease,pad=%newScale%:(ow-iw)/2:(oh-ih)/2,setsar=1" "!str:.mp4=_new.mp4!"
  )
endlocal
Share a link to this answer
CC BY-SA 4.0
0

Goal: make a 1728x1080 video a 1920x1080 video by filling 192 pixels of width.

Solution: add 96-pixel wide black bars to each side of the video.

Solution, command:
ffmpeg -i input.mp4 -filter_complex "[0]pad=w=1920:h=ih:x=96:y=0:color=black" output.mp4

Solution, command, explanation: w=1920 is the output width, h=ih is the output height (unchanged), x=96 and y=0 means the original video will be placed 96 pixels to the right of the top left of the output layout; think of the output layout as a 1920x1080 rectangle which is black due to color=black.

The answer of @Gyan did not work for me, no matter if I used single or double quotes.

The answer of @Sanjay Hadiya did not really address OP's problem. Also, Sanjay Hadiya's answer is poorly written and confusing (but it did kinda help me); I am curious if he is an ESL speaker.

Share a link to this answer
CC BY-SA 4.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.