Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- from google.cloud import texttospeech
- from os.path import join as pjoin
- import os
- import sys
- import json
- import time
- VOICE_BY_GENDER = {
- 'male': 'ru-RU-Wavenet-B',
- 'female': 'ru-RU-Wavenet-C'
- }
- # время в секундах, поставь нормальное значение
- TIMEOUT = 0.5
- def synthesize_text(text, outfile, voice_name='ru-RU-Standard-A'):
- client = texttospeech.TextToSpeechClient()
- input_text = texttospeech.types.SynthesisInput(text=text)
- # 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='ru-RU',
- name=voice_name
- )
- audio_config = texttospeech.types.AudioConfig(
- audio_encoding=texttospeech.enums.AudioEncoding.MP3)
- response = client.synthesize_speech(input_text, voice, audio_config)
- # The response's audio_content is binary.
- out = open(outfile, 'wb')
- out.write(response.audio_content)
- out.close()
- def free_filename(dir, filename):
- fpath = pjoin(dir, filename)
- fbase, ext = os.path.splitext(fpath)
- i = 0
- while os.path.exists(fpath):
- fpath = '%s-%d%s' % (fbase, i, ext)
- i += 1
- return fpath
- def save_phrases(phrases_json, dict_json, outdir):
- if not os.path.exists(outdir):
- os.mkdir(outdir)
- phrases_file = open(phrases_json, 'r')
- dict_file = open(pjoin(outdir, dict_json), 'w')
- phrases = json.load(phrases_file)
- meta = []
- for phrase in phrases:
- phrase_meta = {'phrase': phrase.replace("\n", "")}
- for gender in ['male', 'female']:
- track_fn = '%s_%s.mp3' % (phrases[phrase], gender)
- phrase_meta['track_' + gender] = track_fn
- synthesize_text(phrase, free_filename(outdir, track_fn), VOICE_BY_GENDER[gender])
- # к примеру, здесь:
- time.spleep(TIMEOUT)
- meta.append(phrase_meta)
- meta_raw = json.dumps(meta, indent=4, ensure_ascii=False)
- dict_file.write(meta_raw)
- phrases_file.close()
- dict_file.close()
- phrases_fn = sys.argv[1]
- meta_fn = phrases_fn.split('.')[0] + '_meta.json'
- save_phrases(sys.argv[1], meta_fn, './tracks')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement