add --letterbox and --audio options

This commit is contained in:
Brooke Vibber 2023-01-15 10:25:47 -08:00
parent af0592e892
commit 868f2d9d4f
1 changed files with 61 additions and 24 deletions

View File

@ -2,8 +2,8 @@
<?php
// Squishes given video input into sub-4000kb .mp4
// Crops to 16:9
// Strips audio
// Crops or pads to 16:9 (crop default; --letterbox to pad)
// Strips audio (to override, pass --audio for 96 kbps AAC)
// HDR to SDR tonemapping
// Picks bitrate to match
// Picks resolution based on bitrate target
@ -11,12 +11,26 @@
$args = $_SERVER['argv'];
$self = array_shift( $args );
$options = [
'letterbox' => 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 <srcfile.mp4> <destfile.mp4>\n" );
die(
"Usage: $self [options...] <srcfile.mp4> <destfile.mp4>\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
] )
);
}
/*