Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 89 lines (72 sloc) 2.478 kB
#!/usr/bin/env ruby
require 'optparse'
# Default options
options = {
:verbose => false,
:preset => "High Profile"
}
OptionParser.new do |opt|
opt.banner = "Usage: HandBrakeCLIBatch [sourcedir] [destination] \n" \
" HandBrakeCLIBatch SOURCEFILE [sourcefile ...] [destination] \n" \
"Options: "
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
args = ARGV.dup
# Without args, use the current directory as an implicit source directory
if args.empty?
movies_dir = Dir.pwd
else # Try treating the first argument as a source dir if it's a directory
movies_dir = File.expand_path(args.shift) if args.first && File.directory?(args.first)
end
# If we have a directory source
if movies_dir
# Search for avi and mkv files in the directory
movies_glob = File.join(movies_dir, "**/*.{avi,mkv,dvdmedia}")
files = Dir[movies_glob]
puts "Converting files matching pattern\n #{movies_glob}"
puts
abort "No files to convert!" if files.empty?
end
# If the last argument left is a directory, it's the destination
if args.last && File.directory?(args.last)
dest_dir = File.expand_path(args.pop)
else # Default to the source dir or the current directory
dest_dir = movies_dir || Dir.pwd
end
abort "Destination directory '#{dest_dir}' does not exist!" unless File.exist?(dest_dir)
# With a dest dir but no source dir, treat remaining args as source files
files ||= args
files.each do |path|
abort "Source file '#{path}' does not exist!" unless File.exist?(path)
end
# Explain what we're about to do
files_count = "#{files.size} file#{'s' unless files.size == 1}"
puts "Converting #{files_count} into"
puts " #{dest_dir}"
puts " with preset '#{options[:preset]}'"
puts
# Convert each file
files.each do |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.map {|arg| arg =~ /\s/ ? %{"#{arg}"} : arg }.join(" ")
system *cmd
end
puts "Done!"
Jump to Line
Something went wrong with that request. Please try again.