Skip to content

Instantly share code, notes, and snippets.

@jaikoo
Created May 15, 2009 13:04
Show Gist options
  • Save jaikoo/112203 to your computer and use it in GitHub Desktop.
Save jaikoo/112203 to your computer and use it in GitHub Desktop.
some really old and hacky padding code
def calculate_padding(current_size, target_size)
return nil if current_size <= target_size
diff = current_size - target_size
return nil if diff < 0
while diff%4 != 0
target_size = target_size -1
diff = current_size - target_size
end
p = (diff/2).to_i
return [ target_size, p, p ]
end
def calculate_ffmpeg_args(file, width, height)
pad_top, pad_bottom, pad_left, pad_right = 0,0,0,0
video = RVideo::Inspector.new(:file => file)
x, y = video.resolution.split('x')
ratio = x.to_f/y.to_f
new_width = width
new_height = (width/ratio).to_i
if new_height > height
new_height = height
new_width = (new_height * ratio).to_i
end
if height > new_height
pad = calculate_padding(height, new_height)
new_height = pad[0]
pad_top = pad[1]
pad_bottom = pad[2]
end
if width > new_width
pad = calculate_padding(width, new_width)
new_width = pad[0]
pad_left = pad[1]
pad_right = pad[2]
end
{:res => "#{new_width}x#{new_height}", :pad_top => pad_top, :pad_bottom => pad_bottom, :pad_left => pad_left, :pad_right => pad_right}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment