From 42f20fbc898dc6162bdd7b39b70cc124a3ceab3e Mon Sep 17 00:00:00 2001 From: 1826013250 <1826013250@qq.com> Date: Sun, 16 Apr 2023 14:00:41 +0800 Subject: [PATCH] Fix Load Error --- modules/functions/load_file_song.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/modules/functions/load_file_song.py b/modules/functions/load_file_song.py index d4604dc..d860f52 100644 --- a/modules/functions/load_file_song.py +++ b/modules/functions/load_file_song.py @@ -7,6 +7,7 @@ from multiprocessing import Process, Queue from queue import Empty from time import sleep +import mutagen.mp3 from progress.bar import Bar from Cryptodome.Cipher import AES from mutagen import File, flac @@ -19,7 +20,10 @@ from modules.utils.inputs import cinput, rinput def load_information_from_song(path) -> str | dict: """从音乐文件中的 Comment 字段获取 163 key 并解密返回歌曲信息""" - file = File(path) # 使用 mutagen 获取歌曲信息 + try: + file = File(path) # 使用 mutagen 获取歌曲信息 + except mutagen.mp3.HeaderNotFoundError: + return "not_a_music" if os.path.splitext(path)[-1] == ".mp3": # 当文件为 mp3 时使用 ID3 格式读取 if file.tags.get("COMM::XXX"): if file.tags["COMM::XXX"].text[0][:7] == "163 key": @@ -133,15 +137,15 @@ def load_and_decrypt_from_ncm(file_path, target_dir) -> dict: # nondanee的源 # 对解密后的文件进行信息补全 if meta_data["format"] == "mp3": # 针对 mp3 使用 ID3 进行信息补全 - audio = ID3(os.path.join(target_dir, os.path.splitext(file_path.split("/")[-1])[0] + ".mp3")) + audio = File(os.path.join(target_dir, os.path.splitext(file_path.split("/")[-1])[0] + ".mp3")) artists = [] for i in meta_data["artist"]: artists.append(i[0]) - audio["TPE1"] = TPE1(encoding=3, text=artists) # 插入歌手 - audio["APIC"] = APIC(encoding=3, mime='image/jpg', type=3, desc='', data=image_data) # 插入封面 - audio["COMM::XXX"] = COMM(encoding=3, lang='XXX', desc='', text=[comment.decode("utf-8")]) # 插入 163 key 注释 - audio["TIT2"] = TIT2(encoding=3, text=[meta_data["musicName"]]) # 插入歌曲名 - audio["TALB"] = TALB(encoding=3, text=[meta_data["album"]]) # 插入专辑名 + audio.tags["TPE1"] = TPE1(encoding=3, text=artists) # 插入歌手 + audio.tags["APIC"] = APIC(encoding=3, mime='image/jpg', type=3, desc='', data=image_data) # 插入封面 + audio.tags["COMM::XXX"] = COMM(encoding=3, lang='XXX', desc='', text=[comment.decode("utf-8")]) # 插入 163 key 注释 + audio.tags["TIT2"] = TIT2(encoding=3, text=[meta_data["musicName"]]) # 插入歌曲名 + audio.tags["TALB"] = TALB(encoding=3, text=[meta_data["album"]]) # 插入专辑名 audio.save() elif meta_data["format"] == "flac": # 针对 flac 使用 FLAC 进行信息补全 audio = flac.FLAC(os.path.join(target_dir, os.path.splitext(file_path.split("/")[-1])[0] + ".flac")) @@ -193,6 +197,9 @@ def get_lyric_from_folder(self): elif result == "not_a_normal_music": fails += 1 print(f"文件 \"{i}\" 内 163 key 不是一个普通音乐文件,这可能是一个电台曲目") + elif result == "not_a_music": + fails += 1 + print(f"文件 \"{i}\" 不是一个音乐文件,请检查该文件是否正常") else: musics.append({"id": result['musicId'], "name": result["musicName"], "artists": result["artist"]}) elif ext == ".ncm": # 对于 ncm 先加入到列表,等待解密