Python 音频生成器的实现示例


Posted in Python onDecember 24, 2019

使用Python生成不同声音的音频

第一步先去百度AI中注册账号,在控制台中创建语音技术应用,获取AppID,API Key,Secret Key

第二步 引用

from tkinter import *
from tkinter.filedialog import askdirectory
from aip import AipSpeech
from tkinter import ttk

第三步搭建窗体

root = Tk()
root.title('生成语音') 
path = StringVar()
pathmc=StringVar() 
pathnr=StringVar() 

Label(root,text = "保存路径:").grid(row = 0, column = 0)
Entry(root, textvariable = path).grid(row = 0, column = 1)
Button(root, text = "路径选择", command = selectPath).grid(row = 0, column = 3)  
Label(root,text = "语音名称:").grid(row = 2, column = 0)
Entry(root, textvariable = pathmc).grid(row = 2, column = 1)
Label(root,text = "语音内容:").grid(row = 3, column = 0)
Entry(root, textvariable = pathnr).grid(row = 3, column = 1)
Button(root, text = "保存", command = Save).grid(row = 4, column = 0)  
#下拉框
Label(root,text = "声音类型:").grid(row =1, column = 0)
number = StringVar()
 
numberChosen = ttk.Combobox(root, width=12, textvariable=number)
 
numberChosen['values'] = ('女声', '男声', '度逍遥', '度丫丫') 
 
numberChosen.grid(column=1, row=1) 
 
numberChosen.current(0) 

root.mainloop()

第四步 创建方法

#保存地址
def selectPath():
 path_ = askdirectory()
 path.set(path_)
 print(path_)
 生成音频的参数 
def Save():
   switch = {'女声': 0,       
     '男声': 1,       
     '度逍遥': 3, 
     '度丫丫': 4,  
     }

   lx=switch.get(number.get(),"0")
   yuying(path.get(),pathmc.get(),pathnr.get(),lx)
#生成音频   
def yuying(url,title,contain,lx):
  APP_ID = 'XXX'#百度AI中获得
  API_KEY = 'XXX'
  SECRET_KEY = 'XXX'
  client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
  result = client.synthesis(contain, 'zh', 1, {
  'vol': 5,'per':lx,'spd':2,# per  发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女  否 
  })
  if not isinstance(result, dict):
    with open(url+'\\'+title+'.mp3', 'wb') as f:
     f.write(result)

合起来的代码就是

from tkinter import *
from tkinter.filedialog import askdirectory
from aip import AipSpeech
from tkinter import ttk

def selectPath():
 path_ = askdirectory()
 path.set(path_)
 print(path_)
def Save():
   switch = {'女声': 0,       
     '男声': 1,       
     '度逍遥': 3, 
     '度丫丫': 4,  
     }

   lx=switch.get(number.get(),"0")
   yuying(path.get(),pathmc.get(),pathnr.get(),lx)
def yuying(url,title,contain,lx):
  APP_ID = 'XXX'#百度AI中获得
  API_KEY = 'XXX'
  SECRET_KEY = 'XXX'
  client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
  result = client.synthesis(contain, 'zh', 1, {
  'vol': 5,'per':lx,'spd':2,# per  发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女  否 
  })
  if not isinstance(result, dict):
    with open(url+'\\'+title+'.mp3', 'wb') as f:
     f.write(result) 

root = Tk()
root.title('生成语音') 
path = StringVar()
pathmc=StringVar() 
pathnr=StringVar() 

Label(root,text = "保存路径:").grid(row = 0, column = 0)
Entry(root, textvariable = path).grid(row = 0, column = 1)
Button(root, text = "路径选择", command = selectPath).grid(row = 0, column = 3)  
Label(root,text = "语音名称:").grid(row = 2, column = 0)
Entry(root, textvariable = pathmc).grid(row = 2, column = 1)
Label(root,text = "语音内容:").grid(row = 3, column = 0)
Entry(root, textvariable = pathnr).grid(row = 3, column = 1)
Button(root, text = "保存", command = Save).grid(row = 4, column = 0)  

 
Label(root,text = "声音类型:").grid(row =1, column = 0)
number = StringVar()
 
numberChosen = ttk.Combobox(root, width=12, textvariable=number)
 
numberChosen['values'] = ('女声', '男声', '度逍遥', '度丫丫') 
 
