Last active
April 15, 2022 10:02
-
-
Save kkdai/f014d96504bf490eb4308115339b338f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Refer https://github.com/kkdai/linebot-video-gcp for more detail. | |
// Must use "apiv1p1beta1" version to enable support on Chinese and MP3 | |
speech "cloud.google.com/go/speech/apiv1p1beta1" //v1p1beta1 | |
speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1p1beta1" //v1p1beta1 | |
func (c *ClientUploader) SpeachToText() (error, string) { | |
ctx := context.Background() | |
ctx, cancel := context.WithTimeout(ctx, time.Second*50) | |
defer cancel() | |
// Creates a client. | |
client, err := speech.NewClient(ctx) | |
if err != nil { | |
log.Fatalf("Failed to create client: %v", err) | |
return err, fmt.Sprintf("Failed to create client: %v", err) | |
} | |
// The path to the remote audio file to transcribe. | |
fileURI := fmt.Sprintf("gs://%s/%s", c.bucketName, c.uploadPath+c.objectName) | |
// Detects speech in the audio file. | |
resp, err := client.Recognize(ctx, &speechpb.RecognizeRequest{ | |
Config: &speechpb.RecognitionConfig{ | |
Encoding: speechpb.RecognitionConfig_MP3, | |
SampleRateHertz: 48000, | |
LanguageCode: "zh-TW", | |
}, | |
Audio: &speechpb.RecognitionAudio{ | |
AudioSource: &speechpb.RecognitionAudio_Uri{Uri: fileURI}, | |
}, | |
}) | |
if err != nil { | |
log.Fatalf("failed to recognize: %v", err) | |
return err, fmt.Sprintf("failed to recognize: %v", err) | |
} | |
// Prints the results. | |
var resultStr string | |
for _, result := range resp.Results { | |
for _, alt := range result.Alternatives { | |
log.Printf("\"%v\" (confidence=%3f)\n", alt.Transcript, alt.Confidence) | |
resultStr = resultStr + alt.Transcript + " " | |
} | |
} | |
return nil, resultStr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment