tag = eyeD3.Tag()
tag.link('/some/file.mp3')# no tag in this file, link returned False
tag.header.setVersion(eyeD3.ID3_V2_3)
tag.setArtist('Fugazi')
tag.update()
I used eyeD3 the other day with a lot of success. I found that it could add artwork to the ID3 tag which the other modules I looked at couldn’t. You’ll have to install using pip or download the tar and execute python setup.py install from the source folder.
Relevant examples from the website are below.
Reading the contents of an mp3 file containing either v1 or v2 tag info:
tag = eyeD3.Tag()
tag.link('/some/file.mp3') # no tag in this file, link returned False
tag.header.setVersion(eyeD3.ID3_V2_3)
tag.setArtist('Fugazi')
tag.update()
I’ve used mutagen to edit tags in media files before. The nice thing about mutagen is that it can handle other formats, such as mp4, FLAC etc. I’ve written several scripts with a lot of success using this API.
回答 2
问题eyed3在于它将抛出NotImplementedError("Unable to write ID3 v2.2")普通的MP3文件。
from ID3 import*try:
id3info = ID3('file.mp3')print id3info
# Change the tags
id3info['TITLE']="Green Eggs and Ham"
id3info['ARTIST']="Dr. Seuss"for k, v in id3info.items():print k,":", v
exceptInvalidTagError, message:print"Invalid ID3 tag:", message
What you’re after is the ID3 module. It’s very simple and will give you exactly what you need. Just copy the ID3.py file into your site-packages directory and you’ll be able to do something like the following:
from ID3 import *
try:
id3info = ID3('file.mp3')
print id3info
# Change the tags
id3info['TITLE'] = "Green Eggs and Ham"
id3info['ARTIST'] = "Dr. Seuss"
for k, v in id3info.items():
print k, ":", v
except InvalidTagError, message:
print "Invalid ID3 tag:", message
A simple example from the book Dive Into Python works ok for me, this is the download link, the example is fileinfo.py. Don’t know if it’s the best, but it can do the basic job.
from tinytag importTinyTag
tag =TinyTag.get('/some/music.mp3')
您可以使用TinyTag获得的可能属性的列表:
tag.album # album as string
tag.albumartist # album artist as string
tag.artist # artist name as string
tag.audio_offset # number of bytes before audio data begins
tag.bitrate # bitrate in kBits/s
tag.disc # disc number
tag.disc_total # the total number of discs
tag.duration # duration of the song in seconds
tag.filesize # file size in bytes
tag.genre # genre as string
tag.samplerate # samples per second
tag.title # title of the song
tag.track # track number as string
tag.track_total # total number of tracks as string
tag.year # year or data as string
After trying the simple pip install route for eyeD3, pytaglib, and ID3 modules recommended here, I found this fourth option was the only one to work. The rest had import errors with missing dependencies in C++ or something magic or some other library that pip missed. So go with this one for basic reading of ID3 tags (all versions):
from tinytag import TinyTag
tag = TinyTag.get('/some/music.mp3')
List of possible attributes you can get with TinyTag:
tag.album # album as string
tag.albumartist # album artist as string
tag.artist # artist name as string
tag.audio_offset # number of bytes before audio data begins
tag.bitrate # bitrate in kBits/s
tag.disc # disc number
tag.disc_total # the total number of discs
tag.duration # duration of the song in seconds
tag.filesize # file size in bytes
tag.genre # genre as string
tag.samplerate # samples per second
tag.title # title of the song
tag.track # track number as string
tag.track_total # total number of tracks as string
tag.year # year or data as string
An issue I encountered while trying to use eyed3 for the first time had to do with an import error of libmagic even though it was installed. To fix this install the magic-bin whl from here
It can depend on exactly what you want to do in addition to reading the metadata. If it is just simply the bitrate / name etc. that you need, and nothing else, something lightweight is probably best.
If you’re manipulating the mp3 past that PyMedia may be suitable.
There are quite a few, whatever you do get, make sure and test it out on plenty of sample media. There are a few different versions of ID3 tags in particular, so make sure it’s not too out of date.
Personally I’ve used this small MP3Info class with luck. It is quite old though.
After some initial research I thought songdetails might fit my use case, but it doesn’t handle .m4b files. Mutagen does. Note that while some have (reasonably) taken issue with Mutagen’s surfacing of format-native keys, that vary from format to format (TIT2 for mp3, title for ogg, \xa9nam for mp4, Title for WMA etc.), mutagen.File() has a (new?) easy=True parameter that provides EasyMP3/EasyID3 tags, which have a consistent, albeit limited, set of keys. I’ve only done limited testing so far, but the common keys, like album, artist, albumartist, genre, tracknumber, discnumber, etc. are all present and identical for .mb4 and .mp3 files when using easy=True, making it very convenient for my purposes.
import eyed3
import os
for root, dirs, files in os.walk(folderp):for file in files:try:if file.find(".mp3")<0:continue
path = os.path.abspath(os.path.join(root , file))
t = eyed3.load(path)print(t.tag.title , t.tag.artist)#print(t.getArtist())exceptExceptionas e:print(e)continue
import eyed3
import os
for root, dirs, files in os.walk(folderp):
for file in files:
try:
if file.find(".mp3") < 0:
continue
path = os.path.abspath(os.path.join(root , file))
t = eyed3.load(path)
print(t.tag.title , t.tag.artist)
#print(t.getArtist())
except Exception as e:
print(e)
continue
from tinytag importTinyTag
fileNameL ='''0bd1ab5f-e42c-4e48-a9e6-b485664594c1.mp3
0ea292c0-2c4b-42d4-a059-98192ac8f55c.mp3
1c49f6b7-6f94-47e1-a0ea-dd0265eb516c.mp3
5c706f3c-eea4-4882-887a-4ff71326d284.mp3
'''.split()for fn in fileNameL:
fpath ='./data/'+fn
tag =TinyTag.get(fpath)print()print('"artist": "%s",'% tag.artist)print('"album": "%s",'% tag.album)print('"title": "%s",'% tag.title)print('"duration(secs)": "%s",'% tag.duration)
结果
JoeTagPj>python joeTagTest.py
"artist":"Conan O’Brien Needs A Friend","album":"Conan O’Brien Needs A Friend","title":"17. Thomas Middleditch and Ben Schwartz","duration(secs)":"3565.1829583532785","artist":"Conan O’Brien Needs A Friend","album":"Conan O’Brien Needs A Friend","title":"Are you ready to make friends?","duration(secs)":"417.71840447045264","artist":"Conan O’Brien Needs A Friend","album":"Conan O’Brien Needs A Friend","title":"Introducing Conan’s new podcast","duration(secs)":"327.22187551899646","artist":"Conan O’Brien Needs A Friend","album":"Conan O’Brien Needs A Friend","title":"19. Ray Romano","duration(secs)":"3484.1986772305863",
C:\1d\PodcastPjs\JoeTagPj>