Created
November 7, 2016 22:13
-
-
Save Hypnopompia/f3f3ac80eb0fc61a12908d02a6b2dc9f to your computer and use it in GitHub Desktop.
transcode a toxbox archive webm file containing variable dimensions to an mp4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
$input = isset($argv[1]) ? $argv[1] : null; | |
$output = isset($argv[2]) ? $argv[2] : null; | |
if (!$input || !file_exists($input)) { | |
die("No valid input file specified.\n"); | |
} | |
if (!$output) { | |
die("No output file specified.\n"); | |
} | |
function getMaxSize($input) { | |
$largestDimension = [ | |
'width' => null, | |
'height' => null | |
]; | |
// Find the max width/height of the video | |
$dimensionList = shell_exec("ffprobe -loglevel quiet -show_entries frame=width,height -of compact=p=0:nk=1 " . $input . "|sort|uniq"); | |
foreach (explode("\n", $dimensionList) as $dimension) { | |
list($width, $height) = array_pad(explode("|", $dimension), 2, null); | |
if ($width && $height && | |
($largestDimension['width'] == null || | |
$largestDimension['width'] < $width || | |
$largestDimension['height'] < $height | |
) | |
) { | |
$largestDimension['width'] = $width; | |
$largestDimension['height'] = $height; | |
} | |
} | |
return $largestDimension; | |
} | |
function transcode($input, $output, $width, $height) { | |
shell_exec("ffmpeg -y -i ".$input." -vf \"scale=".$width.":-1,pad=".$width.":".$height.":(".$width."-iw)/2:(".$height."-ih)/2\" -strict -2 ".$output); | |
} | |
$size = getMaxSize($input); | |
transcode($input, $output, $size['width'], $size['height']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment