nicklasos
11/6/2013 - 2:44 PM

FFMPG + Video functions

FFMPG + Video functions

<?php


/**
 * Compile video from images with FFMPG
 *
 * @param string $tmpDirWithImages
 * @param string $ext jpg|png...
 * @param string $videoFilePath
 */
function compileVideo($tmpDirWithImages, $ext, $videoFilePath)
{
    $command =
        "/usr/local/bin/ffmpeg ".
        "-i {$tmpDirWithImages}%5d.{$ext} ".
        "-vf 'split [a], pad=iw*2:ih [b], [a] alphaextract, [b] overlay=w' ".
        "-qmin 0 ".
        "-qmax 39 ".
        "-y {$videoFilePath}"; system($command);
}

/**
 * Get the dimensions of a video file
 *
 * @param string $video path
 * @return bool|array(width,height)
 * @author Jamie Scott
 */
public function getVideoDimensions($video)
{
    if (!file_exists($video)) {
        return false;
    }

    $command = 'ffmpeg -i ' . $video . ' -vstats 2>&1';
    $output = shell_exec($command);

    preg_match('/[0-9]?[0-9][0-9][0-9]x[0-9][0-9][0-9][0-9]?/', $output, $regs);

    if (!isset($argc[0])) {
        return false;
    }

    $vals = explode('x', $regs [0]);
    $width = $vals[0] ? $vals[0] : null;
    $height = $vals[1] ? $vals[1] : null;
    return ['width' => $width, 'height' => $height];
}

/**
 * Generate thumb from video file
 * @param string $videoFilePath
 * @param string $thumbPath
 */
function generateThumbnail($videoFilePath, $thumbPath)
{
    shell_exec(escapeshellcmd("ffmpeg -i {$videoFilePath} -r 1 -f image2 -vframes 1 -s 100x100 -an {$thumbPath}"));
}