-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgctts.py
77 lines (62 loc) · 2.46 KB
/
gctts.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import requests
import base64
import json
import click
import os
class googleTTS:
def __init__(self, apiKey):
self.apiKey = apiKey
def _makePostRequest(self, endpoint, payload):
url = f"https://texttospeech.googleapis.com/v1/{endpoint}"
querystring = {"key": f"{self.apiKey}"}
headers = {"content-type": "application/json"}
r = requests.request(
"POST", url, data=payload, headers=headers, params=querystring
)
if r.status_code == requests.codes.ok:
return r.json()
elif (
r.json()["error"] is not "null"
and r.json()["error"]["message"].find("API key not valid") != -1
):
raise ValueError("Invalid API Key. Did you specify one with --apikey?")
def _decodeB64Mp3(self, data):
return base64.b64decode(data)
def writeMp3toFile(self, dataBytes, path):
with open(path, "wb") as file:
file.write(dataBytes)
def generateMp3(self, voice, language, string, speakingRate):
payload = json.dumps(
{
"voice": {"name": voice, "languageCode": language},
"input": {"text": string},
"audioConfig": {"audioEncoding": "mp3", "speakingRate": speakingRate},
}
)
reqObj = self._makePostRequest("text:synthesize", payload)
return self._decodeB64Mp3(reqObj["audioContent"])
@click.group()
def cli():
pass
@click.command()
@click.option("--apikey", envvar="GCTTS_APIKEY", help="Google Cloud API key that has acceess to Cloud TTS.",)
@click.option("--voice", default="en-US-Wavenet-F", help="The Google TTS voice.")
@click.option("--language", default="en-US", help="The langage the voice should be created in.")
@click.option("--rate", default=1.00, help="The speed in which the voice speaks.")
@click.option("--text", prompt="Text", help="Text that should be spoken.")
@click.argument("filename")
def mp3(apikey, voice, language, rate, text, filename):
click.echo(
"This script nor the developer (JamesClick) are not fuffiliated with Google whatsoever."
)
dirpath = os.getcwd()
ttsInst = googleTTS(apikey)
try:
mp3Obj = ttsInst.generateMp3(voice, language, text, str(rate))
with open(os.path.join(os.getcwd(), filename), "wb") as file:
file.write(mp3Obj)
except ValueError as error:
click.echo(f"ERROR: {error}")
cli.add_command(mp3)
if __name__ == "__main__":
cli()