Skip to content

Instantly share code, notes, and snippets.

@sahya
Created June 27, 2020 02:01
Show Gist options
  • Save sahya/37e315ac6de4bf97beba47f8c6e43081 to your computer and use it in GitHub Desktop.
Save sahya/37e315ac6de4bf97beba47f8c6e43081 to your computer and use it in GitHub Desktop.
# coding: utf-8
def synthesize_text(text, path):
import os
from google.cloud import texttospeech
from pydub import AudioSegment
import pathlib
client = texttospeech.TextToSpeechClient()
input_text = texttospeech.types.SynthesisInput(text=text)
voice = texttospeech.types.VoiceSelectionParams(
language_code='ja-jp', name='ja-jp-Wavenet-B')
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.LINEAR16,
speaking_rate=1.5)
response = client.synthesize_speech(input_text, voice, audio_config)
with open(path + '.wav', 'wb') as out:
out.write(response.audio_content)
audio1 = AudioSegment.from_wav(path + '.wav')
audio2 = AudioSegment.from_wav(path + '_all.wav')
audio = audio2 + audio1
audio.export(path + '.mp3', format='mp3')
# os.remove(path + '.wav')
if __name__ == '__main__':
with open('scripts.txt', mode='rt', encoding='utf-8') as fi:
print(fi)
path = "/home/sayaka/sample01"
synthesize_text(fi, path)
import os
os.remove(path + '.wav')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment