FFMPEG 4 sources to four windows

I recently wanted to stream four sources to one output similar to this:

/--------|--------\  
|INPUT 1 | INPUT 2|  
|--------+--------|  
|INPUT 3 | INPUT 4|  
\--------|--------/  

.four streams combined into one output

Windows:

set i1=http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/TearsOfSteel.mp4  
set i2=http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4  
set i3=http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4  
set i4=http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/WhatCarCanYouGetForAGrand.mp4  
ffmpeg -hide_banner -loglevel error -hwaccel cuda -stream_loop -1 -i %i1% -stream_loop -1 -i %i2% -stream_loop -1 -i %i3% -stream_loop -1 -i %i4% -filter_complex "[0:a][1:a][2:a][3:a]amerge=inputs=4[a];[0:v]scale=800:480[v0];[1:v]scale=800:480[v1];[2:v]scale=800:480[v2];[3:v]scale=800:480[v3];[v0][v1][v2][v3]xstack=inputs=4:layout=0_0|w0_0|0_h0|w0_h0" -c:v h264_nvenc -map "[a]" -ac 2 -f flv pipe:1|ffplay -i -  

Explanation:

ffmpeg -hide_banner -loglevel error -hwaccel cuda  

-hide_banner: do not output ffmpeg build information
-loglevel error: only output failure information
-hwaccel cuda: use nVidia cuda (you may need to change this to suit your system)

-stream_loop -1 -i %i1%  
-stream_loop -1 -i %i2%  
-stream_loop -1 -i %i3%  
-stream_loop -1 -i %i4%  

loop the inputs when they finish, input is %i1% to %i4% (from the earlier set commands)

-filter_complex  

Create a filter chain

"[0:a][1:a][2:a][3:a]amerge=inputs=4[a];  

merge all the video audios into one output

[0:v]scale=800:480[v0];  
[1:v]scale=800:480[v1];  
[2:v]scale=800:480[v2];  
[3:v]scale=800:480[v3];  

Resize all the videos to 800 x 480 resolution (16x9 aspect ratio) and name them v0 to v3

[v0][v1][v2][v3]xstack=inputs=4:layout=0_0|w0_0|0_h0|w0_h0"  

make a video output, with
v0 at the top left (0_0), v1 at the top right (w0_0),
v2 at the bottom left (0_h0), v3 at the bottom right (w0_h0)
0_0 is 0 pixels from the left, 0 pixels from the top
w0 means the width of the first video
h0 means the height of the first video
We can use absolute coordinates, top left is 0_0
If we wanted to make the window 4 wide, we could use

layout=0_0|0_w0|0_w0+w0|0_w0+w0+w0  

If we wanted to make the window 4 high, we could use

layout=0_0|h0_0|h0+h0_0|h0_h0+h0_0  

If we wanted to make the window 4 diagonal like this “", we could use

layout=0_0|h0_w0|h0+h0_w0+w0|h0+h0+h0_w0+w0+w0  

As you can see, we can create any layout we like. See the bottom of this page for some examples

-c:v h264_nvenc  

Encode the video in h.264 using the nVidia hardware encoder. You may need to change this to suit your system.

-map "[a]" -ac 2  

Multiplex the earlier combined audio into 2 audio channels

-f flv  

Make the format of the video FLV (Flash Video)

pipe:1|ffplay -i -  

Create a pipe with the output stream and open that pipe with ffplay (-i - means pipe from stdin)

As promised, some more examples:

/v1 | v2\  
-- v5 --  
\v3 | v4/  

Where v5 is actually just v3 resized to 1200x720. We could have added a new input but I will leave that for you to figure out.

-filter_complex "[0:a][1:a][2:a][3:a]amerge=inputs=4[a];[0:v]scale=800:480[v0];[1:v]scale=800:480[v1];[2:v]scale=800:480[v2];[3:v]scale=800:480[v3];[3:v]scale=1200:720[v4];[v0][v1][v2][v3][v4]xstack=inputs=5:layout=0_0|w0_0|0_h0|w0_h0|200_120"