Search code examples
pythonopencvvideovideo-capturevideo-processing

How to create multiple VideoCapture Objects


I wanted to create multiple VideoCapture Objects for stitching video from multiple cameras to a single video mashup.

for example: I have path for three videos that I wanted to be read using Video Capture object shown below to get the frames from individual videos,so they can be used for writing.

Expected:For N number of video paths

   cap0=cv2.VideoCapture(path1)
   cap1=cv2.VideoCapture(path2)
   cap2=cv2.VideoCapture(path3)
   .
   . 
   capn=cv2.VideoCapture(path4)

similarly I also wanted to create frame objects to read frames like

ret,frame0=cap0.read()
ret,frame1=cap1.read()
.
.
ret,frameN=capn.read()

I tried using for loop on the lists where the paths are stored but every time only one path is read and frames are stored for that particular video only.I have seen in many forums it is possible to create multiple capture objects in C++ but not in python in dynamic scenario where number of videos are not known before hand. This is my code until now

frames=[]
for path in videoList:
    indices=[]
    cap = cv2.VideoCapture(path)

    while(cap.isOpened()):
        ret,frame=cap.read()
        if not ret:
           break
        indices.append(cap.get(1))
    frames.append(indices)
    cap.release()
    cv2.destroyAllWindows()

Solution

  • I'm not a python programmer, but probably the solution is something like:

    frames = []
    caps = []
    for path in videoList:
        caps.append(cv2.VideoCapture(path))
    
    for cap in caps:
        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                break
            frames.append(frame)
    
    # now "frames" holds your captured images.