欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2022年03月02日 11:44:13   作者:gusijin  
用PHP生成視頻的縮略圖,在網(wǎng)上比較一致的方法就是通過ffmpeg來(lái)做的,下面這篇文章主要給大家介紹了關(guān)于php如何利用ffmpeg獲取視頻第一幀為縮略圖的相關(guān)資料,需要的朋友可以參考下

php ffmpeg獲取視頻縮略圖

1.環(huán)境

  • 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

查看環(huán)境變量是否配置成功

ffmpeg -version

3.項(xiàng)目安裝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]; //播放時(shí)間
    $arr_duration = explode(':', $match[1]);
    $data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //轉(zhuǎn)換播放時(shí)間為秒數(shù)
    $data['start'] = $match[2]; //開始時(shí)間 
    $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']; //實(shí)際播放時(shí)間
  }
  $data['size'] = filesize($file); //文件大小
  return $data;
}
  
//用法
$video_info = getVideoInfo('video.mp4');
print_r($video_info);
?>

總結(jié)

到此這篇關(guān)于php如何利用ffmpeg獲取視頻第一幀為縮略圖的文章就介紹到這了,更多相關(guān)php ffmpeg獲取視頻縮略圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論