class Rupeepeethree::Tagger

Public Class Methods

clear(mp3) click to toggle source

clear all tags

# File lib/rupeepeethree/tagger.rb, line 61
def self.clear(mp3)
  raise FileNotFound if !File.exist? mp3
  TagLib::MPEG::File.open(mp3) do |f|
    f.strip
  end
end
print_tags(mp3) click to toggle source
tag(mp3,tags) click to toggle source
# File lib/rupeepeethree/tagger.rb, line 7
def self.tag(mp3,tags)
  raise FileNotFound if !File.exist? mp3
  TagLib::MPEG::File.open(mp3) do |f|
    t = f.id3v2_tag || TagLib::ID3v2::Tag.new
    if tags[:title]
      t.title = tags[:title]
    end
    if tags[:artist]
      t.artist = tags[:artist]
    end
    if tags[:album]
      t.album = tags[:album]
    end
    if tags[:year]
      t.year = tags[:year].to_i
    end
    if tags[:track]
      t.track = tags[:track].to_i
    end
    if tags[:picture]
      image_file = File.expand_path(tags[:picture])
      # delete old frame if it exists
      cover = t.frame_list('APIC').first
      if cover
        t.remove_frame(cover)
      end
      cover = TagLib::ID3v2::AttachedPictureFrame.new
      cover.mime_type = mime_type(image_file)
      cover.type = TagLib::ID3v2::AttachedPictureFrame::FrontCover
      cover.picture = File.open(image_file,"rb"){|f|f.read}
      t.add_frame(cover)
    end
    f.save
  end
end
tags(mp3) click to toggle source
# File lib/rupeepeethree/tagger.rb, line 43
def self.tags(mp3)
  raise FileNotFound if !File.exist? mp3
  hash = {}
  TagLib::MPEG::File.open(mp3) do |f|
    t = f.id3v2_tag

    hash[:title] =  t.title
    hash[:artist] = t.artist
    hash[:album] = t.album
    hash[:track_number] = t.track
    hash[:year] = t.year
    properties = f.audio_properties
    hash[:length] = properties.length
  end
  hash
end

Private Class Methods

mime_type(file) click to toggle source
# File lib/rupeepeethree/tagger.rb, line 96
def self.mime_type(file)
  MIME::Types.type_for(file).first.simplified.to_s
end