-
Notifications
You must be signed in to change notification settings - Fork 18
/
short_audio_transcribe_bcut.py
137 lines (87 loc) · 3.01 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
import whisper
import torch
import re
device = "cuda:0" if torch.cuda.is_available() else "cpu"
model = whisper.load_model("medium",download_root="./whisper_model/")
lang2token = {
'zh': "ZH|",
'ja': "JP|",
"en": "EN|",
}
def transcribe_one(audio_path):
audio = whisper.load_audio(audio_path)
audio = whisper.pad_or_trim(audio)
mel = whisper.log_mel_spectrogram(audio).to(model.device)
_, probs = model.detect_language(mel)
language = max(probs, key=probs.get)
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,language
else:
return "必剪无法识别",language
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")
]
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)
# 使用正则表达式提取'deedee'
match = re.search(r'(^.*?)_.*?(\..*?$)', wav_file)
if match:
extracted_name = match.group(1) + match.group(2)
else:
print("No match found")
extracted_name = "sample"
text,lang = transcribe_one(f"{input_file}"+wav_file)
if lang == "ja":
language_id = "JA"
elif lang == "en":
language_id = "EN"
elif lang == "zh":
language_id = "ZH"
f.write(file_pos+f"{file_name}|{extracted_name.replace('.wav','')}|{language_id}|{text}\n")
f.flush()
sys.exit(0)