From 868f2d9d4fffb3e76ca1682348b13483676eeaaa Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sun, 15 Jan 2023 10:25:47 -0800 Subject: [PATCH] add --letterbox and --audio options --- pack-vid | 85 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 61 insertions(+), 24 deletions(-) diff --git a/pack-vid b/pack-vid index 14cc324..f5c70bf 100755 --- a/pack-vid +++ b/pack-vid @@ -2,8 +2,8 @@ false, + 'audio' => false, +]; + +while ( count( $args ) > 0 && substr( $args[0], 0, 2 ) == '--' ) { + $option = substr( array_shift( $args ), 2 ); + $options[$option] = true; +} if ( count ( $args ) < 2 ) { - die( "Usage: $self \n" ); + die( + "Usage: $self [options...] \n" . + "Options:\n" . + " --letterbox pad instead of cropping\n" . + " --audio include audio\n" + ); } [ $src, $dest ] = $args; -convert( $src, $dest ); +convert( $src, $dest, $options ); exit(0); // @@ -65,7 +79,7 @@ function evenize( $n ) { return $n; } -function convert( $src, $dest ) { +function convert( $src, $dest, $options ) { $maxBits = 4000 * 1000 * 8; // fit in 4Mb $maxBits = $maxBits * 7 / 8; // leave some headroom @@ -84,18 +98,34 @@ function convert( $src, $dest ) { $bitrate = floor( $maxBits / $duration ); + + if ( $options[ 'audio' ] ) { + $audioBitrate = 96 * 1000; + $audio = [ '-b:a', $audioBitrate ]; + $bitrate -= $audioBitrate; + } else { + $audio = [ '-an' ]; + } + $mbits = 1000 * 1000; if ( $bitrate < 2 * $mbits ) { - $cropWidth = 854; - $scaleHeight = 480; + $frameWidth = 854; + $frameHeight = 480; } else if ( $bitrate <= 4 * $mbits ) { - $cropWidth = 1280; - $scaleHeight = 720; + $frameWidth = 1280; + $frameHeight = 720; } else { - $cropWidth = 1920; - $scaleHeight = 1080; + $frameWidth = 1920; + $frameHeight = 1080; + } + + if ( $options['letterbox'] ) { + $scaleWidth = $frameWidth; + $scaleHeight = evenize( $height * $frameWidth / $width ); + } else { + $scaleHeight = $frameHeight; + $scaleWidth = evenize( $width * $frameHeight / $height ); } - $scaleWidth = evenize( $width * $scaleHeight / $height ); $filters = [ "scale=w=$scaleWidth:h=$scaleHeight" ]; if ( $hdr ) { @@ -104,7 +134,12 @@ function convert( $src, $dest ) { $filters[] = "zscale=t=bt709:m=bt709:r=full"; } $filters[] = "format=yuv420p"; - $filters[] = "crop=w=$cropWidth"; + if ( $options['letterbox'] ) { + $offset = round( ( $frameHeight - $scaleHeight) / 2 ); + $filters[] = "pad=h=$frameHeight:y=$offset"; + } else { + $filters[] = "crop=w=$frameWidth"; + } $vf = implode( ',', $filters ); run( 'ffmpeg', [ @@ -119,17 +154,19 @@ function convert( $src, $dest ) { '-an', '-y', '/dev/null' ] ); - run( 'ffmpeg', [ - '-i', $src, - '-vf', $vf, - '-c:v', 'libx264', - '-b:v', $bitrate, - '-preset', 'veryslow', - '-pass', '2', - '-g', $keyframeInt, - '-an', - '-y', $dest - ] ); + run( 'ffmpeg', + array_merge( [ + '-i', $src, + '-vf', $vf, + '-c:v', 'libx264', + '-b:v', $bitrate, + '-preset', 'veryslow', + '-pass', '2', + '-g', $keyframeInt, + ], $audio, [ + '-y', $dest + ] ) + ); } /*