# Capture video for a certain time then quit and save to a folder using OpenCV 3

I am using this sample code from the OpenCV documentation and what the code is simply doing is capturing a video and when it's interrupted it saves it to a folder.

What I want to do it capture the video for a certain period of time, quit, and then save it to the folder and I really don't know how to do it.

``````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(cap.isOpened()):
if ret==True:
frame = cv2.flip(frame,0)

# write the flipped frame
out.write(frame)

cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
``````

Here are the steps:

1. Get the start time with `startTime = time.time()`
2. Get the time elapsed in seconds with `timeElapsed = startTime - time.time()`
3. Remove the decimal places with `secElapsed = int(timeElapsed)`
4. Stop the program after x seconds `while(secElapsed < 100)`

If you you need further help you will find this code useful:

``````import numpy as np
import cv2
import time

# Define the duration (in seconds) of the video capture here
capture_duration = 10

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))

start_time = time.time()
while( int(time.time() - start_time) < capture_duration ):
if ret==True:
frame = cv2.flip(frame,0)

# write the flipped frame
out.write(frame)

cv2.imshow('frame',frame)
#if cv2.waitKey(1) & 0xFF == ord('q'):
#    break
else:
break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.725802,"math_prob":0.9596811,"size":817,"snap":"2020-34-2020-40","text_gpt3_token_len":218,"char_repetition_ratio":0.11316113,"word_repetition_ratio":0.0,"special_character_ratio":0.27906978,"punctuation_ratio":0.16959064,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95703954,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-12T23:52:29Z\",\"WARC-Record-ID\":\"<urn:uuid:f2d8bf1a-b9c2-44e6-9f3d-ae92cd49bd90>\",\"Content-Length\":\"146994\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8873f49d-ff7b-403a-aa9c-dba0063df770>\",\"WARC-Concurrent-To\":\"<urn:uuid:5add4b59-40ac-4478-8513-38508746e6c1>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://raspberrypi.stackexchange.com/questions/66976/capture-video-for-a-certain-time-then-quit-and-save-to-a-folder-using-opencv-3\",\"WARC-Payload-Digest\":\"sha1:D4SGSQEUIQ3EDZRTGD25DGAPFAQ46FZW\",\"WARC-Block-Digest\":\"sha1:4XSDC2CYE2WXC7ITZT7W6WV7KIZ5HAFV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439738950.31_warc_CC-MAIN-20200812225607-20200813015607-00434.warc.gz\"}"}