Blogger Information
Blog 43
fans 0
comment 3
visits 26534
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
linux(本文为AlibabaLinux)上 ,pbootcms(ueditor上传视频) :php使用FFMpeg获取视频第一帧为缩略图方法
Time
Original
965 people have browsed it

1.在linux上安装FFMpeg


1.下载解压

  1. wget https://ffmpeg.org/releases/ffmpeg-5.1.2.tar.gz
  2. tar -zxvf ffmpeg-5.1.2.tar.gz

2.进入解压后目录,配置自己指定安装目录/usr/local/ffmpeg

  1. cd ffmpeg-5.1.2
  2. ./configure --prefix=/usr/local/ffmpeg
  3. make && make install

3.配置变量(vi或者vim都可以)

  1. vim /etc/profile
  2. 在文件内容最下面添加环境变量:(a是切入输入模式)
  3. export PATH=$PATH:/usr/local/ffmpeg/bin
  4. 保存退出(ESC切换模式输入 :wq
  5. 使修改内容立即生效执行命令
  6. source /etc/profile
  7. 查看所有环境变量命令:export

4.查看版本(安装完成)

  1. ffmpeg -version

注意:

若安装过程中出现以下错误:

  1. yasm/nasm not found or too old. Use disable-yasm for a crippled build. If you think configure made a mistake, make sure you are using the latest version from Git. If the latest version fails, report the problem to the ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net. Include the log file “config.log” produced by configure as this will help solve the problem.

需要安装 yasm

  1. wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
  2. tar -zxvf yasm-1.3.0.tar.gz
  3. cd yasm-1.3.0
  4. ./configure
  5. make && make install

2.进入项目,用composer安装FFMpeg

  1. 1.在项目根目录新建一个composer.json文件,文件里写一个空对象(就是一个空的大括号)
  2. 2.初始化,执行命令:composer install
  3. 3.安装命令:composer require php-ffmpeg/php-ffmpeg

3.进入项目apps\common\function.php 文件

添加公共函数方法:

  1. //FFMpeg获取视频第一帧为图片,使用composer下载插件。处理自动加载冲突
  2. function autoloadAdjust($video_src,$ico_src)
  3. {
  4. // 取原有的加载方法
  5. $oldFunctions = spl_autoload_functions();
  6. // 逐个卸载
  7. if ($oldFunctions){
  8. foreach ($oldFunctions as $f) {
  9. spl_autoload_unregister($f);
  10. }
  11. }
  12. // 注册本框架的自动载入
  13. require $_SERVER['DOCUMENT_ROOT'] .'/vendor/autoload.php';
  14. $ffmpeg = FFMpeg\FFMpeg::create(['ffmpeg.binaries'=>'/usr/local/ffmpeg/bin/ffmpeg','ffprobe.binaries'=>'/usr/local/ffmpeg/bin/ffprobe']);
  15. $video_src = $_SERVER['DOCUMENT_ROOT'] . $video_src;
  16. $uqid = uniqid();//获取无序字符串
  17. $ico_img = $ico_src .'/'. $uqid . '.png'; //获取符合缩略图格式路径
  18. $ico_src_new = $_SERVER['DOCUMENT_ROOT'] . $ico_src;//获取绝对路径目录
  19. //没有文件夹,就创建
  20. if(!is_dir($ico_src_new)){
  21. mkdir($ico_src_new,0777,true);
  22. }
  23. $ico_src_new = $ico_src_new .'/'. $uqid . '.png';
  24. $video = $ffmpeg->open($video_src);//打开一个本地的视频文件
  25. $frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(1));//截取视频中第几秒的截图
  26. $frame->save($ico_src_new);//保存截图
  27. // 如果引用本框架的其它框架已经定义了__autoload,要保持其使用
  28. if (function_exists('__autoload')) {
  29. spl_autoload_register('__autoload');
  30. }
  31. // 再将原来的自动加载函数放回去
  32. if ($oldFunctions){
  33. foreach ($oldFunctions as $f) {
  34. spl_autoload_register($f);
  35. }
  36. }
  37. return $ico_img;
  38. }

4.可能遇到问题

1.程序运行异常: Unable to load FFProbe

  1. 我找到的真实错误是:file_exists(): open_basedir restriction in effect. File.
  2. pbootcms只报Unable to load FFProbe错误。
  3. 解决方法:在宝塔中的网络目录,将防跨站攻击关闭

图示:

2.报错The Process class relies on proc_open, which is not available on your PHP installation.

  1. 错误原因是禁用了 proc_open 函数,找到相应版本的php.ini文件,查找 disable_functions
  2. disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,proc_open
  3. 删除 proc_open 重启php即可。

5.php添加和修改视频调用代码,可查看window端博客文章

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!