【C#语音文字互转】C#语音转文字(方法一)

Whisper.NET开源项目:https://github.com/sandrohanea/whisper.net/tree/main

一. 环境准备

在VS中安装 Whisper.net,在NuGet包管理器控制台中运行以下命令:

Install-Package Whisper.net
Install-Package Whisper.net.Runtime

其中运行时包 Whisper.net.Runtime 包含本机whisper.cpp库,它是运行 Whisper.net 所必需的。

huggingface下载预训练的Ggml模型。
中等模型及以上效果较好,模型越大越消耗性能

二. 项目示例

准备一段wav格式的音频,运行下面示例

using System;
using System.IO;
using Whisper.net;

namespace ConsoleWhisperTranscription
{
    class Program
    {
        static async Task Main(string[] args)
        {

            // 检查命令行参数中是否包含音频文件路径
            if (args.Length == 0)
            {
                Console.WriteLine("Please provide the path to the audio file.");
                return;
            }

            string audioFilePath = args[0]; // 从命令行参数获取音频文件路径

            // 确保文件存在
            if (!File.Exists(audioFilePath))
            {
                Console.WriteLine("The audio file does not exist.");
                return;
            }

            // 初始化Whisper工厂和处理器
            var whisperFactory = WhisperFactory.FromPath("C:\\Users\\26366\\source\\repos\\whisperDemo\\whisperDemo\\ggml-medium.bin");
            var processor = whisperFactory.CreateBuilder()
                .WithLanguage("zh") // 设置识别的语言为中文
                .Build();

            try
            {
                // 读取音频文件
                using var audioStream = File.OpenRead(audioFilePath);

                // 处理音频文件并输出结果
                Console.WriteLine("Transcribing audio file...");
                await foreach (var result in processor.ProcessAsync(audioStream, default))
                {
                    Console.WriteLine($"{result.Start}->{result.End}: {result.Text}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }


    }
}

效果如下:
在这里插入图片描述

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值