前言

最近在写个小需求,利用opencv将4个独立的视频文件并列合并成了一个视频文件,并在屏幕的4个区域进行播放。

思路

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import cv2
import numpy as np

videoLeftUp = cv2.VideoCapture('./video/1.mp4')
videoLeftDown = cv2.VideoCapture('./video/2.mp4')
videoRightUp = cv2.VideoCapture('./video/3.mp4')
videoRightDown = cv2.VideoCapture('./video/4.mp4')

fps = videoLeftUp.get(cv2.CAP_PROP_FPS)

width = (int(videoLeftUp.get(cv2.CAP_PROP_FRAME_WIDTH)))
height = (int(videoLeftUp.get(cv2.CAP_PROP_FRAME_HEIGHT)))

videoWriter = cv2.VideoWriter('./merge.mp4', cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), fps, (width, height))

successLeftUp, frameLeftUp = videoLeftUp.read()
successLeftDown , frameLeftDown = videoLeftDown.read()
successRightUp, frameRightUp = videoRightUp.read()
successRightDown, frameRightDown = videoRightDown.read()

while successLeftUp and successLeftDown and successRightUp and successRightDown:
frameLeftUp = cv2.resize(frameLeftUp, (int(width / 2), int(height / 2)), interpolation=cv2.INTER_CUBIC)
frameLeftDown = cv2.resize(frameLeftDown, (int(width / 2), int(height / 2)), interpolation=cv2.INTER_CUBIC)
frameRightUp = cv2.resize(frameRightUp, (int(width / 2), int(height / 2)), interpolation=cv2.INTER_CUBIC)
frameRightDown = cv2.resize(frameRightDown, (int(width / 2), int(height / 2)), interpolation=cv2.INTER_CUBIC)

frameUp = np.hstack((frameLeftUp, frameRightUp))
frameDown = np.hstack((frameLeftDown, frameRightDown))
frame = np.vstack((frameUp, frameDown))

videoWriter.write(frame)
successLeftUp, frameLeftUp = videoLeftUp.read()
successLeftDown, frameLeftDown = videoLeftDown.read()
successRightUp, frameRightUp = videoRightUp.read()
successRightDown, frameRightDown = videoRightDown.read()

videoWriter.release()
videoLeftUp.release()
videoLeftDown.release()
videoRightUp.release()
videoRightDown.release()



©2018 - Felicx 使用 Stellar 创建
总访问 113701 次 | 本页访问 326
共发表 83 篇Blog · 总计 127.5k 字