mutagen

Mutagen是用于处理音频元数据的python模块。它支持ASF、FLAC、MP4、Monkey’s Audio、MP3、Musepack、Ogg Opus、Ogg FLAC、Ogg Speex、Ogg Theora、Ogg Vorbis、True Audio、WavPack、OptimFROG和AIFF 音频文件。 支持所有版本的ID3v2和能解释所有标准的ID3v2框架。它能读取Xing头部信息来精确计算MP3文件的比特率和长度。无论哪种音频格式的ID3和APEv2标签都可以编辑。它还能在独立包层面修改Ogg格式流。

Mutagen兼容linux、windows和macOS平台上的Python 2.7, 3.4+ (CPython and PyPy),需要python标准库的支持。Mutagen遵守GPL2及之后的协议。

Installing
安装

pip install mutagen
or 或者

sudo apt-get install python-mutagen python3-mutagen

File函数可以输入任意音频文件,然后分析其可能的类型,并输出一个文件类型或者None。

import mutagen
mutagen.File(“11. The Way It Is.ogg”)
{‘album’: [u’Always Outnumbered, Never Outgunned’],
‘title’: [u’The Way It Is’], ‘artist’: [u’The Prodigy’],
‘tracktotal’: [u’12’], ‘albumartist’: [u’The Prodigy’],‘date’: [u’2004’],
‘tracknumber’: [u’11’],

_.info.pprint()
u’Ogg Vorbis, 346.43 seconds, 499821 bps’

The following code loads a FLAC file, sets its title, prints all tag data, then saves the file.

下面的代码是载入一个flac文件,然后重新写入’title’标签内容,打印所有标签内容,最好保存文件

from mutagen.flac import FLAC
audio = FLAC(“example.flac”)
audio[“title”] = u"An example"
audio.pprint()
audio.save()
The following example gets the length and bitrate of an MP3 file

下面的例子是获得mp3文件的长度和比特率

from mutagen.mp3 import MP3

audio = MP3(“example.mp3”)
print(audio.info.length)
print(audio.info.bitrate)
The following deletes an ID3 tag from an MP3 file

下面例子是删除mp3文件的id3标签

from mutagen.id3 import ID3

audio = ID3(“example.mp3”)
audio.delete()
Here we parse a Vorbis file as FLAC, which leads to an MutagenError being raised.

下面我们尝试用flac解释一个Vorbis文件,这时候会导致MutagenError错误发生

import mutagen.flac
mutagen.flac.FLAC(“11. The Way It Is.ogg”)
Traceback (most recent call last):
File “”, line 1, in
File “/usr/lib/python2.7/dist-packages/mutagen/_file.py”, line 42, in init
self.load(filename, *args, **kwargs)
File “/usr/lib/python2.7/dist-packages/mutagen/flac.py”, line 759, in load
self.__check_header(fileobj)
File “/usr/lib/python2.7/dist-packages/mutagen/flac.py”, line 867, in __check_header
“%r is not a valid FLAC file” % fileobj.name)
mutagen.flac.FLACNoHeaderError: ‘11. The Way It Is.ogg’ is not a valid FLAC file
Unicode

Mutagen has full Unicode support for all formats. When you assign text strings, we strongly recommend using Python unicode objects rather than str objects. If you use str objects, Mutagen will assume they are in UTF-8 (This does not apply to filenames)

Mutagen对所有格式都全编码支持。当你需要指派一个字符串时候,我们强烈建议你采用python的unicode对象,而不是str对象。如果你用str对象,Mutagen会默认假设他们是UTF-8编码(UTF-8不支持应用在文件名)

Multiple Values

Most tag formats support multiple values for each key, so when you access then (e.g. audio[“title”]) you will get a list of strings rather than a single one ([u"An example"] rather than u"An example"). Similarly, you can assign a list of strings rather than a single one.

大部分标签格式都支持单个键对于复数的值。所以当你访问这些内容时(例如:audio[“title”),你会得到一个字符串的列表,而不是一个字符串。([u"An example"] 比 u"An example"完整)。类似地,你指派一个字符串列表会比指定一个字符串要好。

为什么选择Mutagen

Quod Libet比其它程序有更多的处理标签的需求。因此我们觉得有必要自己写一个模块。

1、Mutagen有一个简单的API,几乎对标签格式和版本都一样,并整合进了python的内建类别和界面。

New frame types and file formats are easily added, and the behavior of the current formats can be changed by extending them.

2、新的框架和文件格式可以很容易就被添加进文件,且可以很容易通过扩展修改当前的格式

Freeform keys, multiple values, Unicode, and other advanced features were considered from the start and are fully supported.

3、完全支持任意形式的键值、复数的数值、编码和其它高级特性

All ID3v2 versions and all ID3v2.4 frames are covered, including rare ones like POPM or RVA2.

4、覆盖所有版本的ID3v2标签和所有ID3v2.4框架,包括不常见的POPM或者RVA2

We take automated testing very seriously. All bug fixes are committed with a test that prevents them from recurring, and new features are committed with a full test suite.

5、我们对待自动化测试很认真。所有bug的修复都会经过测试,以防再次出现。新功能推出前都要经过一整套测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值