0

I'm attempting to read and save an APIC picture to file using taglib-ruby but I'm struggling to understand how to go about it. From what I can tell the image is 'image/jpeg' and ASCII-8BIT. Anyone tried to accomplish this?

adamyonk
  • 2,976
  • 4
  • 22
  • 23

2 Answers2

1

You also asked that question in a taglib-ruby issue. The answer from there:

The documentation includes an example of reading the picture data, see TagLib::ID3v2::Tag examples.

Then just write the picture data into a file, e.g. like this:

TagLib::MPEG::File.open("file.mp3") do |file|
  tag = file.id3v2_tag
  covers = tag.frame_list('APIC')
  unless covers.empty?
    cover = covers.first
    File.open("output.jpg", "wb") do |f|
      f.write(cover.picture)
    end
  end
end

You should also check the picture's MIME type using cover.mime_type and adjust the file extension accordingly.

robinst
  • 30,027
  • 10
  • 102
  • 108
0

This ended up solving the problem I was having - I wasn't understanding how to prep the extracted image for Paperclip. https://gist.github.com/adamyonk/5621295

adamyonk
  • 2,976
  • 4
  • 22
  • 23