日本語

Add Black Bars to Portrait Video with FFmpeg to Make It Landscape

​​FFmpeg​command line,H.264,video

When you want to turn a portrait video shot on a phone (9:16) into a landscape video (16:9) for a regular YouTube video or a slide deck, scale the video down with FFmpeg, then add padding (black bars) to the left and right. The two filters you need are scale, which changes the size, and pad, which adds the padding. Below are commands that turn a 1080×1920 portrait video into 1920×1080, along with the sizes and color values FFmpeg reports, from runs on my test environment.

Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), and libx264 0.164.3108. The input videos were all generated with FFmpeg's testsrc2: H.264, 1080×1920, 30fps, yuv420p (3 seconds, with AAC audio), plus other videos of different sizes made the same way. I did not test videos shot with a camera or phone, videos with rotation metadata, videos with a SAR other than 1:1, or how the videos look after uploading them to a video site.

The basic command combines scale and pad

Scale the 1080×1920 video down to a height of 1080, then center it in a 1920×1080 frame.

bash
ffmpeg -i src.mp4 -vf "scale=-2:1080,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1" -c:a copy out.mp4

The -vf chain does the following steps from left to right. (The last step, setsar=1, is explained in a later section.)

  1. scale=-2:1080: sets the height to 1080 and sets the width to whatever keeps the aspect ratio. -2 means the width is rounded to a multiple of 2.
  2. pad=1920:1080:(ow-iw)/2:(oh-ih)/2: creates a 1920×1080 black canvas and places the scaled video so that its top-left corner is at ((ow-iw)/2, (oh-ih)/2).

The four values of pad are the output width, the output height, and the x and y position where the video is placed. ow and oh are the output width and height, and iw and ih are the width and height of the video coming into pad. (ow-iw)/2 is "half of the leftover width," so the video is centered horizontally.

Running with -v verbose prints the sizes that scale and pad decided on. (I extracted the two relevant lines.)

text
[Parsed_scale_0 @ 0x560c5754afc0] w:1080 h:1920 fmt:yuv420p sar:1/1 -> w:608 h:1080 fmt:yuv420p sar:1215/1216 flags:0x00000004
[Parsed_pad_1 @ 0x560c5754b100] w:608 h:1080 -> w:1920 h:1080 x:656 y:0 color:0x000000FF

Scaling 1080×1920 down to a height of 1080 gives a width of 1080 × 1080 ÷ 1920 = 607.5. With the -2 setting, the width became 608, a multiple of 2. Pad placed the video at x=656, so there are 656-pixel black margins on each side.

A figure with three panels. The top shows the original portrait color bar video, the middle shows the landscape video with black bars added to the left and right, and the bottom shows the same video made landscape by filling the sides with a blurred copy of itself. Below the middle panel, a line marks the widths: a left margin of 656, the video of 608, and a right margin of 656
The original portrait video, and two landscape versions with padding added

When I took the color of each pixel in the first frame of the output, the pixels at x=0 and x=655 were black (RGB 0, 0, 0), and the left edge of the video at x=656 was red (253, 0, 0). So the video starts exactly at x=656.

Without scaling first, pad fails with an error

Pad only adds padding; it does not shrink the video. If you specify pad=1920:1080 directly on the 1080×1920 video, the 1920 height does not fit into 1080, so processing stopped with the following messages (I extracted the relevant lines). The exit code was 234.

text
[Parsed_pad_0 @ 0x55909b830d40] Padded dimensions cannot be smaller than input dimensions.
[Parsed_pad_0 @ 0x55909b830d40] Failed to configure input pad on Parsed_pad_0

Conversely, you can keep the height at 1920 without scaling and only widen the frame to 16:9. With pad=ih*16/9:ih:(ow-iw)/2:(oh-ih)/2, the output was 3412×1920.

text
[Parsed_pad_0 @ 0x55cba4ab3fc0] w:1080 h:1920 -> w:3412 h:1920 x:1166 y:0 color:0x000000FF

Writing pad=aspect=16/9:x=(ow-iw)/2:y=(oh-ih)/2 with pad's aspect option gave the same 3412×1920 output with x=1166. This keeps every original pixel without shrinking, but if you want a standard 1920×1080, scale the height to 1080 first.

Changing the padding color

Specify the padding color with the fifth value of pad or with color=. The default is black.

bash
ffmpeg -i src.mp4 -vf "scale=-2:1080,pad=1920:1080:(ow-iw)/2:(oh-ih)/2:color=white,setsar=1" -c:a copy white.mp4

The color: field on the pad line in -v verbose output shows the color that was used. Here are the values I tried and what was printed:

Specified Color printed
(not specified) color:0x000000FF
color=white color:0xFFFFFFFF
color=0xeefafd color:0xEEFAFDFF
color=#00c8e8@1 color:0x00C8E8FF

Colors can be written as names (such as white) or as hexadecimal values in the form 0xRRGGBB or #RRGGBB. The syntax is explained in the color syntax section of the FFmpeg utilities documentation. @1 is the opacity; if omitted, the color is treated as fully opaque. In the video made with color=white, the leftmost pixel was RGB 255, 255, 255.

SAR drifts from 1:1, so add setsar=1

I checked a video made without setsar=1 from the first command using ffprobe. The SAR (sample aspect ratio, the pixel aspect ratio) and DAR (display aspect ratio, the aspect ratio when played) were as follows:

text
codec_name=h264|width=1920|height=1080|sample_aspect_ratio=1215:1216|display_aspect_ratio=135:76|pix_fmt=yuv420p

The resolution is 1920×1080, but the DAR is 135:76, not 16:9. The cause is scale. Because 607.5 was rounded to 608, the video is stretched slightly horizontally, so scale sets the SAR to 1215:1216 to cancel that out. In the scale source code (libavfilter/vf_scale.c) of FFmpeg 6.1.1, the output SAR is computed from the ratio of "output height × input width" to "output width × input height." Pad inherits this SAR, so the entire 1920×1080 video, including the added padding, carries that SAR.

Adding setsar=1 at the end sets the SAR to 1:1 and the DAR to 16:9.

text
codec_name=h264|width=1920|height=1080|sample_aspect_ratio=1:1|display_aspect_ratio=16:9|pix_fmt=yuv420p

Depending on the input size, no rounding may occur and the SAR stays 1:1. Scaling a 1000×1500 video with scale=-2:1080 gave a width of exactly 720, and the SAR was 1:1. If you want the same result for any input size, adding setsar=1 is the safe choice.

Fitting any input size into 1920×1080

If you use scale=-2:1080 on a video that is wider than 16:9, the width exceeds 1920 and pad fails. For a 1920×800 video, the width became 2592, and processing stopped with Padded dimensions cannot be smaller than input dimensions. When you want to convert videos of unknown orientation to 1920×1080 in one go, use force_original_aspect_ratio=decrease in scale.

bash
ffmpeg -i in.mp4 -vf "scale=1920:1080:force_original_aspect_ratio=decrease:force_divisible_by=2,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1" out.mp4

According to the scale documentation, decrease shrinks the width or height below the specified values if needed to keep the aspect ratio. In other words, it scales to the largest size that fits within a 1920×1080 frame. I tried several input sizes, and scale and pad chose the following sizes:

Input size After scale Pad position
1080×1920 608×1080 x=656, y=0
720×1280 608×1080 x=656, y=0
1000×1500 720×1080 x=600, y=0
1000×1700 636×1080 x=642, y=0
1920×800 1920×800 x=0, y=140
1280×720 1920×1080 x=0, y=0

A video wider than 16:9, such as 1920×800, gets padding at the top and bottom. A video that is already 16:9, such as 1280×720, is simply enlarged and gets no padding.

force_divisible_by=2 makes the scaled width and height multiples of 2. For the 1000×1700 video, without this option the width after scale was 635. Pad still makes the whole frame 1920×1080, so encoding finished with exit code 0 and the output was 1920×1080. However, pad placed the video at x=642, so the left margin was 642 and the right margin was 1920−642−635=643, which is off by one pixel. With the option, the width becomes 636, and both margins are 1920−642−636=642, which are equal.

Filling the bars with a blurred copy of the same video

Instead of black bars, you can use a heavily enlarged and blurred copy of the same video as the background. In this case, pad is not used. Instead, two video streams are created and overlaid.

bash
ffmpeg -i src.mp4 -filter_complex "[0:v]split[a][b];[a]scale=1920:1080:force_original_aspect_ratio=increase,crop=1920:1080,boxblur=20[bg];[b]scale=-2:1080[fg];[bg][fg]overlay=(W-w)/2:(H-h)/2,setsar=1" -c:a copy blur.mp4

split divides the video into two streams. One stream (the background) is scaled with force_original_aspect_ratio=increase to a size covering 1920×1080 (1920×3413), then crop cuts out the center 1920×1080, and boxblur blurs it. The other stream (the foreground) is scaled to a height of 1080, as in the basic command. Finally, overlay places the foreground in the center of the background. W and H are the width and height of the background, and w and h are the width and height of the foreground.

The output was 1920×1080, SAR 1:1, DAR 16:9, and the audio was kept. Here too, removing setsar=1 gave a SAR of 10239:10240, because enlarging the background rounded 3413.33… to 3413.

I made one output from the same 3-second video with each method. The black-bar version took 1.06 seconds and the blurred-background version took 2.42 seconds (the real value from time). The file sizes were 401,882 bytes for the black-bar version and 532,859 bytes for the blurred-background version.

The audio can be copied as is

Since scale and pad only affect video, -c:a copy keeps the audio without re-encoding it. I compared the audio of the original video with that of the padded video. Both were AAC, 44,100 Hz, mono, and the MD5 of the audio data was the same.

​Posted by 86