Class: Mp3Info

Inherits:
Object
  • Object
show all
Defined in:
lib/mp3info/extension_modules.rb,
lib/mp3info.rb

Overview

License

Ruby

Author

Guillaume Pierronnet ([email protected])

Defined Under Namespace

Modules: HashKeys, Mp3FileMethods Classes: EncodingHelper

Constant Summary collapse

VERSION =
"0.8.7"
LAYER =
[ nil, 3, 2, 1]
BITRATE =
{
  1 =>
  [
    [32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448],
    [32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384],
    [32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320] ],
  2 =>
  [
    [32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256],
    [8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160],
    [8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160]
  ],
  2.5 =>
  [
    [32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256],
    [8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160],
    [8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160]
  ]
}
SAMPLERATE =
{
  1 => [ 44100, 48000, 32000 ],
  2 => [ 22050, 24000, 16000 ],
  2.5 => [ 11025, 12000, 8000 ]
}
CHANNEL_MODE =
[ "Stereo", "JStereo", "Dual Channel", "Single Channel"]
GENRES =
[
"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk",
"Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies",
"Other", "Pop", "R&B", "Rap", "Reggae", "Rock",
"Techno", "Industrial", "Alternative", "Ska", "Death Metal", "Pranks",
"Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk",
"Fusion", "Trance", "Classical", "Instrumental", "Acid", "House",
"Game", "Sound Clip", "Gospel", "Noise", "AlternRock", "Bass",
"Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock",
"Ethnic", "Gothic", "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk",
"Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta",
"Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret",
"New Wave", "Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi",
"Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical",
"Rock & Roll", "Hard Rock", "Folk", "Folk/Rock", "National Folk", "Swing",
"Fast-Fusion", "Bebob", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde",
"Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band",
"Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson",
"Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", "Primus",
"Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba",
"Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle", "Duet",
"Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall",
"Goa", "Drum & Bass", "Club House", "Hardcore", "Terror",
"Indie", "BritPop", "NegerPunk", "Polsk Punk", "Beat",
"Christian Gangsta", "Heavy Metal", "Black Metal", "Crossover", "Contemporary C",
"Christian Rock", "Merengue", "Salsa", "Thrash Metal", "Anime", "JPop",
"SynthPop" ]
TAG1_SIZE =
128
TAG_MAPPING_2_2 =

