本文介绍了ffmpeg-在中心的视频中合成视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求使用ffmpeg合成视频,无论合成视频的长宽比/大小如何,该视频都将居中放置.

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.

背景"视频将始终为16:9和1920x1080px.我不知道重叠式视频的长宽比或尺寸,因为它将被用户上传,并且可以是任何尺寸/比例.

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.

以下是我要实现的目标的一个示例:

这是背景图片:

现在,我想在顶部覆盖视频:

Now I want to overlay a video over the top:

这也应该起作用:

基本上,无论我要确保将其尺寸调整为始终适合1920x1080以内的尺寸,而且还要确保其始终居中.

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.

最后,如果上传的视频也是16:9,则应仅覆盖整个视频:

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

推荐答案

使用

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).

由于您不希望BG显示前景是否正好是16:9,因此事先检查并跳过运行任何ffmpeg命令的效率会更高.为此,您可以使用

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

将产生单行输出:

16:9

这篇关于ffmpeg-在中心的视频中合成视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 05:20