Ask Your Question

Video Stitching of overlapped videos

asked 2016-11-02 11:58:55 -0500

tinku gravatar image

Hello I have multiple videos which overlap at some time and want to stitch these videos to get a single video stream.

for example below are three videos where video 1 and video 2 overlap at some time

enter image description here

Now I wanted the final o/p to be

enter image description here

Where initially video 1 frames are read and written to the video writer object until there is an overlap. Once there is an overlap the view switches from video 1 or video 2 based on some decisions(which is not my question). Once the overlap ends the frames from video 2 are read and written as the video 1 frames are completed. And finally when they are no frames left from video 2, frames from video 3 are read and written.

My question is how to read and write the overlapped videos as i wanted to have the frames from video or video 2 for the overlapped period. The problem I am facing is how to identify the overlap while reading and writing the frames without checking the video content.

I am finding the overlapped time by analyzing the time stamps and also getting the video indices for which there is an overlap. So, how can I use this information to read and write frames for overlapped videos. Below is the on how I am getting the information which will be used for writing

def dict_cam(key,values):
        return {key[i]:values[i] for i in range (len(key))}

 users=dict_cam(indices,intervals) # Creates dictionary with videos indices as keys(indices) with their start and end time as their values(intervals).

 overlapping = [ [i,j,x,y] for i,x in users.iteritems() for j,y in users.iteritems() if x is not y and x[1]>y[0] and x[0]<y[0] ] # For identifying overlapping.

 for x in overlapping

   if begin < begg and begg<end:
   if ens < end:

over_rang=overlap + overend

Where, camera is a list of video indices where there is an overlap, and over_rang is a list which contains the duration of the overlap.

Before starting video writing I am sorting all the video indices with their start time.

I do not want to analyze the video content rather use the information about the overlapped time and the indices where there is a overlap.

edit retag flag offensive close merge delete


There is a lot of content above, are you after creating a panorama?

Messina Vision Systems gravatar imageMessina Vision Systems ( 2016-11-03 03:35:01 -0500 )edit

@Messina Vision Systems No I am not after creating a panorama but a video mashup by combining multiple videos.

tinku gravatar imagetinku ( 2016-11-03 04:30:02 -0500 )edit

Okay I see now, the x axis is time. So will the end of video1 have the same frames as the start of video2? Then what you want to do is swap between video1 to video2 then video3 seamlessly?

Messina Vision Systems gravatar imageMessina Vision Systems ( 2016-11-03 04:56:43 -0500 )edit

@Messina Vision Systems No, video 1 will not have the same frames(in terms of content) as the start of video 2 the view will be different, but at a point of time there may be two videos like in this case video 1 & video2 .So, i wanted to swap between from video 1 to video 2 to get different frames. Once the overlapped duration is completed If they are no frames to be read in video 2 then I will shift to video3 or else I will read the remaining frames left in video 2 and then shift to video 3.

Video content is not to be considered but there should be switching whenever there is an overlap over time but not on time.

tinku gravatar imagetinku ( 2016-11-03 06:46:08 -0500 )edit

I think you have lost me when they need to swap between videos, but you can use multiple cap1=cv2.VideoCapture("video1.mpeg"). Are you trying to detect when they are "similar" and then perform the swap? This would be dependent on how similar they are and what features you can use.

Messina Vision Systems gravatar imageMessina Vision Systems ( 2016-11-03 07:53:09 -0500 )edit

@Messina Vision Systems I want to swap when there is overlap at a particular time. Like for example video1 is started from 19-13-28 and ends at 19-13-45 and video 2 starts at 19-13-33 and ends at 19-13-59 and video 3 starts at 19-14-05 and ends at 19-14-20. So there is an overlap between video 1 and video 2 at 19-13-33 till 19-13-45. So,I want a final video starting from 19-13-28 to 19-13-45. For doing this i am starting with reading frames from video1 as it is the first video according to the start time. But whenever there is a overlap at 19-33-33 I want to swap with the video with which it is overlapped in this scenario it is video 2 as it starts at 19-13-33.

tinku gravatar imagetinku ( 2016-11-03 08:11:11 -0500 )edit

Not to be rude, but why would you use OpenCV for that, which is a library focussing on image processing? I would think that there are enough video editing libraries out there that do a way better job at this!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-11-04 05:30:12 -0500 )edit

@StevenPuttemans Thank you for your suggestion I am not aware of that good library than OpenCV which reads frames from a video and writes to a new video. Can you provide me some libraries which can detect the overlap over time and then take switching decisions for composing a video like for the example scenario in the question asked?

tinku gravatar imagetinku ( 2016-11-04 05:44:41 -0500 )edit

It was merely a consideration from my side. I do not directly have an idea of a valid other package, just not sure if OpenCV is exactly what you would be using for this.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-11-07 02:46:08 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-11-03 17:02:31 -0500

Here is an example using frames rather than time; hope this helps.

import numpy as np
import cv2

total_frames=0 # Counts frames executed
state = 1 #controls which video to play

# 3 video captures can be set up
cap1=cv2.VideoCapture("video1.mpeg") # First 10 frames
cap2=cv2.VideoCapture("video2.mpeg") # Runs till the end
cap3=cv2.VideoCapture("video3.mpeg") # Runs till the end

   # Check which state it shuold be in and when to swap
   if state == 1:
      ret, frame =
      if total_frames > 10: # After so many frames swap to 2
         state = 2
   if state == 2:
      ret, frame =
      if ret == False: # End of video 2 swap to 3
         state = 3
   if state == 3:
      ret, frame =
      if ret == False: # End of video 3 break

  # Increment counter
  total_frames = total_frames + 1    
  # Perform some conversion to time if you like
  #seconds = total_frames*(1/fps)      

   # Display the resulting frame
   cv2.imshow('frame',frame )
   if cv2.waitKey(1) & 0xFF == ord('q'):
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-11-02 11:58:55 -0500

Seen: 1,184 times

Last updated: Nov 03 '16