-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathbaidu.py
28 lines (22 loc) · 925 Bytes
/
baidu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import json
from aip import AipSpeech
class Baidu:
def __init__(self, baidu_setting, options=None):
if options is None:
options = {'spd': 5, 'pit': 5, 'vol': 5, 'per': 1}
self.setting = baidu_setting
self.options = options
self.client = AipSpeech(self.setting.get('app_id', ''), self.setting.get('app_key', ''),
self.setting.get('secret_key', ''))
def process(self, text, file_name):
result = self.client.synthesis(text=text, lang='zh', ctp=1, options=self.options)
if not isinstance(result, dict):
with open(file_name, 'wb') as f:
f.write(result)
return result
if __name__ == '__main__':
with open('setting.json', 'r') as ff:
setting = json.load(ff)
bd_st = setting.get('baidu', {})
baidu = Baidu(bd_st)
baidu.process('百度你好', '百度语音.mp3')