Skip to content

Instantly share code, notes, and snippets.

@oneman
Created January 27, 2010 03:00
Show Gist options
  • Save oneman/287506 to your computer and use it in GitHub Desktop.
Save oneman/287506 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# encode dir of flacs to ogg and move to an ogg dir
require 'fileutils'
$oggdir = "/home/oneman/Music/Ogg/"
$maxthreads = 4
$total_thread_time = 0
start_time = Time.now
dir = ARGV.shift
FileUtils.mkdir_p $oggdir + dir
def oggit(dir, file)
start_time = Time.now
puts "Started Encoding: #{file}"
oggenc = "oggenc -Q -q9 " + "\"#{dir}#{file}\""
Kernel.system(oggenc)
thread_time = Time.now - start_time
$total_thread_time = $total_thread_time + thread_time
puts "Finished Encoding: #{file} took #{thread_time}"
end
puts "Encoding #{dir} flacs to ogg with #{$maxthreads} threads"
threads = []
Dir.foreach("./" + dir) do |file|
# only flac files
if file.include? ".flac"
# only go if we are under max threads else wait until we are
if Thread.list.length >= ($maxthreads + 1)
while(Thread.list.length >= ($maxthreads + 1)) do
sleep 0.1
end
end
# do it
Thread.new { oggit(dir, file); }
end
end
if Thread.list.length != 1
while(Thread.list.length != 1) do
sleep 0.1
end
end
end_time = Time.now - start_time
puts "Encoding Finished"
saved_time = $total_thread_time - end_time
puts "Took #{end_time} time, total thread time is #{$total_thread_time} multithread saved #{saved_time}"
move = "mv " + "\"#{dir}\"*ogg #{$oggdir}\"#{dir}\""
Kernel.system(move)
puts "Oggs moved"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment