Class: Flac2mp3

Inherits:
Object
  • Object
show all
Defined in:
lib/flac2mp3/version.rb,
lib/flac2mp3.rb

Overview

:nodoc:

Defined Under Namespace

Modules: VERSION

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Flac2mp3

Returns a new instance of Flac2mp3.



7
8
9
10
# File 'lib/flac2mp3.rb', line 7

def initialize(options = {})
  load_config
  set_options(options)
end

Class Method Details

.convert(filename, options = {}) ⇒ Object



112
113
114
# File 'lib/flac2mp3.rb', line 112

def convert(filename, options = {})
  new(options).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 convert_tags(tags)
  mp3_tags = {}

  tags.each do |key, value|
    next unless mp3tag = tag_mapping[key]

    if format = tag_formats[mp3tag]
      value = format.gsub(/:(\w+)/) do
        field = $1
        tags[field.to_sym]
      end
    end

    target = tag2_fields.include?(key) ? :tag2 : :tag
    mp3_tags[mp3tag] = { :target => target, :value => value }
  end

  mp3_tags
end

.default_encodingObject



159
160
161
# File 'lib/flac2mp3.rb', line 159

def default_encoding
  '--preset standard'
end

.string_fieldsObject



163
164
165
# File 'lib/flac2mp3.rb', line 163

def string_fields
  [:title, :description]
end

.tag2_fieldsObject



167
168
169
# File 'lib/flac2mp3.rb', line 167

def tag2_fields
  [:bpm, :composer, :compilation, :tracktotal, :tracknumber, :disctotal, :discnumber, :tag]
end

.tag_formatsObject



171
172
173
174
175
176
# File 'lib/flac2mp3.rb', line 171

def tag_formats
  {
    :TRCK => ':tracknumber/:tracktotal',
    :TPOS => ':discnumber/:disctotal'
  }
end

.tag_mappingObject



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

#configObject



87
88
89
# File 'lib/flac2mp3.rb', line 87

def config
  @config.dup
end

#convert(filename) ⇒ Object

Raises:

  • (TypeError)


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

Returns:

  • (Boolean)


91
92
93
# File 'lib/flac2mp3.rb', line 91

def delete?
  !!options[:delete]
end

#encodingObject



99
100
101
# File 'lib/flac2mp3.rb', line 99

def encoding
  options[: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).tags.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_configObject



71
72
73
74
75
76
# File 'lib/flac2mp3.rb', line 71

def load_config
  @config = {}
  yaml = YAML.load(File.read(File.expand_path('~/.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

#optionsObject



83
84
85
# File 'lib/flac2mp3.rb', line 83

def options
  @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

Raises:

  • (TypeError)


61
62
63
64
65
66
67
68
69
# File 'lib/flac2mp3.rb', line 61

def set_mp3data(filename, tags)
  raise TypeError, "Tags must be a hash" unless tags.is_a?(Hash)
  Mp3Info.open(filename) do |mp3|
    self.class.convert_tags(tags).each do |mp3tag, data|
      tag = mp3.send(data[:target])
      tag.send("#{mp3tag}=", data[:value])
    end
  end
end

#set_options(options) ⇒ Object

Raises:

  • (TypeError)


78
79
80
81
# File 'lib/flac2mp3.rb', line 78

def set_options(options)
  raise TypeError, 'options must be a hash' unless options.is_a?(Hash)
  @options = config.merge(options)
end

#silent?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/flac2mp3.rb', line 95

def silent?
  !!options[:silent]
end