Skip to content

Instantly share code, notes, and snippets.

@johnnoel
Created October 11, 2012 17:20

Revisions

  1. johnnoel created this gist Oct 11, 2012.
    61 changes: 61 additions & 0 deletions screenshotter.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    <?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}\"");
    }
    }
    }