diff --git a/utils/add_files_to_database.py b/utils/add_files_to_database.py index 1fe6b82..24d5fcd 100644 --- a/utils/add_files_to_database.py +++ b/utils/add_files_to_database.py @@ -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 diff --git a/utils/get_id3_tags.py b/utils/get_id3_tags.py index 85be3e3..9d7e821 100644 --- a/utils/get_id3_tags.py +++ b/utils/get_id3_tags.py @@ -4,6 +4,36 @@ from mutagen.id3 import ID3 from mutagen.id3._frames import TIT2 from mutagen.id3._util import ID3NoHeaderError +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) + except ID3NoHeaderError: + audio = ID3() + + try: + if os.path.exists(filename): + audio.save(os.path.abspath(filename)) + + # NOTE: If 'TIT2' tag is not set, we add it with a default value + # title = filename without extension + + title = os.path.splitext(os.path.basename(filename))[0] + 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 + audio.save() + return audio, "" + + except Exception as e: + return {}, f"Could not assign ID3 tag to file: {e}" def get_id3_tags(filename: str) -> tuple[ID3 | dict, str]: """ @@ -16,38 +46,13 @@ def get_id3_tags(filename: str) -> tuple[ID3 | dict, str]: - filename Returns - tuple(ID3/dict, fail_reason) + ID3 dict looks like this: + {'TIT2': TIT2(encoding=, text=['song title']), 'TSSE': TSSE(encoding=, text=['Lavf59.27.100'])} """ - # debug(filename) - if filename.endswith(".mp3"): - try: - # Open the MP3 file and read its content - audio = ID3(filename) - except ID3NoHeaderError: - audio = ID3() - - try: - if os.path.exists(filename): - audio.save(os.path.abspath(filename)) - - # NOTE: If 'TIT2' tag is not set, we add it with a default value - # 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: - audio.save() - return audio, "" - else: - tit2_tag = TIT2(encoding=3, text=[title]) - audio["TIT2"] = tit2_tag - # Save the updated tags - audio.save() - 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" + tags, details = get_mp3_tags(filename) + else: + tags, details = {}, "non mp3 file" + return tags, details