Python将两个视频横向的拼接--视频效果对比
1、直接上干货!
import cv2
import numpy as np
#视频的路径
INPUT_FILE1 = 'E:/Download/BUG/Video/32.mp4'
INPUT_FILE2 = 'E:/Download/BUG/Video/33.mp4'
OUTPUT_FILE = 'E:/Download/BUG/Video/32_33.mp4'
reader1 = cv2.VideoCapture(INPUT_FILE1)
reader2 = cv2.VideoCapture(INPUT_FILE2)
width = int(reader1.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(reader1.get(cv2.CAP_PROP_FRAME_HEIGHT))
#视频的输出格式
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
print(width)
print(height)
writer = cv2.VideoWriter(OUTPUT_FILE,fourcc,
30, # fps
(width*2, height))
print(reader1.isOpened())
print(reader2.isOpened())
have_more_frame = True
while have_more_frame:
have_more_frame, frame1 = reader1.read()
_, frame2 = reader2.read()
#frame1 = cv2.resize(frame1, (width, height))
#frame2 = cv2.resize(frame2, (width, height))
img = np.hstack((frame1, frame2))
cv2.waitKey(1)
writer.write(img)
#释放所有的资源
writer.release()
reader1.release()
reader2.release()
cv2.destroyAllWindows()
效果如下: