mirror of
https://github.com/csd4ni3l/music-player.git
synced 2026-01-01 12:13:42 +01:00
Fix audio normalization not working, add online metadata helper that uses MusicBrainz, Cover Art Archive and LRCLIB to gather as much data as possible
This commit is contained in:
@@ -267,13 +267,14 @@ class Main(arcade.gui.UIView):
|
|||||||
if action != "Close":
|
if action != "Close":
|
||||||
webbrowser.open(metadata["uploader_url"] if action == "Uploader" else metadata["source_url"])
|
webbrowser.open(metadata["uploader_url"] if action == "Uploader" else metadata["source_url"])
|
||||||
|
|
||||||
def open_metadata(self, file_path):
|
def view_metadata(self, file_path):
|
||||||
metadata = self.file_metadata[file_path]
|
metadata = self.file_metadata[file_path]
|
||||||
|
|
||||||
metadata_text = f'''File path: {file_path}
|
metadata_text = f'''File path: {file_path}
|
||||||
File size: {metadata['file_size']}MiB
|
File size: {metadata['file_size']}MiB
|
||||||
Artist: {metadata['artist']}
|
Artist: {metadata['artist']}
|
||||||
Title: {metadata['title']}
|
Title: {metadata['title']}
|
||||||
|
Upload Year: {metadata['upload_year'] or 'Unknown'}
|
||||||
Amount of times played: {metadata['play_count']}
|
Amount of times played: {metadata['play_count']}
|
||||||
Last Played: {convert_timestamp_to_time_ago(int(metadata['last_played']))}
|
Last Played: {convert_timestamp_to_time_ago(int(metadata['last_played']))}
|
||||||
Sound length: {convert_seconds_to_date(int(metadata['sound_length']))}
|
Sound length: {convert_seconds_to_date(int(metadata['sound_length']))}
|
||||||
@@ -310,7 +311,7 @@ Sample rate: {metadata['sample_rate']}KHz'''
|
|||||||
|
|
||||||
self.music_buttons[f"{tab}/{music_filename}"] = self.music_box.add(MusicItem(metadata=metadata, width=self.window.width / 1.2, height=self.window.height / 11))
|
self.music_buttons[f"{tab}/{music_filename}"] = self.music_box.add(MusicItem(metadata=metadata, width=self.window.width / 1.2, height=self.window.height / 11))
|
||||||
self.music_buttons[f"{tab}/{music_filename}"].button.on_click = lambda event, music_path=f"{tab}/{music_filename}": self.music_button_click(event, music_path)
|
self.music_buttons[f"{tab}/{music_filename}"].button.on_click = lambda event, music_path=f"{tab}/{music_filename}": self.music_button_click(event, music_path)
|
||||||
self.music_buttons[f"{tab}/{music_filename}"].view_metadata_button.on_click = lambda event, music_path=f"{tab}/{music_filename}": self.open_metadata(music_path)
|
self.music_buttons[f"{tab}/{music_filename}"].view_metadata_button.on_click = lambda event, music_path=f"{tab}/{music_filename}": self.view_metadata(music_path)
|
||||||
|
|
||||||
elif self.current_mode == "playlist":
|
elif self.current_mode == "playlist":
|
||||||
self.current_playlist = tab
|
self.current_playlist = tab
|
||||||
@@ -330,7 +331,7 @@ Sample rate: {metadata['sample_rate']}KHz'''
|
|||||||
metadata = self.file_metadata[music_path]
|
metadata = self.file_metadata[music_path]
|
||||||
self.music_buttons[music_path] = self.music_box.add(MusicItem(metadata=metadata, width=self.window.width / 1.2, height=self.window.height / 11))
|
self.music_buttons[music_path] = self.music_box.add(MusicItem(metadata=metadata, width=self.window.width / 1.2, height=self.window.height / 11))
|
||||||
self.music_buttons[music_path].button.on_click = lambda event, music_path=music_path: self.music_button_click(event, music_path)
|
self.music_buttons[music_path].button.on_click = lambda event, music_path=music_path: self.music_button_click(event, music_path)
|
||||||
self.music_buttons[music_path].view_metadata_button.on_click = lambda event, music_path=music_path: self.open_metadata(music_path)
|
self.music_buttons[music_path].view_metadata_button.on_click = lambda event, music_path=music_path: self.view_metadata(music_path)
|
||||||
|
|
||||||
self.music_buttons["add_music"] = self.music_box.add(MusicItem(metadata=None, width=self.window.width / 1.2, height=self.window.height / 11))
|
self.music_buttons["add_music"] = self.music_box.add(MusicItem(metadata=None, width=self.window.width / 1.2, height=self.window.height / 11))
|
||||||
self.music_buttons["add_music"].button.on_click = lambda event: self.add_music()
|
self.music_buttons["add_music"].button.on_click = lambda event: self.add_music()
|
||||||
@@ -397,8 +398,6 @@ Sample rate: {metadata['sample_rate']}KHz'''
|
|||||||
self.should_reload = True # needed because the observer runs in another thread and OpenGL is single-threaded.
|
self.should_reload = True # needed because the observer runs in another thread and OpenGL is single-threaded.
|
||||||
|
|
||||||
def load_tabs(self):
|
def load_tabs(self):
|
||||||
self.tab_box.clear()
|
|
||||||
|
|
||||||
if self.current_mode == "files":
|
if self.current_mode == "files":
|
||||||
for tab in self.tab_options:
|
for tab in self.tab_options:
|
||||||
self.tab_buttons[os.path.expanduser(tab)] = self.tab_box.add(arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text=os.path.basename(os.path.normpath(os.path.expanduser(tab))), style=button_style, width=self.window.width / 10, height=self.window.height / 15))
|
self.tab_buttons[os.path.expanduser(tab)] = self.tab_box.add(arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text=os.path.basename(os.path.normpath(os.path.expanduser(tab))), style=button_style, width=self.window.width / 10, height=self.window.height / 15))
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ readme = "README.md"
|
|||||||
requires-python = ">=3.11"
|
requires-python = ">=3.11"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arcade==3.2.0",
|
"arcade==3.2.0",
|
||||||
|
"iso3166>=2.1.1",
|
||||||
|
"musicbrainzngs>=0.7.1",
|
||||||
"mutagen>=1.47.0",
|
"mutagen>=1.47.0",
|
||||||
"pydub>=0.25.1",
|
"pydub>=0.25.1",
|
||||||
"pypresence>=4.3.0",
|
"pypresence>=4.3.0",
|
||||||
|
|||||||
@@ -6,11 +6,13 @@ from arcade.gui.widgets.slider import UISliderStyle
|
|||||||
menu_background_color = (17, 17, 17)
|
menu_background_color = (17, 17, 17)
|
||||||
log_dir = 'logs'
|
log_dir = 'logs'
|
||||||
discord_presence_id = 1368277020332523530
|
discord_presence_id = 1368277020332523530
|
||||||
|
|
||||||
audio_extensions = ["mp3", "m4a", "aac", "flac", "ogg", "opus", "wav"]
|
audio_extensions = ["mp3", "m4a", "aac", "flac", "ogg", "opus", "wav"]
|
||||||
|
|
||||||
view_modes = ["files", "playlist"]
|
view_modes = ["files", "playlist"]
|
||||||
|
|
||||||
|
MUSICBRAINZ_PROJECT_NAME = "csd4ni3l/music-player"
|
||||||
|
MUSCIBRAINZ_VERSION = "git"
|
||||||
|
MUSICBRAINZ_CONTACT = "csd4ni3l@proton.me"
|
||||||
|
|
||||||
DARK_GRAY = Color(45, 45, 45)
|
DARK_GRAY = Color(45, 45, 45)
|
||||||
GRAY = Color(70, 70, 70)
|
GRAY = Color(70, 70, 70)
|
||||||
LIGHT_GRAY = Color(150, 150, 150)
|
LIGHT_GRAY = Color(150, 150, 150)
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ def extract_metadata_and_thumbnail(file_path: str, thumb_resolution: tuple) -> t
|
|||||||
sample_rate = 0
|
sample_rate = 0
|
||||||
last_played = 0
|
last_played = 0
|
||||||
play_count = 0
|
play_count = 0
|
||||||
|
upload_year = 0
|
||||||
|
|
||||||
basename = os.path.basename(file_path)
|
basename = os.path.basename(file_path)
|
||||||
name_only = os.path.splitext(basename)[0]
|
name_only = os.path.splitext(basename)[0]
|
||||||
@@ -37,6 +38,7 @@ def extract_metadata_and_thumbnail(file_path: str, thumb_resolution: tuple) -> t
|
|||||||
try:
|
try:
|
||||||
artist = str(thumb_audio["artist"][0])
|
artist = str(thumb_audio["artist"][0])
|
||||||
title = str(thumb_audio["title"][0])
|
title = str(thumb_audio["title"][0])
|
||||||
|
upload_year = int(thumb_audio["date"][0])
|
||||||
except KeyError:
|
except KeyError:
|
||||||
artist_title_match = re.search(r'^.+\s*-\s*.+$', title)
|
artist_title_match = re.search(r'^.+\s*-\s*.+$', title)
|
||||||
if artist_title_match:
|
if artist_title_match:
|
||||||
@@ -111,6 +113,7 @@ def extract_metadata_and_thumbnail(file_path: str, thumb_resolution: tuple) -> t
|
|||||||
"file_size": file_size,
|
"file_size": file_size,
|
||||||
"last_played": last_played,
|
"last_played": last_played,
|
||||||
"play_count": play_count,
|
"play_count": play_count,
|
||||||
|
"upload_year": upload_year,
|
||||||
"sample_rate": sample_rate,
|
"sample_rate": sample_rate,
|
||||||
"uploader_url": uploader_url,
|
"uploader_url": uploader_url,
|
||||||
"source_url": source_url,
|
"source_url": source_url,
|
||||||
@@ -120,6 +123,12 @@ def extract_metadata_and_thumbnail(file_path: str, thumb_resolution: tuple) -> t
|
|||||||
}
|
}
|
||||||
|
|
||||||
def adjust_volume(input_path, volume):
|
def adjust_volume(input_path, volume):
|
||||||
|
audio = AudioSegment.from_file(input_path)
|
||||||
|
change = volume - audio.dBFS
|
||||||
|
|
||||||
|
if abs(change) < 1.0:
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
easy_tags = EasyID3(input_path)
|
easy_tags = EasyID3(input_path)
|
||||||
tags = dict(easy_tags)
|
tags = dict(easy_tags)
|
||||||
@@ -143,10 +152,7 @@ def adjust_volume(input_path, volume):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
cover_path = None
|
cover_path = None
|
||||||
|
|
||||||
audio = AudioSegment.from_file(input_path)
|
audio = audio.apply_gain(change)
|
||||||
|
|
||||||
if int(audio.dBFS) == volume:
|
|
||||||
return
|
|
||||||
|
|
||||||
export_args = {
|
export_args = {
|
||||||
"format": "mp3",
|
"format": "mp3",
|
||||||
@@ -155,8 +161,6 @@ def adjust_volume(input_path, volume):
|
|||||||
if cover_path:
|
if cover_path:
|
||||||
export_args["cover"] = cover_path
|
export_args["cover"] = cover_path
|
||||||
|
|
||||||
change = volume - audio.dBFS
|
|
||||||
audio.apply_gain(change)
|
|
||||||
audio.export(input_path, **export_args)
|
audio.export(input_path, **export_args)
|
||||||
|
|
||||||
def update_last_play_statistics(filepath):
|
def update_last_play_statistics(filepath):
|
||||||
|
|||||||
134
utils/online_metadata.py
Normal file
134
utils/online_metadata.py
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
import musicbrainzngs as music_api
|
||||||
|
from iso3166 import countries
|
||||||
|
import urllib.request, json
|
||||||
|
from utils.constants import MUSICBRAINZ_PROJECT_NAME, MUSICBRAINZ_CONTACT, MUSCIBRAINZ_VERSION
|
||||||
|
|
||||||
|
WORD_BLACKLIST = ["compilation", "remix", "vs", "cover"]
|
||||||
|
LRCLIB_BASE_URL = "https://lrclib.net/api/search"
|
||||||
|
|
||||||
|
def check_blacklist(text, blacklist):
|
||||||
|
return any(word in text for word in blacklist)
|
||||||
|
|
||||||
|
def finalize_blacklist(title):
|
||||||
|
blacklist = WORD_BLACKLIST[:]
|
||||||
|
|
||||||
|
for word in WORD_BLACKLIST:
|
||||||
|
if word in title:
|
||||||
|
blacklist.remove(word)
|
||||||
|
|
||||||
|
return blacklist
|
||||||
|
|
||||||
|
def is_release_valid(release_id):
|
||||||
|
try:
|
||||||
|
release_data = music_api.get_release_by_id(release_id, includes=["release-groups"])
|
||||||
|
rg = release_data.get("release", {}).get("release-group", {})
|
||||||
|
if rg.get("primary-type", "").lower() == "album":
|
||||||
|
return True
|
||||||
|
except music_api.ResponseError:
|
||||||
|
pass
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_country(country_code):
|
||||||
|
try:
|
||||||
|
country = countries.get(country_code)
|
||||||
|
except KeyError:
|
||||||
|
country = None
|
||||||
|
|
||||||
|
return country.name if country else None
|
||||||
|
|
||||||
|
def get_music_metadata(artist, title):
|
||||||
|
music_api.set_useragent(MUSICBRAINZ_PROJECT_NAME, MUSCIBRAINZ_VERSION, MUSICBRAINZ_CONTACT)
|
||||||
|
|
||||||
|
if artist:
|
||||||
|
results = music_api.search_recordings(query=f"{artist} - {title}", limit=100)["recording-list"]
|
||||||
|
else:
|
||||||
|
results = music_api.search_recordings(query=title, limit=100)["recording-list"]
|
||||||
|
|
||||||
|
finalized_blacklist = finalize_blacklist(title)
|
||||||
|
|
||||||
|
for r in results:
|
||||||
|
if not r.get("title") or not r.get("isrc-list"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if check_blacklist(r["title"].lower(), finalized_blacklist) or check_blacklist(r.get("disambiguation", "").lower(), finalized_blacklist):
|
||||||
|
continue
|
||||||
|
|
||||||
|
recording_id = r["id"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
detailed = music_api.get_recording_by_id(
|
||||||
|
recording_id,
|
||||||
|
includes=["artists", "releases", "isrcs", "tags", "ratings"]
|
||||||
|
)["recording"]
|
||||||
|
except music_api.ResponseError:
|
||||||
|
continue
|
||||||
|
|
||||||
|
release = None
|
||||||
|
for rel in detailed.get("release-list", []):
|
||||||
|
release_title = rel.get("title", "").lower()
|
||||||
|
|
||||||
|
if any(word in release_title for word in ["single", "ep", "maxi"]):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if rel.get("status") == "Official" and is_release_valid(rel["id"]): # Only do it if the album is official, skipping many API calls
|
||||||
|
release = rel
|
||||||
|
|
||||||
|
metadata = {
|
||||||
|
"musicbrainz_id": recording_id,
|
||||||
|
"isrc": detailed["isrc-list"][0] if "isrc-list" in detailed else "Unknown",
|
||||||
|
"musicbrainz_album_id": release.get("id") if release else "Unknown",
|
||||||
|
"album_name": release.get("title") if release else "Unknown",
|
||||||
|
"album_date": release.get("date") if release else "Unknown",
|
||||||
|
"album_country": (get_country(release.get("country")) or "Worldwide") if release else "Unknown",
|
||||||
|
"recording_length": int(detailed["length"]) if "length" in detailed else "Unknown",
|
||||||
|
"musicbrainz_rating": detailed["rating"]["rating"] if "rating" in detailed else "Unknown",
|
||||||
|
"tags": [tag["name"] for tag in detailed.get("tag-list", [])]
|
||||||
|
}
|
||||||
|
|
||||||
|
return metadata
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_artist_metadata(artist):
|
||||||
|
music_api.set_useragent(MUSICBRAINZ_PROJECT_NAME, MUSCIBRAINZ_VERSION, MUSICBRAINZ_CONTACT)
|
||||||
|
|
||||||
|
result = music_api.search_artists(query=artist, limit=10)
|
||||||
|
|
||||||
|
for r in result["artist-list"]:
|
||||||
|
if not r["type"] == "Person":
|
||||||
|
continue
|
||||||
|
|
||||||
|
return {
|
||||||
|
"musicbrainz_id": r["id"],
|
||||||
|
"gender": r.get("gender", "Unknown"),
|
||||||
|
"country": get_country(r.get("country")) or "Unknown",
|
||||||
|
"ipi-list": r.get("ipi-list", "None"),
|
||||||
|
"isni-list": r.get("isni-list", "None"),
|
||||||
|
"born": r.get("life-span", {}).get("begin", "Unknown"),
|
||||||
|
"dead": r.get("life-span", {}).get("ended").lower() == "true",
|
||||||
|
"comment": r["disambiguation"]
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_lyrics(artist, title):
|
||||||
|
if artist:
|
||||||
|
query = f"{artist} - {title}"
|
||||||
|
else:
|
||||||
|
query = title
|
||||||
|
|
||||||
|
query_string = urllib.parse.urlencode({"q": query})
|
||||||
|
full_url = f"{LRCLIB_BASE_URL}?{query_string}"
|
||||||
|
|
||||||
|
with urllib.request.urlopen(full_url) as request:
|
||||||
|
data = json.loads(request.read().decode("utf-8"))
|
||||||
|
|
||||||
|
for result in data:
|
||||||
|
if result.get("plainLyrics"):
|
||||||
|
return result["plainLyrics"]
|
||||||
|
|
||||||
|
return "Unknown"
|
||||||
|
|
||||||
|
def get_album_cover_art(musicbrainz_album_id):
|
||||||
|
cover_art_bytes = music_api.get_image_front(musicbrainz_album_id)
|
||||||
|
with open("music_cover_art.jpg", "wb") as file:
|
||||||
|
file.write(cover_art_bytes)
|
||||||
|
|
||||||
22
uv.lock
generated
22
uv.lock
generated
@@ -71,12 +71,32 @@ wheels = [
|
|||||||
{ url = "https://files.pythonhosted.org/packages/7c/fc/6a8cb64e5f0324877d503c854da15d76c1e50eb722e320b15345c4d0c6de/cffi-1.17.1-cp313-cp313-win_amd64.whl", hash = "sha256:f6a16c31041f09ead72d69f583767292f750d24913dadacf5756b966aacb3f1a", size = 182009, upload_time = "2024-09-04T20:44:45.309Z" },
|
{ url = "https://files.pythonhosted.org/packages/7c/fc/6a8cb64e5f0324877d503c854da15d76c1e50eb722e320b15345c4d0c6de/cffi-1.17.1-cp313-cp313-win_amd64.whl", hash = "sha256:f6a16c31041f09ead72d69f583767292f750d24913dadacf5756b966aacb3f1a", size = 182009, upload_time = "2024-09-04T20:44:45.309Z" },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "iso3166"
|
||||||
|
version = "2.1.1"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/5c/11/b5023c736a185a88ebd0d38646af6f4d1b4c9b91f2ca84e08e5d2bc7ac3c/iso3166-2.1.1.tar.gz", hash = "sha256:fcd551b8dda66b44e9f9e6d6bbbee3a1145a22447c0a556e5d0fb1ad1e491719", size = 12807, upload_time = "2022-07-12T04:07:57.294Z" }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/08/d0/bf18725b8d47f37858ff801f8e4d40c6982730a899725bdb6ded62199954/iso3166-2.1.1-py3-none-any.whl", hash = "sha256:263660b36f8471c42acd1ff673d28a3715edbce7d24b1550d0cf010f6816c47f", size = 9829, upload_time = "2022-07-12T04:07:55.54Z" },
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "musicbrainzngs"
|
||||||
|
version = "0.7.1"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/0a/67/3e74ae93d90ceeba72ed1a266dd3ca9abd625f315f0afd35f9b034acedd1/musicbrainzngs-0.7.1.tar.gz", hash = "sha256:ab1c0100fd0b305852e65f2ed4113c6de12e68afd55186987b8ed97e0f98e627", size = 117469, upload_time = "2020-01-11T17:38:47.581Z" }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/6d/fd/cef7b2580436910ccd2f8d3deec0f3c81743e15c0eb5b97dde3fbf33c0c8/musicbrainzngs-0.7.1-py2.py3-none-any.whl", hash = "sha256:e841a8f975104c0a72290b09f59326050194081a5ae62ee512f41915090e1a10", size = 25289, upload_time = "2020-01-11T17:38:45.469Z" },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "musicplayer"
|
name = "musicplayer"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = { virtual = "." }
|
source = { virtual = "." }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "arcade" },
|
{ name = "arcade" },
|
||||||
|
{ name = "iso3166" },
|
||||||
|
{ name = "musicbrainzngs" },
|
||||||
{ name = "mutagen" },
|
{ name = "mutagen" },
|
||||||
{ name = "pydub" },
|
{ name = "pydub" },
|
||||||
{ name = "pypresence" },
|
{ name = "pypresence" },
|
||||||
@@ -87,6 +107,8 @@ dependencies = [
|
|||||||
[package.metadata]
|
[package.metadata]
|
||||||
requires-dist = [
|
requires-dist = [
|
||||||
{ name = "arcade", specifier = "==3.2.0" },
|
{ name = "arcade", specifier = "==3.2.0" },
|
||||||
|
{ name = "iso3166", specifier = ">=2.1.1" },
|
||||||
|
{ name = "musicbrainzngs", specifier = ">=0.7.1" },
|
||||||
{ name = "mutagen", specifier = ">=1.47.0" },
|
{ name = "mutagen", specifier = ">=1.47.0" },
|
||||||
{ name = "pydub", specifier = ">=0.25.1" },
|
{ name = "pydub", specifier = ">=0.25.1" },
|
||||||
{ name = "pypresence", specifier = ">=4.3.0" },
|
{ name = "pypresence", specifier = ">=4.3.0" },
|
||||||
|
|||||||
Reference in New Issue
Block a user