PHP

[PHP] 오디오 runtime 구하는 함수

은둔한량 2024. 8. 22. 12:12
반응형

php 에서 오디오 runtime 구하는 방법을 찾아보다가 getID3가 있었는데 최신버젼으로 컨버팅이 되어 있지 않았다.

그래서  ffmpeg 를 이용해서 구하는 방법을 찾아냈다. ㅎㅎ

server ubuntu  설치

apt install ffmpeg

윈도우에서는 https://ffmpeg.org/

 

FFmpeg

Converting video and audio has never been so easy. $ ffmpeg -i input.mp4 output.avi     News June 2rd, 2024, native xHE-AAC decoder FFmpeg now implements a native xHE-AAC decoder. Currently, streams without (e)SBR, USAC or MPEG-H Surround are supported,

ffmpeg.org

이쪽에 가서 다운받아 설치 한다.

 

function getAudioRuntime($path) {
    if(!is_file($path)) return;
    $cmd = "ffmpeg -i " . escapeshellarg($path) . " 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//";
    $time = exec($cmd);
    list($hms, $milli) = explode('.', $time);
    list($hours, $minutes, $seconds) = explode(':', $hms);
    $totalSeconds = ($hours * 3600) + ($minutes * 60) + $seconds;
    return (($totalSeconds * 1000) + $milli) / 1000;
}

function getAudioRuntime($path) {
    if(!is_file($path)) return;
    $cmd = "ffmpeg -i " . escapeshellarg($path) . " 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//";
    $time = exec($cmd);
    list($hms, $milli) = explode('.', $time);
    list($hours, $minutes, $seconds) = explode(':', $hms);
    $totalSeconds = ($hours * 3600) + ($minutes * 60) + $seconds;
    return (($totalSeconds * 1000) + $milli) / 1000;
}

 

결과값은 초(sec)로 나온다. ㅎㅎ

반응형