Class: Sh::TagReader

Inherits:
Object show all
Defined in:
lib/sh_tagreader.rb

Class Method Summary collapse

Class Method Details

.read(path) {|metadata| ... } ⇒ Object

Yields:

  • (metadata)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sh_tagreader.rb', line 3

def TagReader.read path, &block
  @path = File.expand_path path
   = {}
  
  if try_require 'shared-mime-info'
    type = MIME.check(path)
    @mime = type.to_s if type.media == 'audio'
  end
  @mime ||= {
    '.mp3' => 'audio/mpeg',
    '.m4a' => 'audio/mp4',
    '.flac' => 'audio/x-flac',
    '.ogg' => 'audio/ogg',
    '.wav' => 'audio/x-wav',
    '.wma' => 'audio/x-ms-wma'
  }[File.extname path]

  begin
    case @mime
    when 'audio/mpeg'
       = TagReader.read_mp3 path
    when 'audio/mp4'
       = TagReader.read_m4a path
    when 'audio/ogg'
       = TagReader.read_ogg path
    when 'audio/x-flac'
       = TagReader.read_flac path
    when 'audio/x-ms-wma'
       = TagReader.read_wma path
    when 'audio/x-wav'
       = TagReader.read_wave path
    end
  rescue Exception
    puts "Error parsing tags for " + path
  end

  yield  if block
  return 
end

.read_flac(path) {|metadata| ... } ⇒ Object

Yields:

  • (metadata)


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
# File 'lib/sh_tagreader.rb', line 98

def TagReader.read_flac path, &block
   = {}
  if try_require 'flacinfo'
    flac = FlacInfo.new(path)
    flac.comment.each do |comment|
      split = comment.split('=')
      key = split.first.downcase
      value = (split[1..-1]).join
      value = nil if value == '' or value.is_binary_data?
      case key
      when 'title'
        [:title] = value.to_u if value
      when 'artist'
        [:artist] = value.to_u if value
      when 'album'
        [:album] = value.to_u if value
      when 'year'
        [:year] = value.to_i
      when 'tracknumber'
        [:track_num] = value.to_i
      end
    end
  else
    puts 'Please install the "flacinfo" gem in order to read FLAC info'
  end
  yield  if block
  return 
end

.read_m4a(path) {|metadata| ... } ⇒ Object

Yields:

  • (metadata)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sh_tagreader.rb', line 62

def TagReader.read_m4a path, &block
   = {}
  if try_require 'mp4info'
    mp4 = MP4Info.open path
    [:title] = mp4.NAM.to_u if mp4.NAM
    [:artist] = mp4.ART.to_u if mp4.ART
    [:album] = mp4.ALB.to_u if mp4.ALB
    [:year] = mp4.DAY.to_i
    [:track_num] = mp4.TRKN.first.to_i
    [:duration] = mp4.SECS
  else
    puts 'Please install the "mp4info" gem in order to read M4A info'
  end
  yield  if block
  return 
end

.read_mp3(path) {|metadata| ... } ⇒ Object

Yields:

  • (metadata)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sh_tagreader.rb', line 43

def TagReader.read_mp3 path, &block
   = {}
  if try_require 'mp3info'
    Mp3Info.open path do |mp3|
      t = mp3.tag
      [:title] = t.title.to_u if t.title
      [:artist] = t.artist.to_u if t.artist
      [:album] = t.album.to_u if t.album
      [:year] = t.year.to_i
      [:track_num] = t.tracknum.to_i
      [:duration] = mp3.length
    end
  else
    puts 'Please install the "mp3info" gem in order to read MP3 info'
  end
  yield  if block
  return 
end

.read_ogg(path) {|metadata| ... } ⇒ Object

Yields:

  • (metadata)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sh_tagreader.rb', line 79

def TagReader.read_ogg path, &block
   = {}
  if try_require 'ogginfo'
    OggInfo.open path do |ogg|
      t = ogg.tag
      [:title] = t.title.to_u if t.title
      [:artist] = t.artist.to_u if t.artist
      [:album] = t.album.to_u if t.album
      [:year] = t.date.to_i
      [:track_num] = t.tracknumber.to_i
      [:duration] = ogg.length
    end rescue Exception
  else
    puts 'Please install the "ogginfo" gem in order to read OGG info'
  end
  yield  if block
  return 
end

.read_wave(path) {|metadata| ... } ⇒ Object

Yields:

  • (metadata)


155
156
157
158
159
160
161
162
163
164
165
# File 'lib/sh_tagreader.rb', line 155

def TagReader.read_wave path, &block
   = {}
  if try_require 'waveinfo'
    wave = WaveInfo.new path
    [:duration] = wave.duration
  else
    puts 'Please install the "waveinfo" gem in order to read WAV info'
  end
  yield  if block
  return 
end

.read_wma(path) {|metadata| ... } ⇒ Object

Yields:

  • (metadata)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/sh_tagreader.rb', line 127

def TagReader.read_wma path, &block
   = {}
  if try_require 'wmainfo'
    wma = WmaInfo.new(path)
    wma.tags.each do |key, value|
      key = key.downcase
      value = nil if value == '' or value.is_binary_data?
      case key
      when 'title'
        [:title] = value.to_u if value
      when 'author'
        [:artist] = value.to_u if value
      when 'albumtitle'
        [:album] = value.to_u if value
      when 'year'
        [:year] = value.to_i
      when 'tracknumber'
        [:track_num] = value.to_i
      end
    end
    [:duration] = wma.info["playtime_seconds"]
  else
    puts 'Please install the "wmainfo" gem in order to read WMA info'
  end
  yield  if block
  return 
end