php如何利用ffmpeg獲取視頻第一幀為縮略圖

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獲取視頻縮略圖內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: