Last active
June 20, 2018 14:41
-
-
Save mrichman/58ff7e8fe4d89693f67b9b80082df9ba to your computer and use it in GitHub Desktop.
Batch convert videos into MP4 format using HandBrakeCLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
# Default options | |
options = { | |
verbose: false, | |
preset: "Fast 1080p30" | |
} | |
OptionParser.new do |opt| | |
opt.banner = "Usage: brake.rb [sourcedir] [destination] \n" \ | |
" brake.rb 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: Fast 1080p30)') 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 mp4, avi, and mkv files in the directory | |
movies_glob = File.join(movies_dir, '**/*.{mp4,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)) + "-handbraked.mp4" | |
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!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you don't have
HandBrakeCLI
installed,brew install handbrake
.