php ffmpeg获取视频缩略图

1.环境

  • centos 7
  • ffmpeg version 2.8.15 Copyright

2.centos7安装ffmpeg

centos7安装ffmpeg

sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm

sudo yum install ffmpeg ffmpeg-devel -y

【Ubuntu安装ffmpeg】

sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next

sudo apt-get update

sudo apt-get install ffmpeg

查看环境变量是否配置成功

ffmpeg -version

3.项目安装ffmpeg包

composer require php-ffmpeg/php-ffmpeg

github链接https://github.com/PHP-FFMpeg/PHP-FFMpeg

4.php代码

<?php
include '../vendor/autoload.php';
use FFMpeg\FFMpeg;
class MyFfmpeg
{
    public function run()
    {
        $file_path = __DIR__ . '/' . uniqid() . '.jpg';

        $ffmpeg = FFMpeg::create([
            'ffmpeg.binaries' => '/usr/bin/ffmpeg',
            'ffprobe.binaries' => '/usr/bin/ffprobe'
        ]);
        $video = $ffmpeg->open('1.mp4');
        $video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(1))->save($file_path);
    }
}

$a = new MyFfmpeg();
$a->run();

附Fmpeg读取视频信息

<?php
define('FFMPEG_PATH', '/usr/local/ffmpeg2/bin/ffmpeg -i "%s" 2>&1');
  
function getVideoInfo($file) {
   
  $command = sprintf(FFMPEG_PATH, $file);
   
  ob_start();
  passthru($command);
  $info = ob_get_contents();
  ob_end_clean();
  
  $data = array();
  if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
    $data['duration'] = $match[1]; //播放时间
    $arr_duration = explode(':', $match[1]);
    $data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转换播放时间为秒数
    $data['start'] = $match[2]; //开始时间 
    $data['bitrate'] = $match[3]; //码率(kb)
  }
  if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {
    $data['vcodec'] = $match[1]; //视频编码格式
    $data['vformat'] = $match[2]; //视频格式
    $data['resolution'] = $match[3]; //视频分辨率
    $arr_resolution = explode('x', $match[3]);
    $data['width'] = $arr_resolution[0];
    $data['height'] = $arr_resolution[1];
  }
  if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {
    $data['acodec'] = $match[1]; //音频编码
    $data['asamplerate'] = $match[2]; //音频采样频率
  }
  if (isset($data['seconds']) && isset($data['start'])) {
    $data['play_time'] = $data['seconds'] + $data['start']; //实际播放时间
  }
  $data['size'] = filesize($file); //文件大小
  return $data;
}
  
//用法
$video_info = getVideoInfo('video.mp4');
print_r($video_info);
?>

总结

到此这篇关于php如何利用ffmpeg获取视频第一帧为缩略图的文章就介绍到这了,更多相关php ffmpeg获取视频缩略图内容请搜索阿兔在线工具以前的文章或继续浏览下面的相关文章希望大家以后多多支持阿兔在线工具!

点赞(0)

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部