首页 > 解决方案 > 使用python在Google文本中暂停第二个到语音

问题描述

from gtts import gTTS 
import os
import random

with open('symbols.txt', 'r') as file:
    symbols = file.read().replace('\n', '')

def getunique():
    for r, d, f in os.walk(path):
        for file in f:
            if file.endswith('.mp3'):
                unique.add(file.split('.')[0])
            
            
def generate(unique):
    chars = symbols
    while True:
        value = "".join(random.choice(chars) for _ in range(8))
        if value not in unique:
            language = 'en'
            myobj = gTTS(text=value, lang=language, slow=True) 
            myobj.save("audio_captcha_training_set2/"+value+".mp3")
            unique.add(value)
            break

path = "C:\\Users\\DNS\\Desktop\\Audio Captcha\\Test Python code\\audio_captcha_training_set\\"
unique = set()
getunique()

for _ in range(20):
    generate(unique)

这是一个读取包含 0-9 和 AZ 字符的文件的代码。这是为从文件中读取的随机 8 个字符生成音频,但在生成字符音频时不会出现任何暂停或延迟。我想在每个字符之间添加一秒钟的停顿。有什么帮助吗?

标签: python-3.xgtts

解决方案


推荐阅读