Stack Videos Horizontally, Vertically, in a Grid With FFmpeg

Often times, when you want to compare two videos side-by-side or you want to create an effect during post-processing, you might want to stack videos together. It can get expensive if you end up buying a tool to do this, but, guess what?

FFmpeg offers a variety of tools to help stack videos together – horizontally, vertically, or in a grid fashion. In this tutorial, let’s learn about FFmpeg’s hstack and vstack filters for stacking videos.


How to Stack Videos Horizontally using FFmpeg?

“Horizontally stacking videos” refers to placing videos side-by-side (one on the left and the other on the right).

Before you do this, there are a couple of points that you need to consider.

  1. The videos that you want to stack need to have the same height.
  2. The videos need to have the same pixel format.

The command line is shown below where we try and stack two mp4 videos.

ffmpeg -i input0.mp4 -i input1.mp4 -filter_complex hstack=inputs=2 horizontal-stacked-output.mp4

The hstack filter has a simple format. You need to specify the number of inputs and it parses that from the beginning portion of the commandline. The order of stacking follows the order of inputs.

Here is a screenshot of what it looks like.

stack videos using ffmpeg

And, here is a video!

Here is another use case. Companies or teams working on video compression often like to compare videos side-by-side in the lab or showcase their work in conferences. FFmpeg’s horizontal stacking is an easy way to do this and achieve a very good result.

Below are two videos encoded at different video quality settings and stacked horizontally. Comparison made simple, right? (note: Vimeo’s choise of bitrate might mess with the comparison, but, when done offline (downloaded), the hstack filter makes comparisons easy!)

Stacking Videos Vertically using FFmpeg

“Vertically stacked videos” results in placing videos one below the other. Unlike in horizontal stacking, inputs need to be having the same width. The command is as shown. 

For vertical stacking, we need to use the vstack filter whose syntax is similar to the hstack filter we used in the previous horizontal stacking example.

ffmpeg -i input0.mp4 -i input1.mp4 -filter_complex vstack=inputs=2 vertical-stack-output.mp4
stack videos using ffmpeg

Both functions pretty much use the same commands with a simple distinction, the hstack and the vstack under the -filter_complex argument. 

Here’s a video of stacking two videos vertically using FFmpeg.

Stacking Videos of Different Lengths

Well, there’s a really nifty ability for both of these to prioritize the length of the shortest video. And as luck would have it the parameter is named shortest, and it’s applicable to both the horizontal and vertical stacking filters. Using shortest=1 ensures the shortest length is used.

For example –

ffmpeg -i input0.mp4 -i input1.mp4 -filter_complex hstack=inputs=2:shortest=1 shortest-output.mp4

As a side note, if you run into an error that claims frames are being duplicated, the easiest workaround is to slip the vsync 2 parameter into your command, and it worked like a charm.

Stacking Videos of Different Lengths Without the shortest parameter

To test what happens in this situation, let’s stack two videos vertically – a 10 second clip and an 18 second clip. You’ll see that the shorter clip just stops after it completes, but the output video continues till the longest of the input clips complete.

If you want to truncate the clips to the length of the shortest clip, then you need to use the shortest=1 parameter. Let’s look at that in the next section.

Stacking Videos of Different Lengths With the shortest=1 parameter

In this example, we use the shortest=1 command-line parameter and as you can see, the length of the final video is truncated to the length of the shortest of the inputs.

2×2 Grid of Videos using FFmpeg

We can achieve a 2×2 grid of videos using a combination of the hstack and vstack filters. Let’s start by looking at the command-line and then break it down. It’s actually pretty simple!

ffmpeg \
-i input0.mp4 -i input1.mp4 -i input2.mp4 -i input3.mp4 \
-filter_complex \
"[0:v][1:v]hstack=inputs=2[top]; \
[2:v][3:v]hstack=inputs=2[bottom]; \
[top][bottom]vstack=inputs=2[v]" \
-map "[v]" \
finalOutput.mp4

What’s happening here?

  • firstly, you need to provide 4 input videos with the same height and width
  • next, you stack the first two videos horizontally and call it “top” i.e. [0:v][1:v]hstack=inputs=2[top]
  • then, you you stack the next two videos horizontally and call it “bottom” i.e. [2:v][3:v]hstack=inputs=2[bottom]
  • then, you stack top and bottom vertically to create a 2×2 grid. — [top][bottom]vstack=inputs=2[v]
  • then using the map command, we can extract and push the video track to the output container.

Here is what the video looks like.

3×2 Grid of Videos using FFmpeg

Along the same lines, here is a 3×2 grid of videos using hstack and vstack filters.

ffmpeg \
-i input0.mp4 -i input1.mp4 \
-i input2.mp4 -i input3.mp4 \
-i input4.mp4 -i input5.mp4 \
-filter_complex \
"[0:v][1:v][2:v]hstack=inputs=3[top];\
[3:v][4:v][5:v]hstack=inputs=3[bottom];\
[top][bottom]vstack=inputs=2[v]" \
-map "[v]" \
finalOutput.mp4

Conclusion

That’s it, folks. Now you know how to stack videos horizontally, vertically, and in a grid. This is very useful in comparing videos and creating fun effects!

If you enjoyed this post, do check out the rest of OTTVerse’s FFmpeg tutorials to learn more about how to use FFmpeg to improve your video editing, compression, and processing skills!

Akshay Rajeev
Akshay Rajeev

Akshay is a student at the NYU Tandon School of Engineering, completing his M.S. in Computer Engineering. His area of interest includes software system design, machine learning, and cybersecurity.

Akshay also serves as a Contributing Author on OTTVerse.com

Pallycon April NAB 2024

4 thoughts on “Stack Videos Horizontally, Vertically, in a Grid With FFmpeg”

  1. Pingback: Stack Videos Horizontally, Vertically, in a Grid With FFmpeg - OTTVerse | Hacker News | AnotherFN.com - Another FN

  2. Pingback: Stack Videos Horizontally, Vertically, in a Grid With FFmpeg – Hacker News Robot

Leave a Comment

Your email address will not be published. Required fields are marked *

Enjoying this article? Subscribe to OTTVerse and receive exclusive news and information from the OTT Industry.