源于自己是上班族,父母或者长辈在上班时间发语音或者打电话的话,接听或者回复比较麻烦,所以就想着自己写一个小玩意可以将文字转换成语音,这样就可以方便很多~
环境:Python3
安装:
pip install pyttsx3
代码:
import pyttsx3msg = '''你是不是没睡醒'''
# 初始化
teacher = pyttsx3.init()
# 调节语速
rate = Property('rate')
teacher.setProperty('rate', rate + 20)
# 变换声音
voices = Property('voices')
for i in voices:teacher.setProperty('voice', i.id)teacher.say(msg)
teacher.runAndWait()
简单测试下,但是这种只能作为小程序里面的语音播报,也可以使用之前写过的将python文件打包成.exe文件,直接使用。
安装:
pip install baidu-aip
首先我们要注册账号,拿到我们的身份秘钥。
登录后:
创建应用:
创建后(全选即可)
文档地址:
代码步骤:
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)
完整代码:
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)print(client)
result = client.synthesis('你是不是没睡醒', 'zh', 1, {'vol': 5,'per': 0,
})# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):with open('auido2.mp3', 'wb') as f:f.write(result)
到这里就大功告成了,附上聊天截图哈哈
最后来一篇朱自清的《背影》
代码:
# encoding: utf-8
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)# 读取文件
def get_file_content(filePath):with open(filePath, 'r', encoding="utf-8") as fp:ad()# 音频合成结果
result = client.synthesis(get_file_content('), 'zh', '0', {'vol': 5, # 音量(0-15)'spd': 2, # 语速(0-9)'per': 1 # 发音模式
})if not isinstance(result, dict):with open('背影.mp3', 'wb') as f: # 将音频文件写入本地f.write(result)# 利用 pygame 模块播放(没有的话 pip install pygame)
import pygame
import timepygame.mixer.init()
pygame.mixer.music.load('背影.mp3')
pygame.mixer.music.play()
time.sleep(120) # 休眠120s时间这段时间里播放。
免费伴读书童~
转载请注明转自:
本文发布于:2024-01-28 01:49:17,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17063777653943.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |