如何将多个文本文件加载到Google文本到语音中?

2024-06-01 09:41:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在使用谷歌文本到语音API,因为这些声音是多么美妙。唯一的问题是试图找到如何使其用户友好。最大的问题是google文本到语音只能接受5000个或更少字符的文本文件。我发现的主要问题是,目前我所能做的就是使用一个文本文件副本,在保存之前将我的东西粘贴到上面。有人知道我如何上传一个装满文本文件的文件夹来加快上传速度吗?另外还保存mp3而不是覆盖它们

# [START tts_ssml_address_imports]
from google.cloud import texttospeech
import os
import html

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 
# [END tts_ssml_address_imports]


# [START tts_ssml_address_audio]
def ssml_to_audio(ssml_text, outfile):
    # Generates SSML text from plaintext.
    #
    # Given a string of SSML text and an output file name, this function
    # calls the Text-to-Speech API. The API returns a synthetic audio
    # version of the text, formatted according to the SSML commands. This
    # function saves the synthetic audio to the designated output file.
    #
    # Args:
    # ssml_text: string of SSML text
    # outfile: string name of file under which to save audio output
    #
    # Returns:
    # nothing

    # Instantiates a client
    client = texttospeech.TextToSpeechClient()

    # Sets the text input to be synthesized
    synthesis_input = texttospeech.types.SynthesisInput(text=ssml_text)

    # Builds the voice request, selects the language code ("en-US") and
    # the SSML voice gender ("MALE")
    voice = texttospeech.types.VoiceSelectionParams(language_code='en-US',
                                                    name="en-US-Wavenet-D",
                                                    ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE))

    # Selects the type of audio file to return
    audio_config = texttospeech.types.AudioConfig(audio_encoding="LINEAR16", pitch = 0, speaking_rate = 0.9)

    # Performs the text-to-speech request on the text input with the selected
    # voice parameters and audio file type
    response = client.synthesize_speech(synthesis_input, voice, audio_config)

    # Writes the synthetic audio to the output file.
    with open(outfile, 'wb') as out:
        out.write(response.audio_content)
        print('Audio content written to file ' + outfile)
    # [END tts_ssml_address_audio]

def main():
    # test example address file
    file = 'input_text.txt'
    with open(file, 'r') as f:
        text = f.read()
    ssml_text = text
    ssml_to_audio(ssml_text, 'file_output_speech.mp3')
    # [END tts_ssml_address_test]


if __name__ == '__main__':
    main()

Tags: ofthetotextinputoutputaddressaudio