package main
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"io/ioutil"
"net/http"
)
type TokenResult struct {
ErrMsg string
Token struct {
UserId string
Id string
ExpireTime int64
}
}
type SpeechFlashRecognizerDemo struct {
appkey string
}
func NewSpeechFlashRecognizerDemo(appkey string) *SpeechFlashRecognizerDemo {
return &SpeechFlashRecognizerDemo{
appkey: appkey,
}
}
func ObtainToken() string {
client, err := sdk.NewClientWithAccessKey("", "", "")
if err != nil {
panic(err)
}
request := requests.NewCommonRequest()
request.Method = "POST"
request.Domain = "nls-meta.cn-shanghai.aliyuncs.com"
request.ApiName = "CreateToken"
request.Version = "2019-02-28"
response, err := client.ProcessCommonRequest(request)
if err != nil {
panic(err)
}
var tr TokenResult
err = json.Unmarshal([]byte(response.GetHttpContentString()), &tr)
return tr.Token.Id
}
func sendPostLink(url string, headers map[string]string, link string) string {
if link == "" {
fmt.Println("The send link is empty.")
return ""
}
url += "&audio_address=" + link
req, err := http.NewRequest("POST", url, nil)
if err != nil {
fmt.Println("Failed to create POST request:", err)
return ""
}
for key, value := range headers {
req.Header.Set(key, value)
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failed to send POST request:", err)
return ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Failed to read response:", err)
return ""
}
return string(body)
}
func (demo *SpeechFlashRecognizerDemo) process(fileName, token, format string, sampleRate int) string{
url := "https://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/FlashRecognizer"
request := url + "?appkey=" + demo.appkey + "&token=" + token + "&format=" + format + "&sample_rate=" + fmt.Sprintf("%d", sampleRate)
headers := make(map[string]string)
headers["Content-Type"] = "application/text"
response := sendPostLink(request, headers, fileName)
return response
}
func main() {
token := ObtainToken()
appkey := ""
demo := NewSpeechFlashRecognizerDemo(appkey)
fileName := "音视频地址"
format := "mp4"
sampleRate := 16000
response :=demo.process(fileName, token, format, sampleRate)
fmt.Println(response)
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111