Created own metadata dataclass instead of using TinyTag.tags()

This commit is contained in:
The Wobbler 2025-03-04 17:24:24 +01:00
parent 7ff1ad7a02
commit 072f5c7691
3 changed files with 32 additions and 16 deletions

View file

@ -3,18 +3,35 @@
from pydub import AudioSegment
from pygame.mixer import Sound
from tinytag import TinyTag
from tinytag.tinytag import Images as TTImages
from dataclasses import dataclass
@dataclass
class TrackMetadata:
title: str
artist: str
album: str
images: TTImages | None=None # tinytag images
class Track:
"""
Class containing data for a track like file path, raw data...
Class representing a track.
"""
def __init__(self, app, path: str, cache: bool=False):
def __init__(self, app, path: str, cache: bool=False, metadata: TrackMetadata=None):
self.app = app
self.path = path
self.tags = TinyTag.get(self.path, ignore_errors=True, duration=False)
if metadata is None:
# load metadata from audio file
tags = TinyTag.get(self.path, ignore_errors=True, duration=False)
self.metadata = TrackMetadata(tags.title, tags.artist, tags.album)
else:
self.metadata = metadata
self.cached = False
self.audio = None
@ -67,7 +84,8 @@ class Track:
self.duration = len(self.audio) # track duration in milliseconds
self.tags = TinyTag.get(self.path, ignore_errors=True, duration=False, image=True) # metadata with images
# metadata with images
self.metadata.images = TinyTag.get(self.path, ignore_errors=True, duration=False, image=True).images
self.cached = True
@ -78,11 +96,9 @@ class Track:
self.sound = None
self.duration = 0
self.tags = TinyTag.get(self.path, ignore_errors=True, duration=False) # metadata without images
self.metadata.images = None
def load_audio(self):
#file_type = self.path.split(".")[-1]
self.audio = AudioSegment.from_file(self.path)
def remaining(self, position: int):

View file

@ -38,9 +38,9 @@ class TrackItem(QTreeWidgetItem):
)
self.setText(0, str(self.index + 1))
self.setText(1, track.tags.title)
self.setText(2, track.tags.artist)
self.setText(3, track.tags.album)
self.setText(1, track.metadata.title)
self.setText(2, track.metadata.artist)
self.setText(3, track.metadata.album)
self.setText(4, str(self.index_user_sort + 1))
def mark(self):

View file

@ -61,25 +61,25 @@ class TrackInfo(QToolBar):
if current_playlist is not None and current_playlist.current_track is not None:
current_track = current_playlist.current_track
title = current_track.tags.title
artist = current_track.tags.artist
album = current_track.tags.album
title = current_track.metadata.title
artist = current_track.metadata.artist
album = current_track.metadata.album
self.title.setText(title)
if artist is not None and not artist == "":
self.artist.setText(f"By {current_track.tags.artist}")
self.artist.setText(f"By {artist}")
else:
self.artist.setText("No Artist")
if album is not None and not album == "":
self.album.setText(f"In {current_track.tags.album}")
self.album.setText(f"In {album}")
else:
self.album.setText("No Album")
cover = current_track.tags.images.any
cover = current_track.metadata.images.any
if cover is None:
self.track_cover.setPixmap(self.wobuzz_logo)