Last active
July 4, 2018 19:53
-
-
Save lc-at/b162c87a4f9c4a4989fd2aa345b5dd52 to your computer and use it in GitHub Desktop.
google_tts: simple python script to convert text to speech based on Google Translate
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/python | |
import requests | |
import sys | |
import playsound | |
import time | |
try: | |
import readline | |
except: | |
pass | |
def speak_(text, hl): | |
url = "https://translate.google.com/translate_tts" | |
params = { | |
'ie': 'UTF-8', | |
'q': text, | |
'tl': hl, | |
'client': 'tw-ob', | |
} | |
headers = { | |
'Referer': 'http://translate.google.com/', | |
'User-Agent': 'stagefright/1.2 (Linux;Android 5.0)', | |
} | |
tts_r = requests.get(url, params=params, headers=headers) | |
with open("temp_audio.mp3", "wb") as file_au: | |
file_au.write(tts_r.content) | |
file_au.close() | |
playsound.playsound("temp_audio.mp3", True) | |
if __name__ == "__main__": | |
print("""\ | |
.---------------------------------. | |
| Google Translate Text-to-Speech | | |
| Author: P4kL0nc4t | | |
`---------------------------------` | |
""") | |
if len(sys.argv) != 2 or len(sys.argv[1]) != 2: | |
print("usage: {} <language_code:id/en/etc>".format(sys.argv[0])) | |
sys.exit(-1) | |
hl = sys.argv[1] | |
while True: | |
try: | |
text = raw_input("[{}-text]: ".format(hl)).rstrip() | |
except: | |
print("") | |
sys.exit(-1) | |
if text == "" in text.lower(): | |
continue | |
print("Speaking . . .") | |
speak_(text, hl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment