Display date and time in videos using OpenCV - Python
In video processing, adding a timestamp directly onto video frames is a useful feature for applications like surveillance, monitoring and logging. Using OpenCV in Python, we can capture each video frame and overlay the current date and time fetched from the datetime module, making the video feed more informative and time-aware.
Prerequisites:
Approach
To display the current date and time on video frames:
- Capture the video using cv2.VideoCapture().
- Read video frames in a loop.
- Fetch the current system time using datetime.datetime.now().
- Use cv2.putText() to overlay the timestamp on each frame.
- Display the updated frames until the video ends or the user quits.
Python Implementation
import cv2
import datetime
vid = cv2.VideoCapture('sample.mp4')
while vid.isOpened():
ret, frame = vid.read()
if not ret:
break
font = cv2.FONT_HERSHEY_SCRIPT_COMPLEX
dt = str(datetime.datetime.now())
frame = cv2.putText(frame, dt,
(10, 100), # Position (x, y)
font, 1, # Font and scale
(210, 155, 155), # Color (B, G, R)
2, # Thickness
cv2.LINE_8) # Line type
cv2.imshow('Video with Date & Time', frame)
key = cv2.waitKey(1)
if key == ord('q') or key == 27: # Quit on 'q' or ESC
break
vid.release()
cv2.destroyAllWindows()
Output

Explanation:
- Cv2.VideoCapture('sample.mp4') opens the video file and creates a capture object.
- vid.read() reads frames one by one; ret indicates success, and frame contains the image.
- datetime.datetime.now() gets the current system date and time.
- cv2.putText(frame, dt, ...) overlays the date & time text onto each video frame.
- cv2.imshow('Video with Date & Time', frame) displays the video with the timestamp in a window.
- cv2.waitKey(1) waits for a key press and pressing q or ESC exits, then vid.release() frees the video and cv2.destroyAllWindows() closes windows.