2023-01-13 11:08:16 +00:00
|
|
|
from datetime import datetime
|
2023-01-09 10:45:22 +00:00
|
|
|
import re
|
|
|
|
class SrtFileParser:
|
2023-01-13 11:08:16 +00:00
|
|
|
def __init__(self, filename):
|
2023-01-09 10:45:22 +00:00
|
|
|
self.filename = filename
|
|
|
|
self.data = []
|
|
|
|
|
2023-01-13 11:08:16 +00:00
|
|
|
def get_entry(self, timestamp: datetime):
|
2023-01-09 10:45:22 +00:00
|
|
|
if not self.data:
|
|
|
|
self.parse()
|
|
|
|
|
2023-01-10 11:19:50 +00:00
|
|
|
# check min and max
|
2023-01-13 11:08:16 +00:00
|
|
|
if timestamp < self.data[0]["start"] or timestamp > self.data[len(self.data) - 1]["end"]:
|
2023-01-10 11:19:50 +00:00
|
|
|
return None
|
|
|
|
|
2023-01-09 10:45:22 +00:00
|
|
|
for entry in self.data:
|
2023-01-13 11:08:16 +00:00
|
|
|
if entry["start"] <= timestamp and entry["end"] >= timestamp:
|
2023-01-09 10:45:22 +00:00
|
|
|
return entry
|
|
|
|
|
2023-01-13 11:08:16 +00:00
|
|
|
return None
|
2023-01-09 10:45:22 +00:00
|
|
|
|
|
|
|
def parse(self):
|
|
|
|
|
|
|
|
# The SRT file is formatted this way:
|
|
|
|
|
|
|
|
# 1
|
|
|
|
# 00:00:00,000 --> 00:00:00,016
|
|
|
|
# <font size="36">SrtCnt : 1, DiffTime : 16ms
|
|
|
|
# 2023-01-06 18:56:48,380,821
|
|
|
|
# [iso : 3200] [shutter : 1/60.0] [fnum : 280] [ev : 0] [ct : 3925] [color_md : default] [focal_len : 240] [latitude: 0.000000] [longitude: 0.000000] [altitude: 0.000000] </font>
|
|
|
|
|
|
|
|
with open(self.filename, 'r') as f:
|
|
|
|
|
2023-01-13 11:08:16 +00:00
|
|
|
srtcnt = None
|
|
|
|
difftime = None
|
|
|
|
timestamp = None
|
|
|
|
iso = None
|
|
|
|
shutter = None
|
|
|
|
fnum = None
|
|
|
|
ev = None
|
|
|
|
ct = None
|
|
|
|
color_md = None
|
|
|
|
focal_len = None
|
|
|
|
latitude = None
|
|
|
|
longitude = None
|
|
|
|
altitude = None
|
|
|
|
start = None
|
|
|
|
end = None
|
|
|
|
|
2023-01-09 10:45:22 +00:00
|
|
|
for line in f:
|
|
|
|
|
|
|
|
# Check if line is empty
|
|
|
|
if not line.strip():
|
|
|
|
if srtcnt is not None:
|
|
|
|
self.data.append({
|
2023-01-13 11:08:16 +00:00
|
|
|
"start": start,
|
|
|
|
"end": end,
|
2023-01-09 10:45:22 +00:00
|
|
|
"srtcnt": srtcnt,
|
|
|
|
"difftime": difftime,
|
|
|
|
"timestamp": timestamp,
|
|
|
|
"iso": iso,
|
|
|
|
"shutter": shutter,
|
|
|
|
"fnum": fnum,
|
|
|
|
"ev": ev,
|
|
|
|
"ct": ct,
|
|
|
|
"color_md": color_md,
|
|
|
|
"focal_len": focal_len,
|
|
|
|
"latitude": latitude,
|
|
|
|
"longitude": longitude,
|
|
|
|
"altitude": altitude
|
|
|
|
})
|
2023-01-10 11:19:50 +00:00
|
|
|
|
2023-01-09 10:45:22 +00:00
|
|
|
srtcnt = None
|
|
|
|
difftime = None
|
|
|
|
timestamp = None
|
|
|
|
iso = None
|
|
|
|
shutter = None
|
|
|
|
fnum = None
|
|
|
|
ev = None
|
|
|
|
ct = None
|
|
|
|
color_md = None
|
|
|
|
focal_len = None
|
|
|
|
latitude = None
|
|
|
|
longitude = None
|
|
|
|
altitude = None
|
2023-01-13 11:08:16 +00:00
|
|
|
start = None
|
|
|
|
end = None
|
|
|
|
|
2023-01-09 10:45:22 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Remove the html font tag
|
|
|
|
line = re.sub('<[^<]+?>', '', line)
|
|
|
|
|
2023-01-13 11:08:16 +00:00
|
|
|
# Search this "00:00:00,000 --> 00:00:00,016"
|
|
|
|
match = re.search("(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})", line)
|
|
|
|
if match:
|
|
|
|
start = datetime.strptime(match.group(1), "%H:%M:%S,%f")
|
|
|
|
end = datetime.strptime(match.group(2), "%H:%M:%S,%f")
|
|
|
|
|
|
|
|
|
2023-01-09 10:45:22 +00:00
|
|
|
match = re.search("SrtCnt : (\d+)", line)
|
|
|
|
if match:
|
|
|
|
srtcnt = int(match.group(1))
|
|
|
|
|
|
|
|
match = re.search("DiffTime : (\d+)ms", line)
|
|
|
|
if match:
|
|
|
|
difftime = int(match.group(1))
|
|
|
|
|
|
|
|
match = re.search("(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3})", line)
|
|
|
|
if match:
|
|
|
|
timestamp = match.group(1)
|
|
|
|
timestamp = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S,%f")
|
|
|
|
|
|
|
|
match = re.search("iso : (\d+)", line)
|
|
|
|
if match:
|
|
|
|
iso = int(match.group(1))
|
|
|
|
|
|
|
|
match = re.search("shutter : (\d+/\d+.\d+)", line)
|
|
|
|
if match:
|
|
|
|
shutter = match.group(1)
|
|
|
|
|
|
|
|
match = re.search("fnum : (\d+)", line)
|
|
|
|
if match:
|
|
|
|
fnum = int(match.group(1))
|
|
|
|
|
|
|
|
match = re.search("ev : (\d+)", line)
|
|
|
|
if match:
|
|
|
|
ev = int(match.group(1))
|
|
|
|
|
|
|
|
match = re.search("ct : (\d+)", line)
|
|
|
|
if match:
|
|
|
|
ct = int(match.group(1))
|
|
|
|
|
|
|
|
match = re.search("color_md : (\w+)", line)
|
|
|
|
if match:
|
|
|
|
color_md = match.group(1)
|
|
|
|
|
|
|
|
match = re.search("focal_len : (\d+)", line)
|
|
|
|
if match:
|
|
|
|
focal_len = int(match.group(1))
|
|
|
|
|
|
|
|
match = re.search("latitude: (\d+.\d+)", line)
|
|
|
|
if match:
|
|
|
|
latitude = float(match.group(1))
|
2023-01-10 11:19:50 +00:00
|
|
|
latitude = latitude if latitude != 0 else None
|
2023-01-09 10:45:22 +00:00
|
|
|
|
|
|
|
match = re.search("longitude: (\d+.\d+)", line)
|
|
|
|
if match:
|
|
|
|
longitude = float(match.group(1))
|
2023-01-10 11:19:50 +00:00
|
|
|
longitude = longitude if longitude != 0 else None
|
2023-01-09 10:45:22 +00:00
|
|
|
|
|
|
|
match = re.search("altitude: (\d+.\d+)", line)
|
|
|
|
if match:
|
|
|
|
altitude = float(match.group(1))
|
2023-01-10 11:19:50 +00:00
|
|
|
altitude = altitude if altitude != 0 else None
|