This Python code provides an easy way to add real-time date and time to the videos in overlayed form. It works with both video files and webcam , making it flexible for various needs. The code uses OpenCV’s cv2 module to manage video playback and the datetime module to fetch the current date and time from the system. These two modules are combined to create an application which is practical and easy to implement.When the code runs, it asks you to choose between choosing a video file from your system or use webcam. If you select a file, it prompts you to mention the file path. If you choose the webcam, it automatically sets it up. Once the video source is ready, the script ensures everything is functioning properly.
For each video frame, the script retrieves the current date and time using the datetime.now() function. It then overlays this information onto the video frame using OpenCV’s cv2.putText(). You can customize the font, size, position, and color of the text as per your convenience.The final output is a video with a visible timestamp that keeps updating in real-time. This feature is useful for many applications, such as tutorials, monitoring systems, or personal projects. The code snippet is simple to modify, allowing you to adapt it for different purposes.
Display date and time in videos using OpenCV in Python, a practical approach
First is to Import the OpenCV and datetime library,cv2 is used for computing tasks like video processing and datetime module helps retrieve and format the current date and time.
import cv2 import datetime
In the next step a function accepts user input to decide the video source. Entering 1 prompts the user to input a video file path from system, while entering 2 selects the webcam. If invalid choices are made then error message is displayed.
def user_input(): user_choice = input("Enter 1 to load the video from your system or Enter 2 to use the webcam directly: ") if user_choice == "1": video_location = input("Enter the path of your video file: ") return video_location elif user_choice == "2": print("Using webcam") return 0 else: print("Invalid choice please enter either 1 or 2") return None
The adding_date_time() function initializes the video capture based on user input. It checks if the video source was successfully opened and handles the error.
def adding_date_time(): video_file = user_input() if video_file is None: print("You didn’t select a proper video source....") return capture = cv2.VideoCapture(video_file) if not capture.isOpened(): print(f"Failed to open the video source: {video_file}") return
In the next step we retrieve the frame rate of the video source. The delay between frames is calculated in milliseconds for proper video playback.A loop fetches the current date and time using datetime.now() and formats it into a readable string.The cv2.putText method overlays the formatted date and time onto the video frame. The cv22.imshow() function displays the updated frame in a window.
fps = capture.get(cv2.CAP_PROP_FPS) if fps == 0: fps = 30 delay = int(1000 / fps) while True: ret, frame = capture.read() if not ret: print("Failed to retrieve all frames.") break now = datetime.datetime.now() date_time = now.strftime('%Y-%m-%d %H:%M:%S') font = cv2.FONT_HERSHEY_SIMPLEX position = (10, 50) font_scale = 1 color = (255, 255, 255) thickness = 2 cv2.putText(frame, date_time, position, font, font_scale, color, thickness) cv2.imshow('Video with Date and Time', frame)
In the final step ,this code line listens for the ‘q’ key to exit the loop, stopping the video playback.
if cv2.waitKey(delay) & 0xFF == ord('q'): break
After running the code this will be output:-
Enter 1 to load the video from your system or Enter 2 to use the webcam directly: 1 Enter the path of your video file: C:\Videos\sample.mp4 (As per the choice video will be opened in a mention window size along with overlayed date and time at the top left corner of the video) Press 'q' to quit.
If an invalid input is provided, the user is prompted again
Invalid choice please enter either 1 or 2
If the video source fails to open:
Failed to open the video source: (video_path)