Class: UnitF::Tag::MP3

Inherits:
File
  • Object
show all
Defined in:
lib/unitf/tag/mp3.rb

Instance Attribute Summary

Attributes inherited from File

#dirname, #extname, #path, #realpath

Instance Method Summary collapse

Methods inherited from File

#auto_cover!, #auto_tag!, #auto_tags, #close, #cover_available?, #cover_path, #flac?, #format_json, #format_line, #mp3?, #open, #print, #properties!, #save, #tag, #to_s, #update

Constructor Details

#initialize(file_path) ⇒ MP3

Returns a new instance of MP3.



4
5
6
7
# File 'lib/unitf/tag/mp3.rb', line 4

def initialize(file_path)
  super(file_path)
  @file = TagLib::MPEG::File.new(file_path)
end

Instance Method Details

#album_artist=(artist) ⇒ Object



43
44
45
46
47
48
# File 'lib/unitf/tag/mp3.rb', line 43

def album_artist=(artist)
  @file.id3v2_tag.remove_frames('TPE2')
  frame = TagLib::ID3v2::TextIdentificationFrame.new('TPE2', TagLib::String::UTF8)
  frame.text = artist
  @file.id3v2_tag.add_frame(frame)
end

#cover!(file_path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/unitf/tag/mp3.rb', line 25

def cover!(file_path)
  return unless ::File.exist?(file_path.to_s)

  delete_cover! if cover?

  UnitF::Log.info("Setting cover #{file_path}")
  apic = TagLib::ID3v2::AttachedPictureFrame.new
  apic.mime_type = 'image/jpeg'
  apic.description = 'Cover'
  apic.type = TagLib::ID3v2::AttachedPictureFrame::FrontCover
  apic.picture = ::File.binread(file_path)
  @file.id3v2_tag.add_frame(apic)
end

#cover?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/unitf/tag/mp3.rb', line 21

def cover?
  @file.id3v2_tag.frame_list('APIC').size.positive?
end

#delete_cover!Object



39
40
41
# File 'lib/unitf/tag/mp3.rb', line 39

def delete_cover!
  @file.id3v2_tag.remove_frames('APIC')
end

#dumpObject



50
51
52
53
54
55
56
57
58
# File 'lib/unitf/tag/mp3.rb', line 50

def dump
  puts "File: #{realpath}"
  tag = @file.id3v2_tag
  tag.frame_list.each do |frame|
    next unless frame.is_a?(TagLib::ID3v2::TextIdentificationFrame)
    puts "#{frame.frame_id}: #{tag.frame_list(frame.frame_id).first}"
  end
  puts
end

#infoObject



13
14
15
16
17
18
19
# File 'lib/unitf/tag/mp3.rb', line 13

def info
  super.merge!({
    stats: stats,
    sample_rate: @file.audio_properties.sample_rate,
    bitrate: @file.audio_properties.bitrate
  })
end

#statsObject



9
10
11
# File 'lib/unitf/tag/mp3.rb', line 9

def stats
  sprintf('%.1fkHz/%dkbps', @file.audio_properties.sample_rate / 1000.to_f, @file.audio_properties.bitrate)
end