大白话 音频指纹:就像人的指纹,每首歌有唯一的"声纹"。录一段音 → 算出指纹 → 去数据库比对 → 告诉你这是什么歌。Shazam 就是这个原理。 音频文件 → fpcalc 算指纹 → AcoustIDAPI查询 → 返回歌曲信息PHP没有原生库能算指纹,标准做法是调 fpcalc 二进制工具。---安装# 安装 fpcalc(Chromaprint 命令行工具)apt install libchromaprint-tools# Ubuntu/Debianbrew install chromaprint# macOS# PHP 库composerrequiresimbiat/php-acoustid# AcoustID API 客户端composerrequirejames-heinrich/getid3# 读音频元数据---完整示例<?phprequire'vendor/autoload.php';// 1. 用 fpcalc 生成音频指纹$file='/path/to/song.mp3';$output=shell_exec('fpcalc -json '.escapeshellarg($file));$data=json_decode($output,true);$duration=(int)$data['duration'];// 时长(秒)$fingerprint=$data['fingerprint'];// 声纹字符串echo"时长:{$duration}s\n";echo"指纹: ".substr($fingerprint,0,40)."...\n";// 2. 查询 AcoustID API 识别歌曲// 免费申请 key: https://acoustid.org/login$apiKey='YOUR_ACOUSTID_API_KEY';$url='https://api.acoustid.org/v2/lookup?'.http_build_query(['client'=>$apiKey,'duration'=>$duration,'fingerprint'=>$fingerprint,'meta'=>'recordings+releasegroups',]);$resp=json_decode(file_get_contents($url),true);// 3. 解析结果foreach($resp['results']??[]as$result){echo"匹配度: ".round($result['score']*100)."%\n";foreach($result['recordings']??[]as$rec){echo"歌曲:{$rec['title']}\n";foreach($rec['artists']??[]as$artist){echo"艺术家:{$artist['name']}\n";}}}---输出示例 时长:213s 指纹:AQAA3UlSRIqHSImSSoqSSoqSSoqSSoqS...匹配度:98%歌曲:Bohemian Rhapsody 艺术家:Queen---用 getID3 读本地元数据(不联网)<?phprequire'vendor/autoload.php';$getID3=newgetID3();$info=$getID3->analyze('/path/to/song.mp3');echo$info['tags']['id3v2']['title'][0]??'无标题';// 歌名echo$info['tags']['id3v2']['artist'][0]??'无艺术家';// 艺术家echo$info['playtime_string'];// 时长 3:33echo$info['audio']['bitrate'];// 比特率 320000---方案对比 ┌───────────────────────┬────────────────────────────┬────────────────────┐ │ 方案 │ 用途 │ 能识别未知歌曲 │ ├───────────────────────┼────────────────────────────┼────────────────────┤ │ fpcalc+AcoustIDAPI│ 识别歌曲(Shazam同款算法) │ 是 │ ├───────────────────────┼────────────────────────────┼────────────────────┤ │ getID3 │ 读MP3/ID3标签元数据 │ 否(只读已有标签) │ ├───────────────────────┼────────────────────────────┼────────────────────┤ │ php-ffmpeg │ 音频格式转换/预处理 │ 否 │ └───────────────────────┴────────────────────────────┴────────────────────┘ 短片段识别(几秒钟)AcoustID 效果不如完整曲目,如果需要 Shazam 级别的短片段匹配,目前没有开源PHP方案。