import cv2
from time import time

fname = "hands_stop2.mp4"
camera_num = 0
timer = 7
frames_num = 1000

stream = cv2.VideoCapture(camera_num)
frame_width = int(stream.get(3))
frame_height = int(stream.get(4))
writer = cv2.VideoWriter(fname,cv2.VideoWriter_fourcc('M','J','P','G'), 15, (frame_width, frame_height))

RUN = True
WRITE = False
start_time = time()
timer += 1.3
frames_counter = 0

while RUN:
    ret, img = stream.read()
    if ret:
        img_to_show = img.copy()
        if not WRITE:
            t = time() - start_time
            cv2.putText(img_to_show, str(int(timer - t)), (25, 50), cv2.FONT_HERSHEY_COMPLEX, 1.3, (0, 0, 255), 2)
            if timer - t <= 0:
                WRITE = True
        else:
            cv2.circle(img_to_show, (30, 35), 15, (20, 255, 40), 25)
            writer.write(img)
            frames_counter += 1
            if frames_counter >= frames_num:
                RUN = False
        cv2.imshow("img", img_to_show)
        if cv2.waitKey(10) & 0xFF == 27:
            RUN = False

cv2.destroyAllWindows()
stream.release()