This commit is contained in:
billy@pom 2026-03-01 21:56:42 -05:00
parent 34dde803ce
commit d76bd67311
3 changed files with 14 additions and 11 deletions

View File

@ -42,7 +42,7 @@ class LyricsWindow(QDialog):
"""Saves the current lyrics text to the USLT/lyrics ID3 tag""" """Saves the current lyrics text to the USLT/lyrics ID3 tag"""
success = set_tag( success = set_tag(
filepath=self.song_filepath, filepath=self.song_filepath,
tag_name="lyrics", db_column="lyrics",
value=self.input_field.toPlainText(), value=self.input_field.toPlainText(),
) )
if success: if success:

View File

@ -130,7 +130,9 @@ class MetadataWindow(QDialog):
# Update the ID3 tag if the tag is not blank, # Update the ID3 tag if the tag is not blank,
# and has been edited # and has been edited
success = set_tag( success = set_tag(
filepath=song[0], db_column=tag, value=field.text() filepath=song[0],
db_column=self.headers.frame_id[tag].db,
value=field.text(),
) )
if success: if success:
update_song_in_database( update_song_in_database(

View File

@ -5,20 +5,21 @@ from mutagen.id3 import ID3
from mutagen.id3._util import ID3NoHeaderError from mutagen.id3._util import ID3NoHeaderError
from mutagen.id3._frames import USLT, Frame from mutagen.id3._frames import USLT, Frame
def set_tag(filepath: str, tag_name: str, value: str):
def set_tag(filepath: str, db_column: str, value: str):
""" """
Sets the ID3 tag for a file given a filepath, tag_name, and a value for the tag Sets the ID3 tag for a file given a filepath, db_column, and a value for the tag
Args: Args:
filepath: path to the mp3 file filepath: path to the mp3 file
tag_name: db column name of the ID3 tag db_column: db column name of the ID3 tag
value: value to set for the tag value: value to set for the tag
Returns: Returns:
True / False True / False
""" """
headers = HeaderTags2() headers = HeaderTags2()
debug(f"filepath: {filepath} | tag_name: {tag_name} | value: {value}") debug(f"filepath: {filepath} | db_column: {db_column} | value: {value}")
try: try:
try: # Load existing tags try: # Load existing tags
@ -26,7 +27,7 @@ def set_tag(filepath: str, tag_name: str, value: str):
except ID3NoHeaderError: # Create new tags if none exist except ID3NoHeaderError: # Create new tags if none exist
audio_file = ID3() audio_file = ID3()
# Lyrics get handled differently # Lyrics get handled differently
if tag_name == "lyrics": if db_column == "lyrics":
try: try:
audio = ID3(filepath) audio = ID3(filepath)
except Exception as e: except Exception as e:
@ -38,14 +39,14 @@ def set_tag(filepath: str, tag_name: str, value: str):
audio.save() audio.save()
return True return True
# DB Tag into Mutagen Frame Class # DB Tag into Mutagen Frame Class
if tag_name in headers.db: if db_column in headers.db:
frame_class = headers.db[tag_name].frame_class frame_class = headers.db[db_column].frame_class
assert frame_class is not None # ooo scary assert frame_class is not None # ooo scary
if issubclass(frame_class, Frame): if issubclass(frame_class, Frame):
frame = frame_class(encoding=3, text=[value]) frame = frame_class(encoding=3, text=[value])
audio_file.add(frame) audio_file.add(frame)
else: else:
warning(f'Tag "{tag_name}" not found - ID3 tag update skipped') warning(f'Tag "{db_column}" not found - ID3 tag update skipped')
audio_file.save(filepath) audio_file.save(filepath)
return True return True
except Exception as e: except Exception as e: