Last active
June 4, 2024 23:17
-
-
Save sainak/a3ad79662d9fef9c72eee9422d15af4a to your computer and use it in GitHub Desktop.
tts using google gstatic dictionary
This file contains 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 python3 | |
# ./tts.py hello world | |
import requests, string, os | |
from playsound import playsound | |
from sys import argv | |
text = ( | |
" ".join(argv[1:]).lower().translate(str.maketrans("", "", string.punctuation)).split(" ") | |
) | |
accent = [ | |
"us", | |
"gb", | |
"in", # isnt avilable on sounds url | |
] | |
for i, word in enumerate(text): | |
audio = requests.get( | |
f"https://ssl.gstatic.com/dictionary/static/pronunciation/2021-03-01/audio/{word[:2]}/{word}_en_{accent[0]}_1.mp3" | |
) | |
if "audio/mpeg" not in audio.headers["content-type"]: | |
audio = requests.get( | |
f"https://ssl.gstatic.com/dictionary/static/sounds/oxford/{word}--_{accent[0]}_1.mp3" | |
) | |
if "audio/mpeg" in audio.headers["content-type"]: | |
with open(f"/tmp/pytts{i}.mp3", "wb") as f: | |
f.write(audio.content) | |
print(word) | |
for i in range(len(text)): | |
try: | |
playsound(f"/tmp/pytts{i}.mp3") | |
os.remove(f"/tmp/pytts{i}.mp3") | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment