How to create an image using ffmpeg and PHP?

I am using this script , but I cannot create an image.

My file is here .

+3
source share
3 answers

It does not work because you never perform $cmd. To perform $cmdyou need to either use popen(), proc_open()or exec().

Try this feature. It should generate an image while ffmpeg is available. To make it available, add it to $pathon linux, put it in a folder windows/system32on Windows. Or add it to the environment variables in the control panel in Windows.

/ **
 * ExtractThumb, extracts a thumbnail from a video
 *
 * This function loads a video and extracts an image from a frame 4 
 * seconds into the clip
 * @param $in string the input path to the video being processed
 * @param $out string the path where the output image is saved
 */
function ExtractThumb($in, $out)
{
    $thumb_stdout;
    $errors;
    $retval = 0;

    // Delete the file if it already exists
    if (file_exists($out)) { unlink($out); }

    // Use ffmpeg to generate a thumbnail from the movie
    $cmd = "ffmpeg -itsoffset -4 -i $in -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 $out 2>&1";
    exec($cmd, $thumb_stdout, $retval);

    // Queue up the error for processing
    if ($retval != 0) { $errors[] = "FFMPEG thumbnail generation failed"; }

    if (!empty($thumb_stdout))
    {
        foreach ($thumb_stdout as $line)
        {
            echo $line . "
\n"; } } if (!empty($errors)) { foreach ($errors as $error) { echo $error . "
\n"; } } }

$thumb_stdout - , CLI. , , ffmpeg, , , .

$errors - , CLI (IE, ffmpeg ).

+8

PHP- /usr/bin/ffmpeg . !

echo "<pre>$cmd</pre>"

, PHP script, .

:

/usr/bin/ffmpeg -i /var/www/beta/clock.avi 2>&1

echo s:

// get the duration and a random place within that
$cmd = "$ffmpeg -i $video 2>&1";
echo "<pre>$cmd</pre>"

if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
    $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
    $second = rand(1, ($total - 1));
}

// get the screenshot
$cmd = "$ffmpeg -i $video -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";
echo "<pre>$cmd</pre>"
$return = `$cmd`;
+2

ffmpeg-php (http://ffmpeg-php.sourceforge.net/)

:

<?php
$frame = 10;
$movie = 'test.mp4';
$thumbnail = 'thumbnail.png';

$mov = new ffmpeg_movie($movie);
$frame = $mov->getFrame($frame);
if ($frame) {
    $gd_image = $frame->toGDImage();
    if ($gd_image) {
        imagepng($gd_image, $thumbnail);
        imagedestroy($gd_image);
        echo '<img src="'.$thumbnail.'">';
    }
}
?>
0

Source: https://habr.com/ru/post/1714757/


All Articles