【代码】百度语音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 ...
随机推荐
- 通过 fork 为项目做出贡献
本文旨在帮助新手小伙伴了解学习如何参与 GitHub 项目,为其献上自己的一份力,留下属于自己的足迹. 普遍流程 通过 fork 为项目做出贡献一个普遍的流程如下图: sequenceDiagram ...
- redis - [02] 安装部署
在Windows和Linux操作系统下的安装部署 一.windows版 (1)访问redis下载地址:https://github.com/tporadowski/redis/releases (2) ...
- php用token做登录认证
https://blog.csdn.net/qq_20869933/article/details/133201967 作用: PHP 使用token验证可有效的防止非法来源数据提交访问,增加数据操作 ...
- C语言 链表操作
#include<stdio.h>#include<stdlib.h>struct node{ int data; struct node *next;};int ...
- 【Matlab】基于KDtree的最近邻搜索和范围搜索
摘要:介绍Matlab的rangesearch()函数和knnsearch()函数. rangesearch() -- 根据给定k-维数据集,返回指定距离范围内的所有数据点 knnsearch() - ...
- selenium 进入页面提示 503 Service Temporarily Unavailable
进入三级页面提示503 Service Temporarily Unavailable,如果手动刷新页面重新加载成功 网上看都是如何配置及原因的,没告诉如何解决 于是我想,如果是这样的话,执行刷新操作 ...
- Windows编程----内核对象竟然如此简单?
什么是内核对象 内核对象本质上就是内存中的一块内存 ,这块内存由操作系统进行管理和分配,任何应用程序都无法直接操作这块内存区域.至于内核对象的作用,我们暂且不说,这里只需要直到它是内存中的一块内存. ...
- k8s dashboard token 生成/获取
创建示例用户 在本指南中,我们将了解如何使用 Kubernetes 的服务帐户机制创建新用户.授予该用户管理员权限并使用与该用户绑定的承载令牌登录仪表板. 对于以下每个和的代码片段ServiceAcc ...
- mac上 Kratos 配置 protoc
前言 protoc 是 protobuf 文件(.proto)的编译器,可以借助这个工具把 .proto 文件转译成各种编程语言对应的源码,包含数据类型定义.调用接口等. protoc 在设计上把 p ...
- storm部署文档
背景 这篇笔记原来是记录在印象笔记中的,没有发布到博客中,这次我重新整理一下发布上来,希望给读者以参考. Storm的部署手册 Zookeepr的部署 首先下载安装包:apache-zookeeper ...