#------------------------------------------------------------------------------ #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 #Read videos and image to be used in place of fourth video Vid1 = "v1.mp4" Vid2 = "v2.mp4" Vid3 = "v1.mp4" Pic4 = "img.png" Out = "vid-3-1-image.mp4" cap1 = cv2.VideoCapture(Vid1) cap2 = cv2.VideoCapture(Vid2) cap3 = cv2.VideoCapture(Vid3) img4 = cv2.imread(Pic4, 1) 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) h1 = int(h1) w1 = int(w1) dim = (w1, h1) imgS = cv2.resize(img4, dim, interpolation = cv2.INTER_AREA) def frameConnect(f1, f2, f3, img4, h1, w1): f2 = cv2.resize(f2, dim, interpolation = cv2.INTER_AREA) f3 = cv2.resize(f3, dim, interpolation = cv2.INTER_AREA) BG = cv2.resize(f1, (2*w1, 2*h1), interpolation=cv2.INTER_AREA) BG[0:h1,0:w1] = f1 BG[0:h1,w1:2*w1] = f2 BG[h1:2*h1,0:w1] = f3 BG[h1:2*h1,w1:2*w1] = imgS return (BG) # Set argument for Video Output fps = fps_c1 if Window == None: size = (w1*2, 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) 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()