-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnarrate.py
32 lines (23 loc) · 910 Bytes
/
narrate.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
from google.cloud import texttospeech
import vlc
import time
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'secrets/text2speech_token.json'
def narrate(text):
client = texttospeech.TextToSpeechClient()
synthesis_input = texttospeech.SynthesisInput(text=text)
voice = texttospeech.VoiceSelectionParams(
language_code='en-US',
ssml_gender=texttospeech.SsmlVoiceGender.FEMALE)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3)
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config)
with open('output.mp3', 'wb') as out:
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
player = vlc.MediaPlayer('output.mp3')
player.play()
time.sleep(3)
while player.is_playing():
time.sleep(1)