반응형
ffmpeg는 디지털 오디오·비디오를 녹음·변환해 스트리밍할 수 있는 오픈소스 컴퓨터 프로그램으로 libavcodec을 포함, 높은 이동성과 코덱 품질을 보장한다.
php에서 ffmpeg를 호출하여 영상정보를 얻는데, ffmpeg를 호출하려면 먼저 서버에 ffmpeg가 설치되어 있어야 하며, 설치방법이 간단하여 스스로 검색할 수 있음을 알려드립니다.
코드는 다음과 같습니다
<?php
// ffmpeg 경로 및 명령 상수 정의
define('FFMPEG_CMD', '/usr/local/bin/ffmpeg -i "%s" 2>&1');
/**
* 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['duration'] = $matches[1]; // 비디오 길이
$duration = explode(':', $matches[1]);
$ret['seconds'] = $duration[0]*3600 + $duration[1]*60 + $duration[2]; // 초로 돌리다
$ret['start'] = $matches[2]; // 시작 시간
$ret['bitrate'] = $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['vcodec'] = $matches[1]; // 인코딩 형식
$ret['vformat'] = $matches[2]; // 비디오 형식
$ret['resolution'] = $matches[3]; // 해상도
list($width, $height) = explode('x', $matches[3]);
$ret['width'] = $width;
$ret['height'] = $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['acodec'] = $matches[1]; // 오디오 부호화
$ret['asamplerate'] = $matches[2]; // 오디오 샘플링 주파수
}
if(isset($ret['seconds']) && isset($ret['start'])){
$ret['play_time'] = $ret['seconds'] + $ret['start']; //실제 재생 시간
}
$ret['size'] = filesize($file); // 비디오 파일 크기
$video_info = iconv('gbk','utf8', $video_info);
return array($ret, $video_info);
}
// 비디오 정보 출력
$video_info = getVideoInfo('myvideo.avi');
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' 카테고리의 다른 글
php는 정칙으로 가로세로 스타일 제거 (0) | 2022.09.05 |
---|---|
php 함수는 가변적인 수의 인자를 사용합니다 (0) | 2022.09.05 |
php 자급자족 id를 기반으로 고유 번호 클래스 만들기 (0) | 2022.09.05 |
php 페이지나 그림이 gzip으로 압축되었는지 판단 (0) | 2022.09.05 |
php 함수는 가변 수량의 인자를 사용합니다 (0) | 2022.08.26 |