ProcessVoice.class.php 3.11 KB
<?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;
    }
}