php 視訊上傳 FFMpeg 生成縮圖 GIF圖
阿新 • • 發佈:2019-02-08
html
PHP程式碼<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table> <caption>視訊上傳/展示</caption> <thead> <td> <form enctype="multipart/form-data" method="post" action="upload"> <input type="file" name="file" placeholder="點選選擇檔案"> <input type="submit" value="上傳視訊"> </form> </td> </thead> </table> </body> </html>
public function upload() { if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; $dirPath = 'upload/';//設定檔案儲存的目錄 if (!is_dir($dirPath)) { //目錄不存在則建立目錄 @mkdir($dirPath); } if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } }
生成縮圖需要下載ffmpeg 外掛
可自行上網下載或於 https://download.csdn.net/download/qq_29099209/10316770 下載
composer 下載依賴庫
composer require php-ffmpeg/php-ffmpeg
在composer.json檔案中新增
"php-ffmpeg/php-ffmpeg": "^0.11.1"
安裝成功即可使用
use FFMpeg\Coordinate\TimeCode; use FFMpeg\FFMpeg; +use FFMpeg\Coordinate\Dimension; $ffmpeg = FFMpeg::create(array( 'ffmpeg.binaries' => 'D:\phpStudy\php\php-7.0.12-nts\ffmpeg\bin\ffmpeg.exe',//外掛下載地址 'ffprobe.binaries' => 'D:\phpStudy\php\php-7.0.12-nts\ffmpeg\bin\ffprobe.exe', //linux 下 //'ffmpeg.binaries' => '/opt/local/ffmpeg/bin/ffmpeg', //'ffprobe.binaries' => '/opt/local/ffmpeg/bin/ffprobe', 'timeout' => 0, 'ffmpeg.threads' => 12 )); $video = $ffmpeg->open("upload/".$_FILES["file"]["name"]); $video->frame(TimeCode::fromSeconds(20))->save('frame.jpg'); $video->gif(TimeCode::fromSeconds(20),new Dimension(300,400),100)->save('upload/frame1.gif');
碼雲下載連結:https://gitee.com/weijunhao1/VideoUpload
最終截圖成功!快來試試吧