微信小程序录音转文字 Laravel框架

2019-04-04  本文已影响0人  gurlan

liunx系统 安装 ffmpeg

申请百度语音识别接口

 /**
     * 录音转文字
     * @param Request $request
     * @return mixed
     */
    public function speech(Request $request){
     //   $path =   Flysystem::put(111,$request->file('record'));

       if(!$request->hasFile('record')) {
           return   new ResultService(RESULT_FAILED,'','没有录音文件');
       }

        $path = Storage::disk('local')->putFile('record', $request->file('record'));
        $in = '/data/www/storage/app/'.$path;
       // $in = '/data/www/storage/app/record/PHEy1oW6HKKNAhKzgLjlzhhceZFdJi5gAKW6YSLS.mpga';
        $out = '/data/www/storage/app/record/'.time().'.wav';

        $shell = "ffmpeg -i $in -y -f wav $out";
        exec($shell,$file,$status);
        try{
           // echo file_get_contents($out);die;
            $client = new AipSpeechService(env('BAIDU_APP_ID'), env('BAIDU_API_KEY'),  env('BAIDU_SECRET_KEY'));
            // 识别本地文件
            $res =  $client->asr(file_get_contents($out),'wav', 16000, array(
                'dev_pid' => 1536,
            ));

            if($res['err_no']==0){
                return   new ResultService(RESULT_SUCCESS,$res['result'][0]);
            }else{
                Log::debug(json_encode( $res));
                return   new ResultService(RESULT_FAILED,'','接口失败');
            }
        }catch (Exception $exception){
           Log::debug(json_encode( $exception->getMessage()));
           return   new ResultService(RESULT_FAILED,'','转换失败');
        }

    }
上一篇 下一篇

猜你喜欢

热点阅读