本地离线部署whisper模型进行话音转写,亲测可用

在本地搭建 Whisper 语音转写环境比较简单,以下是详细步骤,适用于 Windows、macOS 和 Linux 系统,其中windows系统亲测可用:

一、基础环境准备

  1. 安装 Python
    确保安装 Python 3.8+:
  2. 验证 Python 安装
    打开命令行(CMD/PowerShell/ 终端),输入:python --version # 或 python3 --version(macOS/Linux),显示版本号即表示安装成功。

二、安装 Whisper

         # 国内镜像加速(可选)

          pip install openai-whisper -i https://pypi.tuna.tsinghua.edu.cn/simple

  1. 安装核心库
    命令行输入以下命令(国内用户可加镜像加速):
    # 基础安装(默认包含轻量模型支持) pip install openai-whisper
  2. 安装音频处理依赖
    Whisper 需要额外工具处理音频格式:Windows:下载并安装 FFmpeg,将 ffmpeg.exe 所在目录添加到系统环境变量 PATH

三、下载 Whisper 模型(可选)

Whisper 会自动下载所需模型,也可提前手动下载(推荐大型模型 large-v3 以获得最佳效果):

# 安装时指定模型(自动下载) pip install "openai-whisper[large-v3]"

模型会保存在以下路径(可手动替换或管理):

  • Windows:C:\Users\你的用户名\.cache\whisper\
  • macOS/Linux:~/.cache/whisper/

四、基本使用方法

1. 命令行直接转写

# 转写音频文件(支持 WAV/MP3/MP4 等格式)

whisper 你的音频文件路径.wav --model large-v3 --language Chinese

# 示例(替换为你的文件路径)

whisper D:\Net_Program\test\whisper-test.wav --model large-v3 --language Chinese

2. 关键参数说明
  • --model:指定模型(tiny/base/small/medium/large-v3,越大精度越高,需求资源越多)
  • --language Chinese:指定语言为中文(避免自动检测错误)
  • --output_dir 输出目录:指定结果保存路径
  • --format txt:输出格式(支持 txt/srt/vtt 等)

五、Python 脚本调用(进阶)

import whisper
import os
import pathlib
import subprocess
from zhconv import convert  # 用于繁转简

def check_ffmpeg():
    """检查FFmpeg是否安装并配置正确"""
    try:
        subprocess.run(
            ["ffmpeg", "-version"],
            check=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True
        )
        return True
    except FileNotFoundError:
        print("错误:未找到FFmpeg工具,请先安装并配置环境变量")
        return False
    except Exception as e:
        print(f"FFmpeg检查失败:{str(e)}")
        return False

def transcribe_audio(audio_path, model_name="large-v3", language="Chinese"):
    # 检查FFmpeg
    if not check_ffmpeg():
        return None

    # 验证音频文件路径
    audio_path = str(pathlib.Path(audio_path).resolve())
    
    if not os.path.exists(audio_path):
        print(f"错误:音频文件不存在 '{audio_path}'")
        return None
    
    if not os.path.isfile(audio_path):
        print(f"错误:'{audio_path}' 不是有效的文件")
        return None

    # 加载模型并转写
    try:
        print(f"开始加载模型 {model_name}...")
        model = whisper.load_model(model_name, device="cpu")
        
        print(f"开始转写文件:{audio_path}")
        # 关键设置:明确指定中文,并关闭自动语言检测
        result = model.transcribe(
            audio=audio_path,
            language="Chinese",  # 强制指定中文
            verbose=True,
            fp16=False,
            initial_prompt="请用简体中文转写,不要使用繁体中文。"  # 提示模型使用简体
        )
        
        # 强制将结果转换为简体中文(双重保险)
        simplified_text = convert(result["text"], 'zh-cn')
        
        # 保存结果
        output_dir = "whisper_results"
        os.makedirs(output_dir, exist_ok=True)
        audio_name = os.path.splitext(os.path.basename(audio_path))[0]
        output_path = os.path.join(output_dir, f"{audio_name}_transcript.txt")
        
        with open(output_path, "w", encoding="utf-8") as f:
            f.write(simplified_text)
        
        print(f"\n✅ 转写完成(已转换为简体中文),结果保存至:{output_path}")
        return simplified_text
        
    except Exception as e:
        print(f"转写过程出错:{str(e)}")
        return None

if __name__ == "__main__":
    # 安装繁转简依赖(首次运行需要)
    try:
        import zhconv
    except ImportError:
        print("正在安装繁转简依赖...")
        subprocess.run(["pip", "install", "zhconv"], check=True)
        import zhconv

    # 替换为你的音频文件路径
    audio_file = r"D:\Net_Program\test\whisper-test.wav"
    transcribe_audio(audio_file)
    

