Class: Flac2mp3
- Inherits:
-
Object
- Object
- Flac2mp3
- Defined in:
- lib/flac2mp3/version.rb,
lib/flac2mp3.rb
Overview
:nodoc:
Defined Under Namespace
Modules: VERSION
Class Method Summary collapse
- .convert(filename, options = {}) ⇒ Object
- .convert_metadata(infile, outfile) ⇒ Object
- .convert_tags(tags) ⇒ Object
- .default_encoding ⇒ Object
- .string_fields ⇒ Object
- .tag2_fields ⇒ Object
- .tag_formats ⇒ Object
- .tag_mapping ⇒ Object
Instance Method Summary collapse
- #config ⇒ Object
- #convert(filename) ⇒ Object
- #convert_data(filename, outfile) ⇒ Object
- #convert_metadata(filename, outfile) ⇒ Object
- #delete? ⇒ Boolean
- #encoding ⇒ Object
- #flac_command(filename) ⇒ Object
- #get_flacdata(filename) ⇒ Object
-
#initialize(options = {}) ⇒ Flac2mp3
constructor
A new instance of Flac2mp3.
- #load_config ⇒ Object
- #mp3_command(filename) ⇒ Object
- #options ⇒ Object
- #output_filename(filename) ⇒ Object
- #process_conversion(filename) ⇒ Object
- #safequote(filename) ⇒ Object
- #set_mp3data(filename, tags) ⇒ Object
- #set_options(options) ⇒ Object
- #silent? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Flac2mp3
Returns a new instance of Flac2mp3.
7 8 9 10 |
# File 'lib/flac2mp3.rb', line 7 def initialize( = {}) load_config () end |
Class Method Details
.convert(filename, options = {}) ⇒ Object
112 113 114 |
# File 'lib/flac2mp3.rb', line 112 def convert(filename, = {}) new().convert(filename) end |
.convert_metadata(infile, outfile) ⇒ Object
116 117 118 |
# File 'lib/flac2mp3.rb', line 116 def (infile, outfile) new.(infile, outfile) end |
.convert_tags(tags) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/flac2mp3.rb', line 139 def () = {} .each do |key, value| next unless mp3tag = tag_mapping[key] if format = tag_formats[mp3tag] value = format.gsub(/:(\w+)/) do field = $1 [field.to_sym] end end target = tag2_fields.include?(key) ? :tag2 : :tag [mp3tag] = { :target => target, :value => value } end end |
.default_encoding ⇒ Object
159 160 161 |
# File 'lib/flac2mp3.rb', line 159 def default_encoding '--preset standard' end |
.string_fields ⇒ Object
163 164 165 |
# File 'lib/flac2mp3.rb', line 163 def string_fields [:title, :description] end |
.tag2_fields ⇒ Object
167 168 169 |
# File 'lib/flac2mp3.rb', line 167 def tag2_fields [:bpm, :composer, :compilation, :tracktotal, :tracknumber, :disctotal, :discnumber, :tag] end |
.tag_formats ⇒ Object
171 172 173 174 175 176 |
# File 'lib/flac2mp3.rb', line 171 def tag_formats { :TRCK => ':tracknumber/:tracktotal', :TPOS => ':discnumber/:disctotal' } end |
.tag_mapping ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/flac2mp3.rb', line 120 def tag_mapping { :album => :album, :artist => :artist, :bpm => :TBPM, :description => :comments, :composer => :TCOM, :date => :year, :genre => :genre_s, :title => :title, :tracknumber => :TRCK, :tracktotal => :TRCK, :discnumber => :TPOS, :disctotal => :TPOS, :compilation => :TCMP, :tag => :TIT1 } end |
Instance Method Details
#config ⇒ Object
87 88 89 |
# File 'lib/flac2mp3.rb', line 87 def config @config.dup end |
#convert(filename) ⇒ Object
12 13 14 15 16 |
# File 'lib/flac2mp3.rb', line 12 def convert(filename) raise TypeError, "'#{filename}' is not a file" unless FileTest.file?(filename) process_conversion(filename) File.delete(filename) if delete? end |
#convert_data(filename, outfile) ⇒ Object
24 25 26 |
# File 'lib/flac2mp3.rb', line 24 def convert_data(filename, outfile) system "#{flac_command(filename)} | #{mp3_command(outfile)}" end |
#convert_metadata(filename, outfile) ⇒ Object
42 43 44 |
# File 'lib/flac2mp3.rb', line 42 def (filename, outfile) set_mp3data(outfile, get_flacdata(filename)) end |
#delete? ⇒ Boolean
91 92 93 |
# File 'lib/flac2mp3.rb', line 91 def delete? !![:delete] end |
#encoding ⇒ Object
99 100 101 |
# File 'lib/flac2mp3.rb', line 99 def encoding [:encoding] || self.class.default_encoding end |
#flac_command(filename) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/flac2mp3.rb', line 28 def flac_command(filename) command = 'flac' command << ' --silent' if silent? "#{command} --stdout --decode #{safequote(filename)}" end |
#get_flacdata(filename) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/flac2mp3.rb', line 46 def get_flacdata(filename) hash = {} FlacInfo.new(filename)..each do |(key, value)| key = key.to_s.downcase.to_sym value = value.to_i if value.respond_to?(:match) and value.match(/^\d+$/) value = value.to_s if self.class.string_fields.include?(key) value = value.force_encoding('UTF-8') if value.is_a?(String) hash[key] = value end hash end |
#load_config ⇒ Object
71 72 73 74 75 76 |
# File 'lib/flac2mp3.rb', line 71 def load_config @config = {} yaml = YAML.load(File.read(File.('~/.flac2mp3'))) || {} yaml.each { |k, v| @config[k.to_sym] = v } rescue Errno::ENOENT end |
#mp3_command(filename) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/flac2mp3.rb', line 35 def mp3_command(filename) command = 'lame' command << ' --silent' if silent? "#{command} #{encoding} - #{safequote(filename)}" end |
#options ⇒ Object
83 84 85 |
# File 'lib/flac2mp3.rb', line 83 def @options.dup end |
#output_filename(filename) ⇒ Object
103 104 105 |
# File 'lib/flac2mp3.rb', line 103 def output_filename(filename) filename.chomp('.flac') + '.mp3' end |
#process_conversion(filename) ⇒ Object
18 19 20 21 22 |
# File 'lib/flac2mp3.rb', line 18 def process_conversion(filename) outfile = output_filename(filename) convert_data(filename, outfile) (filename, outfile) end |
#safequote(filename) ⇒ Object
107 108 109 |
# File 'lib/flac2mp3.rb', line 107 def safequote(filename) filename.gsub(/(\W)/, '\\\\\1') end |
#set_mp3data(filename, tags) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/flac2mp3.rb', line 61 def set_mp3data(filename, ) raise TypeError, "Tags must be a hash" unless .is_a?(Hash) Mp3Info.open(filename) do |mp3| self.class.().each do |mp3tag, data| tag = mp3.send(data[:target]) tag.send("#{mp3tag}=", data[:value]) end end end |
#set_options(options) ⇒ Object
78 79 80 81 |
# File 'lib/flac2mp3.rb', line 78 def () raise TypeError, 'options must be a hash' unless .is_a?(Hash) @options = config.merge() end |
#silent? ⇒ Boolean
95 96 97 |
# File 'lib/flac2mp3.rb', line 95 def silent? !![:silent] end |