Skip to content

Instantly share code, notes, and snippets.

@akashsharma95
Last active December 16, 2015 23:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akashsharma95/5517933 to your computer and use it in GitHub Desktop.
Save akashsharma95/5517933 to your computer and use it in GitHub Desktop.
Simple Python Google Speech API
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#Last Changed -- Sat 04 May 2013 03:44:14 PM UTC
#By Akash Sharma
import requests
import subprocess
import sys
import time
import os
#Take input wav file as argument
if len(sys.argv)<2 :
print 'Usage: %s <audio file.wav>' %sys.argv[0]
sys.exit(0)
#Use subprocess to execute avconv to convert wav to flac file with rate 16000Hz
wav_file = sys.argv[1]
command = "avconv -i %s -ar 16000 output.flac"%wav_file
#Using subprocess.PIPE to disable output for Python 2.7
cmd = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
cmd.wait()
#Using Python-request module to handle http request and post flac file
flac_file = {'file': open('output.flac')}
#Using custom Headers
headers = {'Content-type':'audio/x-flac;rate=16000'}
#Parameters You can define language below default is 'en-US' rest leave it default
language = 'en-US'
parameters = {'xjerr':'1','client':'chromium','lang':language}
#Everything done now POST data and get results in json format
url = "https://www.google.com/speech-api/v1/recognize"
req = requests.post(url, params=parameters, headers=headers, files = flac_file)
os.remove("output.flac")
#Using builtin json() func to print json output req.json() or print req.json
print req.json
The required modules and programs are :--
-libav
-python-requests
I have used libav here you can also use ffmpeg etc...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment