forked from v3ucn/ASR_TOOLS_SenseVoice_WebUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
short_audio_transcribe_bcut.py
114 lines (72 loc) · 2.34 KB
/
short_audio_transcribe_bcut.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
import os
import argparse
from tqdm import tqdm
import sys
import os
from common.constants import Languages
from common.log import logger
from common.stdout_wrapper import SAFE_STDOUT
from bcut_asr import BcutASR
from bcut_asr.orm import ResultStateEnum
lang2token = {
'zh': "ZH|",
'ja': "JP|",
"en": "EN|",
}
def transcribe_one(audio_path):
asr = BcutASR(audio_path)
asr.upload() # 上传文件
asr.create_task() # 创建任务
# 轮询检查结果
while True:
result = asr.result()
# 判断识别成功
if result.state == ResultStateEnum.COMPLETE:
break
# 解析字幕内容
subtitle = result.parse()
# 判断是否存在字幕
if subtitle.has_data():
text = subtitle.to_txt()
text = repr(text)
text = text.replace("'","")
text = text.replace("\\n",",")
text = text.replace("\\r",",")
print(text)
# 输出srt格式
return text
else:
return "必剪无法识别"
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--language", type=str, default="ja", choices=["ja", "en", "zh"]
)
parser.add_argument("--model_name", type=str, required=True)
parser.add_argument("--input_file", type=str, default="./wavs/")
parser.add_argument("--file_pos", type=str, default="")
args = parser.parse_args()
speaker_name = args.model_name
language = args.language
input_file = args.input_file
if input_file == "":
input_file = "./wavs/"
file_pos = args.file_pos
wav_files = [
f for f in os.listdir(f"{input_file}") if f.endswith(".wav")
]
if language == "ja":
language_id = Languages.JP
elif language == "en":
language_id = Languages.EN
elif language == "zh":
language_id = Languages.ZH
else:
raise ValueError(f"{language} is not supported.")
with open("./esd.list", "w", encoding="utf-8") as f:
for wav_file in tqdm(wav_files, file=SAFE_STDOUT):
file_name = os.path.basename(wav_file)
text = transcribe_one(f"{input_file}"+wav_file)
f.write(file_pos+f"{file_name}|{speaker_name}|{language_id}|{text}\n")
f.flush()
sys.exit(0)