Make fpcalc missing page show up on startup instead of when viewing metadata, add metadata to file automatically with the downloader, fix lyrics caching

This commit is contained in:
csd4ni3l
2025-07-17 14:47:56 +02:00
parent 659f4e4462
commit 90c7859622
8 changed files with 115 additions and 81 deletions

View File

@@ -1,9 +1,7 @@
import os, platform, tarfile, acoustid, urllib.request, shutil, gzip, glob, logging, sys, io
import os, platform, acoustid, logging
from utils.constants import ACOUSTID_API_KEY
from zipfile import ZipFile
def get_fpcalc_name():
system = platform.system()
if system == "Linux" or system == "Darwin":
@@ -14,45 +12,7 @@ def get_fpcalc_name():
def get_fpcalc_path():
return os.path.join(os.getcwd(), "bin", get_fpcalc_name())
def download_fpcalc():
system = platform.system()
architecture = platform.machine()
if system == "Linux":
url = "https://github.com/acoustid/chromaprint/releases/download/v1.5.1/chromaprint-fpcalc-1.5.1-linux-x86_64.tar.gz"
elif system == "Darwin":
if architecture.lower() == "x86_64" or architecture.lower() == "amd64":
url = "https://github.com/acoustid/chromaprint/releases/download/v1.5.1/chromaprint-fpcalc-1.5.1-macos-x86_64.tar.gz"
else:
url = "https://github.com/acoustid/chromaprint/releases/download/v1.5.1/chromaprint-fpcalc-1.5.1-macos-arm64.tar.gz"
elif system == "Windows":
url = "https://github.com/acoustid/chromaprint/releases/download/v1.5.1/chromaprint-fpcalc-1.5.1-windows-x86_64.zip"
if url.endswith(".zip"):
zip_path = os.path.join(os.getcwd(), "bin", "chromaprint.zip")
urllib.request.urlretrieve(url, zip_path)
with ZipFile(zip_path) as file:
file.extractall(os.path.join(os.getcwd(), "bin"))
os.remove(zip_path)
else:
tar_gz_path = os.path.join(os.getcwd(), "bin", "chromaprint.tar.gz")
urllib.request.urlretrieve(url, tar_gz_path)
with gzip.open(tar_gz_path, "rb") as f: # For some reason, tarfile by itself didnt work for tar.gz so i have to uncompress with gzip first and then with tarfile
with tarfile.open(fileobj=io.BytesIO(f.read())) as tar:
tar.extractall(os.path.join(os.getcwd(), "bin"))
os.remove(tar_gz_path)
chromaprint_matches = glob.glob(os.path.join("bin", "chromaprint*"))
if chromaprint_matches:
shutil.move(os.path.join(chromaprint_matches[0], get_fpcalc_name()), os.path.join("bin", get_fpcalc_name()))
shutil.rmtree(chromaprint_matches[0])
os.chmod(get_fpcalc_path(), 0o755)
def get_recording_id_from_acoustic(filename):
def get_recording_id_from_acoustid(filename):
os.environ["FPCALC"] = get_fpcalc_path()
try:

View File

@@ -30,8 +30,8 @@ def get_closest_time(current_time, lyrics_times):
def get_lyrics(artist, title):
metadata_cache = ensure_metadata_file()
if (artist, title) in metadata_cache["lyrics_by_artist_title"]:
return metadata_cache["lyrics_by_artist_title"][(artist, title)]
if artist in metadata_cache["lyrics_by_artist_title"] and title in metadata_cache["lyrics_by_artist_title"][artist]:
return metadata_cache["lyrics_by_artist_title"][artist][title]
else:
if artist:
query = f"{artist} - {title}"
@@ -46,11 +46,13 @@ def get_lyrics(artist, title):
for result in data:
if result.get("plainLyrics") and result.get("syncedLyrics"):
metadata_cache["lyrics_by_artist_title"][(artist, title)] = (result["plainLyrics"], result["syncedLyrics"])
if not artist in metadata_cache["lyrics_by_artist_title"]:
metadata_cache["lyrics_by_artist_title"][artist] = {}
metadata_cache["lyrics_by_artist_title"][artist][title] = (result["plainLyrics"], result["syncedLyrics"])
with open("metadata_cache.json", "w") as file:
file.write(json.dumps(metadata_cache))
return (result["plainLyrics"], result["syncedLyrics"])
with open("metadata_cache.json", "w") as file:
file.write(json.dumps(metadata_cache))
if artist: # if there was an artist, it might have been misleading. For example, on Youtube, the uploader might not be the artist. We retry with only title.
return get_lyrics(None, title)

View File

@@ -190,9 +190,10 @@ def add_metadata_to_file(file_path, musicbrainz_artist_ids, artist, title, synch
id3 = ID3(file_path)
id3.delall("SYLT")
lyrics_dict = parse_synchronized_lyrics(synchronized_lyrics)[1]
synchronized_lyrics_tuples = [(text, int(lyrics_time * 1000)) for lyrics_time, text in lyrics_dict.items()] # * 1000 because format 2 means milliseconds
if synchronized_lyrics:
lyrics_dict = parse_synchronized_lyrics(synchronized_lyrics)[1]
synchronized_lyrics_tuples = [(text, int(lyrics_time * 1000)) for lyrics_time, text in lyrics_dict.items()] # * 1000 because format 2 means milliseconds
id3.add(SYLT(encoding=3, lang="eng", format=2, type=1, desc="From lrclib", text=synchronized_lyrics_tuples))
id3.add(SYLT(encoding=3, lang="eng", format=2, type=1, desc="From lrclib", text=synchronized_lyrics_tuples))
id3.save()