ffmpeg - compositing a video within a video in the centre

我的梦境 提交于 2019-12-12 11:31:05

问题


I'm looking to composite a video with ffmpeg that places the video in the centre no matter what the composited video's aspect ratio/size.

The "background" video will always be 16:9 and 1920x1080px. I won't know the aspect ratio or size of the overlay video as it will be user uploaded and could be any size/ratio.

Here's an example of what I'm trying to achieve:

This is the background image:

Now I want to overlay a video over the top:

This should also work:

Essentially no matter what the dimensions I want to ensure it's always resized to fit within 1920x1080 and in addition ensure it's always centred.

Finally, if the uploaded video is also 16:9 it should simply overlay the entire video:


回答1:


Use

ffmpeg -i bg.mp4 -i overlay.mp4
  -filter_complex
     "[1]scale='if(gt(dar,16/9),1920,iw*sar*1080/ih)':'if(gt(dar,16/9),ih*1920/iw/sar,1080)',
         setsar=1[ol];
      [0][ol]overlay='(W-w)/2':'(H-h)/2':shortest=1[v]"
  -map "[v]" -map 1:a -c:a copy out.mp4

I assume that you want to terminate the output when the overlay ends and that you want keep its audio (only).

Since you don't want the BG to show if the foreground is exactly 16:9, it will be much more efficient to check beforehand and skip running any ffmpeg command. For that you can use,

ffprobe -show_entries stream=display_aspect_ratio -select_streams v -v 0 -of compact=p=0:nk=1 main.mp4

which will produce a single line output:

16:9


来源:https://stackoverflow.com/questions/42519757/ffmpeg-compositing-a-video-within-a-video-in-the-centre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!