Python文本转化语音方法

1.用pywin32模块来将文本转化为语音

通过pip install pywin32安装模块,pywin32是个万金油的模块,太多的场景使用到它,但在文本转语音上,它却是个青铜玩家,简单无脑但效果不好。代码示例:

import win32com.client

speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("一天什么时候最安全?中午,因为早晚会出事...")

2.利用pyttsx3来转化

pyttsx3使用pyttsx移植过来的,因为pyttsx不支持python3…针对不同的系统,模块会自动所有系统对应的语音驱动,前提是你的系统存在该驱动…

它依赖pywin32模块,可以说它时针对无脑的pywin32接口,进行了升级的个性化配置。先来看下最简单的使用:

import pyttsx3
engine = pyttsx3.init()
engine.say("明天你好,我叫干不倒!")
engine.runAndWait()

代码初始化模块后,填写你所需转化的文本,之后执行runAndWait方法完成语音转化。再来看看其相关操作:

事件监听

import pyttsx3

def onStart(name):
    print('starting', name)

def onWord(name, location, length):
    print('word', name, location, length)

def onEnd(name, completed):
    print('finishing', name, completed)

engine = pyttsx3.init()
engine.connect('started-utterance', onStart)
engine.connect('started-word', onWord)
engine.connect('finished-utterance', onEnd)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

中断话语

import pyttsx3
def onWord(name, location, length):
   print 'word', name, location, length
   if location > 10:
      engine.stop()
engine = pyttsx3.init()
engine.connect('started-word', onWord)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

改变声音

import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
   engine.setProperty('voice', voice.id)
   engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

改变语速

import pyttsx3
engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate+50)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

改变音量

import pyttsx3
engine = pyttsx3.init()
volume = engine.getProperty('volume')
engine.setProperty('volume', volume-0.25)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

运行驱动程序事件循环

import pyttsx3
engine = pyttsx3.init()
def onStart(name):
   print 'starting', name
def onWord(name, location, length):
   print 'word', name, location, length
def onEnd(name, completed):
   print 'finishing', name, completed
   if name == 'fox':
      engine.say('What a lazy dog!', 'dog')
   elif name == 'dog':
      engine.endLoop()
engine = pyttsx3.init()
engine.connect('started-utterance', onStart)
engine.connect('started-word', onWord)
engine.connect('finished-utterance', onEnd)
engine.say('The quick brown fox jumped over the lazy dog.', 'fox')
engine.startLoop()

使用外部事件循环

import pyttsx3
engine = pyttsx3.init()
engine.say('The quick brown fox jumped over the lazy dog.', 'fox')
engine.startLoop(False)
# engine.iterate() must be called inside externalLoop()
externalLoop()
engine.endLoop()

3.使用百度语音

百度语音识别api:baidu-aip是百度开放的公共语音转化服务。只需要在百度注册相关的app及秘钥信息即可使用。

image.png

使用流程如下:

  1. 访问语音合成-百度AI开放平台:http://ai.baidu.com/tech/speech/tts

  2. 之后使用百度账号即可登陆(没有百度账号的,自己注册一个)

  3. 创建应用,添加语音识别的功能,并完成注册

  4. 保存你的app_id, API_Key, Secret_Key 三项数据留着后续使用

  5. 切换回语音合成首页,点击立即使用旁边的技术文档按钮,进入API文档

  6. 定位 语音合成—>SDK文档—>Python SDK,即可看到详细的开发文档说明

image.png

接下来,我们看看文档中的相关说明:

  • 接口描述

基于该接口,开发者可以轻松的获取语音合成能力

  • 请求说明

合成文本长度必须小于1024字节,如果本文长度较长,可以采用多次请求的方式。文本长度不可超过限制

举例,要把一段文字合成为语音文件:

from aip import AipSpeech

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

result  = client.synthesis('你好百度', 'zh', 1, {
   'vol': 5,
})

# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
   with open('auido.mp3', 'wb') as f:
       f.write(result)

# 识别正确返回语音二进制 错误则返回dict 参照下面错误码

if not isinstance(result, dict):

    with open('auido.mp3', 'wb') as f:

        f.write(result)

在上面代码中,常量APP_ID在百度云控制台中创建,常量API_KEY与SECRET_KEY是在创建完毕应用后,系统分配给用户的,均为字符串,用于标识用户,为访问做签名验证,可在AI服务控制台中的应用列表中查看。

image.png

相比于前两种模块,baidu-aip却是高端很多啊…喜欢的朋友可以下载了玩玩

转载网址:https://bbs.huaweicloud.com/blogs/115130?from=singlemessage&isappinstalled=0

使用Python将任正非400+篇演讲批量转化为语音https://www.jianshu.com/p/05f9874b6989



猜你喜欢

转载自www.cnblogs.com/ubuntu1987/p/11826586.html