Class: AudioInfo
- Inherits:
-
Object
- Object
- AudioInfo
- Defined in:
- lib/audioinfo.rb
Defined Under Namespace
Classes: Album
Constant Summary collapse
- MUSICBRAINZ_FIELDS =
{ "trmid" => "TRM Id", "artistid" => "Artist Id", "albumid" => "Album Id", "albumtype" => "Album Type", "albumstatus" => "Album Status", "albumartistid" => "Album Artist Id", "sortname" => "Sort Name", "trackid" => "Track Id" }
- SUPPORTED_EXTENSIONS =
%w{mp3 ogg mpc wma mp4 aac m4a flac}
- VERSION =
"0.1.7"
Instance Attribute Summary collapse
-
#album ⇒ Object
Returns the value of attribute album.
-
#artist ⇒ Object
Returns the value of attribute artist.
-
#bitrate ⇒ Object
readonly
Returns the value of attribute bitrate.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#musicbrainz_infos ⇒ Object
readonly
Returns the value of attribute musicbrainz_infos.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#title ⇒ Object
Returns the value of attribute title.
-
#tracknum ⇒ Object
Returns the value of attribute tracknum.
-
#vbr ⇒ Object
readonly
Returns the value of attribute vbr.
Class Method Summary collapse
-
.open(*args) ⇒ Object
“block version” of #new().
Instance Method Summary collapse
-
#[](key) ⇒ Object
hash-like access to tag.
-
#close ⇒ Object
close the file and commits changes to disk.
-
#initialize(filename, encoding = 'utf-8') ⇒ AudioInfo
constructor
open the file with path
fn
and convert all tags from/to specifiedencoding
. -
#mb_tagged? ⇒ Boolean
check if the file is correctly tagged by MusicBrainz.
-
#to_h ⇒ Object
convert tags to hash.
Constructor Details
#initialize(filename, encoding = 'utf-8') ⇒ AudioInfo
open the file with path fn
and convert all tags from/to specified encoding
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/audioinfo.rb', line 53 def initialize(filename, encoding = 'utf-8') 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) default_tag_fill #"TXXX"=> #["MusicBrainz TRM Id\000", #"MusicBrainz Artist Id\000aba64937-3334-4c65-90a1-4e6b9d4d7ada", #"MusicBrainz Album Id\000e1a223c1-cbc2-427f-a192-5d22fefd7c4c", #"MusicBrainz Album Type\000album", #"MusicBrainz Album Status\000official", #"MusicBrainz Album Artist Id\000"] if (arr = @info.tag2["TXXX"]).is_a?(Array) fields = MUSICBRAINZ_FIELDS.invert arr.each do |val| if val =~ /^MusicBrainz (.+)\000(.*)$/ short_name = fields[$1] @musicbrainz_infos[short_name] = $2 end end end @bitrate = @info.bitrate i = @info.tag.tracknum @tracknum = (i.is_a?(Array) ? i.last : i).to_i @length = @info.length.to_i @date = @info.tag["date"] @vbr = @info.vbr @info.close when 'ogg' @info = OggInfo.new(filename, @encoding) default_fill_musicbrainz_fields default_tag_fill @bitrate = @info.bitrate/1000 @tracknum = @info.tag.tracknumber.to_i @length = @info.length.to_i @date = @info.tag["date"] @vbr = true @info.close when 'mpc' fill_ape_tag(filename) mpc_info = MpcInfo.new(filename) @bitrate = mpc_info.infos['bitrate']/1000 @length = mpc_info.infos['length'] when 'ape' fill_ape_tag(filename) when 'wma' @info = WmaInfo.new(filename, :encoding => @encoding) @artist = @info.["Author"] @album = @info.["AlbumTitle"] @title = @info.["Title"] @tracknum = @info.["TrackNumber"].to_i @date = @info.["Year"] @bitrate = @info.info["bitrate"] @length = @info.info["playtime_seconds"] MUSICBRAINZ_FIELDS.each do |key, original_key| @musicbrainz_infos[key] = @info.info["MusicBrainz/" + original_key.tr(" ", "")] || @info.info["MusicBrainz/" + original_key] end when 'aac', 'mp4', 'm4a' @info = MP4Info.open(filename) @artist = @info.ART @album = @info.ALB @title = @info.NAM @tracknum = ( t = @info.TRKN ) ? t.first : 0 @date = @info.DAY @bitrate = @info.BITRATE @length = @info.SECS mapping = MUSICBRAINZ_FIELDS.invert faad_info(filename).match(/^MusicBrainz (.+)$/) do name, value = $1.split(/: /, 2) key = mapping[name] @musicbrainz_infos[key] = value end when 'flac' @info = FlacInfo.new(filename) = (@info., "UTF-8") @artist = ["ARTIST"] || ["artist"] @album = ["ALBUM"] || ["album"] @title = ["TITLE"] || ["title"] @tracknum = (["TRACKNUMBER"]||["tracknumber"]).to_i @date = ["DATE"]||["date"] @length = @info.streaminfo["total_samples"] / @info.streaminfo["samplerate"].to_f @bitrate = File.size(filename).to_f*8/@length/1024 .each do |tagname, tagvalue| next unless tagname =~ /^musicbrainz_(.+)$/ @musicbrainz_infos[$1] = [tagname] end @musicbrainz_infos["trmid"] = ["musicip_puid"] #default_fill_musicbrainz_fields else raise(AudioInfoError, "unsupported extension '.#{@extension}'") end if @tracknum == 0 @tracknum = nil end @musicbrainz_infos.delete_if { |k, v| v.nil? } @hash = { "artist" => @artist, "album" => @album, "title" => @title, "tracknum" => @tracknum, "date" => @date, "length" => @length, "bitrate" => @bitrate, } rescue Exception, Mp3InfoError, OggInfoError, ApeTagError => e raise AudioInfoError, e.to_s, e.backtrace end @needs_commit = false end |
Instance Attribute Details
#album ⇒ Object
Returns the value of attribute album.
34 35 36 |
# File 'lib/audioinfo.rb', line 34 def album @album end |
#artist ⇒ Object
Returns the value of attribute artist.
34 35 36 |
# File 'lib/audioinfo.rb', line 34 def artist @artist end |
#bitrate ⇒ Object (readonly)
Returns the value of attribute bitrate.
33 34 35 |
# File 'lib/audioinfo.rb', line 33 def bitrate @bitrate end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
34 35 36 |
# File 'lib/audioinfo.rb', line 34 def date @date end |
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
33 34 35 |
# File 'lib/audioinfo.rb', line 33 def extension @extension end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
34 35 36 |
# File 'lib/audioinfo.rb', line 34 def length @length end |
#musicbrainz_infos ⇒ Object (readonly)
Returns the value of attribute musicbrainz_infos.
33 34 35 |
# File 'lib/audioinfo.rb', line 33 def musicbrainz_infos @musicbrainz_infos end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
33 34 35 |
# File 'lib/audioinfo.rb', line 33 def path @path end |
#title ⇒ Object
Returns the value of attribute title.
34 35 36 |
# File 'lib/audioinfo.rb', line 34 def title @title end |
#tracknum ⇒ Object
Returns the value of attribute tracknum.
33 34 35 |
# File 'lib/audioinfo.rb', line 33 def tracknum @tracknum end |
#vbr ⇒ Object (readonly)
Returns the value of attribute vbr.
33 34 35 |
# File 'lib/audioinfo.rb', line 33 def vbr @vbr end |
Class Method Details
.open(*args) ⇒ Object
“block version” of #new()
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/audioinfo.rb', line 37 def self.open(*args) audio_info = self.new(*args) ret = nil if block_given? begin ret = yield(audio_info) ensure audio_info.close end else ret = audio_info end ret end |
Instance Method Details
#[](key) ⇒ Object
hash-like access to tag
222 223 224 |
# File 'lib/audioinfo.rb', line 222 def [](key) @hash[key] end |
#close ⇒ Object
close the file and commits changes to disk
232 233 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 |
# File 'lib/audioinfo.rb', line 232 def close if @needs_commit case @info when Mp3Info Mp3Info.open(@path, :encoding => @encoding) 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| { "artist" => @artist, "album" => @album, "title" => @title, "tracknumber" => @tracknum}.each do |k,v| ogg.tag[k] = v.to_s end end else raise(AudioInfoError, "implement me") end end @needs_commit end |
#mb_tagged? ⇒ Boolean
check if the file is correctly tagged by MusicBrainz
277 278 279 |
# File 'lib/audioinfo.rb', line 277 def mb_tagged? ! @musicbrainz_infos.empty? end |
#to_h ⇒ Object
convert tags to hash
227 228 229 |
# File 'lib/audioinfo.rb', line 227 def to_h @hash end |