【代码】百度语音API|Python|文本朗读
简单地写了一个按段落朗读文本的demo:DEMO链接_gitee.com。
有时候会请求不到数据,不知道是网络原因还是什么,已添加自动重新请求。
config.ini:
;关于语音合成的相关配置
[default]
api_key = Your api key
secret_key = Your secret key
;发音人选择, 基础音库:0为度小美,1为度小宇,3为度逍遥,4为度丫丫,
;精品音库:5为度小娇,103为度米朵,106为度博文,110为度小童,111为度小萌,默认为度小美
per = 3
;语速,取值0-15,默认为5中语速
spd = 4
;音调,取值0-15,默认为5中语调
pit = 5
;音量,取值0-9,默认为5中音量
vol = 5
# 下载的文件格式, 3:mp3(default) 4: pcm-16k 5: pcm-8k 6. wav
aue = 3
;下载的文件格式, 可选项:mp3(default), pcm-16k, pcm-8k, wav
format = mp3
cuid = 123456PYTHON
tts_url = http://tsn.baidu.com/text2audio
token_url = http://openapi.baidu.com/oauth/2.0/token
;有此scope表示有tts能力,没有请在网页里勾选
scope = audio_tts_post
[追风筝的人.txt]
text_lct = 12
main.py
# coding=utf-8
import os
import json
from configparser import ConfigParser
from playsound import playsound
from urllib.request import urlopen
from urllib.request import Request
from urllib.error import URLError
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.parse import quote_plus
TEXT = "欢迎使用百度语音合成。"
ini_file = "./config.ini"
cfg_name = "default"
book = "D:/总要删的/追风筝的人.txt"
def load_config(ini, name):
cfg = ConfigParser()
# 读取文件内容
cfg.read(ini, encoding="gbk")
# cfg.items()返回list,元素为tuple
return dict(cfg.items(name))
class DemoError(Exception):
pass
def fetch_token(dft_cfg):
# print("fetch token begin")
params = {'grant_type': 'client_credentials',
'client_id': dft_cfg['api_key'],
'client_secret': dft_cfg['secret_key']}
post_data = urlencode(params)
post_data = post_data.encode('utf-8')
req = Request(dft_cfg['token_url'], post_data)
try:
f = urlopen(req, timeout=5)
result_str = f.read()
except URLError as err:
print('token http response http code : ' + str(err.code))
result_str = err.read()
result_str = result_str.decode()
# print(result_str)
result = json.loads(result_str)
# print(result)
if 'access_token' in result.keys() and 'scope' in result.keys():
if not dft_cfg['scope'] in result['scope'].split(' '):
raise DemoError('scope is not correct')
# print('SUCCESS WITH TOKEN: %s ; EXPIRES IN SECONDS: %s' % (result['access_token'], result['expires_in']))
return result['access_token']
else:
raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')
def update_text(file, book_title, ini):
# 读取配置文件
cfg = ConfigParser()
# 读取文件内容
cfg.read(ini, encoding="gbk")
if cfg.has_option(book_title, "text_lct"):
now_lct = int(cfg.get(book_title, "text_lct"))
else:
cfg.add_section(book_title)
now_lct = 0
if len(file) <= now_lct:
return "已经读到最后一句啦!换本书吧~!"
else:
while not len(file[now_lct].strip()):
now_lct = now_lct + 1
# 更新配置文件
cfg.set(book_title, "text_lct", str(now_lct + 1))
cfg.write(open(ini, "r+"))
return file[now_lct]
def request_api(params):
data = urlencode(params)
req = Request(dft_cfg['tts_url'], data.encode('utf-8'))
try:
f = urlopen(req)
result_str = f.read()
headers = dict((name.lower(), value) for name, value in f.headers.items())
has_error = ('content-type' not in headers.keys() or headers['content-type'].find('audio/') < 0)
except Exception as e:
print('asr http response http code : ' + str(e))
result_str = str(e)
has_error = True
if has_error:
print("tts api error:" + str(result_str, 'utf-8'))
request_api(params)
else:
# Step 3.4: 保存请求的音频结果并输出成temp.mp3,朗读完毕后删除
save_file = "error.txt" if has_error else 'temp.' + dft_cfg['format']
with open(save_file, 'wb') as of:
of.write(result_str)
playsound(save_file)
os.remove(save_file)
if __name__ == '__main__':
# Step 1: 载入配置文件
dft_cfg = load_config(ini_file, cfg_name)
# Step 2: 获取Token
token = fetch_token(dft_cfg)
# Step 3: 向API发起请求
# Step 3.1: 初始化请求参数params、书籍标题
params = {'tok': token, 'tex': '', 'per': dft_cfg['per'], 'spd': dft_cfg['spd'], 'pit': dft_cfg['pit'],
'vol': dft_cfg['vol'], 'aue': dft_cfg['aue'], 'cuid': dft_cfg['cuid'],
'lan': 'zh', 'ctp': 1} # lan ctp 固定参数
book_title = (book.split('/'))[-1]
# 打开指定书籍, 并按行读取
with open(book, "r", encoding='utf-8') as f:
file = f.readlines()
# Step 3.2: 不断获取文本并朗读请求得到的音频
while 1:
# Step 3.2.1: 根据上次阅读的位置,更新需要合成的文本内容
TEXT = update_text(file, book_title, ini_file)
print(TEXT)
params['tex'] = quote_plus(TEXT) # 此处TEXT需要两次urlencode
# Step 3.2.2: 将参数打包,并向指定URL请求,并朗读
request_api(params)
目前的结果:

