I have two videos of the same exact length, and I would like to use ffmpeg to stack them into one video file.
How can I do this?
I have two videos of the same exact length, and I would like to use ffmpeg to stack them into one video file.
How can I do this?
See this answer to this question for a newer way to do this.
Old version:
You should be able to do this using the pad, movie and overlay filters in FFmpeg. The command will look something like this:
First the movie that should be on top is padded to twice its height. Then the bottom movie is loaded. Then the bottom movie is overlaid on the padded top movie at an offset of half the padded movie's height.
Use the vstack (vertical), hstack (horizontal), or xstack (custom layout) filters. It is easier and faster than other methods.
Example 1: Combine/stack two videos
Vertical
Using the vstack filter.
Videos must have the same width.
Horizontal
Using the hstack filter.
Videos must have the same height.
Example 2: Same as above but with audio
Combined audio from both inputs
Add the amerge filter to combine the audio channels from both inputs:
-ac 2
is included to downmix to stereo in case both inputs contain multi-channel audio. For example, if both inputs are stereo, you would get a 4-channel output audio stream instead of stereo if you omit-ac 2
.Using audio from one particular input
This example will use the audio from
input1
:Adding silent audio / If one input does not have audio
If you mix inputs that have audio and inputs that do not have audio then amerge will fail because each input needs audio. You can add silent audio with the anullsrc filter to prevent this:
Example 3: Three videos
Example 4: 2x2 grid
Using xstack
Using hstack and vstack
This syntax is easier to understand, but less efficient than using xstack as shown above.
Example 5: 2x2 grid with text
Using the drawtext filter:
Example 6: Resize/scale an input
Since both videos need to have the same with for vstack, and the same height for hstack, you may need to scale one of the other videos to match the other:
Simple scale filter example to set width of input0 to 640 and automatically set height while preserving the aspect ratio:
For a more advanced method to fit any size video into a specific size while preserving aspect ratio see Resizing videos with ffmpeg to fit into static sized player.
You can also use the scale2ref filter to automatically resize one video to match the dimensions of the other.