将一段PHP脚本改为Python脚本 (或Ruby), 请高手帮忙.
这段PHP脚本主要针对wave文件,读取文件内容,将每一16bit字节转换为10进制正负整数.然后取一特定长度中的最大采样值和最小采样值,写进PHP数组.最终输出成JSO...
这段PHP脚本主要针对wave文件, 读取文件内容, 将每一16bit字节转换为10进制正负整数. 然后取一特定长度中的最大采样值和最小采样值, 写进PHP数组. 最终输出成JSON数组形式. 并不很复杂. 但是我写完测试的时候发现在PHP命令行下运行巨慢, 一个50M的纯采样文件要运行上1分半才能处理完. 所以想试试在Python或Ruby下运行效率如何..
<?
$file = file_get_contents('913.raw');
$filesize = strlen($file);
$samples = array();
$scale = floor($filesize / 1000);
$n = 0;
$max_sample = 0;
$min_sample = 0;
for ($i = 0; $i < $filesize; $i += 2) {
$byte1 = substr($file, $i, 1);
$byre2 = substr($file, $i+1, 1);
$str = $byre2 . $byte1;
$value = hexdec(bin2hex($str));
if (32767 - $value < 0) {
$value = $value - 65536;
}
if ($max_sample < $value) {
$max_sample = floor($value / 768);
}
if ($value < $min_sample) {
$min_sample = floor($value / 768);
}
$n++;
if ($n == $scale) {
$samples[] = $max_sample;
$samples[] = $min_sample;
$max_sample = 0;
$min_sample = 0;
$n = 0;
}
}
$data = json_encode($samples);
$fh = fopen('913.dat', 'w');
fwrite($fh, $data);
fclose($fh);
echo $data;
?> 展开
<?
$file = file_get_contents('913.raw');
$filesize = strlen($file);
$samples = array();
$scale = floor($filesize / 1000);
$n = 0;
$max_sample = 0;
$min_sample = 0;
for ($i = 0; $i < $filesize; $i += 2) {
$byte1 = substr($file, $i, 1);
$byre2 = substr($file, $i+1, 1);
$str = $byre2 . $byte1;
$value = hexdec(bin2hex($str));
if (32767 - $value < 0) {
$value = $value - 65536;
}
if ($max_sample < $value) {
$max_sample = floor($value / 768);
}
if ($value < $min_sample) {
$min_sample = floor($value / 768);
}
$n++;
if ($n == $scale) {
$samples[] = $max_sample;
$samples[] = $min_sample;
$max_sample = 0;
$min_sample = 0;
$n = 0;
}
}
$data = json_encode($samples);
$fh = fopen('913.dat', 'w');
fwrite($fh, $data);
fclose($fh);
echo $data;
?> 展开
1个回答
展开全部
嗯,数据量大的话,python也不会很快,你试试看:
import binascii
import json
f = open('913.mp3','rb')
data = f.read()
f.close()
filesize = len(data)
sample = []
scale = int(filesize/1000)
n = 0
max_sample = 0
min_sample = 0
for i in range(0,filesize-1,2):
bstr = data[i:i+2]
value = int(binascii.b2a_hex(bstr),16)
if 32767 - value < 0:
value = value - 65535
if max_sample < value:
max_sample = int(value / 768)
if value < min_sample:
min_sample = int(value / 768)
n += 1
if n==scale:
sample.append(max_sample)
sample.append(min_sample)
max_sample = 0
min_sample = 0
n = 0
data = json.dumps(sample)
f = open('913.dat','w')
f.write(data)
f.close()
print data
python的缩进很重要,你试试看,在Python2.6以上的环境运行。最后,data是不是类似这样的:
[12, -3, 17, -14, 42, -24, 19, -3, 25, -8, 21, -25, 34, -42, 13, -12, 33, -41, 33, -16, 22, -11, 7, -18, 9, -7, 0, -21, 19, -34, 36, -33, 36, -41, 14, -35, 0, -7, 36, -42, 5, -35, 18, -23, 28, -27, 27, -38, 0, -38, 24, -27, 5, -32, 41, -37, 4, -41, 22, -30, 6, -30, 27, -6, 28, -42, 38, -40, 2, -5, 41, -16, 22, -39, 18, -9, 7, -40, 22, -8, 39, -31, 0, -8, 12, -28, 25, -5, 5, -33, 23, -15, 3, -42, 20, -34, 39, -20, 8, -35, 11, -31, 26, -14, 41, -29, 29, -27, 15, -33, 18, -39, 9, -10, 28, -10, 34, -14, 6, -34, 34, -28, 10, -21, 9, -4, 16, -30, 8, -27, 31, -25, 31, -15, 26, -15, 34, -34, 35, -37, 17, -33, 2, -20, 6, -31, 17, -15, 25, -18, 7, -3, 15, -41, 33, -7, 3, -12, 9, -41, 4, -26, 21, -37, 28, -5, 9, -39, 27, -38, 10, -36, 38, -42, 34, -24, 34, -3, 29, -21, 13, -31, 36, -10, 4, -38, 29, -26, 23, -3, 4, -36, 6, -4, 29, -29, 6, -26, 17, -4, 17, -40, 4, -38, 35, -5, 35, -24, 4, -22, 42, -24, 9, -24, 25, -39, 34, -13, 28, -36, 3, -16, 41, -13, 26, -21, 36, -4, 15, -13, 16, -15, 8, -30, 35, -24, 15, -15, 40, -42, 14, -24, 7, -29, 8, -42, 11,
嗯,试试看,我用的变量名和你基本一样,应该能看懂:)
import binascii
import json
f = open('913.mp3','rb')
data = f.read()
f.close()
filesize = len(data)
sample = []
scale = int(filesize/1000)
n = 0
max_sample = 0
min_sample = 0
for i in range(0,filesize-1,2):
bstr = data[i:i+2]
value = int(binascii.b2a_hex(bstr),16)
if 32767 - value < 0:
value = value - 65535
if max_sample < value:
max_sample = int(value / 768)
if value < min_sample:
min_sample = int(value / 768)
n += 1
if n==scale:
sample.append(max_sample)
sample.append(min_sample)
max_sample = 0
min_sample = 0
n = 0
data = json.dumps(sample)
f = open('913.dat','w')
f.write(data)
f.close()
print data
python的缩进很重要,你试试看,在Python2.6以上的环境运行。最后,data是不是类似这样的:
[12, -3, 17, -14, 42, -24, 19, -3, 25, -8, 21, -25, 34, -42, 13, -12, 33, -41, 33, -16, 22, -11, 7, -18, 9, -7, 0, -21, 19, -34, 36, -33, 36, -41, 14, -35, 0, -7, 36, -42, 5, -35, 18, -23, 28, -27, 27, -38, 0, -38, 24, -27, 5, -32, 41, -37, 4, -41, 22, -30, 6, -30, 27, -6, 28, -42, 38, -40, 2, -5, 41, -16, 22, -39, 18, -9, 7, -40, 22, -8, 39, -31, 0, -8, 12, -28, 25, -5, 5, -33, 23, -15, 3, -42, 20, -34, 39, -20, 8, -35, 11, -31, 26, -14, 41, -29, 29, -27, 15, -33, 18, -39, 9, -10, 28, -10, 34, -14, 6, -34, 34, -28, 10, -21, 9, -4, 16, -30, 8, -27, 31, -25, 31, -15, 26, -15, 34, -34, 35, -37, 17, -33, 2, -20, 6, -31, 17, -15, 25, -18, 7, -3, 15, -41, 33, -7, 3, -12, 9, -41, 4, -26, 21, -37, 28, -5, 9, -39, 27, -38, 10, -36, 38, -42, 34, -24, 34, -3, 29, -21, 13, -31, 36, -10, 4, -38, 29, -26, 23, -3, 4, -36, 6, -4, 29, -29, 6, -26, 17, -4, 17, -40, 4, -38, 35, -5, 35, -24, 4, -22, 42, -24, 9, -24, 25, -39, 34, -13, 28, -36, 3, -16, 41, -13, 26, -21, 36, -4, 15, -13, 16, -15, 8, -30, 35, -24, 15, -15, 40, -42, 14, -24, 7, -29, 8, -42, 11,
嗯,试试看,我用的变量名和你基本一样,应该能看懂:)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询