2012-07-04 73 views
3

我有这个宝石工作,我可以改变给定歌曲的id3数据。不过,我还需要为歌曲添加专辑封面。我在给定的URL上有图片。我如何去做这件事?Ruby-mp3info专辑艺术品

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

回答

4

看来红宝石mp3info只是在一瞬间支持文本框,在这里看到:https://github.com/moumar/ruby-mp3info/blob/v0.7.1/lib/mp3info/id3v2.rb#L319

使用taglib-ruby,它的工作是这样的:

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 
+0

支持MP3,我应该使用标签库:: MP3 :: File.open等。 或不MPEG基本上做同样的事情? –

+0

没有TagLib :: MP3,请参阅[API文档](http://rubydoc.info/gems/taglib-ruby/frames)。 API与[taglib](http://taglib.github.com/)中的API相同,每个文件格式都存在文件类别(然后可以为各个标签格式保存标签)。 – robinst

0

如果您未遇到mp3Info gem,请尝试使用id3Lib,http://id3lib-ruby.rubyforge.org/。根据我的经验,那个更好。

不知道这一点,但尝试读取文件并直接将其设置为

mp3.tag2.APIC

0

使用Ruby-mp3info您可以添加插图:

documentation

file = File.new('input_img','rb') 
Mp3Info.open '1.mp3' do |m| 
    m.tag2.add_picture(file.read) 
end