This commit is contained in:
2026-04-14 17:00:29 +02:00
parent 460b41a8c5
commit 4aba2f02a2
10 changed files with 840 additions and 76 deletions

View File

@@ -20,11 +20,14 @@ logger = logging.getLogger(__name__)
# AcoustID API nøgle — gratis til open-source apps
# https://acoustid.org/api-key
ACOUSTID_API_KEY = "8XaBELgH" # Offentlig test-nøgle, skift til din egen ved produktion
ACOUSTID_API_KEY = "9JYq1saI1H"
ACOUSTID_API_URL = "https://api.acoustid.org/v2/lookup"
# Pause mellem API-kald (AcoustID tillader 3/sek)
API_DELAY = 0.4
# Pause mellem API-kald — rolig baggrundskørsel
API_DELAY = 5.0
# Maks sange per session — fordeles over mange opstart
MAX_PER_SESSION = 20
def find_fpcalc() -> str | None:
@@ -72,16 +75,16 @@ def fingerprint_file(path: str, fpcalc: str) -> tuple[str, int] | None:
return None
def lookup_acoustid(fingerprint: str, duration: int) -> dict | None:
def lookup_acoustid(fingerprint: str, duration: int, api_key: str = "") -> dict | None:
"""
Spørg AcoustID API om MBID for et fingerprint.
Returnerer dict med 'mbid' og 'acoustid' eller None.
"""
try:
import urllib.request, urllib.parse
import urllib.request, urllib.parse, urllib.error
params = urllib.parse.urlencode({
"client": ACOUSTID_API_KEY,
"client": api_key or ACOUSTID_API_KEY,
"fingerprint": fingerprint,
"duration": duration,
"meta": "recordings",
@@ -90,10 +93,16 @@ def lookup_acoustid(fingerprint: str, duration: int) -> dict | None:
req = urllib.request.Request(url)
req.add_header("User-Agent", "LineDancePlayer/1.0")
with urllib.request.urlopen(req, timeout=10) as resp:
data = json.loads(resp.read())
try:
with urllib.request.urlopen(req, timeout=10) as resp:
data = json.loads(resp.read())
except urllib.error.HTTPError as e:
body = e.read().decode("utf-8", errors="replace")
logger.warning(f"AcoustID API fejl {e.code}: {body[:200]}")
return None
if data.get("status") != "ok":
logger.warning(f"AcoustID status: {data.get('status')}{data.get('error', {}).get('message','')}")
return None
results = data.get("results", [])
@@ -116,10 +125,10 @@ def lookup_acoustid(fingerprint: str, duration: int) -> dict | None:
return None
def run_acoustid_scan(db_path: str, on_progress=None, stop_event: threading.Event = None):
def run_acoustid_scan(db_path: str, api_key: str = "", on_progress=None, stop_event: threading.Event = None):
"""
Gennemgå alle sange uden MBID og forsøg AcoustID fingerprinting.
Kører som baggrundsjob.
Kører i batches på MAX_PER_SESSION med BATCH_DELAY pause imellem.
"""
import sqlite3
@@ -130,73 +139,108 @@ def run_acoustid_scan(db_path: str, on_progress=None, stop_event: threading.Even
on_progress(0, 0, "fpcalc ikke installeret")
return
key = api_key or ACOUSTID_API_KEY
if not key:
logger.warning("AcoustID: ingen API-nøgle — spring over")
return
logger.info(f"AcoustID: fpcalc fundet: {fpcalc}")
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
batch_num = 0
total_found = 0
# Find sange uden MBID der har en lokal fil
rows = conn.execute("""
SELECT id, local_path, title, artist
FROM songs
WHERE (mbid IS NULL OR mbid = '')
AND file_missing = 0
AND local_path IS NOT NULL AND local_path != ''
ORDER BY RANDOM()
LIMIT 500
""").fetchall()
total = len(rows)
done = 0
found = 0
logger.info(f"AcoustID: {total} sange uden MBID")
for row in rows:
while True:
if stop_event and stop_event.is_set():
logger.info("AcoustID: stoppet af bruger")
break
path = row["local_path"]
if not Path(path).exists():
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
rows = conn.execute("""
SELECT id, local_path, title, artist
FROM songs
WHERE (mbid IS NULL OR mbid = '')
AND file_missing = 0
AND local_path IS NOT NULL AND local_path != ''
ORDER BY RANDOM()
LIMIT ?
""", (MAX_PER_SESSION,)).fetchall()
conn.close()
if not rows:
logger.info(f"AcoustID: alle sange har nu MBID — stopper")
if on_progress:
on_progress(1, 1, f"Færdig — {total_found} sange fik MBID i alt")
break
batch_num += 1
total = len(rows)
done = 0
found = 0
logger.info(f"AcoustID: batch {batch_num}{total} sange")
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
for row in rows:
if stop_event and stop_event.is_set():
conn.close()
return
path = row["local_path"]
if not Path(path).exists():
done += 1
continue
if on_progress:
on_progress(done, total, row["title"] or path)
fp_result = fingerprint_file(path, fpcalc)
if not fp_result:
done += 1
time.sleep(0.1)
continue
fingerprint, duration = fp_result
result = lookup_acoustid(fingerprint, duration, key)
time.sleep(API_DELAY)
if result:
mbid = result.get("mbid", "")
acoustid = result.get("acoustid", "")
conn.execute(
"UPDATE songs SET mbid=?, acoustid=? WHERE id=?",
(mbid or None, acoustid or None, row["id"])
)
conn.commit()
found += 1
total_found += 1
logger.info(
f"AcoustID: {row['title']} → MBID={mbid[:8] if mbid else ''}"
)
if mbid and path:
try:
from local.tag_reader import write_mbid_to_file
write_mbid_to_file(path, mbid)
except Exception as e:
logger.warning(f"AcoustID: kunne ikke skrive MBID til fil: {e}")
done += 1
continue
conn.close()
logger.info(f"AcoustID: batch {batch_num} færdig — {found}/{total} fik MBID")
if on_progress:
on_progress(done, total, row["title"] or path)
on_progress(done, total, f"Batch {batch_num}: {found}/{total} fik MBID — venter 60 sek")
# Fingerprint filen
fp_result = fingerprint_file(path, fpcalc)
if not fp_result:
done += 1
time.sleep(0.05)
continue
fingerprint, duration = fp_result
# Spørg AcoustID
result = lookup_acoustid(fingerprint, duration)
time.sleep(API_DELAY) # Respektér rate limit
if result:
mbid = result.get("mbid", "")
acoustid = result.get("acoustid", "")
conn.execute(
"UPDATE songs SET mbid=?, acoustid=? WHERE id=?",
(mbid or None, acoustid or None, row["id"])
)
conn.commit()
found += 1
logger.info(
f"AcoustID: {row['title']} → MBID={mbid[:8] if mbid else ''}"
)
done += 1
conn.close()
logger.info(f"AcoustID: færdig — {found}/{total} sange fik MBID")
if on_progress:
on_progress(total, total, f"Færdig — {found} sange fik MBID")
# Vent et minut inden næste batch
for _ in range(60):
if stop_event and stop_event.is_set():
return
time.sleep(1)
class AcoustIDWorker:
@@ -211,7 +255,7 @@ class AcoustIDWorker:
self._stop_event = threading.Event()
self._running = False
def start(self, on_progress=None):
def start(self, api_key: str = "", on_progress=None):
"""Start baggrunds-fingerprinting. Gør ingenting hvis allerede kører."""
if self._running:
return
@@ -222,6 +266,7 @@ class AcoustIDWorker:
try:
run_acoustid_scan(
self._db_path,
api_key=api_key,
on_progress=on_progress,
stop_event=self._stop_event,
)

View File

@@ -256,6 +256,11 @@ MIGRATIONS: dict[int, list[str]] = {
"""ALTER TABLE playlists ADD COLUMN is_linked INTEGER NOT NULL DEFAULT 0""",
"""ALTER TABLE playlists ADD COLUMN server_permission TEXT NOT NULL DEFAULT 'view'""",
],
8: [
# MusicBrainz og AcoustID matching
"""ALTER TABLE songs ADD COLUMN mbid TEXT""",
"""ALTER TABLE songs ADD COLUMN acoustid TEXT""",
],
}

View File

@@ -427,3 +427,66 @@ def analyze_and_save_bpm(path: str | Path, song_id: str) -> float | None:
except Exception as e:
print(f"BPM gem fejl: {e}")
return bpm
# ── MBID skrivning ────────────────────────────────────────────────────────────
def write_mbid_to_file(path: str | Path, mbid: str) -> bool:
"""Skriv MusicBrainz Recording ID til filens tags."""
path = Path(path)
ext = path.suffix.lower()
try:
if ext == ".mp3":
return _write_mbid_mp3(path, mbid)
elif ext in (".flac", ".ogg", ".opus"):
return _write_mbid_vorbis(path, mbid)
elif ext in (".m4a", ".aac"):
return _write_mbid_m4a(path, mbid)
return False
except Exception as e:
logger.warning(f"MBID skrivefejl {path}: {e}")
return False
def _write_mbid_mp3(path: Path, mbid: str) -> bool:
try:
from mutagen.id3 import ID3, TXXX, UFID
tags = ID3(str(path))
# Skriv som TXXX:MusicBrainz Recording Id
tags.delall("TXXX:MusicBrainz Recording Id")
tags.add(TXXX(encoding=3, desc="MusicBrainz Recording Id", text=mbid))
# Skriv også som UFID
tags.delall("UFID:http://musicbrainz.org")
tags.add(UFID(owner="http://musicbrainz.org", data=mbid.encode("utf-8")))
tags.save(str(path))
return True
except Exception as e:
logger.warning(f"MBID MP3 skrivefejl {path}: {e}")
return False
def _write_mbid_vorbis(path: Path, mbid: str) -> bool:
try:
from mutagen import File as MutagenFile
audio = MutagenFile(str(path), easy=False)
if audio is None or audio.tags is None:
return False
audio.tags["musicbrainz_trackid"] = mbid
audio.save()
return True
except Exception as e:
logger.warning(f"MBID Vorbis skrivefejl {path}: {e}")
return False
def _write_mbid_m4a(path: Path, mbid: str) -> bool:
try:
from mutagen.mp4 import MP4, MP4FreeForm
audio = MP4(str(path))
key = "----:com.apple.iTunes:MusicBrainz Recording Id"
audio[key] = [MP4FreeForm(mbid.encode("utf-8"))]
audio.save()
return True
except Exception as e:
logger.warning(f"MBID M4A skrivefejl {path}: {e}")
return False