FFMPEGでは複数の動画を入力に取り、それらを並べた1本の動画を作成することができます。
例えば研究等の結果を示すビデオにおいて、従来手法の結果と提案手法の結果を並べて比較するなど、複数の動画を比較したい場合に非常に便利です。
本記事ではFFMPEGを使って動画を並べる方法を、シンプルな方法から複雑な方法まで紹介します。
FFMPEGの実行バイナリファイルの入手に関しましては、もし入手方法がわからないという方がいらっしゃいましたら、以下の記事を参考にしてみてください。
FFMPEGで2つの動画を横に並べる方法
下記のコマンドで、2つの動画を横に並べることが可能です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "hstack" output.mp4
以下の画像のように、二つの動画を横に並べた動画がoutput.mp4として出力されます。

FFMPEGで2つの動画を縦に並べる方法
2つの動画を縦に並べる場合は”hstack”の代わりに”vstack”を用いることで実現できます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "vstack" output.mp4
以下の画像のように、二つの動画を縦に並べた動画がoutput.mp4として出力されます。

FFMPEGで3つの動画を横に並べる方法
三つの動画を横に並べる場合は、以下のコマンドで実現できます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "[0:v][1:v][2:v]hstack=inputs=3[v]" -map "[v]" output.mp4
以下のように三つの動画が横に並ぶ形で、動画が出力されます。

4本以上の動画を横に並べる場合においても、上記のコマンドを編集すればよく、「-i input4.mp4」など4本目の入力ソースを追加した上で「-filter_complex “[0:v][1:v][2:v][3:v]hstack=inputs=4[v]”」のように変更することで4本の動画を横に並べることができます。
FFMPEGで3つの動画を縦に並べる方法
三つの動画を縦に並べる場合は、以下のコマンドで実現できます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "[0:v][1:v][2:v]vstack=inputs=3[v]" -map "[v]" output.mp4
以下のように三つの動画が縦に並ぶ形で、動画が出力されます。

FFMPEGで4つの動画を左上、右上、左下、右下に並べる方法
次に四つの動画を左上、右上、左下、右下に並べる方法について紹介します。最初に紹介した2つの動画を並べる方法を組み合わせても実現ができますが、よりスマートに実現できる方法は以下のコマンドとなります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -i input4.mp4 -filter_complex "[0:v][1:v][2:v][3:v]xstack=inputs=4:layout=0_0|w0_0|0_h0|w0_h0[v]" -map "[v]" output_hstack.mp4
上記のコマンドを用いることで、4本の動画が以下のように配置されます。
また、左上、右上、左下、右下に並べる際に入力が3本の場合でも、1か所をブランク(緑画面)にして並べる3本の動画を例えば左上、右上、左下に並べることができます。
以下のコマンドでは右下の領域をブランクにして3本の動画を並べることが可能です。「layout=0_0|w0_0|0_h0」の部分は「0_0(左上)」、「w0_0(右上)」、「0_h0(左下)」をそれぞれ表しており、右下にのみ動画が入らないのでここが空白になります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "[0:v][1:v][2:v]xstack=inputs=3:layout=0_0|w0_0|0_h0[v]" -map "[v]" output.mp4

まとめ
FFMPEGを用いることで、複数の動画を比較する比較動画を簡単に作れます。二つの動画の差異を示したい場合に試してみてください。