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

@@ -5,7 +5,9 @@ import arcade, arcade.gui, os, json, threading, subprocess, urllib.request, plat
from arcade.gui.experimental.focus import UIFocusGroup
from utils.music_handling import adjust_volume
from utils.music_handling import adjust_volume, add_metadata_to_file
from utils.musicbrainz_metadata import get_music_metadata
from utils.acoustid_metadata import get_recording_id_from_acoustid
from utils.constants import button_style
from utils.preload import button_texture, button_hovered_texture
@@ -151,6 +153,19 @@ class Downloader(arcade.gui.UIView):
except Exception as e:
self.yt_dl_buffer = f"ERROR: Could not normalize volume due to an error: {e}"
return
self.yt_dl_buffer = "Analyzing with Acoustid to get accurate metadata..."
acoustid_id, musicbrainz_id = get_recording_id_from_acoustid("downloaded_music.mp3")
self.yt_dl_buffer = "Caching MusicBrainz and Lyrics metadata..."
if musicbrainz_id:
music_metadata, artist_metadata, album_metadata, lyrics_metadata = get_music_metadata(musicbrainz_id=musicbrainz_id)
else:
music_metadata, artist_metadata, album_metadata, lyrics_metadata = get_music_metadata(artist=artist, title=title)
self.yt_dl_buffer = "Adding missing metadata to file..."
add_metadata_to_file("downloaded_music.mp3", [artist['musicbrainz_id'] for artist in artist_metadata.values()], artist, title, lyrics_metadata[1], music_metadata["isrc-list"], acoustid_id)
try:
output_filename = os.path.join(path, f"{title}.mp3")
os.replace("downloaded_music.mp3", output_filename)

61
menus/fpcalc_missing.py Normal file
View File

@@ -0,0 +1,61 @@
import arcade, arcade.gui, sys, gzip, platform, tarfile, urllib.request, os, io, glob, shutil
from zipfile import ZipFile
from utils.acoustid_metadata import get_fpcalc_path, get_fpcalc_name
class FpcalcMissing(arcade.gui.UIView):
def __init__(self):
super().__init__()
def on_show_view(self):
super().on_show_view()
msgbox = self.add_widget(arcade.gui.UIMessageBox(width=self.window.width / 2, height=self.window.height / 2, title="Third-party fpcalc download", message_text="We need to download fpcalc from AcoustID to recognize the song for you for better results.\nIf you say no, we will use a searching algorithm instead which might give wrong results.\nEven if fpcalc is downloaded, it might not find the music since its a community-based project.\nIf so, we will fallback to the searching algorithm.\nDo you want to continue?", buttons=("Yes", "No")))
msgbox.on_action = lambda event: self.download_fpcalc() if event.action == "Yes" else self.exit()
def download_fpcalc(self):
system = platform.system()
architecture = platform.machine()
os.makedirs("bin", exist_ok=True)
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)
self.exit()
def exit(self):
from menus.main import Main
self.window.show_view(Main())

View File

@@ -485,16 +485,6 @@ class Main(arcade.gui.UIView):
update_last_play_statistics(music_path)
if not music_path in self.loaded_sounds:
self.loaded_sounds[music_path] = arcade.Sound(music_path, streaming=self.settings_dict.get("music_mode", "Stream") == "Stream")
self.volume = self.settings_dict.get("default_volume", 100)
self.volume_slider.value = self.volume
self.current_music_player = self.loaded_sounds[music_path].play()
self.current_music_player.volume = self.volume / 100
self.current_length = self.loaded_sounds[music_path].get_length()
self.current_music_artist = artist
self.current_music_title = title
self.current_music_title_label.text = title
@@ -503,8 +493,6 @@ class Main(arcade.gui.UIView):
self.current_music_thumbnail_image.texture = self.file_metadata[music_path]["thumbnail"]
self.time_label.text = "00:00"
self.full_length_label.text = "00:00"
self.progressbar.max_value = self.current_length
self.progressbar.value = 0
self.current_synchronized_lyrics = get_lyrics(self.current_music_artist, self.current_music_title)[1]
self.lyrics_times, self.parsed_lyrics = parse_synchronized_lyrics(self.current_synchronized_lyrics) if self.current_synchronized_lyrics else (None, None)
@@ -512,6 +500,16 @@ class Main(arcade.gui.UIView):
self.current_lyrics_label.text = "No known lyrics found"
self.next_lyrics_label.text = "No known lyrics found"
if not music_path in self.loaded_sounds:
self.loaded_sounds[music_path] = arcade.Sound(music_path, streaming=self.settings_dict.get("music_mode", "Stream") == "Stream")
self.volume = self.settings_dict.get("default_volume", 100)
self.volume_slider.value = self.volume
self.current_music_player = self.loaded_sounds[music_path].play()
self.current_music_player.volume = self.volume / 100
self.current_length = self.loaded_sounds[music_path].get_length()
self.progressbar.max_value = self.current_length
self.progressbar.value = 0
else:
if self.current_music_player is not None:
self.skip_sound() # reset properties

