r2mp3 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +12 -0
- data/example/cli_converter.rb +11 -0
- data/example/example1.rb +18 -0
- data/lib/r2mp3.rb +209 -0
- metadata +60 -0
data/README
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'r2mp3'
|
4
|
+
options = Options.new(ARGV)
|
5
|
+
Converter.new(:convert=>:wma,:file=>options[:file]){|f| f.to_mp3 } unless options[:wma].nil? or options[:file].nil?
|
6
|
+
Converter.new(:convert=>:wma,:dir=>options[:dir]){|d| d.to_mp3 } unless options[:wma].nil? or options[:dir].nil?
|
7
|
+
Converter.new(:convert=>:aac,:file=>options[:file]){|f| f.to_mp3 } unless options[:aac].nil? or options[:file].nil?
|
8
|
+
Converter.new(:convert=>:aac,:dir=>options[:dir]){|d| d.to_mp3 } unless options[:aac].nil? or options[:dir].nil?
|
9
|
+
Converter.new(:convert=>:ra,:file=>options[:file]){|f| f.to_mp3 } unless options[:ra].nil? or options[:file].nil?
|
10
|
+
Converter.new(:convert=>:ra,:dir=>options[:dir]){|d| d.to_mp3 } unless options[:ra].nil? or options[:dir].nil?
|
11
|
+
|
data/example/example1.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'r2mp3.rb'
|
4
|
+
|
5
|
+
wma_file = "wma_example.wma"
|
6
|
+
wma_dir = "wma_dir"
|
7
|
+
aac_file = "aac_example.m4a"
|
8
|
+
aac_dir = "aac_dir"
|
9
|
+
rm_file = "rm_example.rm"
|
10
|
+
rm_dir = "rm_dir"
|
11
|
+
|
12
|
+
Converter.new(:convert=>:wma,:file=>wma_file){|f| f.to_mp3 } unless options[:wma].nil? or options[:file].nil?
|
13
|
+
Converter.new(:convert=>:wma,:dir=>wma_dir){|d| d.to_mp3 } unless options[:wma].nil? or options[:dir].nil?
|
14
|
+
Converter.new(:convert=>:aac,:file=>aac_file){|f| f.to_mp3 } unless options[:aac].nil? or options[:file].nil?
|
15
|
+
Converter.new(:convert=>:aac,:dir=>aac_dir){|d| d.to_mp3 } unless options[:aac].nil? or options[:dir].nil?
|
16
|
+
Converter.new(:convert=>:ra,:file=>ra_file){|f| f.to_mp3 } unless options[:ra].nil? or options[:file].nil?
|
17
|
+
Converter.new(:convert=>:ra,:dir=>ra_dir){|d| d.to_mp3 } unless options[:ra].nil? or options[:dir].nil?
|
18
|
+
|
data/lib/r2mp3.rb
ADDED
@@ -0,0 +1,209 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'optparse'
|
3
|
+
require 'optparse/time'
|
4
|
+
require 'ostruct'
|
5
|
+
require 'fileutils'
|
6
|
+
require 'ftools'
|
7
|
+
require 'lame_adapter'
|
8
|
+
|
9
|
+
class Options < Hash
|
10
|
+
def initialize(arg)
|
11
|
+
begin
|
12
|
+
optstruct = OpenStruct.new
|
13
|
+
options = OptionParser.new do |opts|
|
14
|
+
opts.banner = "usage: #{__FILE__} [options]"
|
15
|
+
opts.on('-w','--wma-to-mp3','convert .wma to mp3 ')do
|
16
|
+
self[:wma] = true
|
17
|
+
end
|
18
|
+
opts.on('-a','--aac-to-mp3','convert .m4a to mp3')do
|
19
|
+
self[:aac] = true
|
20
|
+
end
|
21
|
+
opts.on('-r','--ra-to-mp3','convert .rm to mp3')do
|
22
|
+
self[:ra] = true
|
23
|
+
end
|
24
|
+
opts.on('-f','--file [filename]','file') do |f|
|
25
|
+
self[:file] = f
|
26
|
+
end
|
27
|
+
opts.on('-d','--dir [directoryname]','directory') do |d|
|
28
|
+
self[:dir] = d
|
29
|
+
end
|
30
|
+
opts.on_tail('-h','--help','help') do
|
31
|
+
puts opts
|
32
|
+
exit
|
33
|
+
end
|
34
|
+
raise "print help" if ARGV.size < 1
|
35
|
+
raise "choose wma or aac " if self[:wma] and self[:aac]
|
36
|
+
end.parse!(arg)
|
37
|
+
rescue OptionParser::InvalidOption, RuntimeError => e
|
38
|
+
puts e
|
39
|
+
puts "#{$0} -h or --help , for help"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class Converter
|
45
|
+
def initialize(opts={},&block)
|
46
|
+
$opts = opts
|
47
|
+
yield(opts) if block_given?
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class Mplayer
|
52
|
+
# Can be used to dump any audio file to wav
|
53
|
+
def self.dump_to_wav(input, output, opts={})
|
54
|
+
begin
|
55
|
+
platform = `uname -a`
|
56
|
+
binary = "#{%r{Linux} =~ platform}".empty?? "/Applications/mplayer":"mplayer"
|
57
|
+
FileUtils.rm("audiodump.wav") if File.exists? "audiodump.wav"
|
58
|
+
opts[:resample] ||= '44100'
|
59
|
+
args = ""
|
60
|
+
force_opt = {:vo => "null", :vc => "dummy", :af => "resample=#{opts[:resample]}", :ao => "pcm:waveheader"}
|
61
|
+
force_opt.each{|k,v| args << "-#{k.to_s} #{v} "}
|
62
|
+
cmd = "#{binary} #{args} #{input}"
|
63
|
+
success = system(cmd)
|
64
|
+
puts "Success? #{success}"
|
65
|
+
FileUtils.mv("audiodump.wav", output) if File.exists? "audiodump.wav"
|
66
|
+
success
|
67
|
+
rescue Errno::ENOENT => err
|
68
|
+
puts "No audiodump :" + err
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
class Convert
|
75
|
+
def self.ra_to_wav(input, output=nil, opts={})
|
76
|
+
conversion(:rm, :wav, input, output, opts) do | input, output, opts |
|
77
|
+
Mplayer.dump_to_wav(input, output, opts)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.ra_to_mp3(input, output=nil, opts={})
|
82
|
+
output ||= input.gsub(/\.rm$/,".mp3")
|
83
|
+
ra_to_wav(input, 'temp.wav', opts) and wav_to_mp3('temp.wav', output, opts.merge({:delete_input=>true}))
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.aac_to_wav(input, output=nil, opts={})
|
87
|
+
conversion(:m4a, :wav, input, output, opts) do | input, output, opts |
|
88
|
+
Mplayer.dump_to_wav(input, output, opts)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.aac_to_mp3(input, output=nil, opts={})
|
93
|
+
output ||= input.gsub(/\.m4a$/,".mp3")
|
94
|
+
aac_to_wav(input, 'temp.wav', opts) and wav_to_mp3('temp.wav', output, opts.merge({:delete_input=>true}))
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.wma_to_mp3(input, output=nil, opts={})
|
98
|
+
output ||= input.gsub(/\.wma$/,".mp3")
|
99
|
+
wma_to_wav(input, 'temp.wav', opts) and wav_to_mp3('temp.wav', output, opts.merge({:delete_input=>true}))
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.wma_to_wav(input, output=nil, opts={})
|
103
|
+
conversion(:wma, :wav, input, output, opts) do | input, output, opts |
|
104
|
+
Mplayer.dump_to_wav(input, output, opts)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.wav_to_mp3(input, output=nil, opts={})
|
109
|
+
conversion(:wav, :mp3, input, output, opts) do | input, output, opts |
|
110
|
+
opts[:mode] ||= :stereo
|
111
|
+
lame = LameAdapter.new
|
112
|
+
lame.input_file input
|
113
|
+
lame.output_file output
|
114
|
+
lame.mode opts[:mode]
|
115
|
+
lame.convert!
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.conversion(input_format, output_format, input_file, output_file, opts={}, &block)
|
120
|
+
puts "...conversion"
|
121
|
+
input_format = input_format.to_s
|
122
|
+
output_format = output_format.to_s
|
123
|
+
input_regexp = /\.#{input_format}$/
|
124
|
+
return false unless File.exists? input_file and File.extname(input_file) =~ input_regexp
|
125
|
+
output_file ||= input_file.gsub(input_regexp,".#{output_format}")
|
126
|
+
success = yield(input_file, output_file, opts)
|
127
|
+
FileUtils.rm input_file if opts[:delete_input]
|
128
|
+
success and File.exists? output_file
|
129
|
+
end
|
130
|
+
|
131
|
+
def self.wma_dir_to_mp3(dirname)
|
132
|
+
Dir["#{dirname}/*.wma"].each do |name|
|
133
|
+
if !name[/[\s]+/].nil? then
|
134
|
+
new_name = name.gsub(" ","_").downcase
|
135
|
+
FileUtils.mv "#{name}", "#{new_name}", :force => true # no error
|
136
|
+
name = new_name
|
137
|
+
end
|
138
|
+
wma_to_mp3(name)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def self.wma_file_to_mp3(filename)
|
143
|
+
wma_to_mp3(filename)
|
144
|
+
end
|
145
|
+
|
146
|
+
def self.aac_dir_to_mp3(dirname)
|
147
|
+
Dir["#{dirname}/*.m4a"].each do |name|
|
148
|
+
if !name[/[\s]+/].nil? then
|
149
|
+
new_name = name.gsub(" ","_").downcase
|
150
|
+
FileUtils.mv "#{name}", "#{new_name}", :force => true # no error
|
151
|
+
name = new_name
|
152
|
+
end
|
153
|
+
aac_to_mp3(name)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def self.aac_file_to_mp3(filename)
|
158
|
+
aac_to_mp3(filename)
|
159
|
+
end
|
160
|
+
|
161
|
+
def self.ra_dir_to_mp3(dirname)
|
162
|
+
Dir["#{dirname}/*.rm"].each do |name|
|
163
|
+
if !name[/[\s]+/].nil? then
|
164
|
+
new_name = name.gsub(" ","_").downcase
|
165
|
+
FileUtils.mv "#{name}", "#{new_name}", :force => true # no error
|
166
|
+
name = new_name
|
167
|
+
end
|
168
|
+
ra_to_mp3(name)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def self.ra_file_to_mp3(filename)
|
173
|
+
ra_to_mp3(filename)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
class Hash
|
179
|
+
def to_mp3
|
180
|
+
case $opts[:convert]
|
181
|
+
when :wma
|
182
|
+
unless $opts[:dir].nil? then
|
183
|
+
puts "convert all wma files in #{self[:dir]}"
|
184
|
+
Convert.wma_dir_to_mp3(self[:dir])
|
185
|
+
else
|
186
|
+
puts "(wma) convert #{self[:file]} to mp3"
|
187
|
+
Convert.wma_file_to_mp3(self[:file])
|
188
|
+
end
|
189
|
+
when :aac
|
190
|
+
unless $opts[:dir].nil? then
|
191
|
+
puts "convert all aac files in #{self[:dir]}"
|
192
|
+
Convert.aac_dir_to_mp3(self[:dir])
|
193
|
+
else
|
194
|
+
puts "(aac) convert #{self[:file]} to mp3"
|
195
|
+
Convert.aac_file_to_mp3(self[:file])
|
196
|
+
end
|
197
|
+
when :ra
|
198
|
+
unless $opts[:dir].nil? then
|
199
|
+
puts "convert all ra files in #{self[:dir]}"
|
200
|
+
Convert.ra_dir_to_mp3(self[:dir])
|
201
|
+
else
|
202
|
+
puts "(ra) convert #{self[:file]} to mp3"
|
203
|
+
Convert.ra_file_to_mp3(self[:file])
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: r2mp3
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2007-10-16 00:00:00 +07:00
|
8
|
+
summary: A library for converting any audio to MP3 (MPEG-1 Layer3)
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: zdk@codegent.com
|
12
|
+
homepage: ""
|
13
|
+
rubyforge_project:
|
14
|
+
description: This library intends to convert all available audio files on the planet to MP3
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Warachet Samtalee
|
31
|
+
files:
|
32
|
+
- README
|
33
|
+
- lib/r2mp3.rb
|
34
|
+
- example/rm_dir
|
35
|
+
- example/wma_dir
|
36
|
+
- example/aac_dir
|
37
|
+
- example/example1.rb
|
38
|
+
- example/cli_converter.rb
|
39
|
+
test_files: []
|
40
|
+
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
executables: []
|
46
|
+
|
47
|
+
extensions: []
|
48
|
+
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
dependencies:
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: lame_adapter
|
54
|
+
version_requirement:
|
55
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 0.0.0
|
60
|
+
version:
|