python的Mutagen模块入门

原文:https://mutagen.readthedocs.io/en/latest/user/gettingstarted.html

The File functions takes any audio file, guesses its type and returns a FileType instance or None.

 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 "<stdin>", line 1, in <module>
  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"完整)。类似地,你指派一个字符串列表会比指定一个字符串要好。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值