Arnaud Rochez
Arnaud Rochez

Reputation: 668

How to add black borders to video

So I'm using ffmpeg to convert a video to 19201080 px, I found two ways to do so, the first one would be to stretch the video to 19201080, 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?

Upvotes: 31

Views: 34802

Answers (5)

Josh K
Josh K

Reputation: 28893

I had a similar problem today and while this is an old question I came up with a slightly more generic solution to letterbox (add black borders) without manually specifying the sizes:

ffmpeg -i in.mp4 -vf "pad=width=iw:height=trunc(iw/4)*3:x=(ow-iw)/2:y=(oh-ih)/2:color=black" -c:a copy out.mp4

Breaking this down a bit:

  • width=iw the output width is the same as the input width, we're padding on the top and bottom
  • height=trunc(iw/4)*3 the height should be 75% (4/3) of the width, so we get a 4:3 aspect ratio video out
  • x=(ow-iw)/2 and y=(oh-ih)/2 center the output

To flip this around and take a video (say portrait mode) and add black borders (letterboxing) to the sides just flip width and height around:

ffmpeg -i in.mp4 -vf "pad=height=ih:width=trunc(ih/3)*4:x=(ow-iw)/2:y=(oh-ih)/2:color=black" -c:a copy out.mp4

A common error is that the padding conversion is wrong based on the video ratio, for example if I take a portrait video and try to convert it with the first command ffmpeg will try to add padding on the top but it can't, so the conversion fails:

[Parsed_pad_0 @ 0x6000027940b0] Padded dimensions cannot be smaller than input dimensions.
[Parsed_pad_0 @ 0x6000027940b0] Failed to configure input pad on Parsed_pad_0
Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0
Conversion failed!

The solution is to use the other command which adds padding on the sides.

Upvotes: 1

Rublacava
Rublacava

Reputation: 519

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.

Upvotes: 5

Blindspots
Blindspots

Reputation: 1013

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

Upvotes: 0

Gyan
Gyan

Reputation: 93339

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.

Upvotes: 50

Sanjay Hadiya
Sanjay Hadiya

Reputation: 945

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

Upvotes: 20

Related Questions