-
Notifications
You must be signed in to change notification settings - Fork 0
/
dicte.py
318 lines (288 loc) · 9.78 KB
/
dicte.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# -*- coding:utf-8 -*-
#
#
#
# author : hantjscnxp@outlook.com
#
# Requirements:
# | name | version |
# | ffmpeg | 20200603-b6d7c4c |
# | websocket | 0.2.1 |
#
# Reference Document:
# https://www.xfyun.cn/doc/asr/voicedictation/API.html#%E6%8E%A5%E5%8F%A3%E8%B0%83%E7%94%A8%E6%B5%81%E7%A8%8B
# http://xfyun-doc.ufile.ucloud.com.cn/1567133454542862/iat_ws_python3_demo.zip
# https://www.xfyun.cn/doc/asr/voicedictation/Audio.html
# https://ffmpeg.org/ffmpeg.html#Main-options
#
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
import base64
import datetime
import hashlib
import hmac
import json
import logging
import os
import re
import time
from time import mktime
from urllib.parse import urlencode
from wsgiref.handlers import format_date_time
import websocket
import _thread as thread
APPID = ''
APIKEY = ''
APISECRET = ''
STREAMPATH = r''
LANGUAGE = '' # zh_cn, en_us, fr_fr
class WebSocketParam:
def __init__(self,APPID,APIKey,APISecret,AudioFile,language):
self.APPID = APPID
self.APIKey = APIKey
self.APISecret = APISecret
self.AudioFile = AudioFile
self.CommonArgs = {"app_id": self.APPID}
self.BusinessArgs = {
"language" : language,
"domain" : "iat", # iat(日常用语), medical(医疗领域)
"accent" : "mandarin"
}
# 接口鉴权
self.signature = {
"host" : None,
"date" : None,
"authorization" : None
}
# host
if language == "zh_cn" or language == "en_us":
self.url = "wss://iat-api.xfyun.cn/v2/iat"
self.signature["host"] = "ws-api.xfyun.cn"
else:
self.url = "wss://iat-niche-api.xfyun.cn/v2/iat"
self.signature["host"] = "iat-niche-api.xfyun.cn"
# date, RFC1123-formatted
now = datetime.datetime.now()
date = format_date_time(mktime(now.timetuple()))
self.signature["date"] = date
# authorizaiton, base64-encoded
signature_origin = "host: " + self.signature["host"] + "\n"
signature_origin += "date: " + self.signature["date"] + "\n"
signature_origin += "GET " + "/v2/iat " + "HTTP/1.1"
signature_sha = hmac.new(
self.APISecret.encode('utf-8'),
signature_origin.encode('utf-8'),
digestmod=hashlib.sha256
).digest()
signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8')
authorizaiton_origin = 'api_key="{}",'.format(self.APIKey) \
+ 'algorithm="hmac-sha256",' \
+ 'headers="host date request-line",' \
+ 'signature="{}"'.format(signature_sha)
authorization = base64.b64encode(authorizaiton_origin.encode('utf-8')).decode(encoding='utf-8')
self.signature["authorization"] = authorization
self.url += '?'
self.url += urlencode(self.signature)
def ffCommand(exe=r'ffmpeg',input=None,output=None,params={}):
'''
生成ffmpeg命令行命令。
exe : ffmpeg, ffprobe
input : test.pcm
可选关键字参数(本脚本使用的部分):
ffprobe:
Key : Example Value
loglevel : quiet, error, fatal, debug, verbose
show_format : ''
ffmpeg:
Key : Example Value
y : '' #直接覆盖输出文件
acodec : pcm_s16le, copy #解码器
f : s16le #文件格式
ac : 1 #通道数
ar : 16000 #采样率
ss : 00:00:00 #开始时间
t : 00:01:00 #持续时间
vn : '' #禁用视频
'''
command = r''
command += exe + r' '
if exe == r'ffprobe':
for key,value in params.items():
command += r'-{} {} '.format(key,value)
command += input
elif exe == r'ffmpeg':
if not output:
raise ValueError('ffmpeg需要输出文件')
command += r'-i {} '.format(input)
for key,value in params.items():
command += r'-{} {} '.format(key,value)
command += output
else:
raise KeyError('程序名错误')
return command
def split(inputFile):
'''
将输入流剪切为时长1min的片段并转码为合适格式
'''
def timeFormatter(duration):
'''
example: 70(s) ==> 00:01:10
'''
second = duration
minute = 0
hour = 0
while second >= 60:
second -= 60
minute += 1
while minute >= 60:
minute -= 60
hour += 1
if hour >= 60:
raise ValueError('stream duration exceeds maximum')
return(r'{}:{}:{}'.format(hour,minute,second))
command = ffCommand(
exe = 'ffprobe',
input = inputFile,
params = {
'loglevel' : 'quiet',
'show_format' : ''
}
)
f = os.popen(command)
pattern = r'duration=(.+)'
for line in f.readlines():
line = line.strip('\n')
print(line)
matchObj = re.match(pattern,line)
if matchObj:
duration = float(matchObj.group(1))
episodeNumber = int(duration // 60)
episodeNumber += 1
res = []
for n in range(episodeNumber):
command = ffCommand(
exe = 'ffmpeg',
input = inputFile,
output = r'./tmp/temp{}.pcm'.format(n),
params = {
'y' : '',
'acodec' : 'pcm_s16le',
'f' : 's16le',
'ac' : '1',
'ar' : '16000',
'ss' : timeFormatter(n*60),
't' : '00:01:00',
'vn' : ''
}
)
f = os.popen(command).read()
#print(f)
res.append(r'./tmp/temp{}.pcm'.format(n))
return res
# WebSocketApp的四个参数on_message, on_error, on_close, on_open
def on_message(ws,message):
'''
收到websocket消息的处理
'''
try:
code = json.loads(message)["code"]
if code == 0:
data = json.loads(message)["data"]["result"]["ws"]
result = ""
for i in data:
for w in i["cw"]:
result += w["w"]
print(result)
else:
errMsg = json.loads(message)["message"]
print('WEBSOCKET MESSAGE : return code {},\n{}'.format(code,errMsg))
except Exception as e:
print("WEBSOCKET MESSAGE: "+e)
def on_error(ws,error):
'''
收到websocket错误的处理
'''
print('WEBSOCKET ERROR : '+error)
def on_close(ws):
'''
收到websocket关闭的处理
'''
#print('WEBSOCKET CLOSE : WebSocket closed.')
pass
def on_open(ws):
'''
收到websocket连接建立的处理
'''
FIRST_FRAME = 0
CONTINUE_FRAME = 1
LAST_FRAME = 2
frameSize = 8000
intervel = 0.04
wsParam = ws.param
def run(*args):
status = FIRST_FRAME
with open(wsParam.AudioFile,'rb') as fp:
while True:
buffer = fp.read(frameSize)
if not buffer:
status = LAST_FRAME
if status == FIRST_FRAME:
request = {
"common" : wsParam.CommonArgs,
"business" : wsParam.BusinessArgs,
"data" : {
"status" : FIRST_FRAME,
"format" : "audio/L16;rate=16000",
"audio" : str(base64.b64encode(buffer),'utf-8'),
"encoding": "raw"
}
}
ws.send(json.dumps(request))
status = CONTINUE_FRAME
elif status == CONTINUE_FRAME:
request = {
"data" : {
"status" : CONTINUE_FRAME,
"format" : "audio/L16;rate=16000",
"audio" : str(base64.b64encode(buffer),'utf-8'),
"encoding": "raw"
}
}
ws.send(json.dumps(request))
elif status == LAST_FRAME:
request = {
"data" : {
"status" : LAST_FRAME,
"format" : "audio/L16;rate=16000",
"audio" : str(base64.b64encode(buffer),'utf-8'),
"encoding": "raw"
}
}
ws.send(json.dumps(request))
time.sleep(1)
break
time.sleep(intervel)
ws.close()
thread.start_new_thread(run,())
def main():
audios = split(STREAMPATH)
for audio in audios:
wsParam = WebSocketParam(
APPID = APPID,
APIKey = APIKEY,
APISecret = APISECRET,
AudioFile = audio,
language = LANGUAGE
)
websocket.enableTrace(False)
ws = websocket.WebSocketApp(
wsParam.url,
on_message = on_message,
on_error = on_error,
on_close = on_close,
on_open = on_open
)
ws.param = wsParam
ws.run_forever()
if __name__ == "__main__":
main()