【代码】百度语音API|Python|文本朗读的更多相关文章
- 【VC++技术杂谈004】使用微软TTS语音引擎实现文本朗读
本文主要介绍如何使用微软TTS语音引擎实现文本朗读,以及生成wav格式的声音文件. 1.语音引擎及语音库的安装 TTS(Text-To-Speech)是指文本语音的简称,即通过TTS引擎把文本转化为语 ...
- 利用百度语音API进行语音识别。
由于项目需要,这几天都在试图利用百度语音API进行语音识别.但是识别到的都是“啊,哦”什么的,我就哭了. 这里我只是分享一下这个过程,错误感觉出现在Post语音数据那一块,可能是转换问题吧. API请 ...
- 关于百度地图API (持续跟新)
一.初始化地图显示不在正中间,出现偏移 问题描述与解决办法: 代码: body, html, #allmap { width: 100%; height: 100%; overflow: hidden ...
- 百度地图API的应用
做网页的时候,有时候需要有地图的功能.接下来我来记录一下我的做法. 1.引入API秘钥,在网上都可以搜到. <script src="http://api.map.baidu.com/ ...
- 调用百度语音AI实现语音的识别和合成
#coding:utf-8 ## 先去ffmpeg官网下载(https://ffmpeg.zeranoe.com/builds/),好了之后解压缩,配一下环境变量 ## 打开cmd,运行命令,安装如下 ...
- [python]百度语音rest api
百度语音识别提供的api范例只有java, c, php. 如果使用Python, 需要注意: 语音文件长度是指bytes大小 可以通过len(file.read())获得 使用requests.po ...
- 15.Python文本转化语音方法
1.用pywin32模块来将文本转化为语音 通过pip install pywin32安装模块,pywin32是个万金油的模块,太多的场景使用到它,但在文本转语音上,它却是个青铜玩家,简单无脑但效果不 ...
- python调用百度语音(语音识别-斗地主语音记牌器)
一.概述 本篇简要介绍百度语音语音识别的基本使用(其实是斗地主时想弄个记牌器又没money,抓包什么的又不会,只好搞语音识别的了) 二.创建应用 打开百度语音官网,产品与使用->语音识别-> ...
- Python获得百度统计API的数据并发送邮件
Python获得百度统计API的数据并发送邮件 小工具 本来这么晚是不准备写博客的,当是想到了那个狗子绝对会在开学的时候跟我逼逼这个事情,所以,还是老老实实地写一下吧. Baidu统计API的使 ...
- Python 调用百度翻译API
由于实习公司这边做的是日文app,有时要看看用户反馈,对于我这种五十音图都没记住的人,表示百度翻译确实还可以.但不想每次都复制粘贴啊,google被墙也是挺蛋疼的事,所以用python结合baidu ...
随机推荐
- C# 手机号码隐藏中间四位
C# 隐藏手机号码中间四位数字 使用正则表达式隐藏手机号中间四位 if (!string.IsNullOrWhiteSpace(txtPhone.Text) && txtPhone.T ...
- Educational Codeforces Round 175 (Rated for Div. 2) 比赛记录
Educational Codeforces Round 175 (Rated for Div. 2) 比赛记录 比赛连接 手速场,上蓝场,但是有点唐,C 想错了写了半个多小时,想到正解不到 \(10 ...
- 花3分钟来了解一下Vue3中的插槽到底是什么玩意
前言 插槽看着是一个比较神秘的东西,特别是作用域插槽还能让我们在父组件里面直接访问子组件里面的数据,这让插槽变得更加神秘了.其实Vue3的插槽远比你想象的简单,这篇文章我们来揭开插槽的神秘面纱. 欧阳 ...
- Java DecimalFormat四舍五入的坑及正确用法
一.DecimalFormat四舍五入的坑 1.1 有时候我们在处理小数保留几位小数时,想到了DecimalFormat这个类的使用,百度搜一把可能用到以下方式. 1 public static vo ...
- 【Python&Hypermesh】ABAQUS导入网格,并在Part内保留SET
在Hypermesh定义好set,划分好网格以后,可以导出为INP.然后在ABAQUS导入inp,就可以得到网格.但是这样倒进来的网格一般有两个问题: 网格全在一个部件里,原来定义好的Set会出现在装 ...
- VScode美化
RESULT:EVA-初号机 配色 主题效果 1. 需要的东西 vs code background 插件 custom CSS and JS loader 插件 一些png素材,推荐网址: http ...
- Qt ButtonRole参数的作用
文章目录 QMessgageBox中addButton(QAbstructButton* , ButtonRole),ButtonRole的作用 QMessgageBox中addButton(QAbs ...
- 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
效果 来具体介绍之前先来看看效果. 使用C#构建了一个简单的MCP客户端,以下为运行这个简单客户端的截图,同样可以在Cline等其它的一些MCP客户端中玩耍. 创建一个数据库表: 获取数据库中的所有表 ...
- leetcode两数之和变种(找出所有满足总和的两个数)
偶尔看到leetcode 的两数之和,但是之前遇到过两数之和的变种,之前一开始想不出来,后面看了别人的题解才想到解法,这里记录一下. 题目描述: 原leetcode题目描述 给定一个整数数组 nums ...
- WPS常用快捷键汇总
创建新文档 Ctrl+N或者Alt+F+N(对应baiNew) 打开文档 Ctrl+O或者Alt+F+O(对应Open) 关闭文du档 Ctrl+W或者Alt+W+C 保存当前文zhi档 Ctrl+S ...