Record the video, save it automatically, and record it again

594 Views Asked by At

I'm looking for ways to save the video every minute and record it again. When I run my code, I capture it until I press 'q'. Is there a way to automatically save it and record it again? I use 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()
1

There are 1 best solutions below

0
On

You have not posted your code and its not obvious, how you are recording but following code can add a timer for you that offers a lot of customization e.g. recording once in specified seconds, minutes or even hours.

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)

Simply add your recording function as a job