阿里云通过音视频url获取字幕内容

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{
	// 设置REST POST请求URL
	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)

	// 设置HTTP头部字段
	headers := make(map[string]string)
	headers["Content-Type"] = "application/text"

	// 发送HTTP POST请求
	response := sendPostLink(request, headers, fileName)
	return response
}

func main() {
	// 获取token
	token := ObtainToken()
	appkey := ""
	demo := NewSpeechFlashRecognizerDemo(appkey)
	fileName := "音视频地址"
	format := "mp4"
	// 识别率:电话8000,非电话16000
	sampleRate := 16000
	response :=demo.process(fileName, token, format, sampleRate)
	fmt.Println(response)
}

你可能感兴趣的:(阿里云,音视频,云计算)