Module: Metadata

Defined in:
lib/what_cd/metadata.rb

Defined Under Namespace

Classes: BitrateError

Class Method Summary collapse

Class Method Details

.get_album_title(path) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/what_cd/metadata.rb', line 63

def self.get_album_title(path)
  file_path = self.get_first_mp3(path)

  if file_path
    Mp3Info.open(file_path) do |mp3|
      return mp3.tag.album
    end
  end
end

.get_artists(path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/what_cd/metadata.rb', line 31

def self.get_artists(path)
  artists = []

  Dir.entries(path).each do |f|
    if !File.directory? f
      # Fix Tags
      if File.extname(f) == ".mp3"
        filename = File.basename(f, File.extname(f))
        file_path = path + f
        
        Mp3Info.open(file_path) do |mp3|
          if mp3.tag.artist
            artists.push(mp3.tag.artist)
          end
        end
      end
    end
  end

  return artists
end

.get_bitrate(path) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/what_cd/metadata.rb', line 86

def self.get_bitrate(path)
  files = Dir.chdir(path) { Dir["*.mp3"] }
  return if files.empty?

  bitrates = {}

  files.each do |file_name|
    file_path = path + file_name
    Mp3Info.open(file_path) do |mp3|
      hash = {:bitrate => mp3.bitrate, :vbr => mp3.vbr}
      bitrates[file_name] = hash
    end
  end

  vbr_list = bitrates.values.map{|value| value[:vbr] }
  # Will only have 1-2 count (true & false)
  if vbr_list.uniq.count > 1
    raise BitrateError.new("Release has both constant and variable bitrate tracks!")
  else 
    vbr = vbr_list.first
  end

  bitrate_list = bitrates.values.map{|value| value[:bitrate] }
  # Remove duplicates
  uniq_bitrate_list = bitrate_list.uniq

  # If the only vbr value is false, this is CBR
  if not vbr and uniq_bitrate_list.uniq.count > 1
    raise BitrateError.new("Release tracks are constant bit rate but multiple bitrates found!")
  elsif vbr 
    puts  "Calculating average bitrate"
    # Determine average VBR of release
    avg_bitrate = bitrate_list.inject(:+) / bitrate_list.length
    if avg_bitrate > 220
      return 'V0 (VBR)'
    elsif avg_bitrate > 192
      return 'V2 (VBR)'
    else 
      raise BitrateError.new("Release average bit rate is too low")
    end
  else
    return bitrate_list.first
  end
end

.get_first_mp3(path) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/what_cd/metadata.rb', line 73

def self.get_first_mp3(path)
  Dir.entries(path).each do |f|
    if !File.directory? f
      # Fix Tags
      if File.extname(f) == ".mp3"
        filename = File.basename(f, File.extname(f))
        file_path = path + f
        return file_path
      end
    end
  end
end

.get_year(path) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/what_cd/metadata.rb', line 53

def self.get_year(path)
  file_path = self.get_first_mp3(path)

  if file_path
    Mp3Info.open(file_path) do |mp3|
      return mp3.tag.year
    end
  end
end

.run(path) ⇒ Object

Load from config file



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/what_cd/metadata.rb', line 17

def self.run(path)

   = {}
  ['album_title'] = self.get_album_title(path)
  ['artists'] = self.get_artists(path)
  ['year'] = self.get_year(path)
  # We only handle MP3 files at the moment
  ['format'] = 'MP3'
   ['bitrate'] = self.get_bitrate(path)


  puts 
end