使用texttospeech google api的Strage行为

2024-06-16 14:14:50 发布

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

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

此函数从控制台获取ssml文本,并创建正确的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)

Tags: text文本clientconfiginputoutputresponseas