Skip to main content

Simple interface to edit audio file metadata

Project description

music-tag

music-tag is a library for editing audio metadata with an interface that does not depend on the underlying file format. In other words, editing mp3 files shouldn not be any different than flac, m4a, ... This library is just a layer on top of mutagen, which does all the heavy lifting.

Formats

The following file formats are actively tested.

  • aac
  • aiff
  • dsf
  • flac
  • m4a
  • mp3
  • ogg
  • opus
  • wav
  • wv

Keys

Metadata is available using a dictionary-like interface with the following keys. Keys are not case sensitive and can contain arbitrary whitespace, '-', and '_' characters. In other words, Album Artist, album-artist, and album_artist are all synonyms for albumartist. Also, disk is synonymous with disc.

  • album
  • albumartist
  • artist
  • artwork
  • comment
  • compilation
  • composer
  • discnumber
  • genre
  • lyrics
  • totaldiscs
  • totaltracks
  • tracknumber
  • tracktitle
  • year

Examples

Reading tags

import music_tag

f = music_tag.load_file("music-tag/sample/440Hz.m4a")

# dict access returns a MetadataItem
title_item = f['title']

# MetadataItems keep track of multi-valued keys
title_item.values  # -> ['440Hz']

# A single value can be extracted
title_item.first  # -> '440Hz'
title_item.value  # -> '440Hz'

# MetadataItems can also be cast to a string
str(title_item)  # -> '440Hz'

Setting tags

# tags can be set as if the file were a dictionary
f['title'] = '440Hz'

# additional values can be appended to the tags
f.append_tag('title', 'subtitle')
title_item.values  # -> ['440Hz', 'subtitle']
title_item.first  # -> '440Hz'
title_item.value  # -> '440Hz, subtitle'
str(title_item)  # -> '440Hz, subtitle'

Removing tags

del f['title']
f.remove_tag('title')

Album artwork

Album artwork is wrapped in an object that keeps track of some of the extra metadata associated with images. Note that some album art functionality requires the Pillow (modern day PIL) library.

# get artwork
art = f['artwork']

# Note: `art` is a MetadataItem. Use ``art.value`` if there is
#       only one image embeded in the file. This will raise a
#       ValueError if there is more than one image. You can also
#       use ``art.first``, or iterate through ``art.values``.

art.first.mime  # -> 'image/jpeg'
art.first.width  # -> 1280
art.first.height  # -> 1280
art.first.depth  # -> 24
art.first.data  # -> b'... raw image data ...'

# set artwork
with open('music_tag/test/sample/imgA.jpg', 'rb') as img_in:
    f['artwork'] = img_in.read()
with open('music_tag/test/sample/imgB.jpg', 'rb') as img_in:
    f.append_tag('artwork', img_in.read())

# Make a thumbnail (requires Pillow)
art.first.thumbnail([64, 64])  # -> pillow image
art.first.raw_thumbnail([64, 64])  # -> b'... raw thumbnail data ...'

Saving tags

# finally, you can bounce the edits to disk
f.save()

Skipping Type Normalization

By default, tags are validated and normalized. For instance, track numbers and years are return as integers. Some tag formats store everything as strings to enable things like leading zeros in tracknumbers (i.e., track '01'). I think this is ugly, but you can use the file object's raw property if you like this kind of thing.

f.raw['tracknumber'] = '01'
f.raw['tracknumber'].value  # -> '01'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

music-tag-0.4.0.tar.gz (21.6 kB view details)

Uploaded Source

File details

Details for the file music-tag-0.4.0.tar.gz.

File metadata

  • Download URL: music-tag-0.4.0.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

File hashes

Hashes for music-tag-0.4.0.tar.gz
Algorithm Hash digest
SHA256 0e581df8364aa9bc178c525cb8e1b8d1555556111c85e9e0a1a07f6021d95e5b
MD5 3d9d75628f791708e170f53f0f45b68e
BLAKE2b-256 b90825dc14d5a4b8c6f034d24afa6e06400a28320a199f64db3f2c9cd22f805b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page