使用 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}')
相关推荐
Wang's Blog5 分钟前
Java 项目实战: 交友项目 - FastDFS:入门案例
java·开发语言·交友
apcipot_rain1 小时前
C++转Java日记(03)
java·开发语言·c++
布莱克6051 小时前
C++ 中 map 的 insert 与 emplace:key 已存在时会覆盖吗?
开发语言·数据结构·c++·算法·map
xys_6781 小时前
深挖C++:类和对象(中)
开发语言·c++
灿飞屋2 小时前
Rust 编程其实很简单
开发语言·rust
MOONICK3 小时前
QT元数据
开发语言·qt
卷无止境4 小时前
RAGFlow 项目全景报告:从文档解析引擎到 Agent 原生数据库
后端·python
D e re k ZH4 小时前
C++ 系统学习手记:从语法细节到工程实践
开发语言·数据结构·c++·算法
灿飞屋5 小时前
Kotlin与Java虚拟机三:控制流
android·java·开发语言·后端·python·kotlin·安卓
benchmark_cc5 小时前
单只股票五档盘口接入个人研究仓库:QuantDash 读取、校验与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash