0

I have a webcam that captures video stream. After 30 seconds I want to remove 1 sec of the video from the start and keep capturing the video stream and so on. In short, I only want to save the latest 30 seconds of the live video.

OpenCV does not provide video processing

ffmpeg trims the video but creates a new output file, I don't want to keep copies.

#Create a video write before entering the loop
#Create a video write before entering the loop
video_writer = cv2.VideoWriter(
    video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4)))
)

#video_file is the file being saved

start = time.time()
i=0
seconds='1'
while cap.isOpened():


    ret, frame = cap.read()
    if ret == True:
        cv2.imshow("frame", frame)
        if time.time() - start > 10:
            print('video > 10 sec')
        
            subprocess.call(['ffmpeg', '-i', video_file, '-ss', seconds, 'output.avi'])
            break

    # Write the frame to the current video writer
    video_writer.write(frame)
    if i%24 == 0:
        cv2.imwrite('image'+str(i)+'.jpg',frame)
    i+=1
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break
else:
    break
cap.release()
cv2.destroyAllWindows()

What I am looking for is how we can trip a live video and keep saving future frames so that the video don't exceed 30 seconds and keeps the latest frames.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.