Retrieve lyrics from an mp3 file in Python using e

2020-02-15 01:42发布

I am currently trying to use Python and its eyeD3 library to extract the lyrics from an mp3 file. The lyrics have been embedded into the mp3 file already (via: MusicBee). I am trying to use eyeD3 to return the lyrics. I can't figure out how to do it. I have searched online extensively and all I've found were tutorials showing how to SET the lyrics. I just want to read them from the file. Here's my current code:

track = eyed3.load(path)
tag = track.tag
artist = tag.artist
lyrics = tag.lyrics

artist returns the artist's name correctly but lyrics returns the following:

<eyed3.id3.tag.LyricsAccessor object at 0x27402d0>

How can I just return the raw text lyrics embedded into an mp3? Is this possible?

Thank you so much in advanced.

3条回答
太酷不给撩
2楼-- · 2020-02-15 02:36

It looks like that is a iterator. Try

tag.lyrics[0]

or

for lyric in tag.lyrics:
  print lyric

last resort print the objects directory and look for useful functions

print dir(tag.lyrics)
查看更多
乱世女痞
3楼-- · 2020-02-15 02:36

DonJuma, Python tells you:

<iterator object at hex_location> 

You can try the following but it fails on strings

hasattr(myObj, '__iter__')

user:mindu explains here that you can write a robust function that checks

try:
    some_object_iterator = iter(some_object)
except TypeError, te:
    print some_object, 'is not iterable'

BUT!!! This is not Pythonic. Python believes in Duck Typing which basically says, "If it looks like a duck and quacks like a duck, it must be a duck." See the link for more info.

查看更多
家丑人穷心不美
4楼-- · 2020-02-15 02:42
u"".join([i.text for i in tag.lyrics])
查看更多
登录 后发表回答