如何使用python opencv设置捕获视频的时间?

0 python opencv

我有一个代码可以从 github https://gist.github.com/keithweaver/4b16d3f05456171c1af1f1300ebd0f12#file-save-video-w-opencv-py 的摄像头捕获视频。

但是如何设置此捕获的时间限制?。我想连续捕获多个视频,持续时间为 3 分钟,没有任何丢帧。

我对编程有点陌生,任何人都可以帮助编写代码。非常感谢

Ja_*_*cpp 6

你可以这样做:

  1. startTime = time.time()
  2. timeElapsed = startTime - time.time() 片刻之间
  3. secElapsed = int(timeElapsed)
  4. 停止程序时 while(secElapsed < 100)

代码示例,它应该是这样的:

import numpy as np
import cv2
import time

# The duration in seconds of the video captured
capture_duration = 10

cap = cv2.VideoCapture(0)

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

start_time = time.time()
while( int(time.time() - start_time) < capture_duration ):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)
        out.write(frame)
         cv2.imshow('frame',frame)
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)