php如何利用ffmpeg獲取視頻第一幀為縮略圖
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php編寫的mysqli增刪改查數(shù)據(jù)庫操作類示例
這篇文章主要為大家介紹了php編寫的mysqli增刪改查數(shù)據(jù)庫操作類示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
php程序的國際化實(shí)現(xiàn)方法(利用gettext)
這里我們主要介紹window平臺下使用php的擴(kuò)展gettext實(shí)現(xiàn)程序的國際化。2011-08-08
PHP XML Expat解析器知識點(diǎn)總結(jié)
在本文里我們給大家整理了關(guān)于PHP XML Expat解析器的相關(guān)知識點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下這個(gè)吧。2019-02-02
PHP獲取表單textarea數(shù)據(jù)中的換行問題
閑來無事,在網(wǎng)上看到一篇關(guān)于php表單轉(zhuǎn)換textarea換行符的文章,看完后,根據(jù)以往經(jīng)驗(yàn),感覺上這篇文章中的一些信息并不準(zhǔn)確...于是便自己親自對php獲取表單數(shù)據(jù)中的換行符問題進(jìn)行研究2010-09-09
PHP設(shè)計(jì)模式之組合模式定義與應(yīng)用示例
這篇文章主要介紹了PHP設(shè)計(jì)模式之組合模式定義與應(yīng)用,結(jié)合實(shí)例形式詳細(xì)分析了PHP組合模式基本原理、定義與使用方法,需要的朋友可以參考下2020-02-02

