How to Recognize Audio as Text in Go
Manually transcribing notes from a meeting or conference can be a tedious and painstaking process, which is one of the reasons why audio to text technology has become incredibly valuable. In addition, it can assist in creating more accessibility features for your websites and applications, such as a text transcription of a recording to benefit the hearing impaired. This short tutorial will demonstrate how you can use an API in Go to automatically convert MP3 or WAV files into text.
To call the function, simply input the audio file and your API key into the following example code:
package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)func main() {url := "https://api.cloudmersive.com/speech/recognize/file"
method := "POST"payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("speechFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
The text result will be returned promptly at the end of the operation — easy as that. If you need to retrieve an API key, you can do so by registering for a free account on the Cloudmersive website; this provides 800 calls/month across any of our APIs.