Display date and time in videos using OpenCV Python

OpenCV is an Open Source Computer Vision Library in Python that provides numerous functions for image and video processing operations. When building live streaming or video processing applications, you often need to display timestamps on video frames. This can be achieved using OpenCV's cv2.putText() function combined with Python's datetime module.

The cv2.putText() Function

OpenCV provides the cv2.putText() method to write text strings on image or video frames ?

Syntax

cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

Parameters

  • img: Input image/frame

  • text: Text string to be drawn

  • org: Tuple of coordinates (x, y) for text position

  • fontFace: Font type (FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, etc.)

  • fontScale: Font scale factor for text size

  • color: Text color in BGR format

  • thickness: Line thickness for text (default: 1)

  • lineType: Line type (LINE_4, LINE_8, LINE_AA, etc.)

Approach

The general steps to add date and time to videos are ?

  • Open camera or video file using cv2.VideoCapture()

  • Read each frame using video.read()

  • Get current date and time using datetime.datetime.now()

  • Write timestamp on frame using cv2.putText()

  • Display frame using cv2.imshow()

  • Release resources with video.release() and cv2.destroyAllWindows()

Method 1: Using a Video File

You can add timestamps to existing video files by specifying the file path ?

import cv2
import datetime

# Open the video file
video = cv2.VideoCapture('sample_video.mp4')

while video.isOpened():
    ret, frame = video.read()
    
    if ret:
        # Get current date and time
        current_time = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
        
        # Add timestamp to frame
        cv2.putText(frame, current_time, (10, 30), 
                   cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        
        # Display the frame
        cv2.imshow('Video with Timestamp', frame)
        
        # Exit on 'q' key press
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break
    else:
        break

video.release()
cv2.destroyAllWindows()

Method 2: Using Live Camera Feed

For live video streams, use camera index 0 to capture from the default camera ?

import cv2
import datetime

# Open camera (0 for default camera)
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    
    if ret:
        # Get current date and time
        current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        
        # Add timestamp to frame
        cv2.putText(frame, current_time, (10, 30), 
                   cv2.FONT_HERSHEY_DUPLEX, 0.8, (255, 255, 255), 2)
        
        # Display the frame
        cv2.imshow('Live Feed with Timestamp', frame)
        
        # Exit on 'q' key press
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        print("Failed to capture frame")
        break

cap.release()
cv2.destroyAllWindows()

Customizing the Timestamp Display

You can customize the appearance and format of the timestamp ?

import cv2
import datetime

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    
    if ret:
        # Custom date format
        date_str = datetime.datetime.now().strftime("%B %d, %Y")
        time_str = datetime.datetime.now().strftime("%I:%M:%S %p")
        
        # Add date and time at different positions
        cv2.putText(frame, date_str, (10, 30), 
                   cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 0, 255), 2)
        cv2.putText(frame, time_str, (10, 60), 
                   cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 255), 2)
        
        cv2.imshow('Custom Timestamp', frame)
        
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

cap.release()
cv2.destroyAllWindows()

Conclusion

Adding timestamps to videos using OpenCV is straightforward with cv2.putText() and the datetime module. You can customize the format, position, and styling of timestamps for both video files and live camera feeds to suit your application needs.

Updated on: 2026-03-27T06:52:59+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements