PHP利用ffmpeg獲取音頻、視頻的詳細信息
一、目的
使用PHP利用ffmpeg獲取音頻、視頻的詳細信息,音視頻總時長、碼率、視頻分辨率、音頻編碼、音頻采樣頻率、實際播放時間、文件大小。
二、下載并安裝ffmpeg
1、下載地址:https://www.ffmpeg.org/
2、解壓放到項目下,文件夾下有三個exe
三、PHP代碼
說明:ffmpeg 更換成自己實際存放地址;
getVideoInfo()方法有三個判斷,分別是獲取音視頻基本信息(音視頻時長、音視頻秒數(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]; //播放時間 $arr_duration = explode(':', $match[1]); $data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //轉(zhuǎn)換播放時間為秒數(shù) $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; } /** * 調(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); }
四、運行結(jié)果
以上就是PHP利用ffmpeg獲取音頻、視頻的詳細信息的詳細內(nèi)容,更多關(guān)于PHP ffmpeg獲取音頻信息的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
PDO版本問題 Invalid parameter number: no parameters were bound
發(fā)現(xiàn)在客戶的某個PHP版本下,執(zhí)行某類操作的時候,總是會報如下錯誤 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ū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06php使用json_decode后數(shù)字對象轉(zhuǎn)換成了科學(xué)計數(shù)法的解決方法
這篇文章主要介紹了php使用json_decode后數(shù)字對象轉(zhuǎn)換成了科學(xué)計數(shù)法的解決方法,涉及php操作json格式數(shù)據(jù)與數(shù)值轉(zhuǎn)換相關(guān)技巧,需要的朋友可以參考下2017-02-02PHP實現(xiàn)的mysql操作類【MySQL與MySQLi方式】
這篇文章主要介紹了PHP實現(xiàn)的mysql操作類,結(jié)合實例形式分析了MySQL與MySQLi方式連接與操作MySQL數(shù)據(jù)庫的常用方法封裝與使用技巧,需要的朋友可以參考下2017-10-10利用php+mcDropdown實現(xiàn)文件路徑可在下拉框選擇
以下是對php+mcDropdown實現(xiàn)文件路徑可在下拉框進行選擇的方法進行了詳細的分析介紹,需要的朋友可以過來參考下2013-08-08