我有一个大的音频文件,我想得到转录。为此,我选择了基于沉默的转换,即根据句子之间的沉默将音频文件分割成若干块。然而,即使是一个短的音频文件,这也需要比预期更长的时间。
from pydub import AudioSegment
from pydub.silence import split_on_silence
voice = AudioSegment.from_wav(path) #path to audio file
chunks = split_on_silence(voice, min_silence_len=500, silence_thresh=voice.dBFS-14, keep_silence=500,)
为了尝试更快地处理这些块,我尝试使用一个多线程的循环,如图所示
n_threads = len(chunks)
thread_list = []
for thr in range(n_threads):
thread = Thread(target = threaded_process, args=(chunks[thr],))
thread_list.append(thread)
thread_list[thr].start()
for thread in thread_list:
thread.join()
函数'threaded_process'应该执行语音到文本的转换。
def threaded_process(chunks):
fh = open("recognized.txt", "w+")
i = 0
for chunk in chunks:
chunk_silent = AudioSegment.silent(duration = 10)
audio_chunk = chunk_silent + chunk + chunk_silent
print("saving chunk{0}.wav".format(i))
audio_chunk.export("./chunk{0}.wav".format(i), bitrate ='192k', format ="wav")
file = 'chunk'+str(i)+'.wav'
print("Processing chunk "+str(i))
rec = audio_to_text(file) #Another function which actually does the Speech to text conversion(IBM Watson SpeechToText API)
if rec == "Error5487":
return "Error5487E"
fh.write(rec+" ")
os.remove(file)
i += 1
fh.close()
但转换是使用先前的方法完成的,没有使用多线程。
我还收到这样的消息--
[WinError 32] 进程无法访问该文件,因为它正被另一个进程使用。'chunk0.wav'
为什么会出现这种情况?