Ask Your Question
-1

How do I Play Multiple Videos Simultaneously with a Set Position?

asked 2019-02-20 01:02:01 -0600

rrthacks gravatar image

I'm looking for a way to play multiple different videos simultaneously with each video having its own coordinated position when it pops up on the screen. (Also possibly have them automatically pop up on screen as the front window)

https://www.geeksforgeeks.org/opening...

I started off here but I'm not sure where to go from here. Also not sure if it's just my computer or the imshow function having trouble playing 2 different videos at the same time

Here's code I got so far:

% capturing from the first camera attached

cap1 = cv2.VideoCapture('Blue_Bananas.avi') cap2 = cv2.VideoCapture('Green_Oranges.avi')

% will continue to play until 27 sec have passed

while True:

ret1, frame1 = cap1.read()

%Show Video

cv2.imshow('frame1', frame1)

% Program will terminate at 27 sec

k = cv2.waitKey(30) & 0xff if k == 27: break

while True:

ret2, frame2 = cap2.read()     

cv2.imshow('frame2', frame2)

if k == 27:

    break

% Releasing all the resources

cap.release()

cv2.destroyAllWindows()

edit retag flag offensive close merge delete

Comments

please do not try to abuse opencv (a computer vision library) to build a video player.

% Program will terminate at 27 sec

bs. it won't. please read docs,since you obviously didn't

berak gravatar imageberak ( 2019-02-20 02:24:43 -0600 )edit

Program will terminate at 27 sec why ? I hope that the reason is not if k == 27: break

LBerger gravatar imageLBerger ( 2019-02-20 02:56:12 -0600 )edit

-Berak Thanks for being helpful. Now if you could provide a link in the documentation that actually would be helpful. This is for a class and I didn't come in knowing I'd be using opencv. I'm not trying to build a video player, I'm working on a project and thought opencv was the best choice for analyzing the videos. Don't I need to open the videos with opencv to run opencv functions? Or are you so clever you know a way around that that I don't?

rrthacks gravatar imagerrthacks ( 2019-02-20 08:00:50 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-02-20 03:59:01 -0600

supra56 gravatar image

updated 2019-02-20 06:44:45 -0600

Don't used extra while condition block. Just used one condition block. Here is code:

import cv2 as cv

cap = cv.VideoCapture(0)
cap1 = cv.VideoCapture(1)
while True:
   ret, img = cap.read()
   ret1, img1 = cap1.read()
   img1 = cv.resize(img,(360,240))
   img2 = cv.resize(img1,(360,240))
   if cap:
       cv.imshow('img1',img)
   if cap1:
       cv.imshow('img2',img2)

   k = cv.waitKey(0) 
   if k == 27:
      break

cap.release()
cap1.release()
cv.destroyAllWindows()

Updated: This may work for pc:

import cv2 as cv

cap = cv.VideoCapture(0)
cap1 = cv.VideoCapture(1)

if cap.isOpened()== False):
    print("Error camera 1 isn't connecting")
if cap1.isOpened()== False):
    print("Error camera 2 isn't connecting")

while (cap.isOpened() or cap1.isOpened()):
   ret, img = cap.read()
   ret1, img1 = cap1.read()
   if ret == True:
       cv.imshow('Video 1',img)
       cv.imshow('Video 2',img1)

   if cv.waitKey(20) and 0xFF == ord('q'):
        break

cap.release()
cap1.release()
cv.destroyAllWindows()
edit flag offensive delete link more

Comments

if cap1 -- will always be True. (wrong condition)

cv.waitKey(0) -- will block forever (probably not intended)

berak gravatar imageberak ( 2019-02-20 04:18:05 -0600 )edit

In case of cv.waitKey(0). Just add value to cv.waitKey(20). Or set to higher values. Nothing wrong with lf/else block condition.

supra56 gravatar imagesupra56 ( 2019-02-20 04:46:33 -0600 )edit

@supra56 your code is bad :

cap = cv.VideoCapture(62)
if cap:
    print("Every thing is good")
else:
    print("There is a problem")

result is Every thing is good I have no webcam on my PC you cannot use cap to check VideoCapture stream

good code is :

cap = cv.VideoCapture(62)
if cap.isIopened():
    print("Every thing is good")
else:
    print("There is a problem")

result is there is a problem.

to check cap.read use ret and ret1 value

Please edit your answer

LBerger gravatar imageLBerger ( 2019-02-20 05:12:29 -0600 )edit

Accordinly to me that both berak and LBerger are wrongly. The first code I posted will worked on pc too.

supra56 gravatar imagesupra56 ( 2019-02-20 06:01:00 -0600 )edit
1

Thank you supra56, do you know a good library/method to use to place the videos side by side? While they're playing of course.

rrthacks gravatar imagerrthacks ( 2019-02-20 08:14:02 -0600 )edit

Use hstack or vstack. side by side

supra56 gravatar imagesupra56 ( 2019-02-20 08:30:57 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-02-20 00:52:13 -0600

Seen: 10,875 times

Last updated: Feb 20 '19