music-kraken-core/music_kraken/audio/metadata.py

134 lines
4.5 KiB
Python
Raw Normal View History

2023-01-12 18:55:24 +00:00
import mutagen
2024-05-08 10:27:56 +00:00
from mutagen.id3 import ID3, Frame, APIC, USLT
2023-04-04 15:34:16 +00:00
from pathlib import Path
2023-01-11 15:30:53 +00:00
from typing import List
2023-01-09 13:50:06 +00:00
import logging
2024-04-10 16:18:52 +00:00
from PIL import Image
2023-01-09 13:50:06 +00:00
2024-04-10 16:53:36 +00:00
from ..utils.config import logging_settings, main_settings
2023-03-30 14:50:27 +00:00
from ..objects import Song, Target, Metadata
2024-05-08 10:27:56 +00:00
from ..objects.metadata import Mapping
2024-04-10 16:18:52 +00:00
from ..connection import Connection
2023-01-09 13:21:49 +00:00
2023-09-10 14:27:09 +00:00
LOGGER = logging_settings["tagging_logger"]
2024-04-10 16:18:52 +00:00
artwork_connection: Connection = Connection()
2023-01-09 13:50:06 +00:00
class AudioMetadata:
def __init__(self, file_location: str = None) -> None:
2023-01-11 15:30:53 +00:00
self._file_location = None
2023-01-09 13:50:06 +00:00
self.frames: ID3 = ID3()
2023-01-11 15:30:53 +00:00
2023-01-12 15:25:50 +00:00
if file_location is not None:
2023-01-11 15:30:53 +00:00
self.file_location = file_location
2023-04-04 15:34:16 +00:00
2023-03-30 14:50:27 +00:00
def add_metadata(self, metadata: Metadata):
for value in metadata:
2023-01-11 15:30:53 +00:00
"""
2023-01-12 15:25:50 +00:00
https://www.programcreek.com/python/example/84797/mutagen.id3.ID3
2023-01-11 15:30:53 +00:00
"""
if value is None:
continue
2023-01-12 22:01:19 +00:00
self.frames.add(value)
2023-01-09 13:50:06 +00:00
2023-03-30 14:50:27 +00:00
def add_song_metadata(self, song: Song):
self.add_metadata(song.metadata)
2023-04-04 15:34:16 +00:00
def save(self, file_location: Path = None):
LOGGER.debug(f"saving following frames: {self.frames.pprint()}")
2023-03-30 14:50:27 +00:00
2023-01-09 13:50:06 +00:00
if file_location is not None:
self.file_location = file_location
if self.file_location is None:
raise Exception("no file target provided to save the data to")
2023-01-12 15:25:50 +00:00
self.frames.save(self.file_location, v2_version=4)
2023-01-09 13:50:06 +00:00
2023-04-04 15:34:16 +00:00
def set_file_location(self, file_location: Path):
2023-01-11 15:30:53 +00:00
# try loading the data from the given file. if it doesn't succeed the frame remains empty
try:
2023-01-12 15:25:50 +00:00
self.frames.load(file_location, v2_version=4)
2023-04-04 15:34:16 +00:00
LOGGER.debug(f"loaded following from \"{file_location}\"\n{self.frames.pprint()}")
2023-01-11 15:30:53 +00:00
except mutagen.MutagenError:
2023-04-04 15:34:16 +00:00
LOGGER.warning(f"couldn't find any metadata at: \"{self.file_location}\"")
2023-01-12 18:55:24 +00:00
self._file_location = file_location
2023-01-11 15:30:53 +00:00
file_location = property(fget=lambda self: self._file_location, fset=set_file_location)
2023-04-04 15:34:16 +00:00
2024-04-10 16:18:52 +00:00
def write_metadata_to_target(metadata: Metadata, target: Target, song: Song):
2023-03-30 14:50:27 +00:00
if not target.exists:
2024-04-10 16:18:52 +00:00
LOGGER.warning(f"file {target.file_path} not found")
2023-03-30 14:50:27 +00:00
return
2023-04-04 15:34:16 +00:00
2023-03-30 14:50:27 +00:00
id3_object = AudioMetadata(file_location=target.file_path)
2024-04-10 16:18:52 +00:00
2024-04-11 11:53:02 +00:00
LOGGER.info(str(metadata))
2024-04-10 16:18:52 +00:00
if song.artwork.best_variant is not None:
2024-04-26 13:56:13 +00:00
best_variant = song.artwork.best_variant
2024-04-10 16:18:52 +00:00
r = artwork_connection.get(
2024-04-26 13:56:13 +00:00
url=best_variant["url"],
2024-04-26 14:04:48 +00:00
name=song.artwork.get_variant_name(best_variant),
2024-04-10 16:18:52 +00:00
)
temp_target: Target = Target.temp()
with temp_target.open("wb") as f:
f.write(r.content)
converted_target: Target = Target.temp(name=f"{song.title.replace('/', '_')}")
2024-04-10 16:18:52 +00:00
with Image.open(temp_target.file_path) as img:
2024-04-10 16:53:36 +00:00
# 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"]))
2024-04-10 16:18:52 +00:00
img.save(converted_target.file_path, "JPEG")
2024-04-10 16:53:36 +00:00
# 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")
2024-04-10 16:18:52 +00:00
id3_object.frames.add(
APIC(
2024-04-10 16:53:36 +00:00
encoding=0,
2024-04-10 16:18:52 +00:00
mime="image/jpeg",
type=3,
2024-04-10 16:53:36 +00:00
desc=u"Cover",
2024-04-10 16:18:52 +00:00
data=converted_target.read_bytes(),
)
)
2024-05-08 10:27:56 +00:00
id3_object.frames.delall("USLT")
uslt_val = metadata.get_id3_value(Mapping.UNSYNCED_LYRICS)
id3_object.frames.add(
USLT(encoding=3, lang=u'eng', desc=u'desc', text=uslt_val)
)
2024-04-10 16:18:52 +00:00
2023-03-30 14:50:27 +00:00
id3_object.add_metadata(metadata)
id3_object.save()
2023-01-11 15:30:53 +00:00
2023-04-04 15:34:16 +00:00
def write_metadata(song: Song, ignore_file_not_found: bool = True):
target: Target
for target in song.target:
if not target.exists:
if ignore_file_not_found:
continue
else:
raise ValueError(f"{song.target.file} not found")
2023-01-12 18:55:24 +00:00
2024-04-10 16:18:52 +00:00
write_metadata_to_target(metadata=song.metadata, target=target, song=song)
2023-01-11 15:30:53 +00:00
def write_many_metadata(song_list: List[Song]):
for song in song_list:
2023-01-12 18:55:24 +00:00
write_metadata(song=song, ignore_file_not_found=True)