使用 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}')
相关推荐
渣渣xiong1 小时前
从零开始:前端转型AI agent直到就业第五十七天-第五十八天
前端·人工智能·python
吃好睡好便好1 小时前
创建魔方矩阵和单位矩阵
开发语言·人工智能·学习·线性代数·matlab·矩阵
影寂ldy2 小时前
C#数组的属性和方法(Clear / Copy / IndexOf )
开发语言·javascript·c#
i7i8i9com2 小时前
Hermes Agent 安装记录
开发语言·bash·hermes
小娄~~2 小时前
C语言卷子错题集
c语言·开发语言·数据结构
小L~~~2 小时前
基于贪心策略的混合遗传算法求解01背包问题
python·算法
才兄说2 小时前
机器人二次开发机器人动作定制?动作迁移数据优化
python
用户8356290780513 小时前
用 Python 实现 Excel 散点图绘制与定制
后端·python
PAK向日葵3 小时前
从零实现 Python 虚拟机(一):PVM 基本原理介绍
python
神所夸赞的夏天3 小时前
创建虚拟环境提示SSLError错误
python