#------------------------------------------------------------------------------ #Ref: karobben.github.io/2021/04/10/Python/opencv-v-paste #Code adapted to add videos up/down and side. The second to fourth videos are # scaled to width of first video. FPS of all videos changed to that of first. # Arrangement of videos is # First Second # Third Fourth # Code was tested on a video of resolution 1280x720 and the resultant video # having size 2560x1440 could be opened without any error on Ubuntu LTS 22.0, # 15.4" monitor (1366x768) of resolution 100 DPI. #------------------------------------------------------------------------------ import cv2 import numpy as np #Read the input videos and specify name of the output video Vid1 = "v1.mp4" Vid2 = "v2.mp4" Vid3 = "v1.mp4" Out = "vid-3-in-2x2.mp4" cap1 = cv2.VideoCapture(Vid1) cap2 = cv2.VideoCapture(Vid2) cap3 = cv2.VideoCapture(Vid3) Window = None #or '1920x1080' or '1280x720' - check Aspect Ratio of input #Get the fps and the size of the Video one fps_c1 = cap1.get(cv2.CAP_PROP_FPS) h1 = cap1.get(cv2.CAP_PROP_FRAME_HEIGHT) w1 = cap1.get(cv2.CAP_PROP_FRAME_WIDTH) def frameConnect(f1, f2, f3, img4, h1, w1): f2 = cv2.resize(f2, (int(w1), int(h1)), interpolation = cv2.INTER_AREA) f3 = cv2.resize(f3, (int(w1), int(h1)), interpolation = cv2.INTER_AREA) BG = cv2.resize(f1, (int(w1*2), int(2*h1)), interpolation=cv2.INTER_AREA) BG[0:int(h1),0:int(w1)] = f1 BG[0:int(h1),int(w1):int(w1*2)] = f2 BG[int(h1):int(h1*2),0:int(w1)] = f3 BG[int(h1):int(h1*2),int(w1):int(w1*2)] = img4 return (BG) # Set argument for Video Output fps = fps_c1 if Window == None: size = (int(w1*2), int(h1*2)) else: size = (int(Window.split("x")[0]), int(Window.split("x")[1])) fourcc = cv2.VideoWriter_fourcc('m','p','4','v') videowriter = cv2.VideoWriter(Out,fourcc,fps,size) img4 = np.ones((int(h1),int(w1),3), np.uint8) * 255 while (True): ret,f1=cap1.read() ret,f2=cap2.read() ret,f3=cap3.read() #Prevent resize of a Null image, which is the LAST frame of the video if ret == True: img = frameConnect(f1, f2, f3, img4, h1, w1) img = cv2.resize(img, size, interpolation = cv2.INTER_AREA) videowriter.write(img) else: break cap1.release() cap2.release() cap3.release() videowriter.release() cv2.destroyAllWindows()