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