Skip to content

Instantly share code, notes, and snippets.

@mweppler
Created October 27, 2011 04:37
Show Gist options
  • Save mweppler/1318785 to your computer and use it in GitHub Desktop.
Save mweppler/1318785 to your computer and use it in GitHub Desktop.
Batch encoder for handbrakecli.
#!/usr/bin/env ruby
require 'etc'
$currentUser = Etc.getlogin
def execute_encode_file
%x[sh ./batch_handbrakecli.command]
end
def find_valid_movies
valid_movie_extensions = Array['.avi','.mov']
$inputFiles = Array.new
Dir.glob('**/*').each do |file|
unless File.directory?(file)
if valid_movie_extensions.include?(file[file.rindex('.'),file.size])
$inputFiles << file
end
end
end
end
def invalid_arguments
puts "Invalid Argument(s)"
puts " Usage: ./batch_handbrake.rb /directory/with/movies/to/encode /directory/to/save/encoded/movies\n"
exit
end
def list_movies
$inputFiles.each do |file|
puts file
end
end
def write_encode_file
newOutPath = $outputPath.gsub("\\", "")
newOutPath.gsub!(" ", "\\ ")
unless File.exists?(newOutPath)
Dir::mkdir(newOutPath)
end
newInPath = $inputPath.gsub("\\", "")
newInPath.gsub!(" ", "\\ ")
handbrakeCli = "/Applications/HandBrakeCLI"
presets = Array['Universal','iPod','iPhone & iPod Touch','iPhone 4','iPad','AppleTV','AppleTV 2']
preset = "AppleTV 2"
options = "" #Add any additional options you want here
File.open('batch_handbrakecli.command','w') do |command|
command.puts "cd #{newInPath}"
$inputFiles.each do |file|
file_pieces = file.split('/')
outputPathWName = newOutPath + file_pieces.last.chop.chop.chop.chop + ".mp4"
#file.chomp!
command.puts "#{handbrakeCli} -i \"#{file}\" -o \"#{outputPathWName}\" --preset=\"#{preset}\" #{options}"
end
end
%x[chmod +x batch_handbrakecli.command]
end
if ARGV[0] == nil or ARGV[1] == nil
invalid_arguments
else
$inputPath = ARGV[0]
$outputPath = ARGV[1]
if File.exists?($inputPath)
Dir.chdir($inputPath)
find_valid_movies
# list_movies
write_encode_file
execute_encode_file
else
invalid_arguments
end
end
puts "Finished!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment