testing id3 tag things
This commit is contained in:
parent
ea604e719f
commit
6ae97ad878
@ -34,6 +34,9 @@ def add_files_to_database(files, progress_callback=None):
|
|||||||
filename = filepath.split("/")[-1]
|
filename = filepath.split("/")[-1]
|
||||||
|
|
||||||
audio, details = get_id3_tags(filepath)
|
audio, details = get_id3_tags(filepath)
|
||||||
|
print('got id3 tags')
|
||||||
|
print(type(audio))
|
||||||
|
print(audio)
|
||||||
if not isinstance(audio, ID3):
|
if not isinstance(audio, ID3):
|
||||||
failed_dict[filepath] = details
|
failed_dict[filepath] = details
|
||||||
continue
|
continue
|
||||||
|
|||||||
@ -4,22 +4,8 @@ from mutagen.id3 import ID3
|
|||||||
from mutagen.id3._frames import TIT2
|
from mutagen.id3._frames import TIT2
|
||||||
from mutagen.id3._util import ID3NoHeaderError
|
from mutagen.id3._util import ID3NoHeaderError
|
||||||
|
|
||||||
|
def get_mp3_tags(filename: str) -> tuple[ID3 | dict, str]:
|
||||||
def get_id3_tags(filename: str) -> tuple[ID3 | dict, str]:
|
"""Get ID3 tags for mp3 file"""
|
||||||
"""
|
|
||||||
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"):
|
|
||||||
try:
|
try:
|
||||||
# Open the MP3 file and read its content
|
# Open the MP3 file and read its content
|
||||||
audio = ID3(filename)
|
audio = ID3(filename)
|
||||||
@ -34,11 +20,12 @@ def get_id3_tags(filename: str) -> tuple[ID3 | dict, str]:
|
|||||||
# title = filename without extension
|
# title = filename without extension
|
||||||
|
|
||||||
title = os.path.splitext(os.path.basename(filename))[0]
|
title = os.path.splitext(os.path.basename(filename))[0]
|
||||||
list_of_id3_tags = list(audio.keys())
|
if "TIT2" in list(audio.keys()):
|
||||||
if "TIT2" in list_of_id3_tags:
|
|
||||||
audio.save()
|
audio.save()
|
||||||
return audio, ""
|
return audio, ""
|
||||||
else:
|
else:
|
||||||
|
# if title tag doesnt exist,
|
||||||
|
# create it and return
|
||||||
tit2_tag = TIT2(encoding=3, text=[title])
|
tit2_tag = TIT2(encoding=3, text=[title])
|
||||||
audio["TIT2"] = tit2_tag
|
audio["TIT2"] = tit2_tag
|
||||||
# Save the updated tags
|
# Save the updated tags
|
||||||
@ -46,8 +33,26 @@ def get_id3_tags(filename: str) -> tuple[ID3 | dict, str]:
|
|||||||
return audio, ""
|
return audio, ""
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error(f"Could not assign file ID3 tag: {e}")
|
|
||||||
return {}, f"Could not assign ID3 tag to file: {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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user