使用 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}')
相关推荐
dev派9 小时前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain
明月_清风11 小时前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python
曲幽20 小时前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
用户8356290780511 天前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon1 天前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly1 天前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程1 天前
一天一个Python库:pyarrow - 大规模数据处理的利器
python
Flittly1 天前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(4)Subagents (子智能体)
python·agent
明月_清风1 天前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python
明月_清风1 天前
打破“死亡环联”:深挖 Python 分代回收与垃圾回收(GC)机制
后端·python