from __future__ import annotations from pathlib import Path try: from tinytag import TinyTag except ImportError: # optional until metadata phase is invoked TinyTag = None def read_metadata(path: Path) -> dict: if TinyTag is None: return {} tag = TinyTag.get(str(path), image=False) return { "duration": tag.duration, "artist": tag.artist, "title": tag.title, "album": tag.album, "format": path.suffix.lower().lstrip("."), "bitrate": int(tag.bitrate) if tag.bitrate else None, "sample_rate": int(tag.samplerate) if tag.samplerate else None, "bit_depth": int(tag.bitdepth) if tag.bitdepth else None, }