webkul
webkul

Reputation: 2864

How to create an image using ffmpeg and PHP?

I'm using this script, but I'm not able to create the image.

My file is here.

Upvotes: 3

Views: 7760

Answers (3)

Nabi K.A.Z.
Nabi K.A.Z.

Reputation: 10714

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

And then you can use of this simple code:

<?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.'">';
    }
}
?>

Upvotes: 1

bjelli
bjelli

Reputation: 10090

Your PHP program basically calls /usr/bin/ffmpeg twice. Try that on the command line first! You can

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

to find out what exactly your PHP script is doing, and then try that exact command on the command line.

The first command should look something like

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

This is where you place the echos:

// 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`;

Upvotes: 2

Evan Plaice
Evan Plaice

Reputation: 96

It's not working because you never execute the $cmd. To actually execute the $cmd you either need to use popen(), proc_open(), or exec().

Try this function. It should generate an image as long as ffmpeg is accessible. To make it accessible add it to $path in linux, drop it into the windows/system32 folder in Windows. Or add it to the environmental 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 - displays the output the same as it would in the CLI. This is useful to see the details of what ffmpeg is doing, and to see where it crashes if it isn't working.

$errors - Will display an error if the CLI exits with an error code (IE, if ffmpeg crashes).

Upvotes: 8

Related Questions