You can extract, remove, or add images (APIC tags) using tag2.
Extracting images:
m.tag2.pictures returns an array of [description, data]. The description typically ends with .jpg or .png to facilitate saving to disk.
Adding images:
Use m.tag2.add_picture(binary_data, options).
- Note: Only one image is permitted per MP3 for compatibility;
add_picture overwrites existing images. - MIME type: The MIME type is guessed from the input, but you can specify it via the
:mime option. If using :mime, provide only the base type (e.g., 'jpeg', 'gif', or 'png'); the library prepends image/ automatically. - Options: You can pass
pic_type (e.g., 0) in the options hash.
Removing images:
Use m.tag2.remove_pictures to clear all images.
require "mp3info"
file = File.new('input_img','rb')
Mp3Info.open '1.mp3' do |m|
# Extracting pictures
pictures = m.tag2.pictures
pictures.each do |description, data|
File.binwrite(description, data)
end
# Removing all pictures
m.tag2.remove_pictures
# Adding a single picture
m.tag2.add_picture(file.read)
# Adding a picture with specific options
# m.tag2.add_picture(file.read, mime: 'jpeg', pic_type: 3)
end