Skip to content

Instantly share code, notes, and snippets.

@indirect
Created December 20, 2010 18:24
Show Gist options
  • Save indirect/748762 to your computer and use it in GitHub Desktop.
Save indirect/748762 to your computer and use it in GitHub Desktop.
a script to recursively handbrake-ify your avis and mkvs
#!/usr/bin/env ruby
require 'optparse'
options = {
:verbose => false,
:preset => "High Profile"
}
OptionParser.new do |opt|
opt.banner = "Usage: HandBrakeCLIBatch [-v | -p preset] [source] [destination]"
opt.on("-v", "--verbose", "Run HandBrake in verbose mode (default: false)") do |v|
options[:verbose] = v
end
opt.on("-p", "--preset PRESET", "Set the HandBrake preset by name (default: High Profile)") do |p|
options[:preset] = p
end
opt.parse!
end
movies_dir = ARGV[0] || Dir.pwd
abort "Source directory '#{movies_dir}' does not exist!" unless File.exist?(movies_dir)
dest_dir = ARGV[1] || movies_dir
abort "Destination directory '#{dest_dir}' does not exist!" unless File.exist?(dest_dir)
puts "Converting from"
puts " #{movies_dir} to"
puts " #{dest_dir}"
puts " with preset '#{options[:preset]}'"
movies_glob = File.join(movies_dir, "**/*.{avi,mkv}")
files = Dir[movies_glob]
abort("No files to convert!") if files.empty?
files.each do |file|
dest_dir = File.dirname(file)
new_file = File.basename(file, File.extname(file)) + ".m4v"
dest_file = File.join(dest_dir, new_file)
if File.exist?(dest_file)
STDERR.puts "Converted file already exists!\n #{dest_file}"
next
else
puts "Converting #{File.basename(dest_file)}"
end
cmd = ["HandBrakeCLI"]
cmd << "-v" if options[:verbose]
cmd << %{-i "#{file}"}
cmd << %{-o "#{dest_file}"}
cmd << %{--preset "#{options[:preset]}"}
puts cmd.join(" ")
system cmd.join(" ")
end
puts "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment