Skip to content

Instantly share code, notes, and snippets.

@jaymcgavren
Created September 8, 2026 01:13
Show Gist options
  • Select an option

  • Save jaymcgavren/340b1ccda83a375f9fd32347c73b228d to your computer and use it in GitHub Desktop.

Select an option

Save jaymcgavren/340b1ccda83a375f9fd32347c73b228d to your computer and use it in GitHub Desktop.
A Ruby script that uses ffmpeg to crop a source video
#!/usr/bin/env ruby
# Cuts a rectangle out of a video and writes it as a new video of that size.
# The rest of the picture is thrown away; nothing is scaled or stretched.
#
# video_crop.rb --width 1080 --height 1920 wide-recording.mp4
# video_crop.rb -x 100 -y 40 --width 800 --height 600 clip.mp4 corner.mp4
#
# Sizes and positions are in pixels of the source video. x and y are the top
# left corner of the rectangle; leave either one out and the rectangle is
# centred on that axis. Leave a width or a height out and the whole width or
# height is kept.
#
# The video is re-encoded at near-lossless quality; the audio is copied untouched.
require "json"
require "optparse"
require "shellwords"
ENCODE = %w[-c:v libx264 -crf 16 -preset slow -pix_fmt yuv420p -c:a copy]
DEFAULTS = { x: nil, y: nil, width: nil, height: nil }
# Reads the options, and leaves the file names in argv.
def parse_options(argv)
options = DEFAULTS.dup
parser = OptionParser.new do |opt|
opt.banner = "Usage: video_crop.rb [options] INPUT [OUTPUT]"
opt.on("-x", "--x PIXELS", Integer, "left edge of the rectangle (default: centred)") { |v| options[:x] = v }
opt.on("-y", "--y PIXELS", Integer, "top edge of the rectangle (default: centred)") { |v| options[:y] = v }
opt.on("-W", "--width PIXELS", Integer, "how wide to cut (default: the whole width)") { |v| options[:width] = v }
opt.on("-H", "--height PIXELS", Integer, "how tall to cut (default: the whole height)") { |v| options[:height] = v }
opt.on("-h", "--help") { puts opt; exit }
end
parser.parse!(argv)
abort parser.help if argv.empty?
options
end
# Names the output after the input: clip.mp4 becomes clip.crop.mp4.
def default_output(input)
extension = File.extname(input)
input.delete_suffix(extension) + ".crop" + extension
end
# Asks ffprobe how big the frames are.
def video_size(input)
probe = `ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of json #{Shellwords.escape(input)}`
abort "could not read #{input}" unless $?.success?
stream = JSON.parse(probe).fetch("streams").first
[stream.fetch("width"), stream.fetch("height")]
end
# Works out the rectangle to cut, and complains if it does not fit in the frame.
# Video is stored in blocks of two pixels, so the width and height must be even.
def rectangle(options, video_width, video_height)
width = (options[:width] || video_width) / 2 * 2
height = (options[:height] || video_height) / 2 * 2
x = options[:x] || (video_width - width) / 2
y = options[:y] || (video_height - height) / 2
abort "the width and the height must be at least 2 pixels" unless width > 0 && height > 0
unless x >= 0 && y >= 0 && x + width <= video_width && y + height <= video_height
abort "#{width}x#{height} at #{x},#{y} does not fit in a #{video_width}x#{video_height} video"
end
[width, height, x, y]
end
options = parse_options(ARGV)
input = ARGV.shift
output = ARGV.shift || default_output(input)
abort "#{input}: no such file" unless File.exist?(input)
abort "#{output} already exists; delete it or name another output file" if File.exist?(output)
video_width, video_height = video_size(input)
width, height, x, y = rectangle(options, video_width, video_height)
puts "#{video_width}x#{video_height} -> #{width}x#{height} at #{x},#{y} -> #{output}"
exit 1 unless system("ffmpeg", "-i", input, "-vf", "crop=#{width}:#{height}:#{x}:#{y}", *ENCODE, output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment