Mutagen-Python Examples

  • MP3 加封面图片
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error

audio = MP3('example.mp3', ID3=ID3)

# add ID3 tag if it doesn't exist
try:
    audio.add_tags()
except error:
    pass

audio.tags.add(
    APIC(
        encoding=3, # 3 is for utf-8
        mime='image/png', # image/jpeg or image/png
        type=3, # 3 is for the cover image
        desc=u'Cover',
        data=open('example.png').read()
    )
)
audio.save()
  • 给音频加上ID3部分
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Imports
from mutagen.id3 import ID3, TRCK, TIT2, TPE1, TALB, TDRC, TCON, COMM

# Set file
audio = ID3("example.mp3")

# Add tags
audio.add(TRCK(encoding=3, text=u"1"))
audio.add(TIT2(encoding=3, text=u"Title"))
audio.add(TPE1(encoding=3, text=u"Artist"))
audio.add(TALB(encoding=3, text=u"Album"))
audio.add(TDRC(encoding=3, text=u"2011"))
audio.add(TCON(encoding=3, text=u"Genre"))
audio.add(COMM(encoding=3, lang="eng", desc="", text="Comment"))

# Save tags
audio.save()

# Print tags
print audio.pprint()

注意 上面这一段可以同样适用于MP3

  • 另一个给音频加上封面图片 (需要保证)
audio = ID3(u'P:/How Low_2.mp3')
pic = APIC(3, u'image/jpg', 3, u'Front cover', open(u'cover.jpg', 'rb').read())
audio.add(pic)
audio.save()

注意 使用上面这两段代码音频文件必须要有 ID3 标签部分(add_tags 过或者原来有)

API

import mutagen.[format]
metadata = mutagen.[format].Open(filename)

猜测文件类型来创建 mutagen.File(filename, options=None, easy=False)

基类

class mutagen.FileType(filename)

Frame

Attached picture 附加图片
4.14.   Attached picture

   This frame contains a picture directly related to the audio file.
   Image format is the MIME type and subtype [MIME] for the image. In
   the event that the MIME media type name is omitted, "image/" will be
   implied. The "image/png" [PNG] or "image/jpeg" [JFIF] picture format
   should be used when interoperability is wanted. Description is a
   short description of the picture, represented as a terminated
   text string. There may be several pictures attached to one file, each
   in their individual "APIC" frame, but only one with the same content
   descriptor. There may only be one picture with the picture type
   declared as picture type $01 and $02 respectively. There is the
   possibility to put only a link to the image file by using the 'MIME
   type' "-->" and having a complete URL [URL] instead of picture data.
   The use of linked files should however be used sparingly since there
   is the risk of separation of files.

     <Header for 'Attached picture', ID: "APIC">
     Text encoding      $xx
     MIME type          <text string> $00
     Picture type       $xx
     Description        <text string according to encoding> $00 (00)
     Picture data       <binary data>


   Picture type:  $00  Other
                  $01  32x32 pixels 'file icon' (PNG only)
                  $02  Other file icon
                  $03  Cover (front)
                  $04  Cover (back)
                  $05  Leaflet page
                  $06  Media (e.g. label side of CD)
                  $07  Lead artist/lead performer/soloist
                  $08  Artist/performer
                  $09  Conductor
                  $0A  Band/Orchestra
                  $0B  Composer
                  $0C  Lyricist/text writer
                  $0D  Recording Location
                  $0E  During recording
                  $0F  During performance
                  $10  Movie/video screen capture
                  $11  A bright coloured fish
                  $12  Illustration
                  $13  Band/artist logotype

APIC(text_encoding, mime_type, picture_type, description, picture_data)

Comments