Usage

Common interface

pytag defines a common list of tags (or comments) for all the supported audio formats. This tags are defined at pytag.constants.FIELD_NAMES and they are:

  • title
  • artist
  • album
  • date
  • tracknumber
  • organization
  • genre
  • performer

Using the common interface, doesn’t matter if we use want to read from a mp3 or from a ogg vorbis file. If an audio file contains other tags, they are ignored.

There also one extra field in an Audio object, mimetype, which contains the file mimetype

Reading metadata from multiple audio files:

from pytag import AudioReader, FormatNotSupportedError

files = ['audio.ogg', 'audio.mp3', 'image.png']

try:
    for file in files:
        audio = AudioReader(file)
        print (audio.get_tags())
except FormatNotSupportedError:
    print('Process other file...')

Note

If audio.ogg has a tag called band, this is ignored. If you want all the tags, use the Ogg vorbis interface. See: Vorbis comments

Writing metadata to an audio file:

from pytag import Audio

audio = Audio('music.ogg')
audio.write_tags({'album': 'cool', 'year': '2000'})

Note

Here only tag album is saved, year is ignored.

pytag.Audio extends pytag.AudioReader, with pytag.Audio is also possible read the tags. Class pytag.AudioReader is provided just to avoid write some metadata by mistake.

Vorbis comments

Using Vorbis comments is possible to save any metadata.

Writing and reading random tags:

>>> from pytag.format import OggVorbis
>>> vorbis = OggVorbis('music.ogg')
>>> vorbis.write_tags({'foo': 'bar'})
>>> vorbis.get_tags()
{'foo': 'bar'}

Note

Like pytag.Audio has a pytag.AudioReader only for reading, pytag.formats.OggVorbis also has a pytag.formats.OggVorbisReader which only is allow to read the comments.

Mp3 tags

Mp3 files uses ID3 to save the metadata. This format defines a list of codes for the valid tags. See Wikipedia ID3v2 Frames List

As this list is huge and many times confusing, I recommend use only the common interface to read/write Mp3 tags.

Useful Links

Previous topic

Introduction

Next topic

Supported formats

Table Of Contents

This Page