transcribe audio file to text in go

To transcribe an audio file to text in Go, you can make use of the Google Cloud Speech-to-Text API. Here's an example code showing how you can transcribe an audio file to text:

main.go
package main

import (
    "context"
    "fmt"
    "io/ioutil"

    speech "cloud.google.com/go/speech/apiv1"
    "google.golang.org/api/option"
    "google.golang.org/grpc"
    speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1"
)

func main() {
    // Replace the below values with your own credentials and audio file path 
    credentialsFilePath := "/path/to/your/credentials.json"
    audioFilePath := "/path/to/your/audiofile.flac"

    // Read the audio file
    bytes, err := ioutil.ReadFile(audioFilePath)
    if err != nil {
        panic(err)
    }

    // Create a new SpeechClient using your credentials
    ctx := context.Background()
    creds, err := option.CredentialsFromFile(credentialsFilePath)
    if err != nil {
        panic(err)
    }
    client, err := speech.NewClient(ctx, creds, grpc.WithTransportCredentials())
    if err != nil {
        panic(err)
    }

    // Create a new RecognitionConfig
    config := &speechpb.RecognitionConfig{
        Encoding:        speechpb.RecognitionConfig_FLAC,
        SampleRateHertz: 16000,
        LanguageCode:    "en-US",
    }

    // Create a new RecognitionAudio from your audio file bytes
    audio := &speechpb.RecognitionAudio{
        AudioSource: &speechpb.RecognitionAudio_Content{Content: bytes},
    }

    // Call the Speech-to-Text API
    resp, err := client.Recognize(ctx, &speechpb.RecognizeRequest{
        Config: config,
        Audio:  audio,
    })
    if err != nil {
        panic(err)
    }

    // Print the transcribed text
    for _, result := range resp.Results {
        for _, alt := range result.Alternatives {
            fmt.Printf("Transcription: %v\n", alt.Transcript)
        }
    }
}
1705 chars
64 lines

Make sure to replace the credentialsFilePath and audioFilePath variables with your own values. And also, make sure that the audio file is in the correct format (in the example above, it's in FLAC format).

gistlibby LogSnag