tencent_asr.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package services
  2. import (
  3. "fmt"
  4. asr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/asr/v20190614"
  5. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
  6. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
  7. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
  8. )
  9. type TencentRecTaskReq struct {
  10. SecretId string
  11. SecretKey string
  12. FileUrl string
  13. CallbackUrl string
  14. }
  15. // TencentCreateRecTask 创建录音文件识别
  16. func TencentCreateRecTask(req TencentRecTaskReq) (taskId int, err error) {
  17. if req.SecretId == "" || req.SecretKey == "" || req.FileUrl == "" {
  18. err = fmt.Errorf("参数有误, SecretId: %s, SecretKey: %s, FileUrl: %s", req.SecretId, req.SecretKey, req.FileUrl)
  19. return
  20. }
  21. credential := common.NewCredential(
  22. req.SecretId,
  23. req.SecretKey,
  24. )
  25. cpf := profile.NewClientProfile()
  26. cpf.HttpProfile.Endpoint = "asr.tencentcloudapi.com"
  27. client, e := asr.NewClient(credential, "", cpf)
  28. if e != nil {
  29. err = fmt.Errorf("asr NewClient err: %s", e.Error())
  30. return
  31. }
  32. // 实例化一个请求对象, 具体参数看文档https://cloud.tencent.com/document/product/1093/37823
  33. request := asr.NewCreateRecTaskRequest()
  34. request.EngineModelType = common.StringPtr("16k_zh")
  35. request.ChannelNum = common.Uint64Ptr(1)
  36. request.ResTextFormat = common.Uint64Ptr(2)
  37. request.SourceType = common.Uint64Ptr(0)
  38. request.Url = common.StringPtr(req.FileUrl)
  39. if req.CallbackUrl != "" {
  40. request.CallbackUrl = common.StringPtr(req.CallbackUrl)
  41. }
  42. request.SpeakerDiarization = common.Int64Ptr(1)
  43. request.SpeakerNumber = common.Int64Ptr(0)
  44. request.ConvertNumMode = common.Int64Ptr(0)
  45. request.FilterDirty = common.Int64Ptr(2)
  46. request.FilterPunc = common.Int64Ptr(0)
  47. request.FilterModal = common.Int64Ptr(2)
  48. // 返回的resp是一个CreateRecTaskResponse的实例
  49. response, e := client.CreateRecTask(request)
  50. if _, ok := e.(*errors.TencentCloudSDKError); ok {
  51. err = fmt.Errorf("asr CreateRecTask err: %s", e.Error())
  52. return
  53. }
  54. // 唯一标识, 回调会用到
  55. taskId = int(*response.Response.Data.TaskId)
  56. //fmt.Printf("%s", response.ToJsonString())
  57. return
  58. }
  59. // TencentRecTaskCallback 录音识别回调参数
  60. type TencentRecTaskCallback struct {
  61. Code int `description:"任务状态码,0为成功,其他:失败;详见https://cloud.tencent.com/document/product/1093/52632"`
  62. Message string `description:"失败原因文字描述,成功时此值为空"`
  63. RequestId uint64 `json:"requestId" description:"任务唯一标识,与录音识别请求中返回的TaskId一致。数据格式必须设置为Uint64"`
  64. Appid uint64 `description:"腾讯云应用ID"`
  65. ProjectId int `json:"projectid" description:"腾讯云项目ID"`
  66. AudioUrl string `json:"audioUrl" description:"语音URL,如创建任务时为上传数据的方式,则不包含该字段"`
  67. Text string `description:"识别出的结果文本"`
  68. ResultDetail string `json:"resultDetail" description:"包含详细识别结果,如创建任务时 ResTextFormat 为0,则不包含该字段"`
  69. AudioTime float64 `json:"audioTime" description:"语音总时长"`
  70. }
  71. // TencentRecTaskCallbackResp 录音识别回调响应体
  72. type TencentRecTaskCallbackResp struct {
  73. Code int `description:"0为成功, 其他值代表失败"`
  74. Message string `description:"失败原因说明"`
  75. }
  76. // TencentRecTaskSentenceDetail 录音识别相应结果-单句详情
  77. type TencentRecTaskSentenceDetail struct {
  78. FinalSentence string `description:"单句最终结果"`
  79. StartMs int `description:"单句开始时间(毫秒)"`
  80. EndMs int `description:"单句结束时间(毫秒)"`
  81. }