这是我能够获取歌曲图片/封面的代码。
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 或图像来源?
最佳答案
您应该将图片的内容存储在文件中,保存它,并使其在网络服务器上可用。
尝试做类似的事情:
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
关于ruby-on-rails - 如何使用 TagLib 获取歌曲封面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15827810/