Save MP3 ID3V2 APIC to jpeg file

493 views Asked by At

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?

2

There are 2 answers

0
robinst On

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.

0
adamyonk On

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