Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 427
webkul Avatar asked May 23 '26 05:05

webkul


1 Answers

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).

like image 99
Evan Plaice Avatar answered May 24 '26 19:05

Evan Plaice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!