eladg / ffmpeg-by-example

Creative Commons Attribution 4.0 International
8 stars 5 forks source link

Stitch two video files side by side - FFmpeg By Example #51

Open eladg opened 2 years ago

eladg commented 2 years ago

https://www.ffmpegbyexample.com/examples/k1u2p6i9/stitch_two_video_files_side_by_side/

Stitch yoga.mp4 and sharks.mp4 side-by-side and output to output.mp4

The following example assumes the two videos are of the same width. The 2nd video will be overlay'ed to the right of the first video.

[0:v]pad=iw+iw:ih[v1]; using the pad filter, add space to the right of the first input, to make space for the second video. The pad size is the size of the input video width (which is set in the iw parameter to the filter).

[v1][1:v]overlay=overlay_w:0[vid] overlay the second input on top of the padded input (set as [v1]), starting at coordinates overlay_w:0 i.e.: X:size of the overlay, Y:0, this aligns the second input on the black space we just padded.

-an ignores any audio that was set to either inputs.

-map [vid] maps the complex stream named 'vid' to the output file.

g-l-i-t-c-h-o-r-s-e commented 2 years ago

This can also be achieved with: ffmpeg -i input1.mp4 -i input2.mp4 -lavfi "[0:v]scale=1280x720[1];[1:v]scale=1280x720[2];[1][2]hstack" output.mp4

eladg commented 2 years ago

indeed @g-l-i-t-c-h-o-r-s-e, hstack is a great way to do it. However, Simply's solution is more general and not limited to 720p or 16:9 videos.