Skip to content

Instantly share code, notes, and snippets.

@scturtle
Last active December 11, 2015 18:08
Show Gist options
  • Save scturtle/4639054 to your computer and use it in GitHub Desktop.
Save scturtle/4639054 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# coding: utf-8
import json
import urllib2
from os import system, popen
# generate speech audio file with 'say' in osx and ffmpeg
words = 'Hello world.'
system('say "{}" -o say.aiff'.format(words))
system('ffmpeg -i say.aiff say.flac -loglevel quiet -y')
system('ffplay say.flac -autoexit -v quiet -showmode 0')
# get sample rate
rate = popen('ffprobe -i say.flac -v quiet -show_streams | grep sample_rate | cut -c13-').read().strip()
# speech to text with google speech api
audio = open('say.flac', 'rb').read()
request = urllib2.Request("https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US")
request.add_header('Content-type', 'audio/x-flac; rate={}'.format(rate))
request.add_data(audio)
response = urllib2.urlopen(request)
content = json.loads(response.read())
if content['status'] == 0:
print '"{}"'.format(content['hypotheses'][0]['utterance'])
else:
print 'Error'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment