您的位置:

如何用 Python 将文本转换成语音

在本教程中,我们将学习如何将人类语言文本转换为类似人类的语音。

有时我们更喜欢听内容而不是阅读。我们可以一边听关键文件数据,一边进行多任务处理。 Python 提供了很多将文本转换为语音的 API。谷歌文本到语音 API 很受欢迎,通常被称为通用技术终端 API。

这个工具非常容易使用,并提供了许多内置功能,用于将文本文件保存为 mp3 文件。

我们不需要使用神经网络和训练模型来将文件转换成语音,因为这也很难实现。相反,我们将使用这些 API 来完成一项任务。

gTTS API提供了将文本文件转换成不同语言的工具,如英语、印地语、德语、泰米尔语、法语等。我们还可以在快速或慢速模式下播放音频语音。

然而,作为它的最新更新,我们不能改变语音文件;它将由系统生成,不可更改。

要将文本文件转换成,我们将使用另一个名为 pyttsx3 的离线库。

安装通用技术规范 API

在终端中键入以下命令来安装 gTTS API。


pip install gTTS

然后,安装附加模块,以便与 gTTS 一起工作。


pip install playsound

然后安装 pyttsx3。


pip install pyttsx3

让我们了解一下 gTTS API 的工作原理


import gtts
from playsound import playsound

我们可以看到,它非常容易使用;我们需要导入它,并将 gTTS 对象作为一个接口传递给 Google Translator API。


# make a request to google to get synthesis
t1 = gtts.gTTS("Welcome to javaTpoint")

在上一行中,我们以文本形式发送了数据,并接收了实际的音频语音。现在,将此保存为音频文件 welcome.mp3 。


# save the audio file
t1.save("welcome.mp3") 

它会将它保存到一个目录中,我们可以如下收听这个文件:


# play the audio file
playsound("welcome.mp3")

输出:

请打开系统音量,听我们之前保存的文本。

现在,我们将文本定义为语音的完整 Python 程序。

Python 程序


# Import the gTTS module for text
# to speech conversion
from gtts import gTTS

# This module is imported so that we can
# play the converted audio

from playsound import playsound

# It is a text value that we want to convert to audio
text_val = 'All the best for your exam.'

# Here are converting in English Language
language = 'en'

# Passing the text and language to the engine,
# here we have assign slow=False. Which denotes
# the module that the transformed audio should
# have a high speed
obj = gTTS(text=text_val, lang=language, slow=False)

#Here we are saving the transformed audio in a mp3 file named
# exam.mp3
obj.save("exam.mp3")

# Play the exam.mp3 file
playsound("exam.mp3")

输出:

说明:

在上面的代码中,我们已经导入了 API 并使用了 gTTS 函数。 gTTS() 函数接受三个参数-

  • 第一个参数是我们想要转换成语音的文本值。
  • 第二个参数是指定的语言。它支持多种语言。我们可以将文本转换成音频文件。
  • 第三个参数代表演讲的速度。我们已经将慢速值设为假;这意味着演讲将以正常速度进行。

我们将这个文件保存为 exam.py ,可以随时访问,然后我们使用了 playsound() 功能在运行时收听音频文件。

可用语言列表

要获得可用的语言,请使用以下函数-

输出:

{'af': 'Afrikaans', 'sq': 'Albanian', 'ar': 'Arabic', 'hy': 'Armenian', 'bn': 'Bengali', 'bs': 'Bosnian', 'ca': 'Catalan', 'hr': 'Croatian', 'cs': 'Czech', 'da': 'Danish', 'nl': 'Dutch', 'en': 'English', 'et': 'Estonian', 'tl': 'Filipino', 'fi': 'Finnish', 'fr': 'French', 'de': 'German', 'el': 'Greek', 'en-us': 'English (US)','gu': 'Gujarati', 'hi': 'Hindi', 'hu': 'Hungarian', 'is': 'Icelandic', 'id': 'Indonesian', 'it': 'Italian', 'ja': 'Japanese', 'en-ca': 'English (Canada)', 'jw': 'Javanese', 'kn': 'Kannada', 'km': 'Khmer', 'ko': 'Korean', 'la': 'Latin', 'lv': 'Latvian', 'mk': 'Macedonian', 'ml': 'Malayalam', 'mr', 'en-in': 'English (India)'}

我们已经提到了几个重要的语言和它们的代码。你可以在这个图书馆里找到几乎所有的语言。

office api

我们已经使用了谷歌 API,但是如果我们想使用离线将文本转换为语音怎么办。Python 提供了 pyttsx3 库,它寻找预装在我们平台中的 TTS 引擎。

让我们了解如何使用 pyttsx3 库:

示例-


import pyttsx3
# initialize Text-to-speech engine
engine = pyttsx3.init()
# convert this text to speech
text = "Python is a great programming language"
engine.say(text)
# play the speech
engine.runAndWait()

在上面的代码中,我们使用了 say() 方法,并将文本作为参数传递。它用于向队列中添加一个单词,而 runAndWait() 方法运行真实的事件循环,直到所有命令排队。

它还提供了一些我们可以根据需要使用的附加属性。我们来了解一下语速的细节:


# get details of speaking rate
rate = engine.getProperty("rate")
print(rate)

输出:

200

We can change rate of speed as we want:
# setting new voice rate (faster)
engine.setProperty("rate", 300)
engine.say(text)
engine.runAndWait()

如果我们通过了 100 分,那么就会慢一些。


engine.setProperty("rate", 100)
engine.say(text)
engine.runAndWait()

现在,我们可以在声音中听到文本文件。


# get details of all voices available
voices = engine.getProperty("voices")
print(voices)

输出:

[<pyttsx3.voice.Voice object at 0x000002D617F00A20>, <pyttsx3.voice.Voice object at 0x000002D617D7F898>, <pyttsx3.voice.Voice object at 0x000002D6182F8D30>]

在本教程中,我们讨论了使用第三方库将文本文件转换为语音。我们还讨论了线下图书馆。通过使用这个,我们可以建立自己的虚拟辅助。