4/09/2023

Combine Two Videos Side by Side with OpenCV python

 refer to code:

.

import cv2

# Open the two video files
video1 = cv2.VideoCapture('video1.mp4')
video2 = cv2.VideoCapture('video2.mp4')

# Get video properties
width1 = int(video1.get(cv2.CAP_PROP_FRAME_WIDTH))
height1 = int(video1.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps1 = video1.get(cv2.CAP_PROP_FPS)

width2 = int(video2.get(cv2.CAP_PROP_FRAME_WIDTH))
height2 = int(video2.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps2 = video2.get(cv2.CAP_PROP_FPS)

# Check if videos have the same FPS and height
assert fps1 == fps2, "Videos should have the same FPS"
assert height1 == height2, "Videos should have the same height"

# Create a VideoWriter object to save the combined video
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', fourcc, fps1, (width1 + width2, height1))

while video1.isOpened() and video2.isOpened():
ret1, frame1 = video1.read()
ret2, frame2 = video2.read()

if not ret1 or not ret2:
break

# Concatenate the frames side by side
combined_frame = cv2.hconcat([frame1, frame2])

# Write the combined frame to the output video
out.write(combined_frame)

# Display the combined frame
cv2.imshow('Combined Video', combined_frame)

# Press 'q' to stop the process and close the window
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the video files and the output video
video1.release()
video2.release()
out.release()

cv2.destroyAllWindows()

..


Thank you.

www.marearts.com

πŸ™‡πŸ»‍♂️

No comments:

Post a Comment