ffmpeg multiple stream images merged

First of all, this is the merger of two video streams.

To achieve the effect of playing two videos, one left and one right, at the same time, you can use FFmpeg's hstack and vstack filters. Here's a common approach:

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v]scale=640:480[left];[1:v]scale=640:480[right];[left][right]hstack=inputs=2[v]" -map "[v]" -f flv rtmp://your-streaming-server.com/live/stream-key

This command will read the video streams from the two input files input1.mp4 and input2.mp4, scale and splice them horizontally, and then push the results to the specified RTMP server. (There is no video stream displayed, that is, there is no video information)

Optimized:

ffmpeg  -i input.mp4 -i input.mp4 -filter_complex "[0:v]scale=640:360[v0];[1:v]scale=640:360[v1];[v0][v1]hstack=2[out]" -map [out] -c:v libx264 -preset veryfast -tune zerolatency -f flv rtmp://your-streaming-server.com/live/stream-key

Explanation:
-map [out] Used to specify the output audio stream, -c:v libx264used to specify the video encoder as libx264, -preset veryfastused to specify the encoding speed as veryfast, and -tune zerolatencyused to specify the tune parameter of the encoder as zerolatency.

Implement multiple splicing of ffmpeg video streams

To achieve five-frame splicing, you can use the filter function of FFmpeg. First, you need to prepare five video files, which will be spliced ​​together. Then, you can use FFmpeg's concat protocol to splice these five video files together.

ffmpeg -i video1.mp4 -i video2.mp4 -i video3.mp4 -i video4.mp4 -i video5.mp4 -filter_complex "[0:v][1:v][2:v][3:v][4:v]xstack=inputs=5:layout=0_0|w0_0|0_h0|w0_h0|w0+w1_h0[v]" -map "[out]" output.mp4

Explanation:
-filter_complex The parameters are used to specify complex graphics of the filter, and xstackthe filter is used to splice the input video streams. inputs=5Indicates that there are five input videos. layoutThe parameters are used to specify the splicing layout, 0_0|w0_0|0_h0|w0_h0|w0+w1_h0indicate the positions of the five videos, and -map "[out]"are used to specify the output video stream.

layout=0_0|w0_0|w0+w1_0|0_h1|w0_h1[out] :指定了视频流在输出画面中的布局。具体解释如下:   
0_0 :第一个输入视频流的位置是在左上角。 
w0_0 :第二个输入视频流的位置是在第一个视频流的右边。
0_h0 :第三个输入视频流的位置是在第一个视频流的下边。
w0_h0: 第四个输入视频流的位置是在第二个视频流的下边。 
w0+w1_h0 : 第五个输入视频流的位置是在第四个视频流的右边。

Arranged as shown:
Insert image description here

Optimization: (After pushing up the top, the last blank space is in the upper right corner, and the blank space should be moved to the lower right corner below)

ffmpeg -i video1.mp4 -i video2.mp4 -i video3.mp4 -i video4.mp4 -i video5.mp4 -filter_complex 
"[0:v][1:v][2:v][3:v][4:v]xstack=inputs=5:layout=0_0|w0_0|w0+w1_0|0_h1|w0_h1[out]" -map [out] -c:v libx264 -preset veryfast -tune zerolatency -f flv  rtmp://your-streaming-server.com/live/stream-key

explain:

xstack=inputs=5:layout=0_0|w0_0|w0+w1_0|0_h1|w0_h1[out]
这样第三个视频流就放到第二个右边了。

Arranged as shown:
Insert image description here

The most final optimized version

ffmpeg -i video1.mp4 -i video2.mp4 -i video3.mp4 -i video4.mp4 -i video5.mp4 -filter_complex "[0:v]scale=640:400[v0];[1:v]scale=640:400[v1];[2:v]scale=640:400[v2];[3:v]scale=640:400[v3];[4:v]scale=640:400[v4];color=c=black:s=640x400[bg];[v0][v1][v2][v3][v4][bg]xstack=inputs=6:layout=0_0|w0_0|w0+w1_0|0_h1|w0_h1|w0+w1_h1[out]" -map "[out]" -c:v libx264 -preset veryfast -tune zerolatency -f flv rtmp://your-streaming-server.com/live/stream-key

Saves CPU consumption, changes the color of blank spaces, and adjusts the proportional size of the video stream.

The parameters of the color filter use specific values, for example, c=black means black, s=640x400 means the size is 640x400 pixels. This black background is then passed to the xstack filter along with the five previous video streams to produce the final stitched video.

Guess you like

Origin blog.csdn.net/lijunlin0329/article/details/131590015