且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用ffmpeg或opencv将2个重叠的视频合并为一个视频?

更新时间:2023-10-02 15:21:16

对于ffmepg,您可以使用以下脚本.它测试了它.但是在时间上明智的做法是,您必须在视频中将此STARTPTS + 5更改为+25.我在这里输入5来测试合并是否正在发生.

for ffmepg you can use the script below. it tested it. But timing wise, you have to change of this STARTPTS+5 to +25 in your video. I put 5 here to test the merging is happening.

ffmpeg -i 2.mp4 -i 1.mp4 -filter_complex "[1]setpts=PTS-STARTPTS+5/TB[top];[0:0][top]overlay=enable='between(t\,10,15)'[out]" -shortest -map [out] -map 0:1        -pix_fmt yuv420p -c:a copy -c:v libx264 -crf 18 output1.mp4

限制 这需要源足够长,这意味着您需要视频画布,然后使用此脚本将每个视频添加到画布中.

Limitation This one need the source to be long enough which means you need video canvas then use this script to add each video into the canvas.

并且在ffmpeg中没有完全自主的使用方式.

And there is no fully autonomous way of use it in ffmpeg.

你是对的. Opencv无法处理音频.需要第3方库支持才能同时运行.在此之前,我必须使用ROS从网络摄像头获取声音和视觉到机器人系统.然后,使用NLP处理声音以用于母语用户界面,并将视觉分别用于locozlaiton和制图.

You are right. Opencv cant deal with audio. need 3rd party library support to run concurrently. Before then I have to use ROS to get both sound and vision to the robot system from a webcam. The sound is then process with NLP for natual language user interface and vision is used separately for locozlaiton and mapping.

有一些走动的方式.

首先,您在本地窗口批处理上使用OpenCV模板匹配或图像差异.最小的错误位置将为您提供正确的位置A插入.就毫秒级而言,这应该是准确的. (如果错误总是很大,则意味着没有重叠并返回异常)

First, you use OpenCV template matching or image difference on a local window batch. The smallest error position will give you the correct location A to insert. This should be accurate in terms of mili-second level. (if error is always big, then it means there is no overlap and return exception)

第二,基于从opencv获得的正确位置.调用system.call以使用A参数作为输入来调用上述脚本以进行自动合并.

Second, based on the correct location obtained from opencv. call system.call to invoke the above script with A parameter as input to do auto merge.

取决于您的应用程序,如果您需要经常执行此操作,请将opencv python脚本编写为自动保险丝.如果每个月只有一次,那么用ffmepg手动完成就足够了

Depends on your application, if you need to do it frequently, write opencv python script to automatic fuse. If just once every month, do it manually with ffmepg is good enough