Skip to content

Instantly share code, notes, and snippets.

@imbyc
Last active November 25, 2020 03:01
Show Gist options
  • Save imbyc/dcfce947c441bbfaa6dd5e2c2b91d1d5 to your computer and use it in GitHub Desktop.
Save imbyc/dcfce947c441bbfaa6dd5e2c2b91d1d5 to your computer and use it in GitHub Desktop.
[PHP 获取wav音频文件时长]
<?php
// 简单版本
$filename = 'LoveMe.wav';
$wavdataoffset = 44;
$wavdataend = filesize($filename);
$fp = fopen($filename, 'rb');
$fseek = fseek($fp, 28);
$bitrate = unpack('V', fread($fp, 4));
$bitrate = $bitrate[1];
$playtime_seconds = (float)(($wavdataend - $wavdataoffset) / $bitrate);
fclose($fp);
$playtime_string = playtimeString($playtime_seconds);
// 可以考虑四舍五入
$playtime_seconds = (int)ceil($playtime_seconds);
echo "秒数:${playtime_seconds}秒\n时长:${playtime_string}\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment