不带 SDK 的 Amazon Transcribe Streaming API

我正在尝试使用来自 Go 1.11 的亚马逊新的流式转录 API。目前 Amazon 仅提供 Java SDK,所以我正在尝试低级方式。

唯一相关的文档在此处,但未显示端点。我在一个Java 示例中找到了它,https://transcribestreaming.<region>.amazonaws.com我正在尝试爱尔兰地区,即https://transcribestreaming.eu-west-1.amazonaws.com. 这是我打开 HTTP/2 双向流的代码:

import (

    "crypto/tls"

    "github.com/aws/aws-sdk-go-v2/aws"

    "github.com/aws/aws-sdk-go-v2/aws/external"

    "github.com/aws/aws-sdk-go-v2/aws/signer/v4"

    "golang.org/x/net/http2"

    "io"

    "io/ioutil"

    "log"

    "net/http"

    "os"

    "time"

)


const (

    HeaderKeyLanguageCode   = "x-amzn-transcribe-language-code"  // en-US

    HeaderKeyMediaEncoding  = "x-amzn-transcribe-media-encoding" // pcm only

    HeaderKeySampleRate     = "x-amzn-transcribe-sample-rate"    // 8000, 16000 ... 48000

    HeaderKeySessionId      = "x-amzn-transcribe-session-id"     // For retrying a session. Pattern: [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}

    HeaderKeyVocabularyName = "x-amzn-transcribe-vocabulary-name"

    HeaderKeyRequestId = "x-amzn-request-id"

)


...


region := "eu-west-1"


cfg, err := external.LoadDefaultAWSConfig(aws.Config{

    Region: region,

})

if err != nil {

    log.Printf("could not load default AWS config: %v", err)

    return

}


signer := v4.NewSigner(cfg.Credentials)


transport := &http2.Transport{

    TLSClientConfig: &tls.Config{

        // allow insecure just for debugging

        InsecureSkipVerify: true,

    },

}

client := &http.Client{

    Transport: transport,

}


signTime := time.Now()


header := http.Header{}

header.Set(HeaderKeyLanguageCode, "en-US")

header.Set(HeaderKeyMediaEncoding, "pcm")

header.Set(HeaderKeySampleRate, "16000")

header.Set("Content-type", "application/json")


// Bi-directional streaming via a pipe.

pr, pw := io.Pipe()

问题是执行请求 ( client.Do(req)) 冻结五分钟,然后以“意外的 EOF”错误结束。


知道我做错了什么吗?有人在没有 Java SDK 的情况下成功使用了新的流式转录 API 吗?


侃侃尔雅
浏览 125回答 3
3回答

哈士奇WWW

我联系了 AWS 支持,他们现在建议尽可能使用 websockets 而不是 HTTP/2(博客文章在这里)如果这适合您的用例,我强烈建议您查看新的示例存储库: https:&nbsp;//github.com/aws-samples/amazon-transcribe-websocket-static,它显示了 JS 中基于浏览器的解决方案。我还注意到该演示的作者在他的个人 Github 上有一个明确的例子: https:&nbsp;//github.com/brandonmwest/amazon-transcribe-websocket-express但我还没有确认这是否有效。感谢这些示例不是在 Python 中,但我认为使用 Websocket 客户端而不是 HTTP/2 会更好(老实说,它仍然有点可怕:P)

慕工程0101907

尝试不设置内容类型标头,看看您得到什么响应。我正在尝试做同样的事情(但在 Ruby 中)并且“修复”了SerializationException.&nbsp;仍然无法让它工作,但我现在有一个新的错误需要考虑:)更新:我现在已经开始工作了。我的问题是签名。如果同时传递了标头host和标头,则在检查签名时,它们将与服务器端连接并被视为在服务器端,因此签名永远不会匹配。这在 AWS 方面似乎不是正确的行为,但在 Go 中它看起来不会成为您的问题。authority,host

手掌心

我也在与 Node.js 作斗争。文档不清楚的是,在一个地方它说不应该Content-Type是application/json,但在其他地方,它使有效负载看起来应该编码为application/vnd.amazon.eventstream. 看起来有效负载应该仔细格式化为二进制格式而不是 JSON 对象,如下所示:Amazon Transcribe 使用一种称为事件流编码的格式进行流式转录。这种格式使用描述每个事件内容的标头信息对二进制数据进行编码。您可以将此信息用于在不使用 Amazon Transcribe SDK 的情况下调用 Amazon Transcribe 终端节点的应用程序。Amazon Transcribe 使用 HTTP/2 协议进行流式转录。流式请求的关键组件是:标题框架。这包含请求的 HTTP 标头,以及授权标头中的签名,Amazon Transcribe 将其用作种子签名来签署以下数据帧。事件流编码中的一个或消息帧。该帧包含元数据和原始音频字节。结束帧。这是一个带有空主体的事件流编码中的签名消息。

神不在的星期二

我对在节点 js 中将 AWS 转录服务与他们的 WebSocket API 一起使用有类似的要求。鉴于目前官方包中还没有对此提供支持,我已经继续编写了一个名为 AWS-transcribe 的包,可以在此处找到。我希望这有帮助。它提供了一个围绕 WebSocket 的流接口,可以像下面的例子一样使用import { AwsTranscribe, StreamingClient } from "aws-transcribe"const client = new AwsTranscribe({&nbsp; &nbsp; // if these aren't provided, they will be taken from the environment&nbsp; &nbsp; accessKeyId: "ACCESS KEY HERE",&nbsp; &nbsp; secretAccessKey: "SECRET KEY HERE",})const transcribeStream = client&nbsp; &nbsp; .createStreamingClient({&nbsp; &nbsp; &nbsp; &nbsp; region: "eu-west-1",&nbsp; &nbsp; &nbsp; &nbsp; sampleRate,&nbsp; &nbsp; &nbsp; &nbsp; languageCode: "en-US",&nbsp; &nbsp; })&nbsp; &nbsp; // enums for returning the event names which the stream will emit&nbsp; &nbsp; .on(StreamingClient.EVENTS.OPEN, () => console.log(`transcribe connection opened`))&nbsp; &nbsp; .on(StreamingClient.EVENTS.ERROR, console.error)&nbsp; &nbsp; .on(StreamingClient.EVENTS.CLOSE, () => console.log(`transcribe connection closed`))&nbsp; &nbsp; .on(StreamingClient.EVENTS.DATA, (data) => {&nbsp; &nbsp; &nbsp; &nbsp; const results = data.Transcript.Results&nbsp; &nbsp; &nbsp; &nbsp; if (!results || results.length === 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; const result = results[0]&nbsp; &nbsp; &nbsp; &nbsp; const final = !result.IsPartial&nbsp; &nbsp; &nbsp; &nbsp; const prefix = final ? "recognized" : "recognizing"&nbsp; &nbsp; &nbsp; &nbsp; const text = result.Alternatives[0].Transcript&nbsp; &nbsp; &nbsp; &nbsp; console.log(`${prefix} text: ${text}`)&nbsp; &nbsp; })someStream.pipe(transcribeStream)
打开App,查看更多内容
随时随地看视频慕课网APP