Skip to content

Instantly share code, notes, and snippets.

@johnnoel
Created October 11, 2012 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnnoel/3874050 to your computer and use it in GitHub Desktop.
Save johnnoel/3874050 to your computer and use it in GitHub Desktop.
PHP script for controlling mplayer to automatically take screenshots
<?php
define('DS', DIRECTORY_SEPARATOR);
$ssCount = 250;
$cmdMplayer = "e:\downloads\mplayer\mplayer\mplayer.exe";
$argsInfo = " -vo null -nosound -frames 0 -identify";
$argsCapture = " -quiet -nosound -vo jpeg:quality=90 -vf framestep=i{framestep} -fps 360 -lavdopts threads=4";
if($_SERVER['argc'] > 1)
{
array_shift($_SERVER['argv']);
foreach($_SERVER['argv'] AS $file)
{
if(file_exists($file) && is_file($file))
{
$screenshotDir = dirname($file).DS.'screenshots'.DS.basename($file).DS;
if((!is_dir($screenshotDir) && !mkdir($screenshotDir, null, true)) || !is_writable($screenshotDir))
{
echo "Unable to write/create to screenshot directory ({$screenshotDir})";
break;
}
chdir($screenshotDir);
$infoOutput = shell_exec("{$cmdMplayer}{$argsInfo} \"{$file}\"");
// discover file length
$matcher = '/ID_LENGTH=(\d+\.\d+)$/m';
$matches = array();
preg_match_all($matcher, $infoOutput, $matches);
$fileLength = (!empty($matches[1][0])) ? floatval($matches[1][0]) : 0;
if(!$fileLength)
{
echo "Unable to determine file length ({$file})";
break;
}
// discover frame rate
$matcher = '/ID_VIDEO_FPS=(\d+\.\d+)$/m';
$matches = array();
preg_match_all($matcher, $infoOutput, $matches);
$frameRate = (!empty($matches[1][0])) ? floatval($matches[1][0]) : 0;
if(!$frameRate)
{
echo "Unable to determine file frame rate ({$file})";
break;
}
$framestep = floor(($fileLength * $frameRate) / $ssCount);
$args = str_replace(array('{outdir}', '{framestep}'), array($screenshotDir, $framestep), $argsCapture);
//var_dump("{$cmdMplayer}{$args} \"".escapeshellarg($file)."\"");
//var_dump("{$cmdMplayer}{$args} \"{$file}\"");
system("{$cmdMplayer}{$args} \"{$file}\"");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment