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

89 lines
2.8 KiB
Python
Raw Normal View History

2023-01-12 18:55:24 +00:00
import mutagen
2023-01-11 15:30:53 +00:00
from mutagen.id3 import ID3, Frame
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
2023-09-10 14:27:09 +00:00
from ..utils.config import logging_settings
2023-03-30 14:50:27 +00:00
from ..objects import Song, Target, Metadata
2023-01-09 13:50:06 +00:00
2023-01-09 13:21:49 +00:00
2023-09-10 14:27:09 +00:00
LOGGER = logging_settings["tagging_logger"]
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
"""
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
2023-03-30 14:50:27 +00:00
def write_metadata_to_target(metadata: Metadata, target: Target):
if not target.exists:
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)
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
id3_object = AudioMetadata(file_location=target.file_path)
id3_object.add_song_metadata(song=song)
id3_object.save()
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)
2023-01-11 15:30:53 +00:00
2023-01-09 13:50:06 +00:00
if __name__ == "__main__":
print("called directly")
filepath = "/home/lars/Music/deathcore/Archspire/Bleed the Future/Bleed the Future.mp3"
audio_metadata = AudioMetadata(file_location=filepath)
print(audio_metadata.frames.pprint())