Skip to content

Instantly share code, notes, and snippets.

@czj
Created October 5, 2011 07:39
Show Gist options
  • Save czj/1263872 to your computer and use it in GitHub Desktop.
Save czj/1263872 to your computer and use it in GitHub Desktop.
Script to encode with HandbrakeCLI (x264) 720p, high image quality, low file size
#!/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
@gitcnd
Copy link

gitcnd commented Jan 29, 2015

Here's a similar one, for 1080p style encoding:
HandBrakeCLI -i input.avi -o output.mp4 -f mp4 --loose-anamorphic --modulus 2 -e x264 -q 19 --cfr -a 1 -E faac -6 dpl2 -R Auto -B 320 -D 0 --gain 0 --audio-copy-mask none --audio-fallback ffac3 -x ref=16:bframes=16:b-adapt=2:direct=auto:me=tesa:merange=24:subq=11:rc-lookahead=60:analyse=all:trellis=2:no-fast-pskip=1 --verbose=1

@rowdyrotifer
Copy link

Good stuff!

@kalabiyau
Copy link

-mixdown mono wut?

@nilankadesilva
Copy link

How do I use this script?

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