当我运行exec('sox sound.wav -n stat');
时在PHP中
我得到一个输出:
Samples read: 82688
Length (seconds): 0.937506
Scaled by: 2147483647.0
Maximum amplitude: 0.595201
Minimum amplitude: -1.000000
Midline amplitude: -0.202399
Mean norm: 0.004229
Mean amplitude: -0.000005
RMS amplitude: 0.029120
Maximum delta: 1.184857
Minimum delta: 0.000000
Mean delta: 0.002956
RMS delta: 0.028785
Rough frequency: 6938
Volume adjustment: 1.000
如何将粗略的频率放入名为$freq
的变量中。
当我尝试时:
$output = shell_exec('sox sound.wav -n stat');
或
exec('sox sound.wav -n stat', $output);
我在$output中没有得到任何返回的数据。
当我回声时,我想这样做$freq;
我看到 6938。
感谢 @andyvanee 提供 sox 在 stderr 上输出此数据的提示,因此您必须执行 shell 重定向才能获得粗略频率的数字值:
exec('sox sound.wav -n stat 2>&1', $output);
$freq_explode = explode(':', $output[13]);
$freq = $freq_explode[1];
echo $freq; // returns just value of rough frequency
编辑/更新:如果有人遇到同样的问题...我注意到有时使用 SOX 时,如果.wav出现问题,数组中返回的第一个值可以是"sox WARN wav:波头缺少 fmt 块的扩展部分"......这会导致粗略频率移动到数组中的第 14 个值而不是第 13 个值......可能有更优雅/更好的方法来解决这个问题(比如也许有一种方法可以在 Sox 中禁用警告,我不知道,但如果您确实发表评论并且我会修复或搜索"粗略频率"(,但对于我的情况/快速解决方法/解决方案,这就是我解决问题的方式:
if ($output[0] != 'sox WARN wav: wave header missing extended part of fmt chunk'){
$freq_explode = explode(':', $output[13]);
$freq = $freq_explode[1];
}
if ($output[0] == 'sox WARN wav: wave header missing extended part of fmt chunk'){
$freq_explode = explode(':', $output[14]);
$freq = $freq_explode[1];
}