使用 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}')
相关推荐
65岁退休Coder2 小时前
LangGraph v1.2.9 节点容错策略 & 流式输出 & 持久化记忆管理
后端·python·langchain
ikun_文4 小时前
Django框架路由Router的使用
python·pycharm·django
IvanCodes4 小时前
Python 基础语法(二):字符串与常用操作
python
昭昭日月明4 小时前
LangChain 生态:从链到代理,开发者需要掌握的三大核心
python·langchain·agent
Csvn4 小时前
🐍 Day 8:面向对象编程
后端·python
程序员天天困4 小时前
向量检索不准怎么办:混合检索与 Rerank 重排序召回优化实战
后端·python·ai编程
alphaTao6 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
苏灿烤鱼6 小时前
当 AI Agent 遇见真实科学环境:深度拆解 Scientific Agent Skills,把"聊天机器人"变成"AI 科学家"
python·开源·agent
张文君6 小时前
ubuntu26.04坏道坏块分区隔离急速版260831-V0.12
linux·python
Interview Aid1126 小时前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展