Skip to content

Instantly share code, notes, and snippets.

@ahawkins
Created April 11, 2011 06:12
Show Gist options
  • Save ahawkins/913130 to your computer and use it in GitHub Desktop.
Save ahawkins/913130 to your computer and use it in GitHub Desktop.
require 'fileutils'
FLAC_CMD = '/usr/bin/flac'
LAME_CMD = '/usr/bin/lame'
NUMBER_OF_THREADS = 6
desc "Convert all FLAC's to mp3s and delete source"
task :convert do
base_path = File.expand_path('..', __FILE__)
convert_cmds = Dir["#{base_path}/**/*.flac"].inject([]) do |cmds, flac|
output_directory = File.dirname flac
file_name = File.basename(flac, '.flac')
wav_file = File.join(output_directory, "#{file_name}.wav")
mp3_file = File.join(output_directory, "#{file_name}.mp3")
decode_cmd = %Q{#{FLAC_CMD} -f -d -o "#{wav_file}" "#{flac}"}
encode_cmd = %Q{#{LAME_CMD} --quiet -h -b 320 --cbr "#{wav_file}" "#{mp3_file}"}
clean_up_cmd = %Q{rm "#{wav_file}" && rm "#{flac}"}
convert_cmd = "#{decode_cmd} && #{encode_cmd} && #{clean_up_cmd}"
cmds << convert_cmd
cmds
end
queues = []
convert_cmds.each_index do |i|
num = i % NUMBER_OF_THREADS
queues[num] ||= []
queues[num] << convert_cmds[i]
end
threads = []
queues.each do |queue|
threads << Thread.new(queue) do |q|
q.each {|cmd| `#{cmd}`}
end
end
threads.each {|thread| thread.join}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment