赞
踩
# -*- coding:utf-8 -*- ''' 视频拼接 ''' import cv2 import numpy as np cam1 = cv2.VideoCapture("./1sample_video.mp4") cam2 = cv2.VideoCapture("./2sample_video.mp4") # 获取视频1的宽度 ww = int(cam1.get(3)) # 获取视频1的高度 hh = int(cam1.get(4)) print(ww, hh) # 获取视频的帧频 CAMERA_FPS = cam1.get(cv2.CAP_PROP_FPS) # 定义视频编码格式 fourcc = cv2.VideoWriter_fourcc(*'XVID')#也可以是'WMV1',其他格式参考https://www.fourcc.org/codecs.php # 创建视频保存对象 videoWriter = cv2.VideoWriter('./out.avi', fourcc, CAMERA_FPS,(ww * 2, hh))#当是'WMV1'时,out.wmv while True: # 读取视频 (ok1, frame1) = cam1.read() (ok2, frame2) = cam2.read() if ok1 and ok2: # 重置视频大小,使两视频大小一致 frame1 = cv2.resize(frame1, (ww, hh)) frame2 = cv2.resize(frame2, (ww, hh)) # 在视频中添加文字 cv2.putText(frame1, "Video 1", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 255, 0), 2) cv2.putText(frame2, "Video 2", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 255, 0), 2) # 拼接处理 image = np.concatenate([frame1, frame2], axis=1) # axis=0时为垂直拼接;axis=1时为水平拼接 # 视频保存 videoWriter.write(image) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break videoWriter.release() cam1.release() cam2.release()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。