当前位置: 代码网 > it编程>编程语言>Asp.net > C#实现中文录音文件转为文本文字

C#实现中文录音文件转为文本文字

2024年12月26日 Asp.net 我要评论
我们有一个中文录音文件.mp3格式或者是.wav格式,如果我们想要提取录音文件中的文字内容,我们可以采用以下方法,不需要使用azure speech api 密钥注册通过离线的方式实现。1.首先我们先

我们有一个中文录音文件.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#录音转文本内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com