使用TextTospeech Google API的分阶段行为

时间:2018-09-08 15:42:53

标签: python google-api google-text-to-speech ssml

我开始知道如何使用Google API修改texttospeech API的python示例代码,当我在txt文件中使用ssml languaje将文本传递给API时,我发现了一个问题,结果mp3音频改变了字符“é”和句子“ derechos de autor”,字符“á”保持沉默。仅当我从文件中读取文本时才会发生这种情况,如果我在调用它时由argunment将ssml句子直接提供给应用程序,则不会发生此更改。 我搜索了此问题,但没有找到,请问有人提示这是怎么回事?

此功能可从控制台获取ssml texto,并创建正确的mp3音频文件:

def synthesize_ssml(ssml, output):
    from google.cloud import texttospeech as texttospeech   
    client = texttospeech.TextToSpeechClient()
    input_text = texttospeech.types.SynthesisInput(ssml=ssml)
    voice = texttospeech.types.VoiceSelectionParams(language_code='es-ES')
    audio_config = texttospeech.types.AudioConfig(
        audio_encoding=texttospeech.enums.AudioEncoding.MP3)
    response = client.synthesize_speech(input_text, voice, audio_config)
    with open(output, 'wb') as out:
        out.write(response.audio_content)
        print('Audio content written to file "%s"' % output)

这是从文件,相同文本中提取ssml的功能,可产生不同的音频文件:

def synthesize_ssml_file(input, output):
    from google.cloud import texttospeech as texttospeech   
    with open(input,'r') as inp:
        input_text=texttospeech.types.SynthesisInput(ssml=str(inp.read()))
    client = texttospeech.TextToSpeechClient()
    voice = texttospeech.types.VoiceSelectionParams(language_code='es-ES')
    audio_config = texttospeech.types.AudioConfig(
        audio_encoding=texttospeech.enums.AudioEncoding.MP3)
    response = client.synthesize_speech(input_text, voice, audio_config)
    with open(output, 'wb') as out:
        out.write(response.audio_content)
        print('Audio content written to file "%s"' % output)

0 个答案:

没有答案