Open In App
Related Articles

Save frames of live video with timestamps – Python OpenCV

Improve Article
Save Article
Like Article

Images are very important data nowadays from which we can obtain a lot of information which will helpful for analysis. Sometimes data to be processed can be in the form of video. To process this kind of data we have to read video extract frames and save them as images. Security camera videos we have seen that there is a timestamp and date at top of the frame. This kind of data also plays a crucial role in the analysis. So in this article, we are going to see how to extract frames from live video with timestamps and save them in the folder.


# Import necessary libraries
import cv2
import os
from datetime import datetime
# set path in which you want to save images
path = r'C:\Users\vishal\Documents\Bandicam'
# changing directory to given path
# i variable is to give unique name to images
i = 1
wait = 0
# Open the camera
video = cv2.VideoCapture(0)
while True:
    # Read video by read() function and it
    # will extract and  return the frame
    ret, img =
    # Put current DateTime on each frame
    font = cv2.FONT_HERSHEY_PLAIN
    cv2.putText(img, str(, (20, 40),
                font, 2, (255, 255, 255), 2, cv2.LINE_AA)
    # Display the image
    cv2.imshow('live video', img)
    # wait for user to press any key
    key = cv2.waitKey(100)
    # wait variable is to calculate waiting time
    wait = wait+100
    if key == ord('q'):
    # when it reaches to 5000 milliseconds
    # we will save that frame in given folder
    if wait == 5000:
        filename = 'Frame_'+str(i)+'.jpg'
        # Save the images in given path
        cv2.imwrite(filename, img)
        i = i+1
        wait = 0
# close the camera
# close open windows



  • In the above program, the camera will open by giving 0 as input to cv2.VideoCapture() function (if you want to capture a frame from a video that is already recorded then you can give input as a path of that video to this function ).
  • After that, we have written a loop that will run continuously until the user press any key on the keyboard.
  • In this loop first read() function will capture a frame from the opened camera.
  • Then will give the current date and time put this timestamp on that frame and display the image by using the cv2.imshow() function. Because we are doing this in the continuous loop it looks like a live video is playing.
  • There are two variables used in the program are ‘ i ‘ and ‘wait’.
    • ‘i’ variable is used for giving a unique name to images and the ‘wait’ variable for pausing saving images continuously.
  • We are simply waiting for 5 seconds then we are saving images in the specified path.
  • Finally when the user presses any key on the keyboard program will end.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Jan, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials