python语音转文字_Python文字转语音示例

本文介绍了如何在Python中实现文本转语音,包括使用pyttsx3的离线方法和使用gTTS的在线Google文本到语音转换。pyttsx3适用于无互联网连接的情况,而gTTS虽然需要网络,但能生成更接近人类语音的音频。
摘要由CSDN通过智能技术生成

python语音转文字

Here you will get python text to speech example.

在这里,您将获得python文本语音转换示例。

As we know, some people have difficulty reading large amounts of text due to dyslexia and other learning disabilities. Some people have basic literary levels. They often get frustrated trying to browse the internet because so much of it is in text form or on other hand some people prefer to listen or watch a news article (or something like this) instead of reading. So to solve all these problems a concept comes into mind that is ”text to speech”.

众所周知,由于阅读障碍和其他学习障碍,有些人难以阅读大量文字。 有些人具有基本的文学水平。 他们经常尝试浏览Internet时感到沮丧,因为其中很多都是文本形式的,或者另一方面,某些人更喜欢听或看新闻文章(或类似的东西)而不是阅读。 因此,要解决所有这些问题,就会想到一个概念,即“文本到语音”。

So in this tutorial we are going to learn that how to convert text to speech in Python. Here we’ll show you two best and easiest ways to convert your text into speech

因此,在本教程中,我们将学习如何在Python中将文本转换为语音。 在这里,我们将向您展示将文本转换为语音的两种最简便的方法

  1. Text to speech without internet connection (using pyttsx3)

    文本到语音无需互联网连接(使用pyttsx3)

  2. Text to speech having internet connection (using gTTS)

    具有互联网连接的文本到语音(使用gTTS)

Python文字转语音示例 (Python Text to Speech Example)

方法1:使用pyttsx3 (Method 1: Using pyttsx3)

Pyttsx3 is an offline cross-platform Test-to-Speech library which is compatible with both Python 3 and Python 2 and supports multiple TTS engines

Pyttsx3是一个离线跨平台的“测试到语音转换”库,与Python 3和Python 2兼容,并支持多个TTS引擎

To use pyttsx3, first we have to download and install it. In order to install it open your command prompt or terminal and type this command.

要使用pyttsx3,首先我们必须下载并安装它。 为了安装它,请打开命令提示符或终端,然后键入此命令。

pip install pyttsx3

点安装pyttsx3

If you’re using windows operating system then you also have to install “pypiwin32” to make it work. To install pypiwin32 again type this command and hit enter in command prompt.

如果您使用的是Windows操作系统,则还必须安装“ pypiwin32 ”以使其正常运行。 要再次安装pypiwin32 ,请键入此命令,然后在命令提示符下按Enter。

python  -m  pip install pypiwin32

python -m pip安装pypiwin32

Make sure you’ve internet connection while running both of the command. It is one time process, after you’ve installed pyttsx3 now to use it, the program will be as shown below.

同时运行两个命令时,请确保您已连接互联网。 这是一次过程,现在安装pyttsx3以使用它后,程序将如下所示。

import pyttsx3
engine = pyttsx3.init()
engine.say("hello crazy programmer")
engine.setProperty('rate',120)
engine.setProperty('volume', 0.9)
engine.runAndWait()

In this program, in first Line we’re initializing pyttsx3 for use then we’re passing the text in method say(). After it we’re setting some properties like volume and rate of the voice. Here we’re passing 120 as rate, which means it will speak 120 words per minute and last line of above program will be produce an audio saying “hello crazy  programmer”.

在此程序中,在第一行中,我们将初始化pyttsx3以供使用,然后在方法say()中传递文本 之后,我们要设置一些属性,例如声音的音量和速率。 在这里,我们以120的速率传递,这意味着它将每分钟讲120个单词,并且上述程序的最后一行将产生一个音频,说“你好疯狂的程序员”。

We can also modify the voice like we can change it into female voice (by default its male), age and language. For more information please visit http://pyttsx3.readthedocs.io/en/latest/engine.html

我们还可以修改语音,就像可以将其更改为女性语音(默认为男性),年龄和语言一样。 有关更多信息,请访问http://pyttsx3.readthedocs.io/en/latest/engine.html

方法2:使用gTTS(Google文本到语音) (Method 2: Using gTTS (Google Text to Speech))

Google Text to Speech is one of the best TTS API out there, because it will generate audio as approximately similar to human voice while other APIs generate audio like a metallic voice or robotic voice. But there is also a disadvantage of gTTS, it will need an internet connection to convert the text into an audio. So it can be slow then other offline APIs.

Google Text to Speech是目前最好的TTS API之一,因为它会产生与人类语音大致相似的音频,而其他API会产生诸如金属语音或机器人语音之类的音频。 但是gTTS的另一个缺点是,它需要互联网连接才能将文本转换为音频。 因此,它可能比其他脱机API慢。

To install gTTS API open your command prompt or terminal and type this command:

要安装gTTS API,请打开命令提示符或终端,然后输入以下命令:

pip  install gTTS

点安装gTTS

Program for conversion will be as shown below.

转换程序如下所示。

from gtts import gTTS
tts = gTTS(text="Hello crazy programmer", lang='en')
tts.save("audio.mp3")

Unlike other APIs it will generate an audio and will save into the same directory where your program stored.

与其他API不同,它将生成音频并将其保存到程序存储的同一目录中。

To play this audio we’ll need another tool to play audio on command line.

要播放此音频,我们需要另一个工具在命令行上播放音频。

If you’re using Linux (eg. Ubuntu) then mpg321 will be best command line player.

如果您使用的是Linux(例如Ubuntu),则mpg321将是最佳的命令行播放器。

To install it open terminal and type this command-

要安装它,请打开终端并输入以下命令:

sudo apt-get install mpg321

sudo apt安装mpg321

Now we can use this command to play any audio on command line:

现在我们可以使用此命令在命令行上播放任何音频:

mpg321 audio.mp3 -quiet

mpg321 audio.mp3-安静

To run this command in python program, add these two lines into above program

要在python程序中运行此命令,请将这两行添加到上述程序中

import os     #will be on the top
os.system('mpg321 audio.mp3 -quiet')

On other hand, for windows, we doesn’t have to install any new software or API to play the mp3 file. All we have to do is open command prompt and enter the name of your file it will play that file using your default media player. So to run this command in python add these two lines in above program.

另一方面,对于Windows,我们无需安装任何新软件或API即可播放mp3文件。 我们要做的就是打开命令提示符,然后输入文件名,它将使用默认的媒体播放器播放该文件。 因此,要在python中运行此命令,请在上述程序中添加这两行。

import os       #will be on the top
os.system("audio.mp3")

For more information on gTTS please visit https://pypi.org/project/gTTS/

有关gTTS的更多信息,请访问https://pypi.org/project/gTTS/

Comment below if you have queries regarding python text to speech conversion.

如果您有关于python文本到语音转换的查询,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2018/05/python-text-to-speech.html

python语音转文字

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值