audiocheck.py
· 2.1 KiB · Python
原始文件
import argparse
import os
import subprocess
import sys
# 定义ffmpeg支持的音频文件格式后缀列表
AUDIO_EXTENSIONS = (
'.aac', '.aiff', '.alac', '.amr', '.ape', '.au', '.flac',
'.m4a', '.mp3', '.ogg', '.opus', '.wav', '.wma', '.webm'
)
def check_audio_file(file_path, output_file):
try:
result = subprocess.run(
["ffmpeg", "-v", "error", "-i", file_path, "-f", "null", "-"],
stderr=subprocess.PIPE,
universal_newlines=True # 如果Python版本低于3.7,应使用text=True
)
error_output = result.stderr
if error_output:
with open(output_file, "a") as f:
f.write(f"文件损坏: {file_path}\n")
f.write("错误详情:\n")
f.write(error_output + "\n")
except subprocess.CalledProcessError as e:
with open(output_file, "a") as f:
f.write(f"检查文件时发生错误: {e}\n")
def walk_directory(directory, output_file):
for root, dirs, files in os.walk(directory):
for file in files:
if file.lower().endswith(AUDIO_EXTENSIONS):
file_path = os.path.join(root, file)
check_audio_file(file_path, output_file)
def main():
# 如果没有提供任何参数,直接显示帮助信息
if len(sys.argv) == 1:
sys.argv.append('-h')
parser = argparse.ArgumentParser(description="检查音频文件是否损坏")
parser.add_argument("-i", "--input", help="需要检测的文件夹路径", required=True)
parser.add_argument("-o", "--output", help="错误输出的文本文件路径(可选)")
args = parser.parse_args()
# 设置输出文件路径,如果未指定则使用脚本目录下的err.txt
script_dir = os.path.dirname(os.path.realpath(__file__)) if __file__ else '.'
output_file = args.output if args.output else os.path.join(script_dir, 'err.txt')
# 遍历指定文件夹
walk_directory(args.input, output_file)
print(f"所有文件检查完毕,详情请查看输出文件:{output_file}")
if __name__ == "__main__":
main()
| 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() |
| 56 |