python 调用百度api 文字转语音

先创建一个应用
python 调用百度api 文字转语音_第1张图片
然后把appid apikkey secretkey填入以下代码

from aip import AipSpeech

""" 你的 APPID AK SK """
APP_ID = '输入你的'
API_KEY = '输入你的'
SECRET_KEY = '输入你的'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
str = "此处输入文本" 
result  = client.synthesis(str, 'zh', 3, {
    'vol': 5,
})

# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
    with open('D:/输出文件.mp3', 'wb') as f:#路径位于d盘
        f.write(result)

python 调用百度api 文字转语音_第2张图片
输出文件
更多参数

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
str = "合成文本长度必须小于1024字节如果本文长度较长可以采用多次请求的方式文本长度不可超过限制举例,要把一段文字合成为语音文件:" 
result  = client.synthesis(str, 'zh', 3, {
    'vol': 5,   #音量
    'per': 5003,#发音人选择
    'pit': 7,   #音调1~9
    'spd':7     #语速
})

输出
窗口化
python 调用百度api 文字转语音_第3张图片

from cgi import print_form
from cmath import e
from re import A
from turtle import width
from aip import AipSpeech
from tkinter import *
win = Tk()#创建窗口
win.geometry("700x520") #窗口大小
l1 = Label(win,text="文字转语音\n请输入",bd = 5)#标题
l1.pack()
s1 = Entry(win,relief = SUNKEN,width=45)#内容文本框
s1.pack()
var = IntVar()
xz1 = Radiobutton (win, text="度小宇",variable = var,value=1 )#声音选择
xz2 = Radiobutton (win, text="度小美",variable = var,value=0 )#声音选择
xz3 = Radiobutton (win, text="度逍遥",variable = var,value=3 )
xz4 = Radiobutton (win, text="度丫丫",variable = var,value=4 )
xz1.pack()
xz2.pack() 
xz3.pack() 
xz4.pack() 
l2 = Label(win,text="音量调节",bd = 5)#标题
l2.pack()
h1=Scale(win,from_=0,to=9,resolution=1,orient= HORIZONTAL,tickinterval=9)#音量调节
h1.pack()
h1.set(5)#音量初始值
l3 = Label(win,text="音调调节",bd = 5)#标题
l3.pack()
h2=Scale(win,from_=0,to=9,resolution=1,orient= HORIZONTAL,tickinterval=9)#音调调节
h2.pack()
h2.set(5)#音调初始值
l2 = Label(win,text="语速调节",bd = 5)#标题
l2.pack()
h3=Scale(win,from_=0,to=9,resolution=1,orient= HORIZONTAL,tickinterval=9)#语速调节
h3.pack()
h3.set(5)#语速初始值

def yunxing():
 APP_ID = '你的'
 API_KEY = '你的'
 SECRET_KEY = '你的'
 sy = var.get()
 yl = h1.get()
 yd = h2.get()
 ys = h3.get()
 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
 result  = client.synthesis(s1.get(), 'zh', 3, {
    'vol': yl,   #音量
    'per': sy,#发音人选择
    'pit': yd,   #音调1~9
    'spd':ys     #语速
 })
 # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
 if not isinstance(result, dict):
       with open('输出.mp3', 'wb') as f:#路径位于d盘
        f.write(result)
 print("输入的是:",s1.get())
 print("人声选择",str(var.get()))
 print("音量",h1.get())
an1 =Button(win,text="转换并保存",command=yunxing)
an1.pack()      
l5 = Label(win,text="保存位置D盘根目录",bd = 5)
l5.pack()
win.mainloop()

你可能感兴趣的:(python,python)