我正在尝试使用 .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/