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的Django框架中的Cookie相关处理
Jul 22 Python
python 调用win32pai 操作cmd的方法
May 28 Python
python3.5 tkinter实现页面跳转
Jan 30 Python
Python进阶之尾递归的用法实例
Jan 31 Python
使用pandas中的DataFrame数据绘制柱状图的方法
Apr 10 Python
解决安装pycharm后不能执行python脚本的问题
Jan 19 Python
Python面向对象进阶学习
May 21 Python
python之生产者消费者模型实现详解
Jul 27 Python
解决python 读取 log日志的编码问题
Dec 24 Python
django执行原始查询sql,并返回Dict字典例子
Apr 01 Python
python实现126邮箱发送邮件
May 20 Python
python 绘制场景热力图的示例
Sep 23 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
《魔兽争霸3:重制版》更新 多项视觉效果调整
2020/05/04 魔兽争霸
解析php中获取url与物理路径的总结
2013/06/21 PHP
安装ImageMagick出现error while loading shared libraries的解决方法
2014/09/23 PHP
PHP中Session和Cookie是如何操作的
2015/10/10 PHP
浅谈php7的重大新特性
2015/10/23 PHP
PHP CURL与java http使用方法详解
2018/01/26 PHP
浅谈javascript 函数表达式和函数声明的区别
2016/01/05 Javascript
AngularJS控制器详解及示例代码
2016/08/16 Javascript
使用JS实现图片展示瀑布流效果的实例代码
2016/09/12 Javascript
BootStrap实现手机端轮播图左右滑动事件
2016/10/13 Javascript
Node.js中的require.resolve方法使用简介
2017/04/23 Javascript
JS仿QQ好友列表展开、收缩功能(第二篇)
2017/07/07 Javascript
JS中正则表达式要注意lastIndex属性
2017/08/08 Javascript
Three.js利用orbit controls插件(轨道控制)控制模型交互动作详解
2017/09/25 Javascript
Mac中安装nvm的教程分享
2017/12/11 Javascript
vue一个页面实现音乐播放器的示例
2018/02/06 Javascript
js图片无缝滚动插件使用详解
2020/05/26 Javascript
VUE写一个简单的表格实例
2019/08/06 Javascript
Vuex的实战使用详解
2019/10/31 Javascript
Vue如何将页面导出成PDF文件
2020/08/17 Javascript
[13:56]DAC2018 4.5SOLO赛决赛 MidOne vs Paparazi第一场
2018/04/06 DOTA
python利用tkinter实现屏保
2019/07/30 Python
利用python实现短信和电话提醒功能的例子
2019/08/08 Python
基于nexus3配置Python仓库过程详解
2020/06/15 Python
python按顺序重命名文件并分类转移到各个文件夹中的实现代码
2020/07/21 Python
一文详述 Python 中的 property 语法
2020/09/01 Python
CSS3制作苹果风格键盘特效
2015/02/26 HTML / CSS
浅谈HTML5 defer和async的区别
2016/06/07 HTML / CSS
亿阳信通股份有限公司C#笔试题
2016/12/06 面试题
销售部主管岗位职责
2013/12/18 职场文书
医生见习报告范文
2014/11/03 职场文书
2015年母亲节活动策划方案
2015/05/04 职场文书
2016年心理学教育培训学习心得体会
2016/01/12 职场文书
《悬崖边的树》读后感2篇
2019/12/02 职场文书
html5实现点击弹出图片功能
2021/07/16 HTML / CSS
基于MySql验证的vsftpd虚拟用户
2021/11/07 MySQL