Files
timestamp_extraction/extract_ts.py
2024-10-30 22:37:32 +01:00

62 lines
1.5 KiB
Python

import cv2
import pytesseract
import re
# Set Tesseract executable path
pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'
def extract_frame_and_timestamp(video_path):
"""
Extract a frame from the given video and identify its timestamp.
Args:
video_path (str): Path to the video file.
Returns:
None if the video cannot be opened or no timestamp is found.
Otherwise, prints the extracted timestamp.
"""
# Open the video capture
cap = cv2.VideoCapture(video_path)
# Check if the video was opened correctly
if not cap.isOpened():
print("Error opening the video")
return
# Iterate over frames until one with a timestamp is found
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
text = pytesseract.image_to_string(frame)
timestamp = _extract_timestamp(text)
if timestamp:
print(timestamp.group())
break
# Release the video capture
cap.release()
def _extract_timestamp(text):
"""
Extract a timestamp from the given text.
Args:
text (str): Text to search for timestamps.
Returns:
The extracted timestamp as a string, or None if no timestamp is found.
"""
pattern = r'\d{2}-\d{2}-\d{4}\s\d{2}:\d{2}:\d{2}'
return re.search(pattern, text)
# Execute the program
video_path = "/home/alex/Scaricati/2024_08_11_14_55_26.MP4"
extract_frame_and_timestamp(video_path)