34 lines
1008 B
Python
34 lines
1008 B
Python
from __future__ import annotations
|
|
|
|
import re
|
|
from pathlib import Path
|
|
|
|
from .constants import MUSIC_EXTENSIONS, VERSION_MARKERS
|
|
|
|
try:
|
|
from tinytag import TinyTag
|
|
except ImportError:
|
|
TinyTag = None
|
|
|
|
|
|
def read_metadata(path: Path) -> dict:
|
|
data = {"format": path.suffix.lower().lstrip(".")}
|
|
if TinyTag is None:
|
|
return data
|
|
try:
|
|
tag = TinyTag.get(str(path), image=False)
|
|
except Exception:
|
|
return data
|
|
data.update({
|
|
"duration": tag.duration,
|
|
"artist": tag.artist,
|
|
"title": tag.title,
|
|
"album": tag.album,
|
|
"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,
|
|
})
|
|
text = " ".join(str(data.get(k) or "") for k in ("title", "album", "file_name"))
|
|
data["version_flags"] = [m for m in VERSION_MARKERS if re.search(rf"(?<!\w){re.escape(m)}(?!\w)", text, re.I)]
|
|
return data
|