0

I am using ffmpeg to create video thumbnail while uploading video file. And I have created this upload.php file but it's not working. It successfully uploads an mp4 file but fails to create the thumbnail.

if(isset($_FILES["myfile"]) && $_FILES["myfile"]["error"]== UPLOAD_ERR_OK)
{
    $UploadDirectory    = 'uploads/';

    if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
        die();
    }

    $video = $UploadDirectory . escapeshellcmd($_FILES['myfile']['tmp_name']);
    $cmd = "ffmpeg -i $video 2>&1";
    $second = 1;
    if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
    $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
    $second = rand(1, ($total - 1));
    }

    $image  = 'thumbs/random_name.jpg';
    $cmd = "ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";
    $do = `$cmd`;   

    $File_Name          = strtolower($_FILES['myfile']['name']);
    $File_Ext           = substr($File_Name, strrpos($File_Name, '.')); //get file extention
    $Random_Number      = rand(0, 9999999999); //Random number to be added to name.
    $NewFileName        = $Random_Number.$File_Ext; //new file name

    if(!is_array($_FILES["myfile"]["name"])) //single file
    {
        move_uploaded_file($_FILES["myfile"]["tmp_name"],$UploadDirectory.$NewFileName);

        $ret[]= $NewFileName;
    }
    echo json_encode($ret);
}
else
{
    die('Something wrong with upload! Is "upload_max_filesize" set correctly?');
}   
Tristan
  • 3,301
  • 8
  • 22
  • 27
Ayd In
  • 193
  • 1
  • 5
  • 14
  • Possible duplicate of [How to create thumbnails or preview for videos?](http://stackoverflow.com/questions/2265572/how-to-create-thumbnails-or-preview-for-videos) – The Beast Dec 20 '15 at 19:22

2 Answers2

2

You have to use the function exec(); to use the command in php i added echo exec($cmd); inside the code
Try this:

if(isset($_FILES["myfile"]) && $_FILES["myfile"]["error"]== UPLOAD_ERR_OK)
{
$UploadDirectory    = 'uploads/';

if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
    die();
}

$video = $UploadDirectory . escapeshellcmd($_FILES['myfile']['tmp_name']);
$cmd = "ffmpeg -i $video 2>&1";
$second = 1;
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
$total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
$second = rand(1, ($total - 1));
}

$image  = 'thumbs/random_name.jpg';
$cmd = "ffmpeg  -itsoffset -0 -i uploads/" . $_FILES["myfile"]["name"] . " -vcodec mjpeg -vframes 0 -an -f rawvideo -s 200x200 " . $image;
echo exec($cmd);
$do = `$cmd`;   

$File_Name          = strtolower($_FILES['myfile']['name']);
$File_Ext           = substr($File_Name, strrpos($File_Name, '.')); //get file extention
$Random_Number      = rand(0, 9999999999); //Random number to be added to name.
$NewFileName        = $Random_Number.$File_Ext; //new file name

if(!is_array($_FILES["myfile"]["name"])) //single file
{
    move_uploaded_file($_FILES["myfile"]["tmp_name"],$UploadDirectory.$NewFileName);

    $ret[]= $NewFileName;
}
echo json_encode($ret);
}
else
{
die('Something wrong with upload! Is "upload_max_filesize" set correctly?');
}
2

Try this. First install ffmpeg-php (http://ffmpeg-php.sourceforge.net/).which I am sure you have already installed.

And then you can use of this simple code: you can update the code as per your variables.

<?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.'">';
    }
}
?>
Airy
  • 5,484
  • 7
  • 53
  • 78