gpt4 book ai didi

python - 如何以 20 秒的间隔迭代音频文件?

转载 作者:太空宇宙 更新时间:2023-11-04 04:25:45 25 4
gpt4 key购买 nike

我正在尝试使用 SpeechRecognition 转录一个大约 3 分钟长的音频文件,但是,它似乎无法转录超过 20 秒的任何内容。这是我正在使用的代码:

r = sr.Recognizer()

audio = FLAC(output_name +'.' + output_format)
audio_length = audio.info.length

file = sr.AudioFile(output_name +'.' + output_format)

with file as source:
audio = r.record(source, duration = 20)

google = r.recognize_google(audio, language = 'ru-RU' )
print(google)

我如何循环播放它以转录 0 到 20 秒,然后是 20 到 40 秒等等,直到音频文件结束?

我希望尽可能避免将文件拆分为 20 秒长度的单独文件。

最佳答案

所以我想通了。我的错误是没有足够仔细地阅读 SpeechRecognition 模块的文档,但是它们有一个 offset 参数!

count = 0
for audio_path in audio_files:
audio = FLAC(audio_list[count] + '.' + output_format) #specify audio file for length calculation
audio_length = audio.info.length #get length of audio file

#n.b. mutagen module used for calculating audio length

number_of_iterations = int(audio_length/20)

if number_of_iterations == 0:
number_of_iterations = 1

file = sr.AudioFile(audio_list[count] + '.' + output_format)


for i in range(number_of_iterations):
with file as source:
audio = r.record(source, offset = i*20, duration = 20)

google = r.recognize_google(audio, language = 'ru-RU' )
count = count + 1
print(google)

关于python - 如何以 20 秒的间隔迭代音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53540087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com