Zuletzt aktiv 1722360234

xym's Avatar xym hat die Gist bearbeitet 1722360233. Zu Änderung gehen

1 file changed, 55 insertions

audiocheck.py(Datei erstellt)

@@ -0,0 +1,55 @@
1 + import argparse
2 + import os
3 + import subprocess
4 + import sys
5 +
6 + # 定义ffmpeg支持的音频文件格式后缀列表
7 + AUDIO_EXTENSIONS = (
8 + '.aac', '.aiff', '.alac', '.amr', '.ape', '.au', '.flac',
9 + '.m4a', '.mp3', '.ogg', '.opus', '.wav', '.wma', '.webm'
10 + )
11 +
12 + def check_audio_file(file_path, output_file):
13 + try:
14 + result = subprocess.run(
15 + ["ffmpeg", "-v", "error", "-i", file_path, "-f", "null", "-"],
16 + stderr=subprocess.PIPE,
17 + universal_newlines=True # 如果Python版本低于3.7,应使用text=True
18 + )
19 + error_output = result.stderr
20 + if error_output:
21 + with open(output_file, "a") as f:
22 + f.write(f"文件损坏: {file_path}\n")
23 + f.write("错误详情:\n")
24 + f.write(error_output + "\n")
25 + except subprocess.CalledProcessError as e:
26 + with open(output_file, "a") as f:
27 + f.write(f"检查文件时发生错误: {e}\n")
28 +
29 + def walk_directory(directory, output_file):
30 + for root, dirs, files in os.walk(directory):
31 + for file in files:
32 + if file.lower().endswith(AUDIO_EXTENSIONS):
33 + file_path = os.path.join(root, file)
34 + check_audio_file(file_path, output_file)
35 +
36 + def main():
37 + # 如果没有提供任何参数,直接显示帮助信息
38 + if len(sys.argv) == 1:
39 + sys.argv.append('-h')
40 +
41 + parser = argparse.ArgumentParser(description="检查音频文件是否损坏")
42 + parser.add_argument("-i", "--input", help="需要检测的文件夹路径", required=True)
43 + parser.add_argument("-o", "--output", help="错误输出的文本文件路径(可选)")
44 + args = parser.parse_args()
45 +
46 + # 设置输出文件路径,如果未指定则使用脚本目录下的err.txt
47 + script_dir = os.path.dirname(os.path.realpath(__file__)) if __file__ else '.'
48 + output_file = args.output if args.output else os.path.join(script_dir, 'err.txt')
49 +
50 + # 遍历指定文件夹
51 + walk_directory(args.input, output_file)
52 + print(f"所有文件检查完毕,详情请查看输出文件:{output_file}")
53 +
54 + if __name__ == "__main__":
55 + main()
Neuer Älter