python的Mutagen的类的概况

本文介绍了Mutagen库,一个用于处理音频元数据的Python库。重点讲解了核心类如FileType, StreamInfo, Tags和Metadata的功能及使用方法,以及如何处理不同格式的音频文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Class Overview

The main notable classes in mutagen are FileType, StreamInfo, Tags, Metadata and for error handling the MutagenError exception.

类的概览

Mutagen的主要类有FileType, StreamInfo, Tags, Metadata和错误控制的MutagenError。

FileType

A FileType is used to represent container formats which contain audio/video and tags. In some cases, like MP4, the tagging format and the container are inseparable, while in other cases, like MP3, the container does not know about tags and the metadata is loosely attached to the file.

A FileType always represents only one combination of an audio stream (StreamInfo) and tags (Tags). In case there are multiple audio streams in one file, mutagen exposes the primary/first one. In case tags are associated with the audio/video stream the FileType represents the stream and its tags. Given a file containing Ogg Theora video with Vorbis audio, depending on whether you use oggvorbis.OggVorbis or oggtheora.OggTheora for parsing, you get the respective tags and stream info associated with either the vorbis or the theora stream.

It provides a dict-like interface which acts as a proxy to the containing Tags instance.

 

FileType是用来表示一个包含视频、音频或者标签的容器格式。在一些情况下,像MP4,它的标签格式和容量格式是密不可分的。但是在一下特殊情况,像MP3,容器不知道标签情况,相关元数据也只是弱关系地附在文件里。

FileType只会表示一种文件内含的音频流(StreamInfo)和标签(Tags)。有些文件内含多个音频流,Mutagen只会表示最主要的/第一个音频流。为了避免标签对不上音频/视频,FileType会一同表示其流媒体和标签。如果有一个文件,内含Ogg Theora格式的视频、Vorbis格式的音频,视乎你用oggvorbis.OggVorbis还是oggtheora.OggTheora来解析,你可以分别得到vorbis和theora流媒体及其对应的标签和流信息。它提供字典类的界面,可以通过键值来直接查询。

>>> from mutagen.oggvorbis import OggVorbis
>>> f = OggVorbis("11. The Way It Is.ogg")
>>> type(f)
<class 'mutagen.oggvorbis.OggVorbis'>
>>>

Tags

Each FileType has an attribute tags which holds a Tags instance. The Tags interface depends mostly on each format. It exposes a dict-like interface where the type of keys and values depends on the implementation of each format.

每个FileType都有一个tags的属性,tags属性拥有一个标签的实例。Tags的界面视乎其具体格式。它提供一个字典类的界面来查询

>>> type(f.tags)
<class 'mutagen.oggvorbis.OggVCommentDict'>
>>>

StreamInfo

Similar to Tags, a FileType also holds a StreamInfo instance. It represents one audio/video stream and contains its length, codec information, number of channels, etc.

 和Tags类似,FileType也拥有一个StreamInfo的实例。它表示一个音频/视频流和包含其长度、解码器信息和通道数量等。

>>> type(f.info)
<class 'mutagen.oggvorbis.OggVorbisInfo'>
>>> print(f.info.pprint())
Ogg Vorbis, 346.43 seconds, 499821 bps
>>>

Metadata

Metadata is a mixture between FileType and Tags and is used for tagging formats which are not depending on a container format. They can be attached to any file. This includes ID3 and APEv2.

Metadata是融合了FileType和Tags,用来直接记录标签数据,这个数据格式的不依赖于文件的原格式的,能被添加到任何文件。当然包括ID3和APEv2.

>>> from mutagen.id3 import ID3
>>> m = ID3("08. Firestarter.mp3")
>>> type(m)
<class 'mutagen.id3.ID3'>
>>>

MutagenError

The MutagenError exception is the base class for all custom exceptions in mutagen.

 MutagenError异常是Mutagen引发的异常的基础类

from mutagen import MutagenError

try:
    f = OggVorbis("11. The Way It Is.ogg")
except MutagenError:
    print("Loading failed :(")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值