feat: successfully added artwork

This commit is contained in:
Hazel 2024-04-10 18:53:36 +02:00
parent 0c367884e3
commit 15ee1d3317
3 changed files with 19 additions and 4 deletions

View File

@ -5,7 +5,7 @@ from typing import List
import logging import logging
from PIL import Image from PIL import Image
from ..utils.config import logging_settings from ..utils.config import logging_settings, main_settings
from ..objects import Song, Target, Metadata from ..objects import Song, Target, Metadata
from ..connection import Connection from ..connection import Connection
@ -75,14 +75,27 @@ def write_metadata_to_target(metadata: Metadata, target: Target, song: Song):
converted_target: Target = Target.temp(name=f"{song.title}.jpeg") converted_target: Target = Target.temp(name=f"{song.title}.jpeg")
with Image.open(temp_target.file_path) as img: with Image.open(temp_target.file_path) as img:
# crop the image if it isn't square in the middle with minimum data loss
width, height = img.size
if width != height:
if width > height:
img = img.crop((width // 2 - height // 2, 0, width // 2 + height // 2, height))
else:
img = img.crop((0, height // 2 - width // 2, width, height // 2 + width // 2))
# resize the image to the preferred resolution
img.thumbnail((main_settings["preferred_artwork_resolution"], main_settings["preferred_artwork_resolution"]))
img.save(converted_target.file_path, "JPEG") img.save(converted_target.file_path, "JPEG")
# https://stackoverflow.com/questions/70228440/mutagen-how-can-i-correctly-embed-album-art-into-mp3-file-so-that-i-can-see-t
id3_object.frames.delall("APIC")
id3_object.frames.add( id3_object.frames.add(
APIC( APIC(
encoding=3, encoding=0,
mime="image/jpeg", mime="image/jpeg",
type=3, type=3,
desc="Cover", desc=u"Cover",
data=converted_target.read_bytes(), data=converted_target.read_bytes(),
) )
) )

View File

@ -43,7 +43,7 @@ class Artwork:
@property @property
def best_variant(self) -> ArtworkVariant: def best_variant(self) -> ArtworkVariant:
if len(self._variant_mapping) == 0: if len(self._variant_mapping.keys()) <= 0:
return None return None
return min(self._variant_mapping.values(), key=lambda x: x["deviation"]) return min(self._variant_mapping.values(), key=lambda x: x["deviation"])

View File

@ -485,6 +485,8 @@ class YoutubeMusic(SuperYouTube):
def fetch_song(self, source: Source, stop_at_level: int = 1) -> Song: def fetch_song(self, source: Source, stop_at_level: int = 1) -> Song:
ydl_res: dict = self.ydl.extract_info(url=source.url, download=False) ydl_res: dict = self.ydl.extract_info(url=source.url, download=False)
dump_to_file(f"eeee.json", json.dumps(ydl_res), is_json=True, exit_after_dump=False)
self.fetch_media_url(source=source, ydl_res=ydl_res) self.fetch_media_url(source=source, ydl_res=ydl_res)
artist_name = ydl_res.get("artist", ydl_res.get("uploader", "")).rstrip(" - Topic") artist_name = ydl_res.get("artist", ydl_res.get("uploader", "")).rstrip(" - Topic")