View File

@@ -9,7 +9,7 @@ from utils.constants import button_style
from utils.preload import button_texture, button_hovered_texture
from utils.utils import convert_seconds_to_date
from utils.music_handling import convert_timestamp_to_time_ago, truncate_end, add_metadata_to_file
from utils.acoustid_metadata import get_recording_id_from_acoustic, get_fpcalc_path, download_fpcalc
from utils.acoustid_metadata import get_recording_id_from_acoustid, get_fpcalc_path
class MetadataViewer(arcade.gui.UIView):
def __init__(self, pypresence_client, metadata_type="file", metadata=None, file_path=None, *args):
@@ -28,20 +28,11 @@ class MetadataViewer(arcade.gui.UIView):
self.artist = self.file_metadata["artist"] if not self.file_metadata["artist"] == "Unknown" else None
self.title = self.file_metadata["title"]
if metadata.get("confirm_download") is None:
if not os.path.exists(get_fpcalc_path()):
self.msgbox = self.add_widget(arcade.gui.UIMessageBox(width=self.window.width / 2, height=self.window.height / 2, title="Third-party fpcalc download", message_text="We need to download fpcalc from AcoustID to recognize the song for you for better results.\nIf you say no, we will use a searching algorithm instead which might give wrong results.\nEven if fpcalc is downloaded, it might not find the music since its a community-based project.\nIf so, we will fallback to the searching algorithm.\nDo you want to continue?", buttons=("Yes", "No")))
self.msgbox.on_action = lambda event: self.window.show_view(MetadataViewer(pypresence_client, metadata_type, metadata | {"confirm_download": event.action == "Yes"}, file_path, *args))
return
else:
self.acoustid_id, musicbrainz_id = get_recording_id_from_acoustic(self.file_path)
else:
if metadata["confirm_download"]:
download_fpcalc()
self.acoustid_id, musicbrainz_id = get_recording_id_from_acoustic(self.file_path)
else:
self.acoustid_id = None
if os.path.exists(get_fpcalc_path()):
self.acoustid_id, musicbrainz_id = get_recording_id_from_acoustid(self.file_path)
else:
self.acoustid_id, musicbrainz_id = None, None
if self.acoustid_id and musicbrainz_id:
self.music_metadata, self.artist_metadata, self.album_metadata, self.lyrics_metadata = get_music_metadata(musicbrainz_id=musicbrainz_id)

12
run.py
View File

@@ -13,6 +13,7 @@ import logging, datetime, json, sys, arcade
arcade.ArcadeContext.atlas_size = (16384, 16384)
from utils.utils import get_closest_resolution, print_debug_info, on_exception
from utils.acoustid_metadata import get_fpcalc_path
from utils.constants import log_dir, menu_background_color
from menus.main import Main
from arcade.experimental.controller_window import ControllerWindow
@@ -95,11 +96,16 @@ arcade.set_background_color(menu_background_color)
print_debug_info()
if pyglet.media.codecs.have_ffmpeg():
menu = Main()
else:
if not pyglet.media.codecs.have_ffmpeg():
from menus.ffmpeg_missing import FFmpegMissing
menu = FFmpegMissing()
elif not os.path.exists(get_fpcalc_path()):
from menus.fpcalc_missing import FpcalcMissing
menu = FpcalcMissing()
else:
menu = Main()
window.show_view(menu)

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()