This question already has an answer here:
-
Vertically or horizontally stack several videos using ffmpeg?
2 answers
I want to combine four videos into a grid. Currently I'm using vstack
to combine the rows and then hstack
to combine the two outputs, as well as adding the audio.
ffmpeg -ss 0 -i 1.mp4 -ss 8 -i 3.mp4 -filter_complex vstack left.mp4
ffmpeg -ss 0 -i 2.mp4 -ss 0 -i 4.mp4 -filter_complex vstack right.mp4
ffmpeg -i left.mp4 -i right.mp4 -filter_complex hstack -i audio.mp4 output.mp4
It seams possible to do this in one operation using overlay
and pad
. However, the documentation states that using vstack
and hstack
is faster. Can those two filters be combined to one single operation?
You can do it all in one command using the hstack and vstack filters.
ffmpeg -i top_l.mp4 -i top_r.mp4 -i bottom_l.mp4 -i bottom_r.mp4 -i audio.mp4 \
-filter_complex "[0:v][1:v]hstack[t];[2:v][3:v]hstack[b];[t][b]vstack[v]" \
-map "[v]" -map 4:a -c:a copy -shortest output.mp4
- The audio will be stream copied from
audio.mp4
instead of being needlessly re-encoded.
If you want the audio from each input to be combined instead of providing a separate audio input then use the amerge filter. -ac 2
is added to downmix to stereo; otherwise the output would have a cumulative number of audio channels.
ffmpeg -i top_l.mp4 -i top_r.mp4 -i bottom_l.mp4 -i bottom_r.mp4 -filter_complex \
"[0:v][1:v]hstack[t];[2:v][3:v]hstack[b];[t][b]vstack[v]; \
[0:a][1:a][2:a][3:a]amerge=inputs=4[a]" \
-map "[v]" -map "[a]" -ac 2 -shortest output.mp4