lib/audioinfo.rb in ruby-audioinfo-0.1.7 vs lib/audioinfo.rb in ruby-audioinfo-0.2

- old
+ new

@@ -1,6 +1,6 @@ -require "iconv" +# encoding: utf-8 require "stringio" require "mp3info" require "ogginfo" require "wmainfo" @@ -13,10 +13,17 @@ require "audioinfo/mpcinfo" class AudioInfoError < Exception ; end class AudioInfo + if RUBY_VERSION[0..2] == "1.8" + RUBY_1_8 = true + require "iconv" + else + RUBY_1_8 = false + end + MUSICBRAINZ_FIELDS = { "trmid" => "TRM Id", "artistid" => "Artist Id", "albumid" => "Album Id", "albumtype" => "Album Type", @@ -26,11 +33,11 @@ "trackid" => "Track Id" } SUPPORTED_EXTENSIONS = %w{mp3 ogg mpc wma mp4 aac m4a flac} - VERSION = "0.1.7" + VERSION = "0.2" attr_reader :path, :extension, :musicbrainz_infos, :tracknum, :bitrate, :vbr attr_reader :artist, :album, :title, :length, :date # "block version" of #new() @@ -47,24 +54,33 @@ ret = audio_info end ret end - # open the file with path +fn+ and convert all tags from/to specified +encoding+ - def initialize(filename, encoding = 'utf-8') + # test whether +path+ is a valid and supported audiofile + def self.is_audio_file?(path) + begin + AudioInfo.new(path) + return true + rescue AudioInfoError + return false + end + end + + # open the file with path +fn+ + def initialize(filename) raise(AudioInfoError, "path is nil") if filename.nil? @path = filename ext = File.extname(@path) raise(AudioInfoError, "cannot find extension") if ext.empty? @extension = ext[1..-1].downcase @musicbrainz_infos = {} - @encoding = encoding begin case @extension when 'mp3' - @info = Mp3Info.new(filename, :encoding => @encoding) + @info = Mp3Info.new(filename) default_tag_fill #"TXXX"=> #["MusicBrainz TRM Id\000", #"MusicBrainz Artist Id\000aba64937-3334-4c65-90a1-4e6b9d4d7ada", #"MusicBrainz Album Id\000e1a223c1-cbc2-427f-a192-5d22fefd7c4c", @@ -88,11 +104,11 @@ @date = @info.tag["date"] @vbr = @info.vbr @info.close when 'ogg' - @info = OggInfo.new(filename, @encoding) + @info = OggInfo.new(filename) default_fill_musicbrainz_fields default_tag_fill @bitrate = @info.bitrate/1000 @tracknum = @info.tag.tracknumber.to_i @length = @info.length.to_i @@ -109,11 +125,11 @@ when 'ape' fill_ape_tag(filename) when 'wma' - @info = WmaInfo.new(filename, :encoding => @encoding) + @info = WmaInfo.new(filename, :encoding => 'utf-8') @artist = @info.tags["Author"] @album = @info.tags["AlbumTitle"] @title = @info.tags["Title"] @tracknum = @info.tags["TrackNumber"].to_i @date = @info.tags["Year"] @@ -123,11 +139,12 @@ @musicbrainz_infos[key] = @info.info["MusicBrainz/" + original_key.tr(" ", "")] || @info.info["MusicBrainz/" + original_key] end - when 'aac', 'mp4', 'm4a' + when 'mp4', 'aac', 'm4a' + @extension = 'mp4' @info = MP4Info.open(filename) @artist = @info.ART @album = @info.ALB @title = @info.NAM @tracknum = ( t = @info.TRKN ) ? t.first : 0 @@ -142,23 +159,22 @@ @musicbrainz_infos[key] = value end when 'flac' @info = FlacInfo.new(filename) - tags = convert_tags_encoding(@info.tags, "UTF-8") - @artist = tags["ARTIST"] || tags["artist"] - @album = tags["ALBUM"] || tags["album"] - @title = tags["TITLE"] || tags["title"] - @tracknum = (tags["TRACKNUMBER"]||tags["tracknumber"]).to_i - @date = tags["DATE"]||tags["date"] + @artist = @info.tags["ARTIST"] || @info.tags["artist"] + @album = @info.tags["ALBUM"] || @info.tags["album"] + @title = @info.tags["TITLE"] || @info.tags["title"] + @tracknum = (@info.tags["TRACKNUMBER"]||@info.tags["tracknumber"]).to_i + @date = @info.tags["DATE"]||@info.tags["date"] @length = @info.streaminfo["total_samples"] / @info.streaminfo["samplerate"].to_f @bitrate = File.size(filename).to_f*8/@length/1024 - tags.each do |tagname, tagvalue| + @info.tags.each do |tagname, tagvalue| next unless tagname =~ /^musicbrainz_(.+)$/ - @musicbrainz_infos[$1] = tags[tagname] + @musicbrainz_infos[$1] = @info.tags[tagname] end - @musicbrainz_infos["trmid"] = tags["musicip_puid"] + @musicbrainz_infos["trmid"] = @info.tags["musicip_puid"] #default_fill_musicbrainz_fields else raise(AudioInfoError, "unsupported extension '.#{@extension}'") end @@ -231,18 +247,18 @@ # close the file and commits changes to disk def close if @needs_commit case @info when Mp3Info - Mp3Info.open(@path, :encoding => @encoding) do |info| + Mp3Info.open(@path) do |info| info.tag.artist = @artist info.tag.title = @title info.tag.album = @album info.tag.tracknum = @tracknum end when OggInfo - OggInfo.open(@path, @encoding) do |ogg| + OggInfo.open(@path) do |ogg| { "artist" => @artist, "album" => @album, "title" => @title, "tracknumber" => @tracknum}.each do |k,v| ogg.tag[k] = v.to_s @@ -300,18 +316,17 @@ end end def default_tag_fill(tags = @info.tag) %w{artist album title}.each do |v| - instance_variable_set( "@#{v}".to_sym, sanitize(tags[v].to_s) ) + instance_variable_set( "@#{v}".to_sym, sanitize(tags[v]||"") ) end end def fill_ape_tag(filename) begin @info = ApeTag.new(filename) - #tags = convert_tags_encoding(@info.fields, "UTF-8") tags = @info.fields.inject({}) do |hash, (k, v)| hash[k.downcase] = v ? v.first : nil hash end default_fill_musicbrainz_fields(tags) @@ -319,22 +334,9 @@ @date = tags["year"] @tracknum = tags['track'].to_i rescue ApeTagError end - end - - def convert_tags_encoding(tags_orig, from_encoding) - tags = {} - Iconv.open(@encoding, from_encoding) do |ic| - tags_orig.inject(tags) do |hash, (k, v)| - if v.is_a?(String) - hash[ic.iconv(k)] = ic.iconv(v) - end - hash - end - end - tags end def faad_info(file) stdout, stdout_w = IO.pipe stderr, stderr_w = IO.pipe