pack-vid/pack-vid

188 lines
4.5 KiB
Plaintext
Raw Normal View History

2023-01-15 01:20:35 +00:00
#!/usr/bin/env php
<?php
2023-01-15 03:16:50 +00:00
// Squishes given video input into sub-4000kb .mp4
2023-01-15 18:25:47 +00:00
// Crops or pads to 16:9 (crop default; --letterbox to pad)
// Strips audio (to override, pass --audio for 96 kbps AAC)
2023-01-15 03:16:50 +00:00
// HDR to SDR tonemapping
2023-01-15 01:20:35 +00:00
// Picks bitrate to match
// Picks resolution based on bitrate target
// 2-pass encoding with libx264 veryslow
$args = $_SERVER['argv'];
$self = array_shift( $args );
2023-01-15 18:25:47 +00:00
$options = [
'letterbox' => false,
'audio' => false,
];
while ( count( $args ) > 0 && substr( $args[0], 0, 2 ) == '--' ) {
$option = substr( array_shift( $args ), 2 );
$options[$option] = true;
}
2023-01-15 01:20:35 +00:00
if ( count ( $args ) < 2 ) {
2023-01-15 18:25:47 +00:00
die(
"Usage: $self [options...] <srcfile.mp4> <destfile.mp4>\n" .
"Options:\n" .
" --letterbox pad instead of cropping\n" .
" --audio include audio\n"
);
2023-01-15 01:20:35 +00:00
}
[ $src, $dest ] = $args;
2023-01-15 18:25:47 +00:00
convert( $src, $dest, $options );
2023-01-15 01:20:35 +00:00
exit(0);
//
function run( $cmd, $args ) {
$commandLine = implode( ' ',
array_merge(
[ escapeshellcmd( $cmd ) ],
array_map( 'escapeshellarg', $args )
)
);
echo "$commandLine\n";
$output = shell_exec( $commandLine );
if ( $output === false ) {
throw new Error( "Failed to run $cmd" );
}
return $output;
}
function ffprobe( $path ) {
$output = run( 'ffprobe', [
'-hide_banner',
'-show_format',
'-show_streams',
'-print_format',
'json',
'--',
$path
] );
$data = json_decode( $output );
if ( $data === null ) {
throw new Error( "Failed to read JSON from ffprobe: $output" );
}
return $data;
}
function evenize( $n ) {
$n = ceil( $n );
if ( $n & 1 ) {
$n++;
}
return $n;
}
2023-01-15 18:25:47 +00:00
function convert( $src, $dest, $options ) {
2023-01-15 01:20:35 +00:00
$maxBits = 4000 * 1000 * 8; // fit in 4Mb
$maxBits = $maxBits * 7 / 8; // leave some headroom
$probe = ffprobe( $src );
$videoTracks = array_filter( $probe->streams, function ( $stream ) {
return $stream->codec_type === 'video';
} );
$track = $videoTracks[0];
$duration = floatval( $track->duration );
$width = $track->width;
$height = $track->height;
$hdr = $track->color_primaries === 'bt2020';
$keyframeInt = ceil( $duration * 60 );
$bitrate = floor( $maxBits / $duration );
2023-01-15 18:25:47 +00:00
if ( $options[ 'audio' ] ) {
$audioBitrate = 96 * 1000;
$audio = [ '-b:a', $audioBitrate ];
$bitrate -= $audioBitrate;
} else {
$audio = [ '-an' ];
}
2023-01-15 01:20:35 +00:00
$mbits = 1000 * 1000;
2023-01-25 00:48:44 +00:00
if ( $bitrate < $mbits ) {
$frameWidth = 640;
$frameHeight = 360;
} elseif ( $bitrate < 2 * $mbits ) {
2023-01-15 18:25:47 +00:00
$frameWidth = 854;
$frameHeight = 480;
2023-01-25 00:48:44 +00:00
} elseif ( $bitrate < 4 * $mbits ) {
2023-01-15 18:25:47 +00:00
$frameWidth = 1280;
$frameHeight = 720;
2023-01-15 01:20:35 +00:00
} else {
2023-01-15 18:25:47 +00:00
$frameWidth = 1920;
$frameHeight = 1080;
}
if ( $options['letterbox'] ) {
$scaleWidth = $frameWidth;
$scaleHeight = evenize( $height * $frameWidth / $width );
} else {
$scaleHeight = $frameHeight;
$scaleWidth = evenize( $width * $frameHeight / $height );
2023-01-15 01:20:35 +00:00
}
2023-01-31 06:54:34 +00:00
$peakNits = 2000;
$sdrNits = 80;
$peak = $peakNits / $sdrNits;
2023-01-15 01:20:35 +00:00
$filters = [ "scale=w=$scaleWidth:h=$scaleHeight" ];
if ( $hdr ) {
$filters[] = "zscale=t=linear:p=bt709";
2023-01-31 06:54:34 +00:00
$filters[] = "tonemap=hable:peak=$peak";
2023-01-15 01:20:35 +00:00
$filters[] = "zscale=t=bt709:m=bt709:r=full";
}
$filters[] = "format=yuv420p";
2023-01-15 18:25:47 +00:00
if ( $options['letterbox'] ) {
$offset = round( ( $frameHeight - $scaleHeight) / 2 );
$filters[] = "pad=h=$frameHeight:y=$offset";
} else {
$filters[] = "crop=w=$frameWidth";
}
2023-01-15 01:20:35 +00:00
$vf = implode( ',', $filters );
$fps = 30;
2023-01-31 06:54:34 +00:00
$passlog = tempnam( '.', 'pack-vid-passlog' );
run( 'ffmpeg',
array_merge( [
'-i', $src,
'-f', 'mp4',
'-r', $fps,
'-vf', $vf,
'-c:v', 'libx264',
'-b:v', $bitrate,
'-preset', 'veryslow',
'-pass', '1',
2023-01-31 06:54:34 +00:00
'-passlogfile', $passlog,
'-g', $keyframeInt,
], $audio, [
'-y', '/dev/null'
] )
);
2023-01-15 18:25:47 +00:00
run( 'ffmpeg',
array_merge( [
'-i', $src,
'-vf', $vf,
'-r', $fps,
2023-01-15 18:25:47 +00:00
'-c:v', 'libx264',
'-b:v', $bitrate,
'-preset', 'veryslow',
'-pass', '2',
2023-01-31 06:54:34 +00:00
'-passlogfile', $passlog,
2023-01-15 18:25:47 +00:00
'-g', $keyframeInt,
], $audio, [
2023-01-15 18:44:06 +00:00
'-movflags', '+faststart',
2023-01-15 18:25:47 +00:00
'-y', $dest
] )
);
2023-01-15 01:20:35 +00:00
}