ProcessVoice.class.php
3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
class Lib_Utils_ProcessVoice
{
private static $rates = array(
array('ab'=> 32, 'ar'=>11000),
array('ab'=> 96, 'ar'=>11000),
array('ab'=> 32, 'ar'=>24000),
array('ab'=> 96, 'ar'=>24000),
);
/**
* 处理音频
*
* @param string $file
* @return string
*/
public static function process($file)
{
$ext = strtolower(substr(strrchr($file, '.'), 1));
$realfile = $file;
$filesize = 0;
if(array_search($ext, array('amr','mp3','wav')) !== false)
{
$realfile = str_replace($ext,'m4a', $file);
$result = array();
$retval = '';
foreach(self::$rates as $key => $rate)
{
$command = 'ffmpeg -y -i '.escapeshellcmd($file);
$command .= ' -ab ' . $rate['ab']. ' -ar '. $rate['ar'];
$command .= ' '.escapeshellcmd($realfile).' 2>&1';
@exec($command, $result , $retval);
if(count($result) > 54)
{
preg_match_all('/^size= (.+) time=/', $result[54], $out, 0);
$filesize = filesize($realfile);
if($filesize <=0 )
{
if(strpos($out[1][0],'k'))
{
$filesize = intval($out[1][0])*1024;
}
else if(strpos($out[1][0], 'm'))
{
$filesize = intval($out[1][0])*1024*1024;
}
else
{
$filesize = intval($out[1][0]);
}
}
}
else
{
continue;
}
if($filesize>0)
{
@unlink($file);
break;
}
}
//没有转换成功,恢复
if($filesize<=0)
{
@rename($file, $realfile);
}
}
return $realfile;
}
/**
* 保存音频
*
* @param stream $file
* @return string
*/
public static function saveVoice($file)
{
$path = Config_File_Voice::$voiceServer['voice'].Config_File_Upload::$voice['path'];
$up = new Util_Upload_File($file, $path);
$up->setFormat(Config_File_Upload::$voice['format']);
$up->setMaxFileSize(Config_File_Upload::$voice['upload_max_size']);
$checkStatus = $up->check();
$url = '';
if($checkStatus['code'] == 0)
{
$checkStatus = $up->write ();
$url = $checkStatus ['result'] ['hit']['voice']['relaPath'];
self::process($path.$url);
$url = str_replace(substr(strrchr($url, '.'), 1), 'm4a', $url);
}
echo $url;
return $url;
}
}