You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
4.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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语音 URL1语音数据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;
}
}
}