php使用ffmpeg获取视频信息
可以获取到视频的分辨率、文件大小、播放时长、编码格式、视频格式、音频编码、音频采样频率、等.....
废话不多说,直接上代码...
/**
* Name: ffmpeg.php.
* Author: JiaMeng <666@majiameng.com>
* Date: 2018/7/12 14:39
* Description: ffmpeg.php.
*/
class ffmpeg{
const FFMPEG_COMMAND = '/usr/local/ffmpeg/bin/ffmpeg -i %s 2>&1';//操作ffmpeg命令
/**
* Description: 获取视频信息
* Author: JiaMeng <666@majiameng.com>
* @param string $file 视频文件路径
* @return array
*/
static function video_info($file) {
/** 通过使用输出缓冲,获取到ffmpeg所有输出的内容 */
ob_start();
passthru(sprintf(self::FFMPEG_COMMAND, $file));
$info = ob_get_contents();
ob_end_clean();
$result = array();
// Duration: 01:24:12.73, start: 0.000000, bitrate: 456 kb/s
if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
$result['duration'] = $match[1]; // 提取出播放时间
$da = explode(':', $match[1]);
$result['seconds'] = $da[0] * 3600 + $da[1] * 60 + $da[2]; // 转换为秒
$result['start'] = $match[2]; // 开始时间
$result['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)) {
$result['vcodec'] = $match[1]; // 编码格式
$result['vformat'] = $match[2]; // 视频格式
$result['resolution'] = $match[3]; // 分辨率
if(strpos($result['resolution'],'x') === false){
// Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p(tv, smpte170m/smpte170m/bt709), 320x240, 80 kb/s, 29.65 fps, 29.97 tbr, 90k tbn, 59.31 tbc (default)
$result['resolution'] = $match[4]; // 分辨率
}
$a = explode('x', $result['resolution']);
$result['width'] = $a[0];
$result['height'] = $a[1];
}
// Stream #0.0: Audio: cook, 44100 Hz, stereo, s16, 96 kb/s
if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {
$result['acodec'] = $match[1]; // 音频编码
$result['asamplerate'] = $match[2]; // 音频采样频率
}
if (isset($result['seconds']) && isset($result['start'])) {
$result['play_time'] = $result['seconds'] + $result['start']; // 实际播放时间
}
$result['size'] = filesize($file); // 文件大小
return $result;
}
}
怎么用这个类就不用我说了把