From a74337f8fea99ba1b7e62c0811b9c982839d6841 Mon Sep 17 00:00:00 2001 From: Luca Di Leo Date: Tue, 10 Jan 2023 12:19:50 +0100 Subject: [PATCH] Fixed srt parser --- opendm/video/srtparser.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/opendm/video/srtparser.py b/opendm/video/srtparser.py index 28e6f0ae..a69a11d6 100644 --- a/opendm/video/srtparser.py +++ b/opendm/video/srtparser.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timedelta import re class SrtFileParser: def __init__(self, filename, utc_offset): @@ -10,11 +10,15 @@ class SrtFileParser: if not self.data: self.parse() + # check min and max + if timestamp < self.min or timestamp > self.max: + return None + for entry in self.data: if entry["timestamp"] <= timestamp: return entry - return None + return self.data[len(self.data) - 1] def parse(self): @@ -26,6 +30,9 @@ class SrtFileParser: # 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] + self.min = datetime.max + self.max = datetime.min + with open(self.filename, 'r') as f: for line in f: @@ -48,6 +55,10 @@ class SrtFileParser: "longitude": longitude, "altitude": altitude }) + self.min = min(self.min, timestamp) + # account for the difftime milliseconds to get the actual max + self.max = max(self.max, timestamp + timedelta(milliseconds=difftime)) + srtcnt = None difftime = None timestamp = None @@ -112,13 +123,20 @@ class SrtFileParser: match = re.search("latitude: (\d+.\d+)", line) if match: latitude = float(match.group(1)) + latitude = latitude if latitude != 0 else None match = re.search("longitude: (\d+.\d+)", line) if match: longitude = float(match.group(1)) + longitude = longitude if longitude != 0 else None match = re.search("altitude: (\d+.\d+)", line) if match: altitude = float(match.group(1)) + altitude = altitude if altitude != 0 else None + self.data.reverse() + + self.max = self.max.replace(microsecond=0) + self.min = self.min.replace(microsecond=0)