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

Soh*_*pta 8 .net c# 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

key*_*rdP 13

尝试循环阅读.

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();
Run Code Online (Sandbox Code Playgroud)

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

  • 是的,这可行。我还编辑了您的答案,并补充说,如果OP使用WinForms / WPF,则他应在单独的线程中运行代码,因为否则会阻塞UI线程。 (2认同)