how to merge two videos into one with side-by-side when they have different resolution?

I have two video

  1. deskshare.webm 1280x720 whithout audio
  2. webcams.webm 640x480 whith audio

The commands described below work as needed

ffmpeg -i deskshare.webm -i webcams.webm -filter_complex 
"[0:v]pad=(iw+640):ih[bg]; [bg][1:v]overlay=w+640" out.mp4

ffmpeg -i deskshare.webm -i webcams.webm -filter_complex 
"[0:v]pad=(iw+640):ih[bg]; [bg][1:v]overlay=main_w-overlay_w" out.mp4

Is there any syntax which will allow me to get rid of manual input resolution of second video (in my case is 640 px) ?

Is it possible use something like [1:v]width to get that value automatically ?

In my case also works fine this

ffmpeg -i deskshare.webm -i webcams.webm -filter_complex "[0:v]pad=(iw+iw/2):ih[bg]; [bg][1:v]overlay=main_w-overlay_w" out.mp4

but this will only work if width of 1st video is twice as many width of 2nd video.


Solution 1:

This can be done in a roundabout way.

Use

ffmpeg -i deskshare.webm -i webcams.webm -filter_complex 
   "[1:v][0:v]scale2ref=main_w:ih[sec][pri];
   [sec]setsar=1,drawbox=c=black:t=fill[sec];[pri][sec]hstack[canvas];
   [canvas][1:v]overlay=main_w-overlay_w" out.mp4

The scale2ref resizes one copy of the secondary video to the height of the main video while keeping its original width. Then it's filled with black and stacked to the right of the main video.

A second copy of the secondary video is then overlaid on the expanded canvas.

You'll need ffmpeg 4.0 or newer.