Search code examples
rubymp3

Ruby-mp3info album artwork


I have this gem working such that I can change the id3 data for a given song. However I need to also be able to add album artwork to the song. I have the artwork at a given URL. How do I go about this?

Mp3Info.open(file.path) do |mp3|
  mp3.tag.title  = title
  mp3.tag.artist = artist
end

Solution

  • It seems ruby-mp3info only supports text frames at the moment, see here: https://github.com/moumar/ruby-mp3info/blob/v0.7.1/lib/mp3info/id3v2.rb#L319

    Using taglib-ruby, it would work like this:

    require 'taglib'
    require 'open-uri'
    
    picture_data = open(picture_url).read
    
    TagLib::MPEG::File.open(file.path) do |file|
      tag = file.id3v2_tag
    
      pic = TagLib::ID3v2::AttachedPictureFrame.new
      pic.picture = picture_data
      pic.mime_type = "image/jpeg"
      pic.type = TagLib::ID3v2::AttachedPictureFrame::FrontCover
    
      tag.add_frame(pic)
      file.save
    end