六、常见问题解决

  1. 内存不足
    • 若提示 OutOfMemoryError,换用更小的模型(如 medium 或 small
    • 关闭其他占用内存的程序(large-v3 建议至少 16GB 内存)
  2. 音频格式错误
    • 用 FFmpeg 转换格式:ffmpeg -i 输入文件.mp3 -ar 16000 -ac 1 输出文件.wav(转为 16kHz 单声道 WAV)
  3. 模型下载慢
    • 手动下载模型文件(可在 Hugging Face 找到),放入 .cache/whisper/ 目录

通过以上步骤,你可以在本地搭建一个稳定的 Whisper 转写环境,无需依赖 Ollama,直接调用模型进行语音转写。如果追求更高精度,优先使用 large-v3 模型;若注重速度或资源有限,可选择 small 或 base 模型。

Read more

2025 嵌入式 AI IDE 全面对比:Trae、Copilot、Windsurf、Cursor 谁最值得个人开发者入手?

文章目录 * 2025 嵌入式 AI IDE 全面对比:Trae、Copilot、Windsurf、Cursor 谁最值得个人开发者入手? * 一、先给结论(个人开发者视角) * 二、2025 年 9 月最新价格与免费额度 * 三、横向体验对比(2025-11) * 1. 模型与响应 * 2. 项目理解力 * 3. 隐私与离线能力 * 四、怎么选?一句话总结 * 五、官方链接(清晰明了) * 六、结语:AI IDE 2025 的趋势 * 七、AI IDE 的底层工作原理:编辑器为什么突然变聪明了? * 1. 解析层:把你的项目拆得比你自己还清楚 * 2. 索引层:

LLaMAFactory、ModelScope 大模型微调实战(下)

LLaMAFactory、ModelScope 大模型微调实战(下)

一、前言 上次简单介绍了下 LLaMAFactory、ModelScope的微调,今天再来总结下如何部署已经微调好的大模型。 直通车→→→ https://blog.ZEEKLOG.net/tadexinnian/article/details/159154443 本次演示基于魔搭社区(https://www.modelscope.cn/my/mynotebook) 二、将模型转换为gguf 2.1 克隆llama.cpp 并安装环境依赖 -- 进入根目录 cd /mnt/workspace -- 需要用 llama.cpp 仓库的 convert_hf_to_gguf.py 脚本来转换 git clone https://github.com/

找回 Edge 边栏中消失的 Copilot 图标

Edge 边栏的 Copilot 能根据网页内容增强回复,相当于内置了RAG,而且能不限次数使用GPT-5,非常方便。笔者有次打开 Edge 浏览器时发现边栏的Copilot图标消失了,探索了一些方法后终于找到解决方案,以下: 1. win+R 打开运行,输入 powershell 打开,复制以下正则表达式全文到powershell 命令窗口回车运行即可。命令窗口出现“✅ 已将 variations_country 设置为 US。已重新启动 Microsoft Edge”代表已经成功。 & { # 关闭所有 Edge 进程 Get-Process | Where-Object { $_.ProcessName -like "msedge*" } | Stop-Process -Force -ErrorAction SilentlyContinue Start-Sleep -Seconds 3 $localState

一文看懂:AI编程工具深度对比:Cursor、Copilot、Trae与Claude Code

一文看懂:AI编程工具深度对比:Cursor、Copilot、Trae与Claude Code

AI编程工具深度对比:Cursor、Copilot、Trae与Claude Code 引言 在人工智能技术蓬勃发展的今天,AI编程工具已成为开发者提高效率的重要助手。从早期的代码补全插件到如今能够理解整个代码库的智能助手,AI编程工具正在不断进化。本文将对当前主流的AI编程工具——Cursor、GitHub Copilot、Trae和Claude Code进行全面对比,帮助开发者选择最适合自己的工具。 主流AI编程工具概述 Cursor Cursor是一款基于VSCode的AI驱动代码编辑器,它最大的特点是能够理解整个代码库的上下文,提供智能的代码补全和重构建议。Cursor默认使用Claude-3.5-Sonnet模型,即使是OpenAI投资的公司,也选择了Claude模型作为默认选项,这足以说明其在代码生成领域的优势。 GitHub Copilot GitHub Copilot是由GitHub与OpenAI合作开发的AI编码助手,集成在VSCode、Visual Studio等主流编辑器中。它基于OpenAI的模型,能够根据注释和上下文自动生成代码,是AI编程工具