Python OpenCV split recording into multiple files

Viewed 2217

I want to use OpenCV and a webcam to continuously record video from the webcam for 15 minutes and then start the process again so that I have got 15 minute chunks of video. I already wrote a script, but am experiencing unexpected behaviour. Recording works fine for a while and then the program would only create files that are 5kb in size that are not playable.

Would anybody know why that's happening?

This is the code:

import numpy as np
import cv2
import time


cap = cv2.VideoCapture(0)

#Record the current time
current_time = time.time()

#Specify the path and name of the video file as well as the encoding, fps and resolution
out = cv2.VideoWriter('/mnt/NAS326/cctv/' + str(time.strftime('%d %m %Y - %H %M %S' )) + '.avi', cv2.cv.CV_FOURCC('X','V','I','D'), 15, (640,480))




while(True):



    # Capture frame-by-frame
    ret, frame = cap.read() 
    out.write(frame)

    #If the current time is greater than 'current_time' + seconds specified then release the video, record the time again and start a new recording
    if time.time() >= current_time + 900:
        out.release()
        current_time = time.time()
        out = cv2.VideoWriter('/mnt/NAS326/cctv/' + str(time.strftime('%d %m %Y - %H %M %S' )) + '.avi', cv2.cv.CV_FOURCC('X','V','I','D'), 15, (640,480))



out.release()

cap.release()




cv2.destroyAllWindows()
1 Answers
Related