Skip to content

Instantly share code, notes, and snippets.

@sharifulin
Created February 5, 2010 12:48
Show Gist options
  • Save sharifulin/295758 to your computer and use it in GitHub Desktop.
Save sharifulin/295758 to your computer and use it in GitHub Desktop.
FFmpeg config for scale video size
#!/usr/bin/perl
use strict;
my @SOURCE = (2000, 1500);
my @FORMAT = (1920, 1080);
my $K = '16x9';
my @check = check_video($SOURCE[0], $SOURCE[1], $FORMAT[0], $FORMAT[1], $K);
my $x = $check[1] / 2;
my $y = $check[0] / 2;
my $s = join 'x', $FORMAT[0]-$check[1], $FORMAT[1]-$check[0];
print qq{-s $s -padtop $y -padbottom $y -padleft $x -padright $x};
sub check_video($$$$;$) {
my($w, $h, $w2, $h2, $x2) = @_;
{
$w == 848 and $h == 478 and $h -= 1; # 848x477 - fuck :)
}
my $round = sub {
return $_[0] unless $x2 eq '16x9';
my $max = my $min = int shift;
$max += 1, $min -= 1 while $max % 4 != 0 && $min % 4 != 0;
return $max % 4 == 0 ? $max : $min; # max
};
my $calc; $calc = {
'0' => sub {
return (0, 0);
},
'x' => sub { # leftright
return $_ > $w2 ? $calc->{'y'}->(@_) : (0, $round->($w2 - $_))
for map { sprintf "%.0f", $_ } $_[1]*$w/$h; # $w/$h = x/$h2
},
'y' => sub { # topbot
return $_ > $h2 ? $calc->{'x'}->(@_) : ($round->($h2 - $_), 0)
for map { sprintf "%.0f", $_ } $_[0]*$h/$w; # $w/$h = $w2/y
},
};
my $k = {'16x9' => 9/16, '4x3' => 3/4}->{$x2} * $w || return $calc->{'0'}->();
return
($h < $k) # topbot
?
$calc->{'y'}->($w2, $h2)
:
($h > $k) # leftright
?
$calc->{'x'}->($w2, $h2)
:
$calc->{'0'}->();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment