OpenCV和Python:如何录制和保存一定数量的视频帧?

OpenCV和Python:如何录制和保存一定数量的视频帧?,python,opencv,Python,Opencv,我正在使用OpenCV从两个不同的网络摄像头同时录制视频,因为我想稍后同步视频中发生的事件。我想对这些视频进行计时,以30帧/秒的速度精确录制18000帧10分钟,我想知道如何/在我的代码中包括这一点 import numpy as np import cv2 video_capture_1 = cv2.VideoCapture(1) video_capture_2 = cv2.VideoCapture(2) frame_width1 = int(video_capture_1.get(

我正在使用OpenCV从两个不同的网络摄像头同时录制视频,因为我想稍后同步视频中发生的事件。我想对这些视频进行计时,以30帧/秒的速度精确录制18000帧10分钟,我想知道如何/在我的代码中包括这一点

import numpy as np
import cv2


video_capture_1 = cv2.VideoCapture(1)
video_capture_2 = cv2.VideoCapture(2)


frame_width1 = int(video_capture_1.get(3))
frame_height1 = int(video_capture_1.get(4))
frame_width2 = int(video_capture_2.get(3))
frame_height2 = int(video_capture_2.get(4))

out1 = cv2.VideoWriter('test1_200919.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 30, (frame_width1,frame_height1))
out2 = cv2.VideoWriter('test2_200919.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 30, (frame_width2,frame_height2))


while True:
    ret1, frame1 = video_capture_1.read()
    ret2, frame2 = video_capture_2.read()


    if (ret1):
        out1.write(frame1)
        cv2.imshow('Cam 1', frame1)

    if (ret2):
        out2.write(frame2)
        cv2.imshow('Cam 2', frame2)

    if cv2.waitKey(1) & 0xFF == ord('q'): 
        break


video_capture_1.release()
video_capture_2.release()
out1.release()
out2.release()
cv2.destroyAllWindows()

您只需添加一个计数器:

counter = 0

while True:
    counter += 1

    if (cv2.waitKey(1) & 0xFF == ord('q')) or counter == 18000: 
        break