获取音频文件的长度php

Bel*_*ish 9 php audio

我如何在php中获取音频文件的长度.

如果在php中太难做,那么任何其他方式都应该正常工作.

Ste*_*hry 12

如果您使用的是linux/unix并安装了ffmpeg,请执行以下操作:

$time = exec("ffmpeg -i " . escapeshellarg($path) . " 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//");
list($hms, $milli) = explode('.', $time);
list($hours, $minutes, $seconds) = explode(':', $hms);
$total_seconds = ($hours * 3600) + ($minutes * 60) + $seconds;
Run Code Online (Sandbox Code Playgroud)


Rya*_*yan 6

Stephen Fuhry 的答案的改进:

/**
 * /sf/answers/499483911/
 * @param string $path
 * @return int
 */
function getDurationOfWavInMs($path) {
    $time = getDurationOfWav($path);
    list($hms, $milli) = explode('.', $time);
    list($hours, $minutes, $seconds) = explode(':', $hms);
    $totalSeconds = ($hours * 3600) + ($minutes * 60) + $seconds;
    return ($totalSeconds * 1000) + $milli;
}

/**
 * 
 * @param string $path
 * @return string
 */
function getDurationOfWav($path) {
    $cmd = "ffmpeg -i " . escapeshellarg($path) . " 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//";
    return exec($cmd);
}
Run Code Online (Sandbox Code Playgroud)

谢谢,斯蒂芬!这对我来说效果很好(尽管对于数百个文件来说速度很慢)。


Rad*_*ski 2

什么样的音频文件?mp3?瓦夫?无论如何,您可能需要一些特定的库。请参阅: http: //de.php.net/manual/en/refs.utilspec.audio.php