Object
License |
Ruby |
Author |
Guillaume Pierronnet (moumar_AT__rubyforge_DOT_org) |
Website |
map to fill the "universal" tag (tag attribute) for id3v2.2
for id3v2.3 and 2.4
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
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
id3v2 tag attribute as an ID3v2 object. You can modify it, it will be written when calling "close" method.
Test the presence of an id3v1 tag in file or StringIO filename_or_io
# File lib/mp3info.rb, line 161 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
Test the presence of an id3v2 tag in file or StringIO filename_or_io
# File lib/mp3info.rb, line 180 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
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
# File lib/mp3info.rb, line 218 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 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
"block version" of Mp3Info::new()
# 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
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
# 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
Flush pending modifications to tags and close the file not used when source IO is a StringIO
# 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) : "\00\\00\\00\\00""), @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 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 tempfile_name = @filename_or_io + ".tmp" 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 File.rename(tempfile_name, @filename_or_io) end end
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)
# File lib/mp3info.rb, line 478 def each_frame @io.seek(@first_frame_pos, File::SEEK_SET) loop do head = @io.read(4).unpack("N").first frame = Mp3Info.get_frames_infos(head) @io.seek(frame[:size] -4, File::SEEK_CUR) yield frame #puts "frame #{frame_count} len #{frame[:length]} br #{frame[:bitrate]} @io.pos #{@io.pos}" break if @io.eof? end end
close and reopen the file, i.e. commit changes to disk and reload it (only works with "true" files, not StringIO ones)
# File lib/mp3info.rb, line 460 def flush return unless @io_is_a_file close reload end
return the length in seconds of one frame
# File lib/mp3info.rb, line 365 def frame_length SAMPLES_PER_FRAME[@layer][@mpeg_version] / Float(@samplerate) end
Does the file has an id3v1 tag?
# File lib/mp3info.rb, line 332 def hastag1? !@tag1.empty? end
Does the file has an id3v2 tag?
# File lib/mp3info.rb, line 337 def hastag2? @tag2.parsed? end
Does the file has an id3v1 or v2 tag?
# File lib/mp3info.rb, line 327 def hastag? hastag1? || hastag2? end
reload (or load for the first time) the file from disk
# File lib/mp3info.rb, line 229 def reload @header = {} if @filename_or_io.is_a?(String) @io_is_a_file = true @io = File.new(@filename_or_io, "rb") @io_size = @io.stat.size @filename = @filename_or_io elsif @filename_or_io.is_a?(StringIO) @io_is_a_file = false @io = @filename_or_io @io_size = @io.size @filename = nil 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) begin 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 %{year tracknum}.include?(key) @tag[key] = tag_value.to_i end # this is a special case with id3v2.2, which uses # old fashionned id3v1 genres if tag2_name == "TCO" && 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 ensure if @io_is_a_file @io.close end end end
Remove id3v1 from mp3
# File lib/mp3info.rb, line 315 def removetag1 @tag1.clear self end
Remove id3v2 from mp3
# File lib/mp3info.rb, line 321 def removetag2 @tag2.clear self end
write to another filename at close()
# 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
inspect inside Mp3Info
# File lib/mp3info.rb, line 467 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_hash.inspect+"\n" if hastag2? s end
Generated with the Darkfish Rdoc Generator 2.