使用 python ffmpeg 批量检查 音频文件 是否损坏或不完整

自用工具,检查下载的音乐是否有损坏 或 下载不完整

使用方法,把 in_dir = r'D:\158首无损珍藏版' 改成你自己的音乐文件夹路径

如果发现文件有损坏,则会在命令行打印错误文件的路径

注意,要求 ffmpeg 命令可以直接在命令行调用

实现原理,使用 ffmpeg 解码时,会在 stderr 打印错误信息的特性,检测 stderr 里面是否有失败相关的关键字,从而判断媒体文件是否损坏

python 复制代码
import os
from glob import glob
import subprocess
import locale


in_dir = r'D:\158首无损珍藏版'

audio_exts = ('.wav', '.flac', '.mp3', '.wma', '.ogg', '.m4a', '.ape', '.opus', '.aac', '.mka')


def is_bad_file(file):
    p = subprocess.Popen(f'ffmpeg -i "{file}" -v error -f null -', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    err_text = p.stderr.read().decode(locale.getpreferredencoding()).lower()

    is_bad = False

    for bad_text in ['error', 'failed', 'illegal']:
        if bad_text in err_text:
            is_bad = True

    return is_bad


for file in glob(f'{in_dir}/**/*.*', recursive=True):
    ext = os.path.splitext(file)[1].lower()
    if ext not in audio_exts:
        continue

    if is_bad_file(file):
        print(f'Found bad audio. {file}')
相关推荐
cup1114 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi0016 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵18 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf18 小时前
Agent 流程编排
后端·python·agent
copyer_xyf19 小时前
Agent RAG
后端·python·agent
copyer_xyf19 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf19 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵1 天前
用 Pygame 实现 15 puzzle
python·数学·游戏