首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Google-text-to-speech中添加暂停

在Google-text-to-speech中添加暂停
EN

Stack Overflow用户
提问于 2020-01-20 17:05:23
回答 5查看 6.6K关注 0票数 8

我正在寻找一个小的暂停,等待,休息或任何将允许短暂的休息(寻找约2秒+-,可配置将是理想的)时说出所需的文本。

网上有人说,在空格后面加三个句号会产生一个空格,但我似乎没有做到这一点。可悲的是,下面的代码是我的测试,它没有暂停。有什么想法或建议吗?

编辑:如果gTTS中有一些命令允许我这样做,或者使用三个句号这样的技巧,如果这真的有效的话,那将是最理想的。

代码语言:javascript
运行
复制
from gtts import gTTS
import os

tts = gTTS(text=" Testing ... if there is a pause ... ... ... ... ...  longer pause? ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... insane pause   " , lang='en', slow=False)

tts.save("temp.mp3")
os.system("temp.mp3")
EN

回答 5

Stack Overflow用户

发布于 2020-06-29 12:37:41

好的,你需要语音合成标记语言(SSML)来实现这一点。

请注意,您需要设置谷歌云平台credentials

狂欢中的第一个:

代码语言:javascript
运行
复制
pip install --upgrade google-cloud-texttospeech

下面是代码:

代码语言:javascript
运行
复制
import html
from google.cloud import texttospeech

def ssml_to_audio(ssml_text, outfile):
    # Instantiates a client
    client = texttospeech.TextToSpeechClient()

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

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

    # Selects the type of audio file to return
    audio_config = texttospeech.AudioConfig(
        audio_encoding=texttospeech.AudioEncoding.MP3
    )

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

def text_to_ssml(inputfile):

    raw_lines = inputfile

    # Replace special characters with HTML Ampersand Character Codes
    # These Codes prevent the API from confusing text with
    # SSML commands
    # For example, '<' --> '&lt;' and '&' --> '&amp;'

    escaped_lines = html.escape(raw_lines)

    # Convert plaintext to SSML
    # Wait two seconds between each address
    ssml = "<speak>{}</speak>".format(
        escaped_lines.replace("\n", '\n<break time="2s"/>')
    )

    # Return the concatenated string of ssml script
    return ssml



text = """Here are <say-as interpret-as="characters">SSML</say-as> samples.
  I can pause <break time="3s"/>.
  I can play a sound"""

ssml = text_to_ssml(text)
ssml_to_audio(ssml, "test.mp3")

更多文档:

Speaking addresses with SSML

但是,如果您没有Google Cloud Platform credentials,更便宜、更简单的方法是使用time.sleep(1)方法

票数 6
EN

Stack Overflow用户

发布于 2020-01-20 17:15:04

如果需要后台等待,可以使用time模块进行等待,如下所示。

代码语言:javascript
运行
复制
import time
# SLEEP FOR 5 SECONDS AND START THE PROCESS
time.sleep(5)

或者你可以用wait等进行3次检查。

代码语言:javascript
运行
复制
import time

for tries in range(3):
    if someprocess() is False:
        time.sleep(3)
票数 3
EN

Stack Overflow用户

发布于 2020-06-29 13:14:49

遗憾的是,答案是 no ,gTTS包没有针对pause的额外功能,这个问题已经在2018年为 创建了,但是它足够聪明,可以通过添加自然的暂停。

什么是标记器?

一个函数,接受文本并将其拆分成标记(字符串)列表返回。在gTTS上下文中,它的目标是将文本分割成不超过每个TTS请求所允许的最大字符大小(100)的较小片段,同时使语音听起来自然和连续。它通过在语音自然停顿的地方(例如在"."上)拆分文本,同时在不应该处理的地方处理(例如在“10.5”或“U.S.A.”上)。这样的规则被称为记号赋予器用例,它需要一个列表。

下面是一个示例:

代码语言:javascript
运行
复制
text = "regular text speed no pause regular text speed comma pause, regular text speed period pause. regular text speed exclamation pause! regular text speed ellipses pause... regular text speed new line pause \n regular text speed "

因此,在这种情况下,添加sleep()似乎是唯一的解决方案。但是,值得一提的是,欺骗记号赋予器。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59819936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档