2013-04-05 83 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

相關問題