-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.py
72 lines (62 loc) · 2.23 KB
/
convert.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
import mistune
import sys
import argparse
import html
class Renderer(mistune.Renderer):
def header(self, text, level, raw=None):
return f"{text}<break time=\"{max(4-level,0)}00ms\"/>\n"
def emphasis(self, text):
return f"<emphasis level=\"moderate\">{text}</emphasis>"
def double_emphasis(self, text):
return f"<emphasis level=\"strong\">{text}</emphasis>"
def linebreak(self):
return f"<break time=\"400ms\"/>\n"
def hrule(self):
return f"<break time=\"600ms\"/>\n"
def image(self, src, title, text):
if title:
return f"image of {title}"
def paragraph(self, text):
return f"<p>{text}<break time=\"100ms\"/></p>\n"
def footnote_ref(self, key, index):
return key
def autolink(self, link, is_email=False):
return ""
def link(self, link, title, content):
return content
def text(self, text):
return html.escape(text)
def block_code(self, text):
return self.text(text)
def codespan(self, text):
return self.text(text)
renderer = Renderer()
markdown = mistune.Markdown(renderer=renderer)
out_text = markdown("\n".join(sys.stdin.readlines()))
ssml = f"<speak>{out_text}</speak>"
print(ssml)
def get_ogg(ssml):
"""
Get the MP3 from google cloud TTS API
"""
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
input_text = texttospeech.types.SynthesisInput(ssml=ssml)
# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices().
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
name="en-US-Wavenet-F"
)
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.OGG_OPUS)
response = client.synthesize_speech(input_text, voice, audio_config)
# The response's audio_content is binary.
with open('output.ogg', 'wb') as out:
out.write(response.audio_content)
print('Audio content written to file "output.ogg"')
parser = argparse.ArgumentParser()
parser.add_argument("--get_ogg", action='store_true', default=False)
args = parser.parse_args()
if args.get_ogg:
get_ogg(ssml)