如何在Python中将文本转换为语音

问题描述 投票:0回答:8

我现在想知道如何在 python 中将文本转换为语音。

在.NET中我使用过

Dim SAPI

Msg = 'Hi this is a test'

SAPI = CreateObject("sapi.spvoice")
SAPI.Speak(Msg)
python text-to-speech speech
8个回答
3
投票

您可以通过pyttsx模块来实现。它使用默认的 MS 语音识别系统。

import pyttsx

engine = pyttsx.init()
engine.say("Your Message")
engine.runAndWait()

2
投票

我知道在这里回答真的很晚了,但我想我应该在这里发布,因为我有基于

TTS
转换的解决方案,使用
SAPI
中的
python
,这是OP的原始问题。

这对于其他使用

SAPI
中的
python
寻找解决方案的人来说可能很有用。

from win32com.client import constants, Dispatch

Msg = "Hi this is a test"

speaker = Dispatch("SAPI.SpVoice")  #Create SAPI SpVoice Object
speaker.Speak(Msg)                  #Process TTS
del speaker                         #Delete speaker object and free up memory

1
投票
import pyttsx3
speaker=pyttsx3.init()
speaker.say("Your message")
speaker.runAndWait()

1
投票
# pip install pywin32
# pip install pyttsx3
 
import pyttsx3

pyttsx3.speak('Hello Woeld')

0
投票

您可以使用 gTTS 模块来完成此操作。它将文本转换为语音。 您必须使用的第二个模块是 playsound 来播放转换后的文本。

    from gtts import gTTS #pip install gtts
    import playsound #pip install playsound
    import os

    my_aud = gTTS("hello how are you") #converts the text into speech
    my_aud.save('demo.mp3') #save the file with .mp3 extension
    playsound('demo.mp3') #to play it
    os.remove('demo.mp3')

0
投票

这是我自己创建的男声和女声功能。
只需定义一个文件名并保存即可。
现在您可以将其导入另一个文件并一次又一次地重复使用。

pip install pyttsx3

import pyttsx3
def femaleVoice(text):
    print("Program : "+text)
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[-1].id)
    engine.say(text)
    engine.runAndWait()

def maleVoice(text):
    print("Program : "+text)
    pyttsx3.speak(text)

femaleVoice("There we go.")#Text
maleVoice("There we go.")

0
投票

如果您想获得大量声音。我们有超过 500 个。

这是一个片段

python
import apiaudio 
import os


apiaudio.api_key = os.environ['APIKEY']
first_track = apiaudio.Orchestrator.create_audio(scriptText="Hello World my first audio track", 
                                                 voice="Ryan",
                                                 soundTemplate="jakarta")
print(first_track)

您只需要一个免费的 api 密钥。看看 http://www.api.audio


0
投票

堆栈溢出策略不建议用户推广或广告工具,所以我决定告诉你一个方法。

我认为你可以使用纯Python自己创建一个TTS:

  1. 文本映射到 64k mp3 音频

  2. 使用“,”。符号来分隔文本,这样你就可以得到更少的重复文本数据

  3. 当您获得 1GB 数据时,您将获得功能良好的 TTS

您甚至可以使用语音识别来收集音频到文本听写数据。

通过使用此方法,您可以获得几乎 100% 准确的语音 TTS

© www.soinside.com 2019 - 2024. All rights reserved.