Created
May 6, 2011 14:01
-
-
Save danielfm/958999 to your computer and use it in GitHub Desktop.
Google Translate text-to-speech client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding:utf-8 -*- | |
import os | |
import urllib | |
import urllib2 | |
import subprocess | |
def speak(text, language, filename): | |
""" | |
""" | |
headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"} | |
query_string = urllib.urlencode({"tl": language, "q": text}) | |
request = urllib2.Request("http://translate.google.com/translate_tts?%s" % query_string, headers=headers) | |
f = urllib2.urlopen(request) | |
out = file(filename, "w") | |
out.write(f.read()) | |
out.close() | |
subprocess.call(["afplay", filename]) | |
def main(): | |
""" | |
""" | |
import optparse | |
p = optparse.OptionParser() | |
p.add_option("--text", "-t", default="Testing", | |
help="input text") | |
p.add_option("--language", "-l", default="en", | |
help="input text language") | |
p.add_option("--file", "-f", default="speech.mp3", | |
help="output file") | |
options = p.parse_args()[0] | |
if options.text: | |
return speak(options.text, options.language, options.file) | |
p.print_help() | |
sys.exit(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment