Search code examples
pythonopencvvideovideo-processingvideo-recording

How to save 1 min video named date and time continuously with opencv


I want to repeatedly every minute to create a new video file named with the current date and time saved to directory path.

In the below code all it can do is a single video of 1 min length.

import cv2
import time 
from datetime import datetime
import schedule
import os
import sys
import numpy as np
from os import path



cap = cv2.VideoCapture(0)
if (cap.isOpened() == False): 
   print("Error reading video file")
fps = 24
res = '480p'
def change_res(cap, width, height):
   cap.set(3, width)
   cap.set(4, height)
STD_DIMENSIONS =  {"480p": (640, 480),"720p": (1280, 720),}
def get_dims(cap, res='720p'):
   width, height = STD_DIMENSIONS["480p"]
   if res in STD_DIMENSIONS:
       width,height = STD_DIMENSIONS[res]
   change_res(cap, width, height)
   return width, height
PREFIX = 'Cam'
EXTENSION = 'avi'
file_name_format = "{:s}-{:%d-%m-%Y %H:%M-%S}.{:s}"
date = datetime.now()
today=time.strftime('%d-%m-%Y')
file_name = file_name_format.format(PREFIX, date, EXTENSION)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter ("/home/Desktop/CAM/"+str(today)+"/"
                       +file_name, fourcc, 24.0, get_dims(cap, res))

start_time = time.time()
while (time.time() - start_time) < 60:
   ret, frame = cap.read()
   if ret == True:
       out.write(frame)
       cv2.imshow('CAM', frame)
   if cv2.waitKey(1) & 0xFF == ord('q'):
      break

cap.release()
out.release()
cv2.destroyAllWindows()

Solution

  • just wrap everything in a while True:, like so:

    import cv2
    import time
    from datetime import datetime
    import schedule
    import os
    import sys
    import numpy as np
    from os import path
    
    
    while True:
        cap = cv2.VideoCapture(0)
        if (cap.isOpened() == False):
           print("Error reading video file")
        fps = 24
        res = '480p'
        def change_res(cap, width, height):
           cap.set(3, width)
           cap.set(4, height)
        STD_DIMENSIONS =  {"480p": (640, 480),"720p": (1280, 720),}
        def get_dims(cap, res='720p'):
           width, height = STD_DIMENSIONS["480p"]
           if res in STD_DIMENSIONS:
               width,height = STD_DIMENSIONS[res]
           change_res(cap, width, height)
           return width, height
        PREFIX = 'Cam'
        EXTENSION = 'avi'
        file_name_format = "{:s}-{:%d-%m-%Y %H:%M-%S}.{:s}"
        date = datetime.now()
        today=time.strftime('%d-%m-%Y')
        file_name = file_name_format.format(PREFIX, date, EXTENSION)
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter (file_name, fourcc, 24.0, get_dims(cap, res))
    
        start_time = time.time()
        while (time.time() - start_time) < 5:
           ret, frame = cap.read()
           if ret == True:
               out.write(frame)
               cv2.imshow('CAM', frame)
           if cv2.waitKey(1) & 0xFF == ord('q'):
              break
    
        cap.release()
        out.release()
        cv2.destroyAllWindows()
    

    Or instead of while True: you can add a counter in the loop and e.g. break out of the loop when the counter reaches a certain value etc.