來源:http://www.kindcent.com/blog/view/php-ffmpeg
通過passthru函數調用系統 ffmpeg命令,解析返回的字符串,提取出視頻信息。PHP和Perl有很大的不一樣,調用方法也不一樣,實際返回的數據和上一篇Perl調用的並不完全一樣,因為用在不同的地方,要去掉或添加其他信息,可自行修改。參考了網上比較流行的代碼。ffmpeg中有個start時間,這個時間為開始的延時時間,通過time命令測試后發現,文件的時間播放時間 = duration + start 。
//
定義 FFmpeg的路徑,因為一般情況下系統路徑會不一致,所以,最好在網站的全局配置文件里定義好
// 另外,重定向符號在FreeBSD等csh系統中為 >&
define('KC_FFMPEG_PATH', '/usr/local/bin/ffmpeg -i "%s" 2>&1');
//本人經過測試,windows的命令也適用,如下(ffmpeg.exe放在c:\windows\system32,如果沒有放在system32,需要指明路徑)
// define('KC_FFMPEG_PATH', 'ffmpeg -i "%s" 2>&1');
function video_info( $file) {
ob_start();
passthru( sprintf(KC_FFMPEG_PATH, $file));
$info = ob_get_contents();
ob_end_clean();
// 通過使用輸出緩沖,獲取到ffmpeg所有輸出的內容。
$ret = array();
// Duration: 01:24:12.73, start: 0.000000, bitrate: 456 kb/s
if ( preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
$ret['duration'] = $match[1]; // 提取出播放時間
$da = explode(':', $match[1]);
$ret['seconds'] = $da[0] * 3600 + $da[1] * 60 + $da[2]; // 轉換為秒
$ret['start'] = $match[2]; // 開始時間
$ret['bitrate'] = $match[3]; // bitrate 碼率 單位 kb
}
// Stream #0.1: Video: rv40, yuv420p, 512x384, 355 kb/s, 12.05 fps, 12 tbr, 1k tbn, 12 tbc
if ( preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {
$ret['vcodec'] = $match[1]; // 編碼格式
$ret['vformat'] = $match[2]; // 視頻格式
$ret['resolution'] = $match[3]; // 分辨率
$a = explode('x', $match[3]);
$ret['width'] = $a[0];
$ret['height'] = $a[1];
}
// Stream #0.0: Audio: cook, 44100 Hz, stereo, s16, 96 kb/s
if ( preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {
$ret['acodec'] = $match[1]; // 音頻編碼
$ret['asamplerate'] = $match[2]; // 音頻采樣頻率
}
if ( isset( $ret['seconds']) && isset( $ret['start'])) {
$ret['play_time'] = $ret['seconds'] + $ret['start']; // 實際播放時間
}
$ret['size'] = filesize( $file); // 文件大小
return $ret;
}
// 調用方法:
print_r( video_info('test.mpg'));
// 輸出的結果:
$ret = array (
'duration' => '02:07:42.58',
'seconds' => 7662.58,
'start' => '0.000000',
'bitrate' => '843',
'vcodec' => 'rv40',
'vformat' => 'yuv420p',
'resolution' => '800x452',
'width' => '800',
'height' => '452',
'acodec' => 'cook',
'asamplerate' => '44100',
'play_time' => 7662.58,
'size' => 807562411,
// 另外,重定向符號在FreeBSD等csh系統中為 >&
define('KC_FFMPEG_PATH', '/usr/local/bin/ffmpeg -i "%s" 2>&1');
//本人經過測試,windows的命令也適用,如下(ffmpeg.exe放在c:\windows\system32,如果沒有放在system32,需要指明路徑)
// define('KC_FFMPEG_PATH', 'ffmpeg -i "%s" 2>&1');
function video_info( $file) {
ob_start();
passthru( sprintf(KC_FFMPEG_PATH, $file));
$info = ob_get_contents();
ob_end_clean();
// 通過使用輸出緩沖,獲取到ffmpeg所有輸出的內容。
$ret = array();
// Duration: 01:24:12.73, start: 0.000000, bitrate: 456 kb/s
if ( preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
$ret['duration'] = $match[1]; // 提取出播放時間
$da = explode(':', $match[1]);
$ret['seconds'] = $da[0] * 3600 + $da[1] * 60 + $da[2]; // 轉換為秒
$ret['start'] = $match[2]; // 開始時間
$ret['bitrate'] = $match[3]; // bitrate 碼率 單位 kb
}
// Stream #0.1: Video: rv40, yuv420p, 512x384, 355 kb/s, 12.05 fps, 12 tbr, 1k tbn, 12 tbc
if ( preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {
$ret['vcodec'] = $match[1]; // 編碼格式
$ret['vformat'] = $match[2]; // 視頻格式
$ret['resolution'] = $match[3]; // 分辨率
$a = explode('x', $match[3]);
$ret['width'] = $a[0];
$ret['height'] = $a[1];
}
// Stream #0.0: Audio: cook, 44100 Hz, stereo, s16, 96 kb/s
if ( preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {
$ret['acodec'] = $match[1]; // 音頻編碼
$ret['asamplerate'] = $match[2]; // 音頻采樣頻率
}
if ( isset( $ret['seconds']) && isset( $ret['start'])) {
$ret['play_time'] = $ret['seconds'] + $ret['start']; // 實際播放時間
}
$ret['size'] = filesize( $file); // 文件大小
return $ret;
}
// 調用方法:
print_r( video_info('test.mpg'));
// 輸出的結果:
$ret = array (
'duration' => '02:07:42.58',
'seconds' => 7662.58,
'start' => '0.000000',
'bitrate' => '843',
'vcodec' => 'rv40',
'vformat' => 'yuv420p',
'resolution' => '800x452',
'width' => '800',
'height' => '452',
'acodec' => 'cook',
'asamplerate' => '44100',
'play_time' => 7662.58,
'size' => 807562411,
);
