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 max内置函数详细介绍
Nov 17 Python
Python输出带颜色的字符串实例
Oct 10 Python
用python制作游戏外挂
Jan 04 Python
Python 3.x基于Xml数据的Http请求方法
Dec 28 Python
Python线程池模块ThreadPoolExecutor用法分析
Dec 28 Python
在python中获取div的文本内容并和想定结果进行对比详解
Jan 02 Python
pyqt5 禁止窗口最大化和禁止窗口拉伸的方法
Jun 18 Python
wxPython电子表格功能wx.grid实例教程
Nov 19 Python
Python数组并集交集补集代码实例
Feb 18 Python
Python使用jpype模块调用jar包过程解析
Jul 29 Python
python 使用openpyxl读取excel数据
Feb 18 Python
pip install命令安装扩展库整理
Mar 02 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 smarty 二级分类代码和模版循环例子
2011/06/01 PHP
php中的ini配置原理详解
2014/10/14 PHP
php简单实现快速排序的方法
2015/04/04 PHP
laravel 解决paginate查询多个字段报错的问题
2019/10/22 PHP
Firebug入门指南(Firefox浏览器)
2010/08/21 Javascript
jQuery前台数据获取实现代码
2011/03/16 Javascript
对new functionName()定义一个函数的理解
2014/05/22 Javascript
angularjs学习笔记之完整的项目结构
2015/09/26 Javascript
详解使用vue-admin-template的优化历程
2018/05/20 Javascript
jQuery实现为动态添加的元素绑定事件实例分析
2018/09/07 jQuery
Vue axios设置访问基础路径方法
2018/09/19 Javascript
微信小程序自定义带价格显示日历效果
2018/12/29 Javascript
Vue render函数实战之实现tabs选项卡组件
2019/04/22 Javascript
如何获取vue单文件自身源码路径
2019/05/06 Javascript
解决vue项目F5刷新mounted里的函数不执行问题
2019/11/05 Javascript
原生js+canvas实现贪吃蛇效果
2020/08/02 Javascript
[04:04]显微镜下的DOTA2第六期——电影级别的华丽团战
2014/06/20 DOTA
[01:05:00]2018国际邀请赛 表演赛 Pain vs OpenAI
2018/08/24 DOTA
Python中统计函数运行耗时的方法
2015/05/05 Python
使用Mixin设计模式进行Python编程的方法讲解
2016/06/21 Python
Python之str操作方法(详解)
2017/06/19 Python
Python三种遍历文件目录的方法实例代码
2018/01/19 Python
Python循环中else,break和continue的用法实例详解
2019/07/11 Python
Python企业编码生成系统之系统主要函数设计详解
2019/07/26 Python
Python hashlib常见摘要算法详解
2020/01/13 Python
python GUI库图形界面开发之PyQt5线程类QThread详细使用方法
2020/02/26 Python
PyCharm2020最新激活码+激活码补丁(亲测最新版PyCharm2020.2激活成功)
2020/11/25 Python
纯CSS3发光分享按钮的实现教程
2014/09/06 HTML / CSS
HTML5之SVG 2D入门13—svg对决canvas及长处和适用场景分析
2013/01/30 HTML / CSS
带病坚持工作事迹
2014/05/03 职场文书
教育系统干部作风整顿心得体会
2014/09/09 职场文书
辞旧迎新演讲稿
2014/09/15 职场文书
2014年体育部工作总结
2014/11/13 职场文书
大学生违纪检讨书范文
2015/05/07 职场文书
歌舞青春观后感
2015/06/10 职场文书
php 解析非标准json、非规范json
2021/04/01 PHP