>  기사  >  백엔드 개발  >  PHP에서 비디오 정보를 얻기 위해 ffmpeg를 호출하는 방법

PHP에서 비디오 정보를 얻기 위해 ffmpeg를 호출하는 방법

怪我咯
怪我咯원래의
2017-07-12 10:40:163195검색

FFmpeg는 디지털 오디오 및 비디오를 녹음하고 변환하고 스트림으로 변환하는 데 사용할 수 있는 오픈 소스 컴퓨터 프로그램 세트입니다. LGPL 또는 GPL 라이센스를 사용하십시오. 오디오 및 비디오 녹음, 변환 및 스트리밍을 위한 완벽한 솔루션을 제공합니다. 여기에는 매우 진보된 오디오/비디오 코덱 라이브러리인 libavcodec이 포함되어 있습니다. 높은 이식성과 코덱 품질을 보장하기 위해 libavcodec의 많은 코드가 처음부터 개발되었습니다.

이 기사에서는 ffmpeg를 호출하여 비디오 정보를 얻는 방법을 소개합니다. ffmpeg를 호출하려면 먼저 서버에 ffmpeg를 설치해야 하며 설치 방법은 매우 간단하며 직접 검색할 수 있습니다.

코드는 다음과 같습니다:

<?php
// 定义ffmpeg路径及命令常量
define(&#39;FFMPEG_CMD&#39;, &#39;/usr/local/bin/ffmpeg -i "%s" 2>&1&#39;);

/**
 * 使用ffmpeg获取视频信息
 * @param String $file 视频文件
 * @return Array
 */
function getVideoInfo($file){
 ob_start();
 passthru(sprintf(FFMPEG_CMD, $file));
 $video_info = ob_get_contents();
 ob_end_clean();

 // 使用输出缓冲,获取ffmpeg所有输出内容
 $ret = array();

 // Duration: 00:33:42.64, start: 0.000000, bitrate: 152 kb/s
 if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $video_info, $matches)){
  $ret[&#39;duration&#39;] = $matches[1]; // 视频长度
  $duration = explode(&#39;:&#39;, $matches[1]);
  $ret[&#39;seconds&#39;] = $duration[0]*3600 + $duration[1]*60 + $duration[2]; // 转为秒数
  $ret[&#39;start&#39;] = $matches[2]; // 开始时间
  $ret[&#39;bitrate&#39;] = $matches[3]; // bitrate 码率 单位kb
 }

 // Stream #0:1: Video: rv20 (RV20 / 0x30325652), yuv420p, 352x288, 117 kb/s, 15 fps, 15 tbr, 1k tbn, 1k tbc
 if(preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $video_info, $matches)){
  $ret[&#39;vcodec&#39;] = $matches[1];  // 编码格式
  $ret[&#39;vformat&#39;] = $matches[2]; // 视频格式
  $ret[&#39;resolution&#39;] = $matches[3]; // 分辨率
  list($width, $height) = explode(&#39;x&#39;, $matches[3]);
  $ret[&#39;width&#39;] = $width;
  $ret[&#39;height&#39;] = $height;
 }

 // Stream #0:0: Audio: cook (cook / 0x6B6F6F63), 22050 Hz, stereo, fltp, 32 kb/s
 if(preg_match("/Audio: (.*), (\d*) Hz/", $video_info, $matches)){
  $ret[&#39;acodec&#39;] = $matches[1];  // 音频编码
  $ret[&#39;asamplerate&#39;] = $matches[2]; // 音频采样频率
 }

 if(isset($ret[&#39;seconds&#39;]) && isset($ret[&#39;start&#39;])){
  $ret[&#39;play_time&#39;] = $ret[&#39;seconds&#39;] + $ret[&#39;start&#39;]; // 实际播放时间
 }

 $ret[&#39;size&#39;] = filesize($file); // 视频文件大小
 $video_info = iconv(&#39;gbk&#39;,&#39;utf8&#39;, $video_info);
 return array($ret, $video_info);

}

// 输出视频信息
$video_info = getVideoInfo(&#39;myvideo.avi&#39;);
print_r($video_info[0]);
?>

출력:

Array
(
 [duration] => 00:33:42.64
 [seconds] => 2022.64
 [start] => 0.000000
 [bitrate] => 152
 [vcodec] => rv20 (RV20 / 0x30325652)
 [vformat] => yuv420p
 [resolution] => 352x288
 [width] => 352
 [height] => 288
 [acodec] => cook (cook / 0x6B6F6F63)
 [asamplerate] => 22050
 [play_time] => 2022.64
 [size] => 38630744
)

위 내용은 PHP에서 비디오 정보를 얻기 위해 ffmpeg를 호출하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.