Ask Your Question
0

Combine several videos in the same window (Python)

asked 2018-09-25 06:52:17 -0600

Franz_F gravatar image

updated 2018-09-25 09:13:38 -0600

Hello,

is it possible to combine several videos either vertical or horizontal into the same window or output file? The videos have the number of frames and format.

I tried using np.concentrate but this causes problems with the dimensions of the arrays.

Many thanks in advance

Edit: This is how it can be done - maybe there is a more elegant way but it works fine for me

import cv2
import numpy as np

cap = cv2.VideoCapture('Video3.avi',0)
cap1 = cv2.VideoCapture('Video4.avi',0)

while(cap.isOpened()):

    ret, frame = cap.read()
    ret1, frame1 = cap1.read()
    if ret == True: 

        both = np.concatenate((frame, frame1), axis=1)


        cv2.imshow('Frame', both)
        out.write(frame)


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


    else: 
        break

cap.release()
out.release()


cv2.waitKey(0)
cv2.destroyAllWindows()

Edit2: Thank you very much. Another question came up now:

The function

out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'XVID'), cap.get(cv2.CAP_PROP_FPS), (frameWidth,frameHeight))

out.write(both)

is not able to write the video anymore. The function has no problem writing just one source like:

    out.write(frame)
    out.write(frame1)

but not both. Is there a solution?

edit retag flag offensive close merge delete

Comments

np.concentrate does not exist !

berak gravatar imageberak ( 2018-09-25 07:48:54 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-09-25 07:48:00 -0600

berak gravatar image

updated 2018-09-25 09:29:59 -0600

it does not matter, if you use np.concatenate() , or cv2.hconcat() --

you have to make sure, both images have the same size ! so:

   if ret == True and ret1 == True: # you *have* to check *both* captures ! 
        h,w,c = frame.shape;
        h1,w1,c1 = frame1.shape;

        if h != h1 or w != w1: # resize right img to left size
            frame1 = cv2.resize(frame1,(w,h))

        both = np.concatenate((frame, frame1), axis=1)
        # or like this:
        # both = cv2.hconcat([frame, frame1])

[EDIT:]

and ofc,

both.shape == (frame.shape[0], 2*frame.shape[1])

so, if you want to write a video with that, you have to use:

out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'XVID'), 
    cap.get(cv2.CAP_PROP_FPS), (2 * frameWidth,frameHeight))

(assuminig, both videos have the same size)

edit flag offensive delete link more

Comments

1

Thank you very much. Another problem came up. I'm not allowed to answer my own question so I made another Edit to my first post.

Franz_F gravatar imageFranz_F ( 2018-09-25 09:14:38 -0600 )edit

^^ "another problem" should not go into an answer anyway ! (so, correct decision)

berak gravatar imageberak ( 2018-09-25 09:25:11 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-09-25 06:52:17 -0600

Seen: 12,255 times

Last updated: Sep 25 '18