我们有一个中文录音文件.mp3格式或者是.wav格式,如果我们想要提取录音文件中的文字内容,我们可以采用以下方法,不需要使用azure speech api 密钥注册通过离线的方式实现。
1.首先我们先在nuget中下载两个包 naudio 2.2.1、whisper.net 1.7.3
2.另外我们还需要从hugging face网址中下载一个 ggml-medium.bin 文件
3. 代码部分,由于我们whisper模型只支持16khz的语音文件
所以我们要把不同音频格式的文件统一转为16000hz的音频数据文件,如下是具体代码:
using naudio.wave; using system; public class audioresampler { public static void convertto16khz(string inputfile, string outputfile) { // 打开原始音频文件 using (var reader = new wavefilereader(inputfile)) { // 创建目标音频格式 16khz,单声道,16位 var targetformat = new waveformat(16000, 1); // 16000hz, mono, 16-bit // 创建转换流,使用 waveformatconversionstream 进行重采样 using (var conversionstream = new waveformatconversionstream(targetformat, reader)) { // 将转换后的音频数据写入新文件 wavefilewriter.createwavefile(outputfile, conversionstream); console.writeline("文件已转换为 16khz 格式"); } } } } // 使用示例 class program { static void main(string[] args) { string inputfile = @"path_to_input_file.wav"; // 输入文件路径 string outputfile = @"path_to_output_file_16khz.wav"; // 输出文件路径 audioresampler.convertto16khz(inputfile, outputfile); } }
4.接下来是详细的具体代码
public async task analyze() { //模型 string modelfilepath = system.io.path.combine(appdomain.currentdomain.basedirectory, "ggml-medium-q8_0.bin"); // 初始化whisper工厂和处理器 var whisperfactory = whisperfactory.frompath(modelfilepath); var processor = whisperfactory.createbuilder() .withlanguage("zh") // 设置识别的语言为中文 .build(); try { string audiofilename = "path_to_output_file_16khz.wav"; string audiofilepath = system.io.path.combine(appdomain.currentdomain.basedirectory, audiofilename); // 读取音频文件 using var audiostream = file.openread(audiofilepath); // 处理音频文件并输出结果 console.writeline("transcribing audio file..."); await foreach (segmentdata result in processor.processasync(audiostream, default)) { console.writeline($"{result.start}->{result.end}: {result.text}"); } } catch (exception ex) { console.writeline($"an error occurred: {ex.message}"); } console.writeline("press any key to exit..."); }
其中需要注意的是 ggml-medium-q8_0.bin文件的绝对路径,此文件的获取方式上述已说明。
string modelfilepath = system.io.path.combine(appdomain.currentdomain.basedirectory, "ggml-medium-q8_0.bin");
到此这篇关于c#实现中文录音文件转为文本文字的文章就介绍到这了,更多相关c#录音转文本内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论