录制视频,自动保存,可重新录制



我正在寻找每分钟保存视频并再次录制的方法。当我运行代码时,我捕获它,直到我按下'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()

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

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)

只需将记录功能添加为作业

最新更新