map to fill the “universal” tag (#tag attribute) for id3v2.2

{
  "title"    => "TT2",
  "artist"   => "TP1",
  "album"    => "TAL",
  "year"     => "TYE",
  "tracknum" => "TRK",
  "comments" => "COM",
  "genre_s"  => "TCO"
}
TAG_MAPPING_2_3 =

for id3v2.3 and 2.4

{
  "title"    => "TIT2",
  "artist"   => "TPE1",
  "album"    => "TALB",
  "year"     => "TYER",
  "tracknum" => "TRCK",
  "comments" => "COMM",
  "genre_s"  => "TCON"
}
SAMPLES_PER_FRAME =
[
  nil,
  {1=>384, 2=>384, 2.5=>384},    # Layer I
  {1=>1152, 2=>1152, 2.5=>1152}, # Layer II
  {1=>1152, 2=>576, 2.5=>576}    # Layer III
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename_or_io, options = {}) ⇒ Mp3Info

Instantiate Mp3Info object with name filename. options hash is used for ID3v2#new. Specify :parse_tags => false to disable the processing of the tags (read and write). Specify :parse_mp3 => false to disable processing of the mp3


220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/mp3info.rb', line 220

def initialize(filename_or_io, options = {})
  warn("#{self.class}::new() does not take block; use #{self.class}::open() instead") if block_given?
  @filename_or_io = filename_or_io
  if @filename_or_io.nil?
    raise ArgumentError, "filename is nil"
  end
  options = {:parse_mp3 => true, :parse_tags => true}.update(options)
  @tag_parsing_enabled = options.delete(:parse_tags)
  @mp3_parsing_enabled = options.delete(:parse_mp3)
  @id3v2_options = options
  reload
end

Instance Attribute Details

#bitrateObject (readonly)

bitrate in kbps


119
120
121
# File 'lib/mp3info.rb', line 119

def bitrate
  @bitrate
end

#channel_modeObject (readonly)

channel mode => “Stereo”, “JStereo”, “Dual Channel” or “Single Channel”


125
126
127
# File 'lib/mp3info.rb', line 125

def channel_mode
  @channel_mode
end

#error_protectionObject (readonly)

error protection => true or false


145
146
147
# File 'lib/mp3info.rb', line 145

def error_protection
  @error_protection
end

#filenameObject (readonly)

the original filename unless used with a StringIO


160
161
162
# File 'lib/mp3info.rb', line 160

def filename
  @filename
end

#headerObject (readonly)

Hash representing values in the MP3 frame header. Keys are one of the following:

  • :private (boolean)

  • :copyright (boolean)

  • :original (boolean)

  • :padding (boolean)

  • :error_protection (boolean)

  • :mode_extension (integer in the 0..3 range)

  • :emphasis (integer in the 0..3 range)

detailled explanation can be found here: www.mp3-tech.org/programmer/frame_header.html


139
140
141
# File 'lib/mp3info.rb', line 139

def header
  @header
end

#layerObject (readonly)

layer = 1, 2, or 3


116
117
118
# File 'lib/mp3info.rb', line 116

def layer
  @layer
end

#lengthObject (readonly)

length in seconds as a Float


142
143
144
# File 'lib/mp3info.rb', line 142

def length
  @length
end

#mpeg_versionObject (readonly)

mpeg version = 1 or 2


113
114
115
# File 'lib/mp3info.rb', line 113

def mpeg_version
  @mpeg_version
end

#samplerateObject (readonly)

samplerate in Hz


122
123
124
# File 'lib/mp3info.rb', line 122

def samplerate
  @samplerate
end

#tagObject (readonly)

a sort of “universal” tag, regardless of the tag version, 1 or 2, with the same keys as @tag1 this tag has priority over @tag1 and @tag2 when writing the tag with #close


149
150
151
# File 'lib/mp3info.rb', line 149

def tag
  @tag
end

#tag1Object

id3v1 tag as a Hash. You can modify it, it will be written when calling “close” method.


153
154
155
# File 'lib/mp3info.rb', line 153

def tag1
  @tag1
end

#tag2Object

id3v2 tag attribute as an ID3v2 object. You can modify it, it will be written when calling “close” method.


157
158
159
# File 'lib/mp3info.rb', line 157

def tag2
  @tag2
end

#vbrObject (readonly)

variable bitrate => true or false


128
129
130
# File 'lib/mp3info.rb', line 128

def vbr
  @vbr
end

Class Method Details

.hastag1?(filename_or_io) ⇒ Boolean

Test the presence of an id3v1 tag in file or StringIO filename_or_io

Returns:

  • (Boolean)

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/mp3info.rb', line 163

def self.hastag1?(filename_or_io)
  if filename_or_io.is_a?(StringIO)
    io = filename_or_io
    io.rewind
  else
    io = File.new(filename_or_io, "rb")
  end

  hastag1 = false
  begin
    io.seek(-TAG1_SIZE, File::SEEK_END)
    hastag1 = io.read(3) == "TAG"
  ensure
    io.close if io.is_a?(File)
  end
  hastag1
end

.hastag2?(filename_or_io) ⇒ Boolean

Test the presence of an id3v2 tag in file or StringIO filename_or_io

Returns:

  • (Boolean)

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/mp3info.rb', line 182

def self.hastag2?(filename_or_io)
  if filename_or_io.is_a?(StringIO)
    io = filename_or_io
    io.rewind
  else
    io = File.new(filename_or_io,"rb")
  end

  hastag2 = false

  begin
    hastag2 = io.read(3) == "ID3"
  ensure
    io.close if io.is_a?(File)
  end
  hastag2
end

.open(*params) ⇒ Object

“block version” of Mp3Info::new()


299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/mp3info.rb', line 299

def self.open(*params)
  m = self.new(*params)
  ret = nil
  if block_given?
    begin
      ret = yield(m)
    ensure
      m.close
    end
  else
    ret = m
  end
  ret
end

.removetag1(filename) ⇒ Object

Remove id3v1 tag from filename


201
202
203
204
205
206
# File 'lib/mp3info.rb', line 201

def self.removetag1(filename)
  if self.hastag1?(filename)
    newsize = File.size(filename) - TAG1_SIZE
    File.open(filename, "rb+") { |f| f.truncate(newsize) }
  end
end

.removetag2(filename) ⇒ Object

Remove id3v2 tag from filename


209
210
211
212
213
# File 'lib/mp3info.rb', line 209

def self.removetag2(filename)
  self.open(filename) do |mp3|
    mp3.tag2.clear
  end
end

Instance Method Details

#audio_contentObject

this method returns the “audio-only” data boundaries of the file, i.e. content stripped form tags. Useful to compare 2 files with the same audio content but with differents tags. Returned value is an array

position_in_the_file, length_of_the_data

351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/mp3info.rb', line 351

def audio_content
  pos = 0
  length = @io_size
  if hastag1?
    length -= TAG1_SIZE
  end
  if hastag2?
    pos = @tag2.io_position
    length -= @tag2.io_position
  end
  [pos, length]
end

#closeObject

Flush pending modifications to tags and close the file not used when source IO is a StringIO


371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/mp3info.rb', line 371

def close
  puts "close" if $DEBUG
  return unless @io_is_a_file
  if !@tag_parsing_enabled
    return
  end
  if @tag != @tag_orig
    puts "@tag has changed" if $DEBUG

    # @tag1 has precedence over @tag
    if @tag1 == @tag1_orig
      @tag.each do |k, v|
        @tag1[k] = v
      end
    end

    # ruby-mp3info can only write v2.3 tags
    TAG_MAPPING_2_3.each do |key, tag2_name|
      @tag2.delete(TAG_MAPPING_2_2[key])
      @tag2[tag2_name] = @tag[key] if @tag[key]
    end
  end

  if @tag1 != @tag1_orig
    puts "@tag1 has changed" if $DEBUG
    raise(Mp3InfoError, "file is not writable") unless File.writable?(@filename_or_io)
    #@tag1_orig.update(@tag1)
    @tag1_orig = @tag1.dup
    File.open(@filename_or_io, 'rb+') do |file|
      if @tag1_orig.empty?
        newsize = @io_size - TAG1_SIZE
        file.truncate(newsize)
      else
        file.seek(-TAG1_SIZE, File::SEEK_END)
        t = file.read(3)
        if t != 'TAG'
          #append new tag
          file.seek(0, File::SEEK_END)
          file.write('TAG')
        end
        str = [
          @tag1_orig["title"]||"",
          @tag1_orig["artist"]||"",
          @tag1_orig["album"]||"",
          ((@tag1_orig["year"] != 0) ? ("%04d" % @tag1_orig["year"].to_i) : "\0\0\0\0"),
          @tag1_orig["comments"]||"",
          0,
          @tag1_orig["tracknum"]||0,
          @tag1_orig["genre"]||255
          ].pack("Z30Z30Z30Z4Z28CCC")
        file.write(str)
      end
    end
  end

  if @tag2.changed?
    puts "@tag2 has changed" if $DEBUG
    raise(Mp3InfoError, "file is not writable") unless File.writable?(@filename_or_io)
    tempfile_name = nil
    @io.close
    File.open(@filename_or_io, 'rb+') do |file|
      #if tag2 already exists, seek to end of it
      if @tag2.parsed?
        file.seek(@tag2.io_position)
      end
#      if @io.read(3) == "ID3"
#        version_maj, version_min, flags = @io.read(3).unpack("CCB4")
#        unsync, ext_header, experimental, footer = (0..3).collect { |i| flags[i].chr == '1' }
#        tag2_len = @io.get_syncsafe
#        @io.seek(@io.get_syncsafe - 4, IO::SEEK_CUR) if ext_header
#        @io.seek(tag2_len, IO::SEEK_CUR)
#      end
      filename_splitted = File.split(@filename_or_io)
      filename_splitted[-1] = ".#{filename_splitted[-1]}.tmp"
      tempfile_name = File.join(filename_splitted)
      File.open(tempfile_name, "wb") do |tempfile|
        unless @tag2.empty?
          tempfile.write(@tag2.to_bin)
        end

        bufsiz = file.stat.blksize || 4096
        while buf = file.read(bufsiz)
          tempfile.write(buf)
        end
      end
    end
    begin
      File.rename(tempfile_name, @filename_or_io)
    rescue Errno::EACCES
      FileUtils.cp(tempfile_name, @filename_or_io)
      FileUtils.rm tempfile_name
    end
  end
end

#each_frameObject

iterates over each mpeg frame over the file, allowing you to write some funny things, like an mpeg lossless cutter, or frame counter, or whatever you like ;) frame is a hash with the following keys: :layer, :bitrate, :samplerate, :mpeg_version, :padding and :size (in bytes)


