Good afternoon, the task was to glue several videos into one (the number of videos is always different (i.e. maybe 1, maybe 2, maybe n)), actually googled this example:

ffmpeg -i new_1.mp4 -i new_2.mp4 -i new_3.mp4 -i new_4.mp4 -filter_complex "nullsrc=size=640x480 [base]; \ [0:v] setpts=PTS-STARTPTS, scale=320x240 [upperleft]; \ [1:v] setpts=PTS-STARTPTS, scale=320x240 [upperright]; \ [2:v] setpts=PTS-STARTPTS, scale=320x240 [lowerleft]; \ [3:v] setpts=PTS-STARTPTS, scale=320x240 [lowerright]; \ [base][upperleft] overlay=shortest=1 [tmp1]; \ [tmp1][upperright] overlay=shortest=1:x=320 [tmp2]; \ [tmp2] [lowerleft] overlay=shortest=1:y=240 [tmp3]; \ [tmp3][lowerright] overlay=shortest=1:x=320:y=240" -c:v output.mp4 

In this example, 4 videos are glued together, but I don’t quite understand, is it possible to write a video position instead of upperright?

example. Let's say that the canvas on which it works will be 1280 by 768 and we have 6 videos (all videos are the same size, but have different lengths, we assume that the size of one video is 1024 by 768). We try to place them in two lines, three videos per line. we get the following dimensions for the video: width: 1280/3 = 426 pixels height: 768/2 = 384, where 2 is the number of lines

Actually questions: 1) Can I set the position of the video wrong?

[0:v] setpts=PTS-STARTPTS, scale=320x240 [upperleft];

but like this:

[0:v] setpts=PTS-STARTPTS, scale=426x384 [0,384];

[1:v] setpts=PTS-STARTPTS, scale=426x384 [426,384];

or something else? How to make a mesh in ffmpeg?

2) in the case of videos that are different in time, after the shortest video has elapsed, the picture is further slowed down, but the audio track goes, how to solve the given question? do I adjust the short timing video to the longest one? then how is this done? Something like we fill the rest with 4 frames?

thanks for the help.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

upperleft here is not a position, but an alias for the scale result stream.

 [0:v] setpts=PTS-STARTPTS, scale=320x240 [upperleft]; 

Take the stream 0:v , shake it up to 320x240 and call it upperleft for further use. Here, the video is still not composited with others - only it is pinched to the desired size.

 [base][upperleft] overlay=shortest=1 [tmp1] 

Take the base and upperleft , apply overlay with such parameters and tmp1 result to the tmp1 stream. The names of the streams themselves can be any. This is where the video is glued together.

The position of the overlay on the result stream is the overlay x and y parameters, which are visible in the last 3 lines. Here is a list of possible options. For the second question, see eof_action and repeatlast

For 6 videos, something like this will turn out:

  ffmpeg -i new_1.mp4 -i new_2.mp4 -i new_3.mp4 -i new_4.mp4 -i new_5.mp4 -i new_6.mp4 -filter_complex "nullsrc=size=1280x768 [base]; \ [0:v] setpts=PTS-STARTPTS, scale=426x384 [upper1]; \ [1:v] setpts=PTS-STARTPTS, scale=426x384 [upper2]; \ [2:v] setpts=PTS-STARTPTS, scale=426x384 [upper3]; \ [3:v] setpts=PTS-STARTPTS, scale=426x384 [lower1]; \ [4:v] setpts=PTS-STARTPTS, scale=426x384 [lower2]; \ [5:v] setpts=PTS-STARTPTS, scale=426x384 [lower3]; \ [base][upper1] overlay=shortest=1 [tmp1]; \ [tmp1][upper2] overlay=shortest=1:x=426 [tmp2]; \ [tmp2][upper3] overlay=shortest=1:x=852 [tmp3]; \ [tmp3][lower1] overlay=shortest=1:y=384 [tmp4]; \ [tmp4][lower2] overlay=shortest=1:y=384:x=426 [tmp5]; \ [tmp5][lower3] overlay=shortest=1:y=384:x=852" -c:v output.mp4