ffmpeg filter is extremely powerful and can be used for mixing and streaming. For beginners, they need to understand the writing rules of the filter. The writing rules of the filter are not very easy to understand intuitively. This paper uses the command line to mix the video and give corresponding instructions. Later, we will mix it in the form of code.
For the description of the filter, the following article is well written. Readers may wish to take a look at it first
https://www.cnblogs.com/vikings-blog/p/11512530.html
Prepare four video files, each of which is 1 minute long. I directly grab the local desktop with my own program and generate four video files. For relevant grabs, please refer to my blog ffmpeg recording desktop (take pictures with gdi)
As shown in the figure below, the backgrounds of the four files are resource browser, desktop, e disk and vs2017.
Now open it separately for readers. I opened it in qq video with a resolution of 1920 * 1080
in-computer. The video corresponding to MP4 is as follows:
in-desktop. The video corresponding to MP4 is as follows:
The video corresponding to in-e.mp4 is as follows:
The video corresponding to in-vs.mp4 is as follows:
In fact, you can find that the size of the four files is different, which is related to the complexity of the picture background. The more complex the picture is, the larger the mp4 file is.
Let's start the picture mixing. First, mix two pictures, one left and one right, as shown below:
The corresponding commands are as follows:
ffmpeg -i in-desktop.mp4 -i in-computer.mp4 -filter_complex "[0:v]pad=iw*2:ih*1[myname];[myname][1:v]overlay=w" out1.mp4
Now give a brief description, filter_complex represents the mixing of complex methods, that is, the mixing of more than two files (including two files). Pad and overlay represent two filters respectively. Pad represents the creation of a background, and the previous [0:v] represents the first video file, that is, in desktop MP4, IW and IH represent the width and height in the video, [0:v]pad=iw2:ih1[myname] represents to create a pad with twice the width and the same height as the video file based on the first video file, and name it myname. This name can be defined as other strings;
Then look at [myname][1:v]overlay=w below. This filter is overlay, which represents the video mixing function. There are two brackets in front of it, which represent two sources, of which the first source is the base and the second source is mixed into the first source. overlay=w, which represents the coordinates of the second video file [1:v] in the whole pad, where the abscissa is w and the ordinate is not written, which is 0.
The effect of mixing is as follows:
The resolution is 3840 * 1080.
The above filter writing method is to directly create a pad based on the first video, or create a pad independently, and then mix two video files in turn. The commands are as follows:
ffmpeg -i in-desktop.mp4 -i in-computer.mp4 -filter_complex "pad=3840:1080[x0];[x0][0:v]overlay=0[x1];[x1][1:v]overlay=w" out1.mp4
The filter is divided into three segments, as shown below. In the first segment, a pad (picture base) is created and named x0. In the second segment, the first video [0:v] is mixed to x0, and X1 is generated after mixing. In the third segment, the second video [1:v] is mixed to x1, and the position of [1:v] in X1 is w:0.
Let's look at the effects of the following commands:
ffmpeg -i in-desktop.mp4 -i in-computer.mp4 -filter_complex "[0:v]pad=iw*2:ih*1[myname];[myname][1:v]overlay=w/2" out2.mp4
Compared with the above, only one place has been modified, overlay=w/2, and the position has changed, which means that the second picture will cover half of the first picture. At the same time, because the total width of the pad is 2w, there will be w/2 images on the right, the background will be black, and the resolution is 3840 * 1080.
As follows:
Let's look at the effects of the following commands:
ffmpeg -i in-desktop.mp4 -i in-computer.mp4 -filter_complex "[0:v]pad=iw*3/2:ih*1[myname];[myname][1:v]overlay=w/2" out3.mp4
The corresponding width of this filter is 3w/2, and the resolution is 2880 * 1080: the effect is as follows:
The above three examples are the horizontal arrangement of two videos. Let's look at the vertical arrangement of the two videos:
Enter the following command:
ffmpeg -i in-desktop.mp4 -i in-computer.mp4 -filter_complex "[0:v]pad=iw*1:ih*2[myname];[myname][1:v]overlay=0:h" out4.mp4
The effects are as follows, with a resolution of 1920 * 2160:
Enter the command again as follows:
ffmpeg -i in-desktop.mp4 -i in-computer.mp4 -filter_complex "[0:v]pad=iw*1:ih*2[myname];[myname][1:v]overlay=0:h/2" out5.mp4
The effects are as follows, with a resolution of 1920 * 2160:
You can see that the bottom one is not displayed
Enter the command again as follows:
ffmpeg -i in-desktop.mp4 -i in-computer.mp4 -filter_complex "[0:v]pad=iw*1:ih*3/2[myname];[myname][1:v]overlay=0:h/2" out6.mp4
The effects are as follows, with a resolution of 1920 * 1620:
Next, let's look at the mixing of the next four videos. The interface layout is as follows:
First of all, there is no V4, but only the mixing of three videos. The command is as follows:
ffmpeg -i in-desktop.mp4 -i in-computer.mp4 -i in-e.mp4 -filter_complex "[0:v]pad=iw*2:ih*2[x0];[x0][1:v]overlay=w[x1];[x1][2:v]overlay=0:h" out7.mp4
The effects are as follows, with a resolution of 3840 * 2160:
Look at the four videos again. The command is as follows:
ffmpeg -i in-desktop.mp4 -i in-computer.mp4 -i in-e.mp4 -i in-vs.mp4 -filter_complex "[0:v]pad=iw*2:ih*2[x0];[x0][1:v]overlay=w[x1];[x1][2:v]overlay=0:h[x2];[x2][3:v]overlay=w:h" out8.mp4
The effects are as follows, with a resolution of 38402160:
When the above four videos are mixed, the resolution also increases, which is obviously unrealistic. Normally, the effect we want is that after the four videos are mixed, the resolution of the final generated mixed video is 19201080.
Enter the following command:
ffmpeg -i in-desktop.mp4 -i in-computer.mp4 -i in-e.mp4 -i in-vs.mp4 -filter_complex "[0:v]pad=iw:ih[x0];[x0][1:v]overlay=w/2[x1];[x1][2:v]overlay=0:h/2[x2];[x2][3:v]overlay=w/2:h/2" out9.mp4
The effect is as follows, and the mixed video resolution is 19201080:
Obviously, there is a problem. The right and lower parts of the first video are blocked.
This is because the size of the original video remains unchanged. scale operation is required, that is, 19201080 is changed to 960 * 540.
At this time, a new filter scale needs to be added. The command is as follows:
ffmpeg -i in-desktop.mp4 -i in-computer.mp4 -i in-e.mp4 -i in-vs.mp4 -filter_complex "pad=1920:1080[x0];[0:v]scale=w=960:h=540[scale0];[x0][scale0]overlay=0[x1];[1:v]scale=w=960:h=540[scale1];[x1][scale1]overlay=960[x2];[2:v]scale=w=960:h=540[scale2];[x2][scale2]overlay=0:540[x3];[3:v]scale=w=960:h=540[scale3];[x3][scale3]overlay=960:540" out11.mp4
The effect is as follows: the mixed video resolution is 1920 * 1080:
The effect is normal this time.
Let's look at the following combination of the four Videos: one on the left and three on the right. The size of the left video is 19201080 and the size of the right three videos is 640360. Note the coordinate position of the annotation abbreviations V2, V3 and V4 relative to the whole figure.
The composition commands are as follows:
ffmpeg -i in-desktop.mp4 -i in-computer.mp4 -i in-e.mp4 -i in-vs.mp4 -filter_complex "pad=2560:1080[x0];[0:v]scale=w=1920:h=1080[scale0];[x0][scale0]overlay=0[x1];[1:v]scale=w=640:h=360[scale1];[x1][scale1]overlay=1920[x2];[2:v]scale=w=640:h=360[scale2];[x2][scale2]overlay=1920:360[x3];[3:v]scale=w=640:h=360[scale3];[x3][scale3]overlay=1920:720" out12.mp4
The effect is as follows, with a resolution of 2560 * 1080:
Finally, I give the download address:
Link: https://pan.baidu.com/s/1UW0meYoi7rIvBPpiFxHo8w
Extraction code: 1234
The QR code is as follows:
Readers can contact me if they encounter difficulties in downloading.