获取wav时长

duration = filesize / byterate

获取wav文件的byterate

wav-sound-format.gif

图片来自:WAVE PCM soundfile format

<?php

function ascii2hex($ascii) {
    $ascii = strrev($ascii);
    $hex = '';
    for ($i = 0; $i < strlen($ascii); $i++) {
        $byte = dechex(ord($ascii{$i}));
        $byte = str_pad($byte, 2, '0', STR_PAD_LEFT);
        $hex .= $byte;
    }
    return $hex;
}

$file = fopen('test.wav', 'rb');
fseek($file, 28);
$byterate = fread($file, 4);
fclose($file);

$byterate = hexdec(ascii2hex($byterate));

echo filesize('test.wav') / $byterate / 60 . PHP_EOL;

标签: none

添加新评论