486
487
488
489
490
491
492
493
494
495
# File 'lib/mp3info.rb', line 486

def each_frame
  @io.seek(@first_frame_pos, File::SEEK_SET)
  loop do
    frame = find_next_frame
    yield frame
    @io.seek(frame[:size] -4, File::SEEK_CUR)
    #puts "frame #{frame_count} len #{frame[:length]} br #{frame[:bitrate]} @io.pos #{@io.pos}"
    break if @io.eof?
  end
end

#flushObject

close and reopen the file, i.e. commit changes to disk and reload it (only works with “true” files, not StringIO ones)


468
469
470
471
472
# File 'lib/mp3info.rb', line 468

def flush
  return unless @io_is_a_file
  close
  reload
end

#get_frame_lengthObject

return the length in seconds of one frame


365
366
367
# File 'lib/mp3info.rb', line 365

def get_frame_length
  SAMPLES_PER_FRAME[@layer][@mpeg_version] / Float(@samplerate)
end

#hastag1?Boolean

Does the file has an id3v1 tag?

Returns:

  • (Boolean)

332
333
334
# File 'lib/mp3info.rb', line 332

def hastag1?
  !@tag1.empty?
end

#hastag2?Boolean

Does the file has an id3v2 tag?

Returns:

  • (Boolean)

