make size configurable

This commit is contained in:
Brooke Vibber 2023-03-26 11:36:46 -07:00
parent 55d9c94887
commit 76aa1345e7
1 changed files with 25 additions and 3 deletions

View File

@ -11,12 +11,17 @@
$args = $_SERVER['argv'];
$self = array_shift( $args );
$maxBytes = 4000 * 1000; // fit in 4MB
$maxBytes = $maxBytes * 7 / 8; // leave some headroom
$options = [
'letterbox' => false,
'audio' => false,
'exposure' => '0',
'peak' => '10000',
'fps' => '60',
'size' => $maxBytes,
];
while ( count( $args ) > 0 && substr( $args[0], 0, 2 ) == '--' ) {
@ -38,7 +43,8 @@ if ( count ( $args ) < 2 ) {
" --audio include audio\n" .
" --exposure=n adjust exposure\n" .
" --peak=n set HDR peak nits\n" .
" --fps=n set frame rate\n"
" --fps=n frame rate limit\n" .
" --size=n target file size in bytes (default 3.5M)\n"
);
}
[ $src, $dest ] = $args;
@ -91,9 +97,25 @@ function evenize( $n ) {
return $n;
}
function sizify( $str ) {
$matches = [];
if ( preg_match( '/^(\d+(?:\.\d+)?)([kmgt]?)$/i', $str, $matches ) ) {
[ , $digits, $suffix ] = $matches;
$n = floatval( $digits );
switch ( strtolower( $suffix ) ) {
case 't': $n *= 1000; // fall through
case 'g': $n *= 1000; // fall through
case 'm': $n *= 1000; // fall through
case 'k': $n *= 1000; // fall through
default: return $n;
}
return $n;
}
die( "Unexpected size format '$str'\n" );
}
function convert( $src, $dest, $options ) {
$maxBits = 4000 * 1000 * 8; // fit in 4Mb
$maxBits = $maxBits * 7 / 8; // leave some headroom
$maxBits = 8 * sizify( $options['size'] );
$probe = ffprobe( $src );