Skip to content

Instantly share code, notes, and snippets.

@cjemorton
Forked from czj/gist:1263872
Created December 15, 2017 22:16
Show Gist options
  • Save cjemorton/b3ed487a918af8b56a318ab5371a79f4 to your computer and use it in GitHub Desktop.
Save cjemorton/b3ed487a918af8b56a318ab5371a79f4 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment