日本語

Put Two Videos Side by Side with FFmpeg hstack

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

To place two videos side by side in one video with FFmpeg, use the hstack filter. It is useful for before-and-after comparisons and for side-by-side videos that show two screens together. Here I cover the basic command, then show what happens when the two videos differ in height, length, or frame rate, using results from my test environment.

Note

My test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), and libx264 0.164.3108. The source videos were H.264 files at 640×360, 30 fps, yuv420p, made with FFmpeg's testsrc2 and testsrc sources (with AAC audio), plus variants of them with different lengths, heights, frame rates, and pixel formats. The video I changed to yuv444p has no audio. I did not test videos shot with a camera or smartphone, videos with rotation metadata, or the GPU versions hstack_vaapi and hstack_qsv.

Basic command

Here I place left.mp4 and right.mp4, both 640×360 and 5 seconds long, side by side.

bash
ffmpeg -i left.mp4 -i right.mp4 \
  -filter_complex "[0:v][1:v]hstack=inputs=2[v]" \
  -map "[v]" -map 0:a -c:a copy out.mp4

[0:v] is the video from the first input, and [1:v] is the video from the second input. hstack places them from left to right in the order given. The result is named [v], and -map "[v]" adds it to the output. The official documentation for hstack says the default for inputs is 2, so for two inputs hstack alone works the same way.

The output video was 1280×360, the combined width of the two inputs, with 150 frames and a duration of 5 seconds.

text
codec_name=h264|width=1280|height=360|pix_fmt=yuv420p|r_frame_rate=30/1|avg_frame_rate=30/1|duration=5.000000|nb_frames=150
codec_name=aac|channels=1|r_frame_rate=0/0|avg_frame_rate=0/0|duration=5.000000|nb_frames=217
In the left half, a testsrc2 color bar image; in the right half, a testsrc image with numbers. The two form one video side by side, with a pink dashed line at the boundary. A Japanese label below gives the English meaning: left is left.mp4, the first input, and right is right.mp4, the second input, both 640×360.
Frame at 1 second of the two videos combined with hstack (the label in the image is in Japanese)

The pixel at (100, 100) in the right video appears at (740, 100) in the output, and both have RGB value (0, 255, 255). The right video is placed offset by the left video's width (640).

Select audio with -map

hstack handles only video. In the command above, -map 0:a selects the audio from the first input, and -c:a copy copies it unchanged.

If I used only -filter_complex "[0:v][1:v]hstack" without -map, the output audio was taken from the first input. However, FFmpeg displayed Stream #0:1 -> #0:1 (aac (native) -> aac (native)), meaning the AAC audio was re-encoded from AAC to AAC. The audio duration also changed from 5.000000 seconds to 5.015011 seconds. To keep the audio as it is, add -map and -c:a copy.

To hear the audio from both inputs, mix them with amix. In the following example, right3s.mp4 is a 3-second video made the same way as right.mp4.

bash
ffmpeg -i left.mp4 -i right3s.mp4 \
  -filter_complex "[0:v][1:v]hstack[v];[0:a][1:a]amix=inputs=2:duration=longest[a]" \
  -map "[v]" -map "[a]" out.mp4

According to the documentation, amix normalizes the input volumes by default. I measured the average volume with volumedetect: the audio of left.mp4 alone was -21.1 dB, and the mixed audio from 0 to 3 seconds (where both are playing) was -24.1 dB. The mix was about 3 dB lower on average than the single source.

A height mismatch causes an error

The documentation says all video inputs to hstack must have the same height and the same pixel format. When I placed left.mp4 (640×360) with right480.mp4 (640×480), FFmpeg printed the following and stopped with exit code 234. I excerpted part of the output.

text
[Parsed_hstack_0 @ 0x557c202a4480] Input 1 height 480 does not match input 0 height 360.
[Parsed_hstack_0 @ 0x557c202a4480] Failed to configure output pad on Parsed_hstack_0

In this case, use scale to match the heights before passing the videos to hstack.

bash
ffmpeg -i left.mp4 -i right480.mp4 \
  -filter_complex "[1:v]scale=-2:360[r];[0:v][r]hstack[v]" \
  -map "[v]" -map 0:a -c:a copy out.mp4

scale=-2:360 sets the height to 360, keeps the aspect ratio, and makes the width a multiple of 2. With -loglevel verbose, I could see that the 640×480 input was scaled down to 480×360. The output video was 1120×360, which is 640 + 480.

text
[Parsed_scale_0 @ 0x557dd678c980] w:640 h:480 fmt:yuv420p sar:1/1 -> w:480 h:360 fmt:yuv420p sar:1/1 flags:0x00000004