337
338
339
# File 'lib/mp3info.rb', line 337

def hastag2?
  @tag2.parsed?
end

#hastag?Boolean

Does the file has an id3v1 or v2 tag?

Returns:

  • (Boolean)

327
328
329
# File 'lib/mp3info.rb', line 327

def hastag?
  hastag1? || hastag2?
end

#reloadObject

reload (or load for the first time) the file from disk


234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/mp3info.rb', line 234

def reload
  @header = {}

  if @filename_or_io.is_a?(StringIO) || @filename_or_io.is_a?(IO)
    @io_is_a_file = false
    @io = @filename_or_io
    @io_size = @io.size
    @filename = nil
  else
    @io_is_a_file = true
    @io = File.new(@filename_or_io, "rb")
    @io_size = @io.stat.size
    @filename = @filename_or_io
  end

  if @io_size == 0
    raise(Mp3InfoError, "empty file or IO")
  end

  @io.extend(Mp3FileMethods)
  @tag1 = @tag = @tag1_orig = @tag_orig = {}
  @tag1.extend(HashKeys)
  @tag2 = ID3v2.new(@id3v2_options)

  if @tag_parsing_enabled
    parse_tags
    @tag1_orig = @tag1.dup

    if hastag1?
      @tag = @tag1.dup
    end

    if hastag2?
      @tag = {}
      # creation of a sort of "universal" tag, regardless of the tag version
      tag2_mapping = @tag2.version =~ /^2\.2/ ? TAG_MAPPING_2_2 : TAG_MAPPING_2_3
      tag2_mapping.each do |key, tag2_name|
        tag_value = (@tag2[tag2_name].is_a?(Array) ? @tag2[tag2_name].first : @tag2[tag2_name])
        next unless tag_value
        @tag[key] = tag_value.is_a?(Array) ? tag_value.first : tag_value

        if %w{year tracknum}.include?(key)
          @tag[key] = tag_value.to_i
        end
        # this is a special case with id3v2.2-3, which uses
        # old fashionned id3v1 genres
 # also id3v2.4 ought not to use the old fashioned genres but examples exist where it does
        if ( ((tag2_name == "TCO") || (tag2_name == "TCON")) && tag_value =~ /^\((\d+)\)$/) ||
(tag2_name == "TCON" && tag_value =~ /^(\d+)$/)
          @tag["genre_s"] = GENRES[$1.to_i]
        end
      end
    end

    @tag.extend(HashKeys)
    @tag_orig = @tag.dup
  end

  if @mp3_parsing_enabled
    parse_mp3
  end

end

#removetag1Object

Remove id3v1 from mp3


315
316
317
318
# File 'lib/mp3info.rb', line 315

def removetag1
  @tag1.clear
  self
end

#removetag2Object

Remove id3v2 from mp3


321
322
323
324
# File 'lib/mp3info.rb', line 321

def removetag2
  @tag2.clear
  self
end

#rename(new_filename) ⇒ Object

write to another filename at close()

Raises:


342
343
344
345
# File 'lib/mp3info.rb', line 342

def rename(new_filename)
  raise(Mp3InfoError, "cannot rename an IO") unless @io_is_a_file
  @filename = new_filename
end

#to_sObject

inspect inside Mp3Info


475
476
477
478
479
480
# File 'lib/mp3info.rb', line 475

def to_s
  s = "MPEG #{@mpeg_version} Layer #{@layer} #{@vbr ? "VBR" : "CBR"} #{@bitrate} Kbps #{@channel_mode} #{@samplerate} Hz length #{@length} sec. header #{@header.inspect} "
  s << "tag1: "+@tag1.to_hash.inspect+"\n" if hastag1?
  s << "tag2: "+@tag2.to_inspect_hash.inspect+"\n" if hastag2?
  s
end