[英]How to get detail (Title,Artist) from .mp3 files in python using eyed3
這是我的代碼
import eyed3
audiofile = eyed3.load("19 Calvin Harris - Summer.mp3")
print(audiofile.tag.artist)
這是一個錯誤
Traceback (most recent call last):
File "C:\Python34\testmp3.py", line 5, in <module>
print(audiofile.tag.artist)
AttributeError: 'NoneType' object has no attribute 'artist'
在Visual Studio中顯示了屬性。 但是當我運行它時發生錯誤
當我寫print(audiofile)
時候。 我不知道為什么PS。 Python 3.4。
我認為問題出在模塊內。
我使用以下代碼進行了一些調試:
from eyed3 import id3
tag = id3.Tag()
tag.parse("myfile.mp3")
print(tag.artist)
在解析函數中,將打開文件,然后將其傳遞到_loadV2Tag(fileobject)。 然后,模塊讀取文件頭的前幾行,並檢查其是否以ID3開頭。
if f.read(3) != "ID3":
return False
在這里它返回false,我認為這是錯誤所在,因為如果我自己嘗試讀取標頭,則絕對是ID3。
>>> f = open("myfile.mp3", "rb")
>>> print(f.read(3))
b'ID3'
但是,根據https://bitbucket.org/nicfit/eyed3/issues/25/python-3-compatibilty (位於此處提供),直到版本0.8才有望獲得完整的python3支持: https : //bitbucket.org/nicfit/ eyed3 / branch / py3
試試這個代碼,對我有用
import eyed3
def show_info():
audio = eyed3.load("[PATH_TO_MP3]")
print audio.tag.artist
print audio.tag.album
print audio.tag.title
show_info()
標題和藝術家可以通過Tag()
返回值的訪問器函數獲得。 下面的示例演示如何使用getArtist()
和getTitle()
方法獲取它們。
import eyed3
tag = eyed3.Tag()
tag.link("/some/file.mp3")
print tag.getArtist()
print tag.getTitle()
嘗試這個:
if audiofile.tag is None:
audiofile.tag = eyed3.id3.Tag()
audiofile.tag.file_info = eyed3.id3.FileInfo("foo.id3")
audiofile.tag.artist=unicode(artist, "utf-8")
對於Python 3,情況發生了變化,我的代碼在Mac上運行。
@yask是正確的,因為您應該檢查不存在的值,這是我的示例:
復制並粘貼,並根據需要調整路徑,並可以在文件路徑循環中使用。
"""PlaceHolder."""
import re
from os import path as ospath
from eyed3 import id3
current_home = ospath.expanduser('~')
file_path = ospath.join(current_home,
'Music',
'iTunes',
'iTunes Media',
'Music',
'Aerosmith',
'Big Ones',
'01 Walk On Water.mp3',
)
def read_id3_artist(audio_file):
"""Module to read MP3 Meta Tags.
Accepts Path like object only.
"""
filename = audio_file
tag = id3.Tag()
tag.parse(filename)
# =========================================================================
# Set Variables
# =========================================================================
artist = tag.artist
title = tag.title
track_path = tag.file_info.name
# =========================================================================
# Check Variables Values & Encode Them and substitute back-ticks
# =========================================================================
if artist is not None:
artist.encode()
artistz = re.sub(u'`', u"'", artist)
else:
artistz = 'Not Listed'
if title is not None:
title.encode()
titlez = re.sub(u'`', u"'", title)
else:
titlez = 'Not Listed'
if track_path is not None:
track_path.encode()
track_pathz = re.sub(u'`', u"'", track_path)
else:
track_pathz = ('Not Listed, and you have an the worst luck, '
'because this is/should not possible.')
# =========================================================================
# print them out
# =========================================================================
try:
if artist is not None and title is not None and track_path is not None:
print('Artist: "{}"'.format(artistz))
print('Track : "{}"'.format(titlez))
print('Path : "{}"'.format(track_pathz))
except Exception as e:
raise e
read_id3_artist(file_path)
# Show Case:
# Artist: "Aerosmith"
# Track : "Walk On Water"
# Path : "/Users/MyUserName/Music/iTunes/iTunes Media/Music/Aerosmith/Big Ones/01 Walk On Water.mp3" # noqa
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.