首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python中的vosk :获取音频文件中转录文本的位置

python中的vosk :获取音频文件中转录文本的位置
EN

Stack Overflow用户
提问于 2021-10-11 15:57:24
回答 1查看 490关注 0票数 2

使用一个与Vosk存储库中的test_ffmpeg.py非常相似的文件,我正在探索从音频文件中可以获得哪些文本信息。

下面是我正在使用的整个脚本的代码。

代码语言:javascript
运行
复制
#!/usr/bin/env python3

from vosk import Model, KaldiRecognizer, SetLogLevel
import sys
import os
import wave
import subprocess
import json

SetLogLevel(0)

if not os.path.exists("model"):
    print ("Please download the model from https://alphacephei.com/vosk/models and unpack as 'model' in the current folder.")
    exit (1)

sample_rate=16000
model = Model("model")
rec = KaldiRecognizer(model, sample_rate)

process = subprocess.Popen(['ffmpeg', '-loglevel', 'quiet', '-i',
                            sys.argv[1],
                            '-ar', str(sample_rate) , '-ac', '1', '-f', 's16le', '-'],
                            stdout=subprocess.PIPE)

file = open(sys.argv[1]+".txt","w+")

while True:
    data = process.stdout.read(4000)
    if len(data) == 0:
        break
    if rec.AcceptWaveform(data):
        file.write(json.loads(rec.Result())['text']+"\n\n")
        #print(rec.Result())
    #else:
        #print(rec.PartialResult())
#print(json.loads(rec.Result())['text'])
file.write(json.loads(rec.Result())['text'])
file.close()

这个示例运行得很好,但是,我在rec.PartialResult()和rec.Result()中唯一能找到的返回结果是一个字符串字典。有没有一种方法可以查询KaldiRecognizer在音频文件中找到单个单词的时间?

当我输入这段代码时,我已经在想,详细说明结果,并检测与当前示例相比部分结果中的变化将会得到我想要的结果,但我在这里坚持这一点,以防它已经实现。

EN

Stack Overflow用户

发布于 2021-10-11 23:52:10

经过一些测试,很明显,相对于定义的采样率(16000),ffmpeg的输出似乎足够稳定,4000个字节的读取结果是8秒的8%。我在while循环中创建了一个计数器,并根据采样率将其除以一个常量。如果您将参数更改为ffmpeg,它可能会抛出这一点。

我使用了一些非常古老的字符串比较,只有当部分结果改变时才打印,并且只包含添加的新字符。

代码语言:javascript
运行
复制
counter = 0
countinc = 2000/sample_rate
lastPR = ""
thisPR = ""
while True:
    data = process.stdout.read(4000)
    counter += 1
    if len(data) == 0:
        break
    rec.AcceptWaveform(data)
    thisPR = json.loads(rec.PartialResult())['partial']
    if lastPR != thisPR:
        print(counter*countinc,thisPR[len(lastPR):len(thisPR)])
        lastPR = thisPR
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69529170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档