testing id3 tag things

This commit is contained in:
billy 2025-04-09 13:00:06 -04:00
parent ea604e719f
commit 6ae97ad878
2 changed files with 39 additions and 31 deletions

View File

@ -34,6 +34,9 @@ def add_files_to_database(files, progress_callback=None):
filename = filepath.split("/")[-1]
audio, details = get_id3_tags(filepath)
print('got id3 tags')
print(type(audio))
print(audio)
if not isinstance(audio, ID3):
failed_dict[filepath] = details
continue

View File

@ -4,22 +4,8 @@ from mutagen.id3 import ID3
from mutagen.id3._frames import TIT2
from mutagen.id3._util import ID3NoHeaderError
def get_id3_tags(filename: str) -> tuple[ID3 | dict, str]:
"""
Get the ID3 tags for an audio file
Returns a tuple of:
- mutagen ID3 object OR python dictionary
- string reason for failure (failure = empty dict above)
Args
- filename
Returns
- tuple(ID3/dict, fail_reason)
"""
# debug(filename)
if filename.endswith(".mp3"):
def get_mp3_tags(filename: str) -> tuple[ID3 | dict, str]:
"""Get ID3 tags for mp3 file"""
try:
# Open the MP3 file and read its content
audio = ID3(filename)
@ -34,11 +20,12 @@ def get_id3_tags(filename: str) -> tuple[ID3 | dict, str]:
# title = filename without extension
title = os.path.splitext(os.path.basename(filename))[0]
list_of_id3_tags = list(audio.keys())
if "TIT2" in list_of_id3_tags:
if "TIT2" in list(audio.keys()):
audio.save()
return audio, ""
else:
# if title tag doesnt exist,
# create it and return
tit2_tag = TIT2(encoding=3, text=[title])
audio["TIT2"] = tit2_tag
# Save the updated tags
@ -46,8 +33,26 @@ def get_id3_tags(filename: str) -> tuple[ID3 | dict, str]:
return audio, ""
except Exception as e:
error(f"Could not assign file ID3 tag: {e}")
return {}, f"Could not assign ID3 tag to file: {e}"
return {}, "non mp3 file"
def get_id3_tags(filename: str) -> tuple[ID3 | dict, str]:
"""
Get the ID3 tags for an audio file
Returns a tuple of:
- mutagen ID3 object OR python dictionary
- string reason for failure (failure = empty dict above)
Args
- filename
Returns
- tuple(ID3/dict, fail_reason)
ID3 dict looks like this:
{'TIT2': TIT2(encoding=<Encoding.UTF8: 3>, text=['song title']), 'TSSE': TSSE(encoding=<Encoding.UTF8: 3>, text=['Lavf59.27.100'])}
"""
if filename.endswith(".mp3"):
tags, details = get_mp3_tags(filename)
else:
tags, details = {}, "non mp3 file"
return tags, details