Placing a yuv420p video and a yuv444p video together did not stop with an error. In the -loglevel verbose output, FFmpeg automatically inserted an auto_scale_0 filter between the second input and hstack to convert the pixel format. The output was yuv420p.

When lengths differ, the shorter video holds its last frame

When I placed a 5-second video and a 3-second video side by side, the output without any options was 5 seconds (150 frames), matching the longer one. After the 3-second video ended, its last frame stayed on the right.

Three frames from the output of a 5-second video and a 3-second video placed side by side, at 1 second, 2.9 seconds, and 4.5 seconds, stacked vertically. The left video's timecode keeps advancing, but the right video shows the number 2 at both 2.9 and 4.5 seconds. The right half at 4.5 seconds is outlined in pink.
Two videos of different lengths side by side. At 4.5 seconds, the right side still shows the last frame of the 3-second video

I compared the right half of the frames before encoding, one frame at a time, using MD5. The 61 frames from frame 90 (the last frame of the 3-second video) to frame 150 (the last frame of the output) all had the same value. The left half had different values at frame 90 and frame 150, so its image kept changing.

To stop at the shorter video's length, set shortest=1. The default in the documentation is 0.

bash
ffmpeg -i left.mp4 -i right3s.mp4 -filter_complex "[0:v][1:v]hstack=shortest=1[v]" -map "[v]" out.mp4

The output was 3 seconds (90 frames). When you also include audio, pay attention to the audio length too. When I copied the 5-second video's audio with -map 0:a -c:a copy, the video was 3 seconds but the audio stayed at 5 seconds. Copying the 3-second video's audio with -map 1:a -c:a copy, or setting amix to duration=shortest, made the audio 3 seconds long (3.018005 seconds with amix).

When frame rates differ, match them with fps

When I placed the 30 fps left.mp4 with right24.mp4, which was made at 24 fps, -loglevel verbose printed the following.

text
[Parsed_hstack_0 @ 0x55fa06b306c0] Video inputs have different frame rates, output will be VFR

In this case, the output video was 25 fps. Checking with ffprobe, it had 126 frames and a duration of 5.04 seconds, with frame timestamps advancing by 0.04 seconds each. That frame rate differs from both source videos.

text
codec_name=h264|width=1280|height=360|pix_fmt=yuv420p|r_frame_rate=25/1|avg_frame_rate=25/1|duration=5.040000|nb_frames=126

If you match one video to the other's frame rate with the fps filter before placing them side by side, the result is a 30 fps video with 150 frames and a duration of 5 seconds.

bash
ffmpeg -i left.mp4 -i right24.mp4 \
  -filter_complex "[1:v]fps=30[r];[0:v][r]hstack[v]" \
  -map "[v]" out.mp4

Adding a divider line and placing three or more videos

To put a line between the two videos, add padding to the right of the left video with pad before placing them side by side. In the following example, a 10-pixel-wide white line is inserted, and the output is 1290×360. The pixel at (645, 100), on the line, was (255, 255, 255).

bash
ffmpeg -i left.mp4 -i right.mp4 \
  -filter_complex "[0:v]pad=iw+10:ih:0:0:white[l];[l][1:v]hstack[v]" \
  -map "[v]" -map 0:a -c:a copy out.mp4

To place three videos, add inputs=3. With three 640×360 videos, the output was 1920×360. If you pass three inputs but write [0:v][1:v]hstack without inputs, only the first two are placed, and the output is 1280×360.

To stack videos vertically, use vstack with the same syntax. Here the widths must match: two 640×360 videos produced a 640×720 video.

How this differs from using pad and overlay

The documentation says hstack is faster than using overlay and pad. I compared it with a method that doubles the width of the left video with pad=iw*2:ih and then places the right video on top with overlay=w. The MD5 values of all frames before encoding were the same for both methods.

I processed the 5-second video with -f null, so nothing was written to a file, and measured it three times with -benchmark. The times (rtime) were as follows.

Method Run 1 Run 2 Run 3
hstack 0.102 s 0.096 s 0.100 s
pad and overlay 0.151 s 0.171 s 0.130 s

The video is re-encoded

hstack is a filter, so it cannot be used with -c copy. With -c copy, FFmpeg printed the following and stopped with exit code 234. I excerpted the first line of the error.

text
[vost#0:0 @ 0x563db2e2b300] Streamcopy requested for output stream fed from a complex filtergraph. Filtering and streamcopy cannot be used together.

Without any options, the video is re-encoded with H.264 (libx264). The audio can still be copied unchanged with -c:a copy, as in the examples above.

​Posted by 86