forked from harshilp24/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gTTS-text-to-speech.py
53 lines (44 loc) · 1.41 KB
/
gTTS-text-to-speech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Text-To-Speech Using gTTS API
# Use: pip install gTTS
# Use: pip install playsound
# Run this program in the command line:
# python gTTS-text-to-speech.py "<text>" --"<output filename>"
from gtts import gTTS
from playsound import playsound
from sys import argv, exit
import os
speechSettings = {
"text": None,
"output": "speech only",
"language": 'en',
"slow": False
}
def main():
if len(argv) < 2 or len(argv) > 3:
print("Usage: python gTTS-text-to-speech.py \"<text>\" --\"<output filename>\"")
exit()
def retrieveText():
speechSettings["text"] = argv[1]
def retrieveOutputLocation():
if len(argv) == 3:
if argv[2][0] != "-" and argv[2][1] != "-":
print("Usage: python gTTS-text-to-speech.py \"<text>\" --\"<output filename>\"")
exit()
saveFile = argv[2]
speechSettings["output"] = saveFile.strip("--")
def output():
speech = gTTS(text=speechSettings["text"], lang=speechSettings["language"], slow=speechSettings["slow"])
if speechSettings["output"] == "speech only":
speech.save("output.mp3")
playsound("output.mp3")
os.remove("output.mp3")
else:
try:
speech.save(speechSettings["output"])
playsound(speechSettings["output"])
except:
print("Output file path could not be found")
main()
retrieveText()
retrieveOutputLocation()
output()