Skip to content

How to Stitch Videos Together

George Stoyanov edited this page Jul 4, 2018 · 3 revisions

Horizontally and Vertically Stitching

Horizontally Videos Stitching

FFmpeg is offering an easy way to stitch videos together using the filter hstack. The easiest way to stitch to videos together horizontally is the following:

ffmpeg -i input1.mp4 -i input2.mp4 -lavfi hstack output.mp4

In this case both input videos should have the same height. You can also add the -shortest in order to define that the output video should have duration equal to the duration of the shorter of the two input videos. If you want to stitch more than two videos, then you need to use the following command:

ffmpeg -i input1.mp4 -i input2.mp4 -lavfi hstack=inputs=3 output.mp4

where inputs=3 is equal to the number of videos you want to stitch.

Vertically Videos Stitching

Similar to hstack ffmpeg also has vstack, which is doing the same but this time vertically.

ffmpeg -i input1.mp4 -i input2.mp4 -lavfi vstack output.mp4

In this case both input videos should have the same width in pixels. You can also use -shortest to limit the duration of the output video and inputs=xxx to stitch more than 2 videos together.

Complex (grid) Videos Stitching

You can also create grids with different configurations like 2x2, 3x3, 4x4 and so on. You have always to have equal numbers of the vertical and horizontal sides. If you want to put uneven number of videos you can use a black picture with the same size, which will make the stitching equal widths and heights. For example if you want to put 3 videos in a 2x2 grid you can and place the upper video in the center by horizontally stitching black picture with the same height as the center video but half the width, the video and the same black picture together horizontally and then stitch the lower two videos vertically. You can create black picture using ffmpeg using the following command:

ffmpeg -f lavfi -i color=c=black:s=1280x720 -vframes 1 black.png

This will create a black.png picture with resolution 1280x720px. So you can use one single command to create your grid:

ffmpeg -i top_left.mp4 -i top_right.mp4 -i bottom_left.mp4 -i bottom_right.mp4 \ 
       -lavfi "[0:v][1:v]hstack[top];[2:v][3:v]hstack[bottom];[top][bottom]vstack" \
       -shortest 2by2grid.mp4

This is saying please stitch the video of the first input with the video of the second input using hstack and label it as top then do the same with the videos of the third and fourth videos and also stitch them horizontally using hstack and label it as bottom and then stitch the [top] and [bottom] vertically together. Another example if we want to stitch 3 videos in 2x2 grid and put the top video in the middle. Assuming that all input videos have the same resolution 1280x720 the command will be:

ffmpeg -f lavfi -i color=c=black:s=640x720 -vframes 1 black.png

This will create a black image with 640x720 resolution or exactly half of the width of the other videos. Now we need to stitch all the parts together:

ffmpeg -i black.png -i top_center.mp4 -i bottom_left.mp4 -i bottom_right.mp4 \
       -lavfi "[0:v][1:v][0:v]hstack=inputs=3[top];[2:v][3:v]hstack[bottom];[top][bottom]vstack" \
       -shortest 3_videos_2x2_grid.mp4

References:

FFmpeg Filtering Guide