PHP利用ffmpeg獲取音頻、視頻的詳細(xì)信息
一、目的
使用PHP利用ffmpeg獲取音頻、視頻的詳細(xì)信息,音視頻總時(shí)長、碼率、視頻分辨率、音頻編碼、音頻采樣頻率、實(shí)際播放時(shí)間、文件大小。
二、下載并安裝ffmpeg
1、下載地址:https://www.ffmpeg.org/
2、解壓放到項(xiàng)目下,文件夾下有三個(gè)exe

三、PHP代碼
說明:ffmpeg 更換成自己實(shí)際存放地址;
getVideoInfo()方法有三個(gè)判斷,分別是獲取音視頻基本信息(音視頻時(shí)長、音視頻秒數(shù)、開始時(shí)間、碼率等),視頻信息(視頻編碼、視頻格式、視頻分辨率、視頻尺寸),音頻信息(音頻編碼、音頻采樣頻率)、音視頻文件大小
/**
* 獲取音視頻基本信息
*/
public function getVideoInfo($file)
{
$command = sprintf('E:/phpstudy_pro/WWW/test/public/ffmpeg/bin/ffmpeg -i "%s" 2>&1', $file); //你的ffmpeg路徑
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;
}
/**
* 調(diào)用
*/
public function video()
{
// 定義視頻路徑
$videoPath = 'E:/phpstudy_pro/WWW/test/public/uploads/8秒.mp4';
$video_info = $this->getVideoInfo($videoPath);
echo '<pre>';
print_r($video_info);
}四、運(yùn)行結(jié)果

以上就是PHP利用ffmpeg獲取音頻、視頻的詳細(xì)信息的詳細(xì)內(nèi)容,更多關(guān)于PHP ffmpeg獲取音頻信息的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
PDO版本問題 Invalid parameter number: no parameters were bound
發(fā)現(xiàn)在客戶的某個(gè)PHP版本下,執(zhí)行某類操作的時(shí)候,總是會(huì)報(bào)如下錯(cuò)誤 Invalid parameter number: no parameters were bound,經(jīng)google,發(fā)現(xiàn)是php版本過低導(dǎo)致2013-01-01
解析php中array_merge與array+array的區(qū)別
本篇文章是對php中array_merge與array+array的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php使用json_decode后數(shù)字對象轉(zhuǎn)換成了科學(xué)計(jì)數(shù)法的解決方法
這篇文章主要介紹了php使用json_decode后數(shù)字對象轉(zhuǎn)換成了科學(xué)計(jì)數(shù)法的解決方法,涉及php操作json格式數(shù)據(jù)與數(shù)值轉(zhuǎn)換相關(guān)技巧,需要的朋友可以參考下2017-02-02
PHP實(shí)現(xiàn)的mysql操作類【MySQL與MySQLi方式】
這篇文章主要介紹了PHP實(shí)現(xiàn)的mysql操作類,結(jié)合實(shí)例形式分析了MySQL與MySQLi方式連接與操作MySQL數(shù)據(jù)庫的常用方法封裝與使用技巧,需要的朋友可以參考下2017-10-10
利用php+mcDropdown實(shí)現(xiàn)文件路徑可在下拉框選擇
以下是對php+mcDropdown實(shí)現(xiàn)文件路徑可在下拉框進(jìn)行選擇的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-08-08
說明的比較細(xì)的php 正則學(xué)習(xí)實(shí)例
首先,讓我們看看兩個(gè)特別的字符:’^’ 和 ‘$’ 他們是分別用來匹配字符串的開始和結(jié)束,一下分別舉例說明2008-07-07

