简体   繁体   English

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

[英]OpenCV and Python: How to record and save certain number of video frames?

I am simultaneously recording videos from two different webcams using OpenCV, as I want to synchronize events happening in the video later on.我正在使用 OpenCV 同时从两个不同的网络摄像头录制视频,因为我想稍后同步视频中发生的事件。 I want to time these videos to record exactly 18,000 frames (10 minutes at 30fps) and am wondering how/where to include this in my code?我想为这些视频计时以准确记录 18,000 帧(10 分钟,30fps),我想知道如何/在哪里将其包含在我的代码中?

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()

You can simply add a counter:您可以简单地添加一个计数器:

counter = 0

while True:
    counter += 1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM