# import the opencv library import cv2 # define a video capture object camera = cv2.VideoCapture(0) camera.set(cv2.CAP_PROP_FPS, 20.0) if not camera.isOpened(): print("Cannot open camera") exit() queue = [(False, None)] * 100 # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'mp4v') #out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (int(camera.get(3)),int(camera.get(4)))) idx = 0 offset = 0 while(True): # Capture the video frame # by frame ret, frame = camera.read() frame = cv2.flip(frame, 1) #recod the frame #out.write(frame) queue[idx] = ret, frame showo, fr = queue[(idx - offset) % len(queue)] if showo: cv2.imshow('vid', fr) # the 'q' button is set as the quitting button you may use any desired button of your choice key = cv2.waitKey(1) & 0xFF if key == ord('q'): break if key == ord('a'): offset += 1 if key == ord('d'): offset -= 1 idx = (idx + 1) % len(queue) # After the loop release the cap object camera.release() #out.release() # Destroy all the windows cv2.destroyAllWindows()