0

我正在寻找每分钟保存视频并再次录制的方法。当我运行我的代码时,我会捕获它,直到我按下“q”。有没有办法自动保存并重新记录?我使用 imutils,cv2

import imutils 
import cv2 # opencv 모듈

video = ""

result_path = "result_video.avi"

if video == "":
    print("[webcam start]")
    vs = cv2.VideoCapture(0)

else:
    print("[video start]")
    vs = cv2.VideoCapture(video)

writer = None

while True:
    ret, frame = vs.read()

    if frame is None:
        break

    frame = imutils.resize(frame, width=320, height=240)

    cv2.imshow("frame", frame)

    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break
                                    
    if writer is None:
        fourcc = cv2.VideoWriter_fourcc(*"MJPG")
        writer = cv2.VideoWriter(result_path, fourcc, 25, (frame.shape[1], frame.shape[0]), True)

    if writer is not None:
        writer.write(frame)

vs.release()
cv2.destroyAllWindows()
4

1 回答 1

1

你还没有发布你的代码,它并不明显,你是如何记录的,但是下面的代码可以为你添加一个计时器,它提供了很多自定义,例如在指定的秒、分钟甚至几个小时内记录一次。

from time import sleep
from apscheduler.schedulers.blocking import BlockingScheduler

def RecordVideo():
    print("Starting the recording")
    #record your content
    #close camera/desktop or other resource
    print('Recording Completed!')
    

scheduler = BlockingScheduler()
scheduler.add_job(RecordVideo, 'interval', minutes=1) #once per minute
#scheduler.add_job(RecordVideo, 'interval', seconds=60) for timer in seconds
#scheduler.add_job(RecordVideo, 'interval', seconds=60) for timer in hours
scheduler.start()

while True: #keeps the program running
    sleep(0.1)

只需将您的录音功能添加为作业

于 2021-09-24T13:59:09.823 回答