Ruby on rails 如何使用TagLib获取歌曲封面艺术

Ruby on rails 如何使用TagLib获取歌曲封面艺术,ruby-on-rails,taglib,Ruby On Rails,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 如何将图片的值转换为url或图片的源?您应该将图片的内容存储在文件中,保存并在web

这是我能够获得歌曲图片/封面艺术的代码

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

如何将图片的值转换为url或图片的源?

您应该将图片的内容存储在文件中,保存并在web服务器上可用

试着做一些类似的事情:

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