How to merge 2 overlapping videos into one video u

2019-07-24 01:01发布

问题:

Merging two videos is easy, been answered couple of times. What I have is multiple overlapping videos. A video might have overlaps with video before it. Meaning if video 1 covers 1-5 timeline then video 2 may overlap 1, and cover 3 to 8. Merging them as is would result in 1-5|3-8, when i need 1-8 only.

Videos are alphabetically sorted.

My general idea of solution is...

  • grab last frame of the video
  • if it's first video continue
  • if it's not first video, ie. 2nd, search for frame saved in previous steps frame by frame
  • if it reaches to last frame of current video then there is no overlap continue
  • if it founds a frame then clip 2nd video up to that frame inclusive and then go to next frame
  • once all videos have been analyzed, merge them into one video.

I need to translate this to ffmpeg commands. Or opencv if that's a better tool.

If there is better way of doing that, I'm interested in that too.

回答1:

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.

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

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.

There is some way to walk around.

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)

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.

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