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'
@LRNAB
Copy link

LRNAB commented Jul 2, 2014

Does anyone know how to get the key?

@voicer
Copy link

voicer commented Jul 3, 2014

@LRNAB
The key for google api you can get in the google APIs console for free.
But the question is how to increase the max limit of 50 requests 'a day?
I found no way to get more, and the link to order quote is dead.

The Google corp. forces me and other developers to find any other way to solve this problem.
I see the solution in java script for Chrome browser. But if anybody need this API i.e. for C# then he must to switch the source of sound from microphone to the file stream in flac format.

My question is now - how to change the source of sound from microphone into binary file on disk?

@sakhnevych
Copy link

@voicer
Could you tell if there is a way to use java script for Chrome browser from terminal? I realize it is a little bit strange question, but I can't find a way to use unlimited access to google speech API from linux using annyang.

@smithas
Copy link

smithas commented Aug 8, 2014

I went through the docs for creating key but it gives me forbidden error 403. I had tried a key few weeks ago shared by someone here which had worked but now that also fails. What step am I missing that I get 403 error? Is there any other way. Please help.

@ijazsarwar
Copy link

Anyone got it working in Java with V2?? I am getting 403 error.
Also looking at the comments looks like it only converts 10 to 15 secs of audio ... Anyway we can extend that time limit to say one minute or even longer??

@amd5200
Copy link

amd5200 commented Oct 11, 2014

I changed to use pocketsphinx, it can be works offline and supports Chinese language.

@marplx
Copy link

marplx commented Oct 16, 2014

I also have issues with the authentication for Google Speech2Text API on Android.
It works with my API key in WiFi if I tell Google my current IP. But the Android Key isn't working. I provided the correct debug-keystore SHA1 but am receiving: The client does not have permission to get url ... from this server. I use the Apache HttpClient within my Android application. Any ideas?

@ijazsarwar This Google API is not intended to continuously recognize speech. Its for short commands. Thats why there is this few seconds limit. Take a look at my master thesis project where I try to send 15sec chunks to Google to get something continuous-like.

@amd5200 I'd love to talk to you about PocketSphinx on Andoird for generic vocabulary searches since I already tried unsuccessfully to get it to work as described here - you'll find my email in my github profile.

@amd5200
Copy link

amd5200 commented Oct 27, 2014

@marfnk I just used it on my single board, like raspberry pi, and for Chinese. ( https://www.youtube.com/watch?v=EucxVToC58E&list=UUgBhkLQyk0LwxpKAmIZQNgA&hd=1 )
I never tried PocketSphinx on Andoird, so i got no idea for this.

@shinichi0802
Copy link

IS this possible to use ajax to post in to Google Speech API ?

@amsehili
Copy link

Hello everybody,

Some time ago I created this shell script which packages everything you need to use the API v2 (record data for a given duration or use a file, specify language, filter out results, etc.): https://github.com/amsehili/gspeech-rec

For more details about the reverse engineering being used, check out this article: https://aminesehili.wordpress.com/2015/02/08/on-the-use-of-googles-speech-recognition-api-version-2/

Cheers!

@rogo21
Copy link

rogo21 commented Feb 4, 2016

how to divide audio into frames of 15 sec using java code or command line??

Copy link

ghost commented Jul 21, 2016

Hi, https://www.google.com/speech-api/v2/recognize -> 400. That’s an error.
I am in Canada

@Swethamr402
Copy link

HI, Sending 'POST' request to URL : https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw&results=6&pfilter=2
Response Code : 200
{"result":[]}

I am getting above code.. But no output of recorded file to be convert in to text. How do I get that ?

@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