參考文章:
http://blog.laobubu.net/546?replytocom=367
http://people.csail.mit.edu/hubert/pyaudio/
http://code.google.com/p/pygalaxy/wiki/SWMixer
http://audiotools.sourceforge.net/install.html
首先安裝工具軟件:
sudo apt-get install flac python-pyaudio
然後執行如下python腳本:
#!/usr/bin/python
""" Record a few seconds of audio and save to a WAVE file. """
import pyaudio
import wave
import sys,os
import urllib2
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
#RATE = 44100
RATE = 16000
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
FLAC_OUTPUT_FILENAME = "output.flac"
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = chunk)
print "* recording"
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
data = stream.read(chunk)
all.append(data)
print "* done recording"
stream.close()
p.terminate()
# write data to WAVE file
data = ''.join(all)
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
cmd = 'flac ' + WAVE_OUTPUT_FILENAME + ' -f -o ' + FLAC_OUTPUT_FILENAME;
#print cmd
os.system(cmd);
os.remove(WAVE_OUTPUT_FILENAME);
url = 'http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN'
audio=open(FLAC_OUTPUT_FILENAME ,'rb').read()
headers = {'Content-Type' : 'audio/x-flac; rate=16000'}
req = urllib2.Request(url, audio, headers)
response = urllib2.urlopen(req)
print response.read().decode('UTF-8')