简体   繁体   English

使用 eyeD3 从 Python 中的 mp3 文件中检索歌词

[英]Retrieve lyrics from an mp3 file in Python using eyeD3

I am currently trying to use Python and its eyeD3 library to extract the lyrics from an mp3 file.我目前正在尝试使用 Python 及其 eyeD3 库从 mp3 文件中提取歌词。 The lyrics have been embedded into the mp3 file already (via: MusicBee).歌词已经嵌入到 mp3 文件中(通过:MusicBee)。 I am trying to use eyeD3 to return the lyrics.我正在尝试使用 eyeD3 返回歌词。 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?我怎样才能返回嵌入到 mp3 中的原始文本歌词? Is this possible?这可能吗?

Thank you so much in advanced.非常感谢你提前。

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)
u"".join([i.text for i in tag.lyrics])

这对我有用:

tag.lyrics[0].text

DonJuma, Python tells you: DonJuma,Python 告诉你:

<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 user:mindu 在这里解释说,您可以编写一个健壮的函数来检查

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

BUT!!!但!!! This is not Pythonic.这不是 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." Python 相信Duck Typing ,它基本上说,“如果它看起来像一只鸭子,叫起来像一只鸭子,那它一定是一只鸭子。” See the link for more info.查看链接了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM