fix: 解决歌曲信息乱码问题
This commit is contained in:
parent
44860d495e
commit
329c6b26bd
@ -21,8 +21,9 @@ async def test_one_music(filename):
|
|||||||
# 获取播放时长
|
# 获取播放时长
|
||||||
try:
|
try:
|
||||||
metadata = get_audio_metadata(filename)
|
metadata = get_audio_metadata(filename)
|
||||||
|
print(metadata.title, metadata.album)
|
||||||
if metadata:
|
if metadata:
|
||||||
lyrics = metadata.get("lyrics")
|
lyrics = metadata.lyrics
|
||||||
if lyrics:
|
if lyrics:
|
||||||
print(f"歌曲 : {filename} 的 {lyrics}")
|
print(f"歌曲 : {filename} 的 {lyrics}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -37,6 +38,9 @@ async def main(directory):
|
|||||||
for _, files in local_musics.items():
|
for _, files in local_musics.items():
|
||||||
for file in files:
|
for file in files:
|
||||||
await test_one_music(file)
|
await test_one_music(file)
|
||||||
|
pass
|
||||||
|
|
||||||
|
await test_one_music("./music/一生何求.mp3")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -491,20 +491,20 @@ def chinese_to_number(chinese):
|
|||||||
|
|
||||||
|
|
||||||
def get_audio_metadata(file_path):
|
def get_audio_metadata(file_path):
|
||||||
|
ret = Metadata()
|
||||||
if file_path.endswith(".mp3"):
|
if file_path.endswith(".mp3"):
|
||||||
return get_mp3_metadata(file_path)
|
ret = get_mp3_metadata(file_path)
|
||||||
elif file_path.endswith(".flac"):
|
elif file_path.endswith(".flac"):
|
||||||
return get_flac_metadata(file_path)
|
ret = get_flac_metadata(file_path)
|
||||||
elif file_path.endswith(".wav"):
|
elif file_path.endswith(".wav"):
|
||||||
return get_wav_metadata(file_path)
|
ret = get_wav_metadata(file_path)
|
||||||
elif file_path.endswith(".ape"):
|
elif file_path.endswith(".ape"):
|
||||||
return get_ape_metadata(file_path)
|
ret = get_ape_metadata(file_path)
|
||||||
elif file_path.endswith(".ogg"):
|
elif file_path.endswith(".ogg"):
|
||||||
return get_ogg_metadata(file_path)
|
ret = get_ogg_metadata(file_path)
|
||||||
elif file_path.endswith(".m4a"):
|
elif file_path.endswith(".m4a"):
|
||||||
return get_m4a_metadata(file_path)
|
ret = get_m4a_metadata(file_path)
|
||||||
else:
|
return ret
|
||||||
raise ValueError("Unsupported file type")
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -524,12 +524,28 @@ def get_mp3_metadata(file_path):
|
|||||||
if tags is None:
|
if tags is None:
|
||||||
return Metadata()
|
return Metadata()
|
||||||
|
|
||||||
|
# 处理编码
|
||||||
|
def get_tag_value(tags, k):
|
||||||
|
if k not in tags:
|
||||||
|
return ""
|
||||||
|
v = tags[k]
|
||||||
|
if isinstance(v, mutagen.id3.TextFrame) and not isinstance(
|
||||||
|
v, mutagen.id3.TimeStampTextFrame
|
||||||
|
):
|
||||||
|
old_ts = "".join(v.text)
|
||||||
|
if v.encoding == mutagen.id3.Encoding.LATIN1:
|
||||||
|
bs = old_ts.encode("latin1")
|
||||||
|
ts = bs.decode("GBK", errors="ignore")
|
||||||
|
return ts
|
||||||
|
return old_ts
|
||||||
|
return v
|
||||||
|
|
||||||
metadata = Metadata(
|
metadata = Metadata(
|
||||||
title=tags.get("TIT2", [""])[0] if "TIT2" in tags else "",
|
title=get_tag_value(tags, "TIT2"),
|
||||||
artist=tags.get("TPE1", [""])[0] if "TPE1" in tags else "",
|
artist=get_tag_value(tags, "TPE1"),
|
||||||
album=tags.get("TALB", [""])[0] if "TALB" in tags else "",
|
album=get_tag_value(tags, "TALB"),
|
||||||
year=tags.get("TDRC", [""])[0] if "TDRC" in tags else "",
|
year=get_tag_value(tags, "TDRC"),
|
||||||
genre=tags.get("TCON", [""])[0] if "TCON" in tags else "",
|
genre=get_tag_value(tags, "TCON"),
|
||||||
)
|
)
|
||||||
|
|
||||||
for tag in tags.values():
|
for tag in tags.values():
|
||||||
|
Loading…
Reference in New Issue
Block a user