From 76aa1345e7b701d5454e21f9f7512ef6d56998d8 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sun, 26 Mar 2023 11:36:46 -0700 Subject: [PATCH] make size configurable --- pack-vid | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/pack-vid b/pack-vid index b3521fc..3bc91e2 100755 --- a/pack-vid +++ b/pack-vid @@ -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 );