OpenCV分割视频文件的四个区域

代码位置:104-SplitTheFourAreasOfTheVideo.py
上一篇写的是《OpenCV将4个视频文件并列合成为1个,在窗口的4个区域播放》这一篇是将一个拆成四个,道理很简单,就是将视频一份四份,左上、左下、右上、右下,分别编辑成视频。
代码:

import cv2
import numpy as np

cameraCapture = cv2.VideoCapture('./res/2_003_013.mp4')
width = int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cameraCapture.get(cv2.CAP_PROP_FPS)

videoWriterLeftUp = cv2.VideoWriter('./out/LeftUp.mp4', cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), fps, (width//2, height//2))
videoWriterLeftDown = cv2.VideoWriter('./out/LeftDown.mp4', cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), fps, (width//2, height//2))
videoWriterRightUp = cv2.VideoWriter('./out/RightUp.mp4', cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), fps, (width//2, height//2))
videoWriterRightDown = cv2.VideoWriter('./out/RightDown.mp4', cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), fps, (width//2, height//2))

success, frame = cameraCapture.read()
while success:
    frameLeftUp = frame[0:height//2, 0:width//2, :]
    videoWriterLeftUp.write(frameLeftUp)

    frameLeftDown = frame[height//2:height, 0:width//2, :]
    videoWriterLeftDown.write(frameLeftDown)

    frameRightUp = frame[0:height//2, width//2:width, :]
    videoWriterRightUp.write(frameRightUp)

    frameRightDown = frame[height//2:height, width//2:width, :]
    videoWriterRightDown.write(frameRightDown)

    success, frame = cameraCapture.read()

cameraCapture.release()
videoWriterLeftUp.release()
videoWriterLeftDown.release()
videoWriterRightUp.release()
videoWriterRightDown.release()

原始视频:
OpenCV分割视频文件的四个区域
分割后:

左上 右上
OpenCV分割视频文件的四个区域 OpenCV分割视频文件的四个区域
左下 右下
OpenCV分割视频文件的四个区域 OpenCV分割视频文件的四个区域

最后给视频配个音乐吧
ffMpeg命令小集合这里有需要的命令使用方式。