php 视频上传 FFMpeg 生成缩略图 GIF图

html 




    
    Title


视频上传/展示
PHP代码
 public function upload()
    {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "Error: " . $_FILES["file"]["error"] . "
"; } else { echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
"; 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  下载

php 视频上传 FFMpeg 生成缩略图 GIF图_第1张图片

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

最终截图成功!快来试试吧

php 视频上传 FFMpeg 生成缩略图 GIF图_第2张图片


你可能感兴趣的:(视频处理,PHP)