读取帧
OpenCV读取视频是读取每一帧,下面的代码功能是截取从startSec到endSec内的视频帧存储起来,实际截取的图像有(endSec-startSec)*fps帧图像。
def video_process(videoPath, saveVideoPath, startSec, endSec, savePic=False):
cap = cv.VideoCapture(videoPath)
img_cnt = 0
print(cap)
fps = cap.get(5)
print(cap.get(3), cap.get(4))
size = (int(cap.get(3)), int(cap.get(4)))
# size = (1232, 1890) # 需要转为视频的图片的尺寸
video = cv.VideoWriter(saveVideoPath, cv.VideoWriter_fourcc('M', 'P', '4', 'V'),
fps, size)
while cap.isOpened():
ret, rawImg = cap.read()
img_cnt += 1
if ret:
# key = cv.waitKeyEx(0)
if startSec*fps < img_cnt < endSec*fps:
video.write(rawImg)
if savePic:
cv.imwrite(r'{0}\{1}_{2}.png'.format(imgDir, v