Search code examples
pythonbashraspberry-pitext-to-speech

Google TTS- Has anyone had any luck with it recently?


I've been trying my hand at a "JARVIS" like system on my Raspberry Pi 2. I've tinkered around with eSpeak, Festival and pico but I've found pico to be the best out of these.

However, pico is very boring to listen to and is completely monotonous. Some sites have posts from 2013-14, wherein users could use the TTS of Google Translate. However, recently Google changed some of their policies so these unofficial apps won't work, so now it asks for a CAPTCHA and an HTTP 503 error.

This is an example of code I found on a website that used to work but stopped ever since Google's policy changes.

#!/usr/bin/python

import urllib, pycurl, os

def downloadFile(url, fileName):
    fp = open(fileName, "wb")
    curl = pycurl.Curl()
    curl.setopt(pycurl.URL, url)
    curl.setopt(pycurl.WRITEDATA, fp)
    curl.perform()
    curl.close()
    fp.close()

def getGoogleSpeechURL(phrase):
    googleTranslateURL = "http://translate.google.com/translate_tts?    tl=en&"
    parameters = {'q': phrase}
    data = urllib.urlencode(parameters)
    googleTranslateURL = "%s%s" % (googleTranslateURL,data)
    return googleTranslateURL

def speakSpeechFromText(phrase):
    googleSpeechURL = getGoogleSpeechURL(phrase)
    downloadFile(googleSpeechURL,"tts.mp3")
    os.system("mplayer tts.mp3 -af extrastereo=0 &")

speakSpeechFromText("testing, testing, 1 2 3.")

Has anybody had any luck with Google TTS?


Solution

  • You can install gtts package for python available. Then by using it you can save your text in a mp3 file then play it. A simple example where i have used gtts for saying hello world is

    tts = gTTS(text=, lang="en")
    tts.save("hello.mp3")
    os.system("mpg321 hello.mp3")