Rimsha Chauhdary
Rimsha Chauhdary

Reputation: 21

real-time video saving after every x seconds in python

i am interested to save real-time video after every 60 seconds in python, if anyone can help me how i can update that,thanks

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(True):
    ret, frame = cap.read()
    out.write(frame)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
out.release()
cv2.destroyAllWindows()

Upvotes: 0

Views: 705

Answers (1)

Rimsha Chauhdary
Rimsha Chauhdary

Reputation: 21

this code is doing what i was asking

import numpy as np
import cv2
import time
import datetime
cap = cv2.VideoCapture(0) #For Laptop Camera (Change to 1,2,3 if other cams attached) 
while(True):
    ret, frame = cap.read()
    date_string = datetime.datetime.now().strftime("%Y-%m-%d  %I.%M.%S%p   %A")
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(date_string+'.avi',fourcc, 30.0, (640,480))
    out.write(frame)
    cv2.imshow('frame',frame)
    time.sleep(10) # Change 10 to x to save video file after 'x' sec
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break  

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

Upvotes: 2

Related Questions