numberChosen.grid(column=1, row=1) 
 
numberChosen.current(0) 

root.mainloop()

效果图

Python 音频生成器的实现示例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
从零学python系列之新版本导入httplib模块报ImportError解决方案
May 23 Python
Python使用MySQLdb for Python操作数据库教程
Oct 11 Python
python类装饰器用法实例
Jun 04 Python
python3使用urllib模块制作网络爬虫
Apr 08 Python
Python环境下安装使用异步任务队列包Celery的基础教程
May 07 Python
理论讲解python多进程并发编程
Feb 09 Python
解决pandas read_csv 读取中文列标题文件报错的问题
Jun 15 Python
Django Rest framework之权限的实现示例
Dec 17 Python
python 利用浏览器 Cookie 模拟登录的用户访问知乎的方法
Jul 11 Python
Python对称的二叉树多种思路实现方法
Feb 28 Python
python 使用tkinter+you-get实现视频下载器
Nov 17 Python
conda安装tensorflow和conda常用命令小结
Feb 20 Python
Python concurrent.futures模块使用实例
Dec 24 #Python
Python hmac模块使用实例解析
Dec 24 #Python
Python hashlib模块实例使用详解
Dec 24 #Python
Python实现使用dir获取类的方法列表
Dec 24 #Python
django数据模型on_delete, db_constraint的使用详解
Dec 24 #Python
Python中filter与lambda的结合使用详解
Dec 24 #Python
节日快乐! Python画一棵圣诞树送给你
Dec 24 #Python
You might like
Re:从零开始的异世界生活 第2季 开播啦
2020/07/24 日漫
php版微信公众号自定义分享内容实现方法
2016/09/22 PHP
PHP+Redis事务解决高并发下商品超卖问题(推荐)
2020/08/03 PHP
htm调用JS代码
2007/03/15 Javascript
jquery 常用操作整理 基础入门篇
2009/10/14 Javascript
js 完美图片新闻轮转效果,腾讯大粤网首页图片轮转改造而来
2011/11/21 Javascript
js中将String转换为number以便比较
2014/07/08 Javascript
javascript中判断json的方法总结
2015/08/27 Javascript
使用PHP+JavaScript将HTML页面转换为图片的实例分享
2016/04/18 Javascript
EXT中单击button按钮grid添加一行(光标位置可设置)的实例代码
2016/06/02 Javascript
针对BootStrap中tabs控件的美化和完善(推荐)
2016/07/06 Javascript
jQuery实现可拖拽的许愿墙效果【附demo源码下载】
2016/09/14 Javascript
JavaScript的变量声明提升问题浅析(Hoisting)
2016/11/30 Javascript
vue通过点击事件读取音频文件的方法
2018/05/30 Javascript
微信小程序canvas绘制圆角base64图片的实现
2019/08/18 Javascript
关于JS解构的5种有趣用法
2019/09/05 Javascript
js中apply和call的理解与使用方法
2019/11/27 Javascript
基于VUE实现判断设备是PC还是移动端
2020/07/03 Javascript
如何实现echarts markline标签名显示自己想要的
2020/07/20 Javascript
详解Vue的mixin策略
2020/11/19 Vue.js
[02:10]2018DOTA2亚洲邀请赛赛前采访-Liquid
2018/04/03 DOTA
python实现根据月份和日期得到星座的方法
2015/03/27 Python
Python 十六进制整数与ASCii编码字符串相互转换方法
2018/07/09 Python
python3爬虫获取html内容及各属性值的方法
2018/12/17 Python
python pexpect ssh 远程登录服务器的方法
2019/02/14 Python
C语言基础笔试题
2013/04/27 面试题
广州盈通面试题
2015/12/05 面试题
函授毕业生自我鉴定
2013/11/06 职场文书
优秀女职工事迹材料
2014/02/06 职场文书
《祁黄羊》教学反思
2014/04/22 职场文书
四风问题班子对照检查材料
2014/09/27 职场文书
2015秋季开学演讲稿范文
2015/07/16 职场文书
优秀范文:《但愿人长久》教学反思3篇
2019/10/24 职场文书
如何用JavaScipt测网速
2021/05/09 Javascript
Mysql开启外网访问
2022/05/15 MySQL
html,css,javascript是怎样变成页面的
2023/05/07 HTML / CSS