c# - 使用 System.Speech 将 mp3 文件转换为文本

标签 c# .net speech-recognition speech-to-text

我正在尝试使用 .net 中的语音识别来识别 mp3 文件中播客的语音并将结果作为字符串。我看到的所有示例都与使用麦克风有关,但我不想使用麦克风并提供示例 mp3 文件作为我的音频源。谁能给我指出任何资源或发布示例。

编辑 -

我将音频文件转换为 wav 文件并在其上尝试了这段代码。但它只提取前 68 个单词。

public class MyRecognizer {
    public string ReadAudio() {
        SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
        Grammar gr = new DictationGrammar();
        sre.LoadGrammar(gr);
        sre.SetInputToWaveFile("C:\\Users\\Soham Dasgupta\\Downloads\\Podcasts\\Engadget_Podcast_353.wav");
        sre.BabbleTimeout = new TimeSpan(Int32.MaxValue);
        sre.InitialSilenceTimeout = new TimeSpan(Int32.MaxValue);
        sre.EndSilenceTimeout = new TimeSpan(100000000);
        sre.EndSilenceTimeoutAmbiguous = new TimeSpan(100000000);
        RecognitionResult result = sre.Recognize(new TimeSpan(Int32.MaxValue));
        return result.Text;
    }
}

最佳答案

尝试循环阅读它。

SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
Grammar gr = new DictationGrammar();
sre.LoadGrammar(gr);
sre.SetInputToWaveFile("C:\\Users\\Soham Dasgupta\\Downloads\\Podcasts\\Engadget_Podcast_353.wav");
sre.BabbleTimeout = new TimeSpan(Int32.MaxValue);
sre.InitialSilenceTimeout = new TimeSpan(Int32.MaxValue);
sre.EndSilenceTimeout = new TimeSpan(100000000);
sre.EndSilenceTimeoutAmbiguous = new TimeSpan(100000000); 

StringBuilder sb = new StringBuilder();
while (true)
{
    try
    {
        var recText = sre.Recognize();
        if (recText == null)
        {               
            break;
        }

        sb.Append(recText.Text);
    }
    catch (Exception ex)
    {   
        //handle exception      
        //...

        break;
    }
}
return sb.ToString();

如果您有 Windows 窗体或 WPF 应用程序,请在单独的线程中运行此代码,否则它会阻塞 UI 线程。

关于c# - 使用 System.Speech 将 mp3 文件转换为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17895933/

相关文章:

cmd - HTK 此处问题

c# - 如何使用 Entity Framework 获取单个列?

c# - 通过 mvc 转发器发布值

c# - 什么是 JScript 区分大小写规则?

javascript - 语音识别如何识别 5 和 5 之间的差异?

ios - 将音频保存到文件时是否可以使用 SFSpeechAudioBufferRecognitionRequest ?

c# - MongoDB中的默认ObjectId

c# - 以特定顺序编写 XML 属性和命名空间声明

c# - 模拟 Web 服务调用时的测试类型

c# - 在 C# 中捕获网络流量