Skip to content

Instantly share code, notes, and snippets.

@alotaiba
Created February 3, 2012 13:20
Show Gist options
  • Save alotaiba/1730160 to your computer and use it in GitHub Desktop.
Save alotaiba/1730160 to your computer and use it in GitHub Desktop.
Google Speech To Text API

Google Speech To Text API

Base URL: https://www.google.com/speech-api/v1/recognize
It accepts POST requests with voice file encoded in FLAC format, and query parameters for control.

Query Parameters

client
The client's name you're connecting from. For spoofing purposes, let's use chromium

lang
Speech language, for example, ar-QA for Qatari Arabic, or en-US for U.S. English

maxresults
Maximum results to return for utterance

POST

body
Should contain FLAC formatted voice binary

HTTP Header

Content-Type
Should be audio/x-flac; rate=16000;, where MIME and sample rate of the FLAC file is included

User-Agent
Can be the client's user agent string, for spoofing purposes, we'll use Chrome's

Examples

These examples assume you have a voice file encoded in FLAC called alsalam-alikum.flac.

wget

This will save JSON response in a file called recognized.json

wget --post-file='alsalam-alikum.flac' \
--user-agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7' \
--header='Content-Type: audio/x-flac; rate=16000;' \
-O 'recognized.json' \
'https://www.google.com/speech-api/v1/recognize?client=chromium&lang=ar-QA&maxresults=10'

curl

curl -X POST \
--data-binary @alsalam-alikum.flac \
--user-agent 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7' \
--header 'Content-Type: audio/x-flac; rate=16000;' \
'https://www.google.com/speech-api/v1/recognize?client=chromium&lang=ar-QA&maxresults=10'
@IvanZhao
Copy link

IvanZhao commented Sep 5, 2016

Hi everybody,
When I was "POST https://speech.googleapis.com/v1beta1/speech:asyncrecognize".
I receive the error message like this:
{
"code": 403,
"errors": [
{
"domain": "global",
"message": "Requests from this Android client application are blocked.",
"reason": "forbidden"
}
],
"message": "Requests from this Android client application are blocked.",
"status": "PERMISSION_DENIED"
}
Dose any buddy knows how to resolve this problem?
Thanks.

@indrabayu
Copy link

indrabayu commented Nov 24, 2016

    public static async Task<string> RequestGoogleSpeechAPIAsync(byte[] byteArray) 
    {
        var httpClient = new HttpClient();
        var mediaType = new MediaTypeWithQualityHeaderValue("audio/x-flac");
        var parameter = new NameValueHeaderValue("rate", "16000");
        mediaType.Parameters.Add(parameter);

        var url = "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-US&key=";
        var appSettings = ConfigurationManager.AppSettings;
        var apiKey = "AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw";
        var uri = new Uri(url + apiKey);

        using (MemoryStream ms = new MemoryStream(byteArray, 0, byteArray.Length))
        {
            var param = new StreamContent(ms);
            param.Headers.ContentType = mediaType;

            var result = await httpClient.PostAsync(uri, param);

            var responseFromServer = await result.Content.ReadAsStringAsync();
            var responseArray = responseFromServer.Split('\n');
            var responseJson = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<SpajamHonsen.Models.GoogleSpeechAPIResponseModel.Resuls>(responceArray[1]));

            return responseJson.result[0].alternative[0].transcript;
        }
    }

@fj4870
Copy link

fj4870 commented Dec 1, 2016

@akifnaeem21
Request you to please share speech to text c# code.

@Nalinh
Copy link

Nalinh commented Apr 4, 2017

i can't run :(
result is blank, tell me why, plz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment