在Python中将文本转换为语音

在Python中,有几种API可用于将文本转换为语音。其中一个API是Google文本到语音API,通常称为GTTSAPI。gTTS是一个非常易于使用的工具,它可以将输入的文本转换为音频,并保存为mp3文件。

null

GTTSAPI支持多种语言,包括英语、印地语、泰米尔语、法语、德语等。演讲可以以两种可用音频速度中的任意一种进行,快速或慢速。但是,截至最新更新,无法更改生成音频的声音。

安装

要安装GTTSAPI,请打开终端并写入

pip install gTTS

这适用于任何平台。 现在我们都准备编写一个将文本转换为语音的示例程序。

# Import the required module for text
# to speech conversion
from gtts import gTTS
# This module is imported so that we can
# play the converted audio
import os
# The text that you want to convert to audio
mytext = 'Welcome to geeksforgeeks!'
# Language in which you want to convert
language = 'en'
# Passing the text and language to the engine,
# here we have marked slow=False. Which tells
# the module that the converted audio should
# have a high speed
myobj = gTTS(text = mytext, lang = language, slow = False )
# Saving the converted audio in a mp3 file named
# welcome
myobj.save( "welcome.mp3" )
# Playing the converted file
os.system( "mpg321 welcome.mp3" )


输出

The output of the above program should be a 
voice saying, 'Welcome to geeksforgeeks!'

本文由 阿克希尔·戈尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享