動画を並べる

2019/07/09

Python2.7.6, OpenCV3.0.0

# -*- coding: utf-8 -*-
import cv2
import numpy as np

# 入力動画
cap1 = cv2.VideoCapture(r'C:\Temp\input1.avi')
cap2 = cv2.VideoCapture(r'C:\Temp\input2.avi')

# 出力先
fps = 24
fourcc = 'DIB '
size = (340,300)
video1 = cv2.VideoWriter(r'C:\Temp\output.avi', cv2.VideoWriter_fourcc(*fourcc), fps, size)

while True:
    ret1, frame1 = cap1.read()
    ret2, frame2 = cap2.read()
    if not ret1 or not ret2:
        break

    #横に並べる    
    frame = cv2.hconcat([frame1, frame2])
    #縦なら
    #frame = cv2.vconcat([frame1, frame2])

    cv2.imshow('view', frame)
    video1.write(frame)

    k = cv2.waitKey(1)
    if k in [27, ord('q')]:
        break

cap1.release()
cap2.release()
video1.release()
cv2.destroyAllWindows()