我正在寻找一个小的暂停,等待,休息或任何将允许短暂的休息(寻找约2秒+-,可配置将是理想的)时说出所需的文本。
网上有人说,在空格后面加三个句号会产生一个空格,但我似乎没有做到这一点。可悲的是,下面的代码是我的测试,它没有暂停。有什么想法或建议吗?
编辑:如果gTTS中有一些命令允许我这样做,或者使用三个句号这样的技巧,如果这真的有效的话,那将是最理想的。
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")
发布于 2020-06-29 12:37:41
好的,你需要语音合成标记语言(SSML)来实现这一点。
请注意,您需要设置谷歌云平台credentials
狂欢中的第一个:
pip install --upgrade google-cloud-texttospeech
下面是代码:
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, '<' --> '<' and '&' --> '&'
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")
更多文档:
但是,如果您没有Google Cloud Platform credentials,更便宜、更简单的方法是使用time.sleep(1)方法
发布于 2020-01-20 17:15:04
如果需要后台等待,可以使用time模块进行等待,如下所示。
import time
# SLEEP FOR 5 SECONDS AND START THE PROCESS
time.sleep(5)
或者你可以用wait等进行3次检查。
import time
for tries in range(3):
if someprocess() is False:
time.sleep(3)
发布于 2020-06-29 13:14:49
遗憾的是,答案是 no ,gTTS包没有针对pause
的额外功能,这个问题已经在2018年为 创建了,但是它足够聪明,可以通过添加自然的暂停。
什么是标记器?
一个函数,接受文本并将其拆分成标记(字符串)列表返回。在gTTS上下文中,它的目标是将文本分割成不超过每个TTS请求所允许的最大字符大小(100)的较小片段,同时使语音听起来自然和连续。它通过在语音自然停顿的地方(例如在"."
上)拆分文本,同时在不应该处理的地方处理(例如在“10.5”或“U.S.A.”上)。这样的规则被称为记号赋予器用例,它需要一个列表。
下面是一个示例:
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()
似乎是唯一的解决方案。但是,值得一提的是,欺骗记号赋予器。
https://stackoverflow.com/questions/59819936
复制相似问题