Python date time: given input file like 1
00:00:00,000 --> 00:00:01,000
11:06:05

2
00:00:01,000 --> 00:00:02,000
11:06:06

3
00:00:02,000 --> 00:00:03,000
11:06:07

4
00:00:03,000 --> 00:00:04,000
11:06:08

5
00:00:04,000 --> 00:00:05,000
11:06:09  the followinf fuction fails to find valid time  why  def get_video_start_time(srt_path):
    with open(srt_path, 'r', encoding='utf-8') as file:
        for line in file:
            line = line.strip()
            if line and not line.startswith(('0', '1', '2')) and ':' in line:
                try:
                    # Attempt to parse the line as a time
                    return datetime.strptime(line, "%H:%M:%S")
                except ValueError:
                    try:
                        return datetime.strptime(line, "%H:%M:%S.%f")
                    except ValueError:
                        continue
    raise ValueError(" def get_video_start_time(srt_path) No valid time found in subtitle file.")


ChatGPT said:
The issue with your function is this line:

python
Copy
Edit
if line and not line.startswith(('0', '1', '2')) and ':' in line:
This line excludes any line that starts with '0', '1', or '2', which includes valid time strings like 11:06:05, since 11 starts with '1'. That’s why it never gets to the strptime part and ends up raising the exception.

✅ Fix
Remove or adjust the not line.startswith(('0', '1', '2')) condition so it doesn't wrongly exclude valid timestamps. Here's a corrected version:

python
Copy
Edit
from datetime import datetime

def get_video_start_time(srt_path):
    with open(srt_path, 'r', encoding='utf-8') as file:
        for line in file:
            line = line.strip()
            if line and ':' in line:  # Only check for colon to suggest time format
                try:
                    return datetime.strptime(line, "%H:%M:%S")
                except ValueError:
                    try:
                        return datetime.strptime(line, "%H:%M:%S.%f")
                    except ValueError:
                        continue
    raise ValueError("No valid time found in subtitle file.")
This version will correctly find and parse lines like 11:06:05.

Optional: Stricter Filter
If you want to be stricter and only accept time-looking lines that are not part of the SRT format (like time ranges), you can use a regex:

python
Copy
Edit
import re
from datetime import datetime

def get_video_start_time(srt_path):
    time_pattern = re.compile(r'^\d{1,2}:\d{2}:\d{2}(?:\.\d+)?$')  # Matches HH:MM:SS or HH:MM:SS.mmm
    with open(srt_path, 'r', encoding='utf-8') as file:
        for line in file:
            line = line.strip()
            if time_pattern.match(line):
                try:
                    return datetime.strptime(line, "%H:%M:%S")
                except ValueError:
                    return datetime.strptime(line, "%H:%M:%S.%f")
    raise ValueError("No valid time found in subtitle file.")
Let me know if you also want to skip subtitle index numbers or timestamps (00:00:00,000 --> 00:00:01,000).









Tools



ChatGPT can make mistakes. Check important info. See Cookie 