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爬取qq music中的音乐url及批量下载
Mar 23 Python
用TensorFlow实现多类支持向量机的示例代码
Apr 28 Python
解决python升级引起的pip执行错误的问题
Jun 12 Python
TensorFlow数据输入的方法示例
Jun 19 Python
详解python while 函数及while和for的区别
Sep 07 Python
对python 合并 累加两个dict的实例详解
Jan 21 Python
django的分页器Paginator 从django中导入类
Jul 25 Python
Python基于gevent实现高并发代码实例
May 15 Python
解决tensorflow读取本地MNITS_data失败的原因
Jun 22 Python
详解python命令提示符窗口下如何运行python脚本
Sep 11 Python
python文件目录操作之os模块
May 08 Python
jupyter notebook保存文件默认路径更改方法汇总(亲测可以)
Jun 09 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
php json_encode奇怪问题说明
2011/09/27 PHP
加载 Javascript 最佳实践
2011/10/30 Javascript
jQuery 获取和设置select下拉框的值实现代码
2013/11/08 Javascript
jquery 获取dom固定元素 添加样式的简单实例
2014/02/04 Javascript
js获取当前地址 JS获取当前URL的示例代码
2014/02/26 Javascript
z-blog SyntaxHighlighter 长代码无法换行解决办法(基于jquery)
2015/11/18 Javascript
Eclipse引入jquery报错如何解决
2015/12/01 Javascript
Bootstrap CSS组件之按钮下拉菜单
2016/12/17 Javascript
日期时间范围选择插件:daterangepicker使用总结(必看篇)
2017/09/14 Javascript
vue-cli常用设置总结
2018/02/24 Javascript
微信开发之微信jssdk录音功能开发示例
2018/10/22 Javascript
vue+iview 实现可编辑表格的示例代码
2018/10/31 Javascript
vuex actions异步修改状态的实例详解
2019/11/06 Javascript
高效jQuery选择器的5个技巧实例分析
2019/11/26 jQuery
JavaScript中的相等操作符使用详解
2019/12/21 Javascript
vue+springboot+element+vue-resource实现文件上传教程
2020/10/21 Javascript
[01:00:54]TI4正赛第二日开场
2014/07/20 DOTA
python 图片验证码代码分享
2012/07/04 Python
Python中的yield浅析
2014/06/16 Python
python通过索引遍历列表的方法
2015/05/04 Python
使用python采集脚本之家电子书资源并自动下载到本地的实例脚本
2018/10/23 Python
python读出当前时间精度到秒的代码
2019/07/05 Python
Django分页功能的实现代码详解
2019/07/29 Python
Python中常用的高阶函数实例详解
2020/02/21 Python
Python正则表达式如何匹配中文
2020/05/27 Python
Python中猜拳游戏与猜筛子游戏的实现方法
2020/09/04 Python
自我评价范文点评
2013/12/04 职场文书
优秀通讯员事迹材料
2014/01/28 职场文书
中国好声音华少广告词
2014/03/17 职场文书
平安家庭事迹材料
2014/12/20 职场文书
销售内勤岗位职责范本
2015/04/13 职场文书
学长教您写论文:经验总结
2019/07/09 职场文书
Python 中random 库的详细使用
2021/06/03 Python
MySQL面试题讲解之如何设置Hash索引
2021/11/01 MySQL
pycharm无法安装cv2模块问题
2022/05/20 Python
MySQL范围查询优化的场景实例详解
2022/06/10 MySQL