|
|
<?php
|
|
|
|
|
|
namespace tencent\asr;
|
|
|
|
|
|
use TencentCloud\Common\Credential;
|
|
|
use TencentCloud\Common\Profile\ClientProfile;
|
|
|
use TencentCloud\Common\Profile\HttpProfile;
|
|
|
use TencentCloud\Common\Exception\TencentCloudSDKException;
|
|
|
use TencentCloud\Asr\V20190614\AsrClient;
|
|
|
use TencentCloud\Asr\V20190614\Models\CreateRecTaskRequest;
|
|
|
use TencentCloud\Asr\V20190614\Models\DescribeTaskStatusRequest;
|
|
|
|
|
|
class Record extends Base
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 录音文件识别请求 (支持wav、mp3、m4a、flv、mp4、wma、3gp、amr、aac、ogg-opus、flac格式)
|
|
|
* @param string $source_type 语音数据来源。0:语音 URL;1:语音数据(post body)
|
|
|
* @param string $content $source_type = 1语音的URL地址,需要公网可下载。长度小于2048字节,当 SourceType 值为 0 时须填写该字段,为 1 时不需要填写。注意:请确保录音文件时长在5个小时之内,否则可能识别失败。请保证文件的下载速度,否则可能下载失败。
|
|
|
* $source_type = 2语音数据,当SourceType 值为1时必须填写,为0可不写。要base64编码(采用python语言时注意读取文件应该为string而不是byte,以byte格式读取后要decode()。编码后的数据不可带有回车换行符)。音频数据要小于5MB
|
|
|
* @param string $callback_url 回调 URL,用户自行搭建的用于接收识别结果的服务URL。
|
|
|
* @param integer $res_text_format 识别结果返回形式。0: 识别结果文本(含分段时间戳); 1:词级别粒度的详细识别结果(不含标点,含语速值);2:词级别粒度的详细识别结果(包含标点、语速值)
|
|
|
* @param integer $channel_num 识别声道数 1:单声道;2:双声道(仅支持 8k_zh 引擎模)。注意:录音识别会自动将音频转码为填写的识别声道数
|
|
|
* @param string $engine_model_type 引擎模型类型
|
|
|
* @date 2021-02-07
|
|
|
*/
|
|
|
public function CreateRecTask($source_type, $content, $callback_url = '', $res_text_format = 0, $channel_num = 1, $engine_model_type = '16k_zh')
|
|
|
{
|
|
|
try {
|
|
|
|
|
|
$cred = new Credential($this->config['secret_id'], $this->config['secret_key']);
|
|
|
$httpProfile = new HttpProfile();
|
|
|
$httpProfile->setEndpoint("asr.tencentcloudapi.com");
|
|
|
|
|
|
$clientProfile = new ClientProfile();
|
|
|
$clientProfile->setHttpProfile($httpProfile);
|
|
|
$client = new AsrClient($cred, "", $clientProfile);
|
|
|
|
|
|
$req = new CreateRecTaskRequest();
|
|
|
|
|
|
$params = array(
|
|
|
"SourceType" => $source_type,
|
|
|
"ResTextFormat" => $res_text_format,
|
|
|
"ChannelNum" => $channel_num,
|
|
|
"EngineModelType" => $engine_model_type,
|
|
|
);
|
|
|
if ($source_type == 0) {
|
|
|
$params['Url'] = $content;
|
|
|
} else if ($source_type == 1) {
|
|
|
$params['Data'] = $content;
|
|
|
}
|
|
|
$req->fromJsonString(json_encode($params));
|
|
|
|
|
|
$resp = $client->CreateRecTask($req);
|
|
|
|
|
|
$resp = $resp->toJsonString();
|
|
|
return json_decode($resp, true);
|
|
|
} catch (TencentCloudSDKException $e) {
|
|
|
echo $e;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 录音文件识别结果查询
|
|
|
* @param string $task_id 从CreateRecTask接口获取的TaskId,用于获取任务状态与结果
|
|
|
* @date 2021-02-07
|
|
|
*/
|
|
|
public function DescribeTaskStatus($task_id)
|
|
|
{
|
|
|
try {
|
|
|
|
|
|
$cred = new Credential($this->config['secret_id'], $this->config['secret_key']);
|
|
|
$httpProfile = new HttpProfile();
|
|
|
$httpProfile->setEndpoint("asr.tencentcloudapi.com");
|
|
|
|
|
|
$clientProfile = new ClientProfile();
|
|
|
$clientProfile->setHttpProfile($httpProfile);
|
|
|
$client = new AsrClient($cred, "", $clientProfile);
|
|
|
|
|
|
$req = new DescribeTaskStatusRequest();
|
|
|
|
|
|
$params = array(
|
|
|
"TaskId" => $task_id
|
|
|
);
|
|
|
$req->fromJsonString(json_encode($params));
|
|
|
|
|
|
$resp = $client->DescribeTaskStatus($req);
|
|
|
|
|
|
$resp = $resp->toJsonString();
|
|
|
return json_decode($resp, true);
|
|
|
} catch (TencentCloudSDKException $e) {
|
|
|
echo $e;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|