Skip to content

Instantly share code, notes, and snippets.

@czj
Created August 5, 2012 07:35
Show Gist options
  • Save czj/3262643 to your computer and use it in GitHub Desktop.
Save czj/3262643 to your computer and use it in GitHub Desktop.
Handbrake MP4 H264 ultra optimized 720p/1080p encoding
#!/usr/bin/env ruby
# encoding: utf-8
# x264 presets guide : https://forum.handbrake.fr/viewtopic.php?f=6&t=19426
X264 = "b-adapt=2:rc-lookahead=50:me=umh:bframes=5:ref=6:direct=auto:trellis=2:subq=10:psy-rd=1.0,0.10:analyse=all"
FORMAT = "--optimize --format mp4"
QUALITY = "--ab 64 --mixdown mono --quality 23 -e x264 -x '#{X264}'"
SIZE = "--width 1280 --height 720"
ARGV.each do |param|
Dir.glob(param) do |entry|
next unless File.file?(entry)
# Open the source file
# Needed to get its mtime
source_file = File.new(entry)
# Use a sanitized filename with -720p extension
destination = source_file.path.gsub(/(.*)[.][a-zA-Z0-9]+$/, "\\1-720p.mp4")
# Compress video file
# -v0 flag disabled most of the verbosity
system "/Applications/HandBrakeCLI -v0 -i '#{source_file.path}' -o '#{destination}' #{FORMAT} #{QUALITY} #{SIZE}}"
# Set original file's modification time + current time for access time
File.utime(Time.now, source_file.mtime, destination)
end
end
#!/usr/bin/env ruby
# encoding: utf-8
# See encode_720p for more options
def bash(command)
system command
end
# x264 presets guide : https://forum.handbrake.fr/viewtopic.php?f=6&t=19426
X264 = "b-adapt=2:rc-lookahead=50:me=umh:bframes=5:ref=6:direct=auto:trellis=2:subq=10:psy-rd=1.0,0.10:analyse=all"
VARIATIONS = { "720p" => "--ab 64 -x '#{X264}' --quality 23", }
for filename in ARGV
for suffix, params in VARIATIONS
new_name = filename.gsub(/(.*)[.][a-zA-Z0-9]+$/, "\\1.#{suffix}.mp4")
bash %{/Applications/HandBrakeCLI -i "#{filename}" -o "#{new_name}" -e x264 --optimize --format mp4 --aencoder faac --mixdown mono #{params}}
end
end
@nilankadesilva
Copy link

How to use this script?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment