-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (55 loc) · 1.81 KB
/
main.go
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
package main
import (
"context"
"log"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/transcribestreamingservice"
)
func main() {
// Create a new AWS session
sess := session.Must(session.NewSession())
// Create a client for the Transcribe Streaming service in the desired region
client := transcribestreamingservice.New(sess, aws.NewConfig().WithRegion("us-west-2"))
lc := "en-US"
me := "pcm"
// Start the stream transcription
resp, err := client.StartStreamTranscription(&transcribestreamingservice.StartStreamTranscriptionInput{
LanguageCode: &lc,//aws.String(transcribestreamingservice.LanguageCodeEnUs),
MediaEncoding: &me,//aws.String(transcribestreamingservice.MediaEncodingOggOpus),
MediaSampleRateHertz: aws.Int64(16000),
})
if err != nil {
log.Fatalf("failed to start streaming, %v", err)
}
// Get the transcription stream
stream := resp.GetStream()
// Close the stream when the function exits
defer stream.Close()
// Open the audio file
audioFile, err := os.Open("tests_integration_assets_test.wav")
if err != nil {
log.Fatalf("failed to open audio file, %v", err)
}
defer audioFile.Close()
// Stream audio from the file to the stream writer
transcribestreamingservice.StreamAudioFromReader(context.Background(), stream.Writer, 10*1024, audioFile)
var textout string
// Process events from the stream
for event := range stream.Events() {
switch e := event.(type) {
case *transcribestreamingservice.TranscriptEvent:
for _, res := range e.Transcript.Results {
if !*res.IsPartial {
for _, alt := range res.Alternatives {
textout += aws.StringValue(alt.Transcript) + " "
}
}
}
default:
log.Fatalf("unexpected event, %T", event)
}
}
log.Printf("Transcribed text: %s", textout)
}