2013-04-05 82 views
2

下面是我能够获得歌曲的图片/封面艺术的代码。如何使用TagLib获取歌曲封面艺术

TagLib::MPEG::File.open("song_file_name.mp3") do |file| 
    tag = file.id3v2_tag 

    cover = tag.frame_list('APIC').first 
    mime_type = cover.mime_type 
    picture = cover.picture 
end 

如何将图片的值转换为网址或图片的来源?

回答

2

您应该将图片的内容存储在一个文件中,保存并在网络服务器上提供。

试着这样做:

TagLib::MPEG::File.open("song_file_name.mp3") do |file| 
    tag = file.id3v2_tag 

    cover = tag.frame_list('APIC').first 
    mime_type = cover.mime_type 
    picture = cover.picture 

    extension = case cover.mime_type 
     when 'image/jpeg', 'image/jpg' 
     'jpg' 
     when 'image/gif' 
     'gif' 
     else 
     raise "Mime not found" 
    end 

    file_name = "my_file.#{extension}" 

    File.open(file_name, "w") do |f| 
     f.write(picture) 
    end 

end 
+0

非常感谢!有用。 :) – MoMo 2013-04-05 08:47:17

+0

随时,很高兴它一次工作:) – rorra 2013-04-05 09:05:19

相关问题