python检查音频文件类型,MP3或FLAC [英] python check audio file type, MP3 or FLAC

查看:82
本文介绍了python检查音频文件类型,MP3或FLAC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查音频文件是否只需要检查其MP3或FLAC,但我想超越仅仅检查文件扩展名

I want to check if a audio file to see if its MP3 or FLAC the checks only have to be basic but I want to go beyond simply checking the file extensions

os.path.splitext

可以,但是如果文件中没有写入扩展名或有人通过了带有假扩展名的文件

Works okay but no good if the file doesn't have an extensions written in or someone passes a file with a fake extension

我已经尝试过,但是只会返回None

I've tried but it just returns None

sndhdr.what(file)

我也尝试使用魔术,但是它返回的'application/octet-stream'没什么用.

I've also tried using magic but it returns 'application/octet-stream' which isn't much use.

magic.from_file(file, mime=True)

我读过Mutagen可能对此有所帮助,但到目前为止,找不到任何将音频编码输出为MP3或FLAC的功能

I've read Mutagen could be good for this but so far failed to find any function that outputs the audio encoding as MP3 or FLAC

推荐答案

要查找文件格式,包括音频,视频,txt等,可以使用 fleep python库检查音频文件格式:

To find the File format including audio, video, txt and more, you can use fleep python library to check the audio file format:

首先,您需要安装该库:

First you need to install the library:

pip install fleep

这是一个测试代码:

import fleep

with open("vida.flac", "rb") as file:
    info = fleep.get(file.read(128))

print(info.type)
print(info.extension)
print(info.mime) 

以下是结果:

['audio']
['flac']
['audio/flac']

即使文件没有扩展名或有人通过带有假扩展名的文件,它也会检测并返回.

Even if the file doesn't have an extension or someone passes a file with a fake extension, it will detect and return.

在这里,我已将vida.wav文件复制到dar.txt,并删除了扩展名

Here I have copied the vida.wav file to dar.txt and also removed the extension

import fleep

with open("dar", "rb") as file:
    info = fleep.get(file.read(128))

print(info.type)
print(info.extension) 
print(info.mime) 

结果仍然相同.

['audio']
['flac']
['audio/flac']


归库作者所有 https://github.com/floyernick/fleep-py

这篇关于python检查音频文件类型,MP3或FLAC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