RocketNuts
RocketNuts

Reputation: 11140

ffmpeg extend (not resize) video size by adding box or border

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.

Upvotes: 10

Views: 7933

Answers (2)

Hashim Aziz
Hashim Aziz

Reputation: 6102

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.

Upvotes: 3

RocketNuts
RocketNuts

Reputation: 11140

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.

Upvotes: 17

Related Questions