8

Say I have a video of a peculiar resolution such as 1280x718 and I want to change this to 1280x720.

But instead of interpolating 718 pixels vertically to 720, I'd rather just add one line at the top and bottom.

So basically, I'm looking for a way to tell ffmpeg to create an output video of 1280x720, where input video of 1280x718 covers up the center, and all uncovered area is black or whatever.

I guess we could call this the opposite of cropping. I know how to resize a video (with interpolation) but in this case I don't want to rescale or mess with the original content, just add a small border.

RocketNuts
  • 9,958
  • 11
  • 47
  • 88

2 Answers2

14

Found the answer, posting it here for reference:

ffmpeg -i input.mp4 -vcodec libx264 \
-vf "pad=width=1280:height=720:x=0:y=1:color=black" -acodec copy result.mkv

width and height are the intended output resolution, x and y are the top left coordinates (within the new output) where to place the input. color (optional) is the border color, can also use color=0xff00ff notation.

RocketNuts
  • 9,958
  • 11
  • 47
  • 88
  • 3
    If the target size is only slightly larger, then besides padding, you can scale using the neighbor algorithm, it will duplicate the neighboring lines, no interp. – Gyan Jan 28 '18 at 11:48
  • After doing a 1600x900 video into 1600x1600 I found it being at the top. Is changing `y` able to shift the video below to center it better ? – George Pligoropoulos Mar 18 '20 at 15:29
3

To add padding to a video and centre the image in that padding:

ffmpeg -i "input.mp4" -c:a copy -c:v libx264 \
 -vf "pad=width=1280:height=720:x=-1:y=-1:color=black" output.mp4

This works because the documentation for the pad filter states:

x, y

Specify the offsets to place the input image at within the padded area, with respect to the top/left border of the output image.

The x expression can reference the value set by the y expression, and vice versa.

The default value of x and y is 0.

If x or y evaluate to a negative number, they’ll be changed so the input image is centered on the padded area.

Hashim Aziz
  • 4,074
  • 5
  • 38
  • 68