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
| """
| Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
| Reserved. MIT License (https://opensource.org/licenses/MIT)
|
| 2023-2024 by zhaomingwork@qq.com
| """
|
| # pip install websocket-client
| # apt install ffmpeg
|
|
| import threading
| import traceback
|
| import time
|
|
|
| # class for recognizer in websocket
| class FunasrTools:
| """
| python asr recognizer lib
|
| """
|
| def __init__(
| self
|
|
| ):
| """
|
| """
| try:
|
| if FunasrTools.check_ffmpeg()==False:
| print("pls instal ffmpeg firest, in ubuntu, you can type apt install -y ffmpeg")
| exit(0)
|
|
| except Exception as e:
| print("Exception:", e)
| traceback.print_exc()
|
|
| # check ffmpeg installed
| @staticmethod
| def check_ffmpeg():
| import subprocess
| try:
| subprocess.run(['ffmpeg', '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
| return True
| except FileNotFoundError:
|
| return False
| # use ffmpeg to convert audio to wav
| @staticmethod
| def audio2wav(audiobuf):
| try:
| import os
| import subprocess
| if FunasrTools.check_ffmpeg()==False:
| print("pls instal ffmpeg firest, in ubuntu, you can type apt install -y ffmpeg")
| exit(0)
| return
|
| ffmpeg_target_to_outwav = ["ffmpeg", "-i", '-', "-ac", "1", "-ar", "16000", "-f", "wav", "pipe:1"]
| pipe_to = subprocess.Popen(ffmpeg_target_to_outwav,
| stdin=subprocess.PIPE,
| stdout=subprocess.PIPE,
| stderr=subprocess.PIPE)
| wavbuf, err = pipe_to.communicate(audiobuf)
| if str(err).find("Error")>=0 or str(err).find("Unknown")>=0 or str(err).find("Invalid")>=0:
| print("ffmpeg err",err)
| return None
| return wavbuf
| except Exception as e:
| print("audio2wav",e)
| return None
|
|
|
|
|
|
|