4

Hi guys I am stuck at this point. I want to play four videos on my screen using opencv . Can anyone help me how to do that? Suppose I want to play simultaneously

  1. first.avi
  2. second.avi
  3. third.avi
  4. fourth.avi

I am referring following code. It plays very well for single avi file. Is it necessary to concatenate or i can run in four different windows?. any suggestions are welcome import cv2 import numpy as np

# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture('first.avi')
cap2 =cv2.VideoCapture('second.avi')

if (cap.isOpened()== False): 
  print("Error opening video stream or file")
if (cap2.isOpened()== False): 
  print("Error opening video stream or file")

while(cap.isOpened()||cap2.isOpened()):
  # Capture frame-by-frame
  ret, frame = cap.read()
  ret, frame1 = cap2.read()
  if ret == True:

   # Display the resulting frame
    cv2.imshow('Frame',frame)
    cv2.imshow('Frame', frame1)


   # Press Q on keyboard to  exit
   if cv2.waitKey(25) & 0xFF == ord('q'):
  break
  else: 
    break


cap.release()
cap2.release()

cv2.destroyAllWindows()
11
  • Just read all and display? Commented Dec 21, 2017 at 9:58
  • I know that while capturing from camera cap = cv2.VideoCapture(0) cap1 =cv2.VideoCapure(1) works well but while playing video from file its not working properly Commented Dec 21, 2017 at 9:58
  • i tried to read all but it is overlapping in same frame Commented Dec 21, 2017 at 9:59
  • 2
    @api55... I think what the OP meant by "overlapping" is that frames from multiple videos are being displayed in a single window in the order in which they are being captured. Commented Dec 21, 2017 at 10:26
  • 2
    @sgarizvi that was my guess, and that is why i suggested diffrent names in imshow function... just that i said camera instead of video.... Commented Dec 21, 2017 at 10:28

2 Answers 2

19

For playing multiple videos, we have to use unique window titles for each video. Here is a sample code demonstrating how it can be achieved.

import numpy as np
import cv2

names = ['first.avi', 'second.avi', 'third.avi', 'fourth.avi'];
window_titles = ['first', 'second', 'third', 'fourth']


cap = [cv2.VideoCapture(i) for i in names]

frames = [None] * len(names);
gray = [None] * len(names);
ret = [None] * len(names);

while True:

    for i,c in enumerate(cap):
        if c is not None:
            ret[i], frames[i] = c.read();


    for i,f in enumerate(frames):
        if ret[i] is True:
            gray[i] = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
            cv2.imshow(window_titles[i], gray[i]);

    if cv2.waitKey(1) & 0xFF == ord('q'):
       break


for c in cap:
    if c is not None:
        c.release();

cv2.destroyAllWindows()

P.S: This code is just a quick and dirty example for demo purpose only. Tested with python 2 and OpenCV 3.2 on Ubuntu 14.04.

Sign up to request clarification or add additional context in comments.

3 Comments

is it possible to play them in the same window without a pause ?
@Fadolphin. Yes it is possible, by providing the same window name for each image in the cv2.imshow call. Also, my opinion is to create the window beforehand and resizing it to fixed size by calling cv2.namedWindow and cv2.resizeWindow respectively before the while loop.
Thank you sir. You saved my program a great amount of resources. I was using moviepy to merge them both into one video, then play it in a single window. it takes about 60 seconds just for the the merge to commence.
0

Here is the Optimised code and worked for me

import cv2
import numpy as np 


cap1 = cv2.VideoCapture('1.mp4')
cap2 = cv2.VideoCapture('2.mp4')
while cap1.isOpened() or cap2.isOpened():

    okay1  , frame1 = cap1.read()
    okay2 , frame2 = cap2.read()

    if okay1:
        hsv1 = cv2.cvtColor(frame1 , cv2.COLOR_BGR2HSV)
        cv2.imshow('fake' , hsv1)

    if okay2:
        hsv2 = cv2.cvtColor(frame2 , cv2.COLOR_BGR2HSV)
        cv2.imshow('real' , hsv2)

    if not okay1 or not okay2:
        print('Cant read the video , Exit!')
        break

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    
    cv2.waitKey(1)

cap1.release()
cap2.release()
cv2.destroyAllWindows()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.