asr

package
v0.0.0-...-06f9d85 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

语音转写 Long Form Automatic Speech Recognition 如果不使用 NewXfyun,必须先 SetLfAsrSecret 设置语音转写密钥 上传音视频文件,可以设置处理结果回调,再调用结果查询接口 https://www.xfyun.cn/doc/asr/ifasr_new/API.html

Index

Constants

View Source
const (
	// 文件上传地址
	UrlUpload = "https://raasr.xfyun.cn/v2/api/upload"
	// 获取结果地址
	UrlGetResult = "https://raasr.xfyun.cn/v2/api/getResult"
)
View Source
const (
	OrderStatusCreated  = 0  // 已创建
	OrderStatusProcess  = 3  // 处理中
	OrderStatusComplete = 4  // 处理完成-成功
	OrderStatusFailed   = -1 // 处理完成-失败
)

订单流程状态

View Source
const (
	WpNormal = "n" // 正常词
	WpSmooth = "s" // 顺滑
	WpPunct  = "p" // 标点
	WpSeg    = "g" // 分段标识,对应Cw.W为空
)

词语类型

View Source
const ResCodeSuss = "000000"

响应成功时,才能获取到响应内容

Variables

View Source
var ErrProcessing = errors.New("order processing")

订单处理中错误

Functions

func Unmarshal

func Unmarshal(res *resty.Response, body ResBodyI) error

Unmarshal 响应体json解码,响应代码不是成功时返回错误 body 必须传不为nil的指针

Types

type Cw

type Cw struct {
	W  string `json:"w"`  // 识别结果
	Wp string `json:"wp"` // 词语类型
}

Cw 词语识别结果

type ErrFail

type ErrFail struct {
	FailType int // 订单失败类型
}

ErrFail 订单失败错误

func (ErrFail) Error

func (e ErrFail) Error() string

type GetResultRes

type GetResultRes struct {
	ResBody
	Content struct {
		OrderInfo struct {
			OrderId          string `json:"orderId"`          // 订单id
			FailType         int    `json:"failType"`         // 订单失败类型,处理成功时响应0
			Status           int    `json:"status"`           // 订单流程状态
			OriginalDuration int    `json:"originalDuration"` // 上传设置音频时长,单位毫秒
			RealDuration     int    `json:"realDuration"`     // 实际处理音频时长,单位毫秒
			ExpireTime       int    `json:"expireTime"`       // 已完成订单删除时间戳,毫秒
		} `json:"orderInfo"` // 转写订单信息
		OrderResult      string `json:"orderResult"`      // 转写结果,json字符串
		TaskEstimateTime int    `json:"taskEstimateTime"` // 订单预估剩余耗时,单位毫秒
	} `json:"content"`
}

GetResultRes 获取处理结果响应体 响应代码为成功时,才能确定订单状态

func (*GetResultRes) CheckOrder

func (r *GetResultRes) CheckOrder() error

CheckOrder 检查订单状态

type Json1best

type Json1best struct {
	St St `json:"st"`
}

Json1best 单句转写结果

type Lattice

type Lattice struct {
	Json1best Json1best `json:"json_1best"`
}

Lattice 单句转写结果

func (*Lattice) UnmarshalJSON

func (l *Lattice) UnmarshalJSON(data []byte) error

UnmarshalJSON json解码 json_1best 响应字段是json字符串,将其直接解码为对象

type Lattice2

type Lattice2 struct {
	Json1best Json1best `json:"json_1best"`
}

Lattice2 单句转写结果

type LfAsr

type LfAsr struct {
	Appid       string `json:"appid" env:"XFYUN_APP_ID"`
	LfAsrSecret string `json:"lfAsrSecret" env:"XFYUN_LF_ASR_SECRET"` // 语音转写密钥
}

func (LfAsr) GetResult

func (a LfAsr) GetResult(orderId string) (*GetResultRes, error)

GetResult 获取处理结果,处理完成后72小时可查 订单处理失败或处理中,会返回错误 同一个订单最多获取100次结果

func (LfAsr) OrderResult

func (a LfAsr) OrderResult(res *GetResultRes) (string, error)

OrderResult 解码转写结果,返回srt字幕

func (LfAsr) Sentence

func (a LfAsr) Sentence(las []Lattice)

Sentence 拼接句子输出

func (LfAsr) SignA

func (a LfAsr) SignA(secret string) map[string]string

SignA 生成签名并构造请求参数,secret为服务密钥

func (LfAsr) SpeechToText

func (a LfAsr) SpeechToText(name string, timeout time.Duration) (string, error)

SpeechToText 上传音视频后,直接轮询获取语音转写结果,并转为srt字幕 name 是本地音视频文件路径,timeout 是轮询超时时间 官方音频时长与理论处理时间分钟: X<10 Y<3 10<=X<30 3<=Y<6 30<=X<60 6<=Y<10 60<=X 10<=Y<20

func (LfAsr) Srt

func (a LfAsr) Srt(las []Lattice) (string, error)

Srt srt字幕

func (LfAsr) Upload

func (a LfAsr) Upload(name string) (*UploadRes, error)

Upload 上传音视频文件

func (LfAsr) UploadCallback

func (a LfAsr) UploadCallback(c *gin.Context)

UploadCallback 订单完成时通知回调处理 get请求

func (LfAsr) Valid

func (a LfAsr) Valid() error

Valid 验证语音转写设置是否有效

func (LfAsr) VideoTime

func (a LfAsr) VideoTime(msec string) (string, error)

VideoTime 将毫秒数转为视频时间字符串

type OrderResult

type OrderResult struct {
	Lattice  []Lattice  `json:"lattice"`  // 顺滑处理后的结果
	Lattice2 []Lattice2 `json:"lattice2"` // 未顺滑处理的结果
}

OrderResult 转写结果 lattice 中 json_1best 是字符串 lattice2 中 json_1best 是对象

type ResBody

type ResBody struct {
	Code     string `json:"code"`     // 响应代码
	DescInfo string `json:"descInfo"` // 响应信息
}

ResBody 通用响应体

func (ResBody) CodeI

func (r ResBody) CodeI() string

CodeI 获取响应代码,实现ResBodyI

func (ResBody) Error

func (r ResBody) Error() error

Error 处理失败时,获取错误

type ResBodyI

type ResBodyI interface {
	Error() error
	CodeI() string
}

ResBodyI 通用响应体接口 嵌入了 ResBody 的结构体可以统一使用该接口

type Rt

type Rt struct {
	Ws []Ws `json:"ws"`
}

Rt 词语识别结果

type St

type St struct {
	Bg string `json:"bg"` // 单句开始时间毫秒数
	Ed string `json:"ed"` // 单句结束时间毫秒数
	Rl string `json:"rl"`
	Rt []Rt   `json:"rt"` // 词语识别结果
}

St 单句转写结果

type UploadRes

type UploadRes struct {
	ResBody
	Content struct {
		OrderId          string `json:"orderId"`          // 订单id
		TaskEstimateTime int    `json:"taskEstimateTime"` // 订单预估耗时,单位毫秒
	} `json:"content"`
}

UploadRes 上传音视频文件响应体

type Ws

type Ws struct {
	Wb int64 `json:"wb"` // 词语相对St.Bg开始帧数 一帧10ms
	We int64 `json:"we"` // 词语相对St.Bg结束帧数 一帧10ms
	Cw []Cw  `json:"cw"` // 词语识别结果
}

Ws 词语识别结果

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL