From bb4b9f89d5a17ab35f28fb0378108f6e1694f57b Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Tue, 16 May 2023 12:14:49 -0700 Subject: [PATCH 1/3] alternate size stuff --- pack-set | 2 +- pack-vid | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/pack-set b/pack-set index 40dbe44..7357af7 100755 --- a/pack-set +++ b/pack-set @@ -8,7 +8,7 @@ do SPEED_SMALL="veryslow" SPEED_LARGE="medium" - SMALL="$COMMON --size=4m --preset=$SPEED_SMALL --quality=0.75" + SMALL="$COMMON --size=4m --preset=$SPEED_SMALL" LARGE="$COMMON --size=25m --preset=$SPEED_LARGE" pack-vid $SMALL "$INFILE" "${INFILE%.mp4}-small.mp4" diff --git a/pack-vid b/pack-vid index e18e7e4..207dbe9 100755 --- a/pack-vid +++ b/pack-vid @@ -26,6 +26,7 @@ $options = [ 'fps' => '60000/1001', 'size' => $maxBytes, 'quality' => 1.0, + 'hdr' => false, ]; while ( count( $args ) > 0 && substr( $args[0], 0, 2 ) == '--' ) { @@ -51,7 +52,8 @@ if ( count ( $args ) < 2 ) { " --preset=key set h.264 encoding preset\n" . " --fps=n frame rate limit\n" . " --size=n target file size in bytes (default 3.5M)\n" . - " --quality=n fraction of base bitrate to break on (deafult 0.75)\n" + " --quality=n fraction of base bitrate to break on (deafult 0.75)\n" . + " --hdr force HDR input processing on\n" ); } [ $src, $dest ] = $args; @@ -97,7 +99,7 @@ function ffprobe( $path ) { } function evenize( $n ) { - $n = ceil( $n ); + $n = round( $n ); if ( $n & 1 ) { $n++; } @@ -140,7 +142,7 @@ function convert( $src, $dest, $options ) { $duration = floatval( $track->duration ); $width = $track->width; $height = $track->height; - $hdr = $track->color_primaries === 'bt2020'; + $hdr = $track->color_primaries === 'bt2020' || $options['hdr']; $keyframeInt = ceil( $duration * 60 ); $bitrate = floor( $maxBits / $duration ); @@ -161,6 +163,7 @@ function convert( $src, $dest, $options ) { $mbits = 1000 * 1000; $base = intval( $mbits * floatval( $options['quality'] ) ); + /* if ( $bitrate < 1 * $base || $height < 480 ) { $frameWidth = 640; $frameHeight = 360; @@ -182,8 +185,49 @@ function convert( $src, $dest, $options ) { $frameHeight = 1080; $bitrate = min( $bitrate, $base * 8 ); } + */ $aspect = $width / $height; + $pixels = $width * $height; + + // canonical base rate is 1 megabit at 480p + $bitrate = min( $bitrate, 4 * $base ); + + $minWidth = 640; + $minHeight = 360; + + $baseWidth = 854; + $baseHeight = 480; + $pixelsPerBit = ( $baseWidth * $baseHeight ) / $base; + + $maxWidth = 1920; + $maxHeight = 1080; + $maxrate = $base * ( $maxWidth * $maxHeight ) / ( $baseWidth * $baseHeight ); + + $pixels = $bitrate * $pixelsPerBit; + $frameHeight = evenize( sqrt( $pixels / $aspect ) ); + $frameWidth = evenize( $frameHeight * $aspect ); + + if ( $aspect > 16 / 9 ) { + if ( $frameWidth < $minWidth ) { + $frameWidth = $minWidth; + $frameHeight = evenize( $frameWidth / $aspect ); + } elseif ( $frameWidth > $maxWidth ) { + $frameWidth = $maxWidth; + $frameHeight = evenize( $frameWidth / $aspect ); + $bitrate = min( $bitrate, $maxrate ); + } + } else { + if ( $frameHeight < $minHeight ) { + $frameHeight = $minHeight; + $frameWidth = evenize( $frameHeight * $aspect ); + } elseif ( $frameWidth > $maxWidth ) { + $frameHeight = $maxHeight; + $frameWidth = evenize( $frameHeight * $aspect ); + $bitrate = min( $bitrate, $maxrate ); + } + } + $wide = $aspect > ( $frameWidth / $frameHeight ); $crop = boolval( $options['crop'] ); $letterbox = boolval( $options['letterbox'] ); From 552a81499cc95de2c825c7fd19c07452934f5428 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 17 May 2023 09:16:30 -0700 Subject: [PATCH 2/3] hack --- pack-set | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pack-set b/pack-set index ac6ae7a..43e9a34 100755 --- a/pack-set +++ b/pack-set @@ -11,7 +11,7 @@ for INFILE in "$@" do echo "$INFILE" - COMMON="$OPTS --exposure=-2.5 --peak=141 --fps=60000/1001" + COMMON="$OPTS --hdr --exposure=-2.5 --peak=141 --fps=60000/1001" SPEED_SMALL="veryslow" SPEED_LARGE="medium" From 9c0e598ce2e559542b6bb6f5dd27c3c0288236af Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 17 May 2023 09:53:50 -0700 Subject: [PATCH 3/3] use the old logic for sizing --- pack-vid | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pack-vid b/pack-vid index eac968d..1c18e4b 100755 --- a/pack-vid +++ b/pack-vid @@ -169,7 +169,6 @@ function convert( $src, $dest, $options ) { $mbits = 1000 * 1000; $base = intval( $mbits * floatval( $options['quality'] ) ); - /* if ( $bitrate < 1 * $base || $height < 480 ) { $frameWidth = 640; $frameHeight = 360; @@ -191,7 +190,6 @@ function convert( $src, $dest, $options ) { $frameHeight = 1080; $bitrate = min( $bitrate, $base * 8 ); } - */ $aspect = $width / $height; $pixels = $width * $height; @@ -199,6 +197,7 @@ function convert( $src, $dest, $options ) { // canonical base rate is 1 megabit at 480p $bitrate = min( $bitrate, 4 * $base ); + /* $minWidth = 640; $minHeight = 360; @@ -233,6 +232,7 @@ function convert( $src, $dest, $options ) { $bitrate = min( $bitrate, $maxrate ); } } + */ $wide = $aspect > ( $frameWidth / $frameHeight ); $crop = boolval( $options['crop'] );