Refactor photo metadata workflow

This commit is contained in:
ajp_anton
2026-08-24 16:34:20 +00:00
parent 100cad1cb5
commit eb5937a629
15 changed files with 2111 additions and 1727 deletions
+1 -46
View File
@@ -10,6 +10,7 @@ from datetime import datetime, timedelta, timezone
from pathlib import Path
from tools.executables import require_executable
from tools.media_metadata import first_tag, parse_exif_datetime, round_half_up
VIDEO_TIME_TAGS = (
@@ -49,13 +50,6 @@ GPS_POSITION_TAGS = (
"Composite:GPSCoordinates",
)
DATETIME_RE = re.compile(
r"(?P<Y>\d{4})[:\-]?(?P<M>\d{2})[:\-]?(?P<D>\d{2})"
r"(?:[ T_])?"
r"(?P<h>\d{2}):?(?P<m>\d{2}):?(?P<s>\d{2})"
r"(?:[.,](?P<sub>\d+))?"
r"(?:\s*(?P<tz>Z|[+-]\d{2}:?\d{2}))?"
)
DMS_RE = re.compile(
r"(?P<deg>-?\d+(?:\.\d+)?)\D+"
r"(?P<min>\d+(?:\.\d+)?)?\D*"
@@ -385,13 +379,6 @@ def summarize_frame_timing(timestamps: list[float]) -> FrameTimingSummary:
)
def first_tag(metadata: dict, tags: tuple[str, ...]) -> object | None:
for tag in tags:
if tag in metadata:
return metadata[tag]
return None
def _gps_coordinates(metadata: dict) -> tuple[float | None, float | None]:
latitude = _parse_gps_coordinate(first_tag(metadata, GPS_LATITUDE_TAGS))
longitude = _parse_gps_coordinate(first_tag(metadata, GPS_LONGITUDE_TAGS))
@@ -449,34 +436,6 @@ def _parse_gps_coordinate(value: object) -> float | None:
return sign * (degrees + minutes / 60.0 + seconds / 3600.0)
def parse_exif_datetime(value: object, assume_utc: bool = False) -> datetime | None:
if value is None:
return None
match = DATETIME_RE.search(str(value).strip())
if not match:
return None
tzinfo = None
tz_text = match.group("tz")
if tz_text == "Z":
tzinfo = timezone.utc
elif tz_text:
tzinfo = _parse_timezone_offset(tz_text)
parsed = datetime(
int(match.group("Y")),
int(match.group("M")),
int(match.group("D")),
int(match.group("h")),
int(match.group("m")),
int(match.group("s")),
tzinfo=tzinfo,
)
if assume_utc and parsed.tzinfo is None:
parsed = parsed.replace(tzinfo=timezone.utc)
return parsed
def _parse_timezone_offset(value: str) -> timezone:
text = value.replace(":", "")
sign = 1 if text[0] == "+" else -1
@@ -495,10 +454,6 @@ def _video_timezone(metadata: dict) -> timezone | None:
return _parse_timezone_offset(text)
def round_half_up(value: float) -> int:
return int(value + 0.5)
def _to_float(value: object) -> float | None:
if value is None or value == "N/A":
return None