'''
用于裁剪视频部分区域并将各区域保存为.mp4格式的新视频
'''
import cv2 as cv
import sys
video = cv.VideoCapture('C:/Users/sjh/Desktop/2021_6_16_19_30_39.h264')
fps = video.get(cv.CAP_PROP_FPS)
print(fps)
fourcc = cv.VideoWriter_fourcc('m', 'p', '4', 'v')
sz = (int(video.get(cv.CAP_PROP_FRAME_WIDTH)/2), int(video.get(cv.CAP_PROP_FRAME_HEIGHT)/2))
print(sz)
vout_1 = cv.VideoWriter()
vout_1.open('C:/Users/sjh/Desktop/left_top.mp4',fourcc,fps,sz,True)
vout_2 = cv.VideoWriter()
vout_2.open('C:/Users/sjh/Desktop/right_top.mp4',fourcc,fps,sz,True)
vout_3 = cv.VideoWriter()
vout_3.open('C:/Users/sjh/Desktop/left_bottom.mp4',fourcc,fps,sz,True)
vout_4 = cv.VideoWriter()
vout_4.open('C:/Users/sjh/Desktop/right_bottom.mp4',fourcc,fps,sz,True)
count = 0
while True:
ret, frame = video.read()
if ret == 1:
print(count)
left_top = frame[0:962, 0:1280]
right_top = frame[0:962, 1280:2560]
left_bottom = frame[962:1924, 0:1280]
right_bottom = frame[962:1924, 1280:2560]
vout_1.write(left_top)
vout_2.write(right_top)
vout_3.write(left_bottom)
vout_4.write(right_bottom)
count += 1
else:
break
vout_1.release()
vout_2.release()
vout_3.release()
vout_4.release()
sys.exit()
类似代码可以借鉴进行修改用于自己需求
import cv2 as cv
import numpy
cap = cv.VideoCapture('./*****.mp4')
fourcc = cv.VideoWriter_fourcc(*'mp4v')
width = int(cap.get(3))
height = int(cap.get(4))
out = cv.VideoWriter('new.mp4',fourcc,60,(width,height))
while cap.isOpened():
ret,frame = cap.read()
if not ret:
print('can not receive frame')
break
out.write(frame)
cv.namedWindow('frame',cv.WINDOW_NORMAL)
if cv.waitKey(1) == ord('q'):
break
cap.release()
out.release()
cv.destroyAllWindows()