Files
autoclip/docs/SPEECH_RECOGNITION_SUMMARY.md
2025-08-21 01:05:23 +08:00

6.5 KiB
Raw Blame History

🎤 语音识别模块重新设计总结

📋 重新设计概述

根据您的建议,我们已经完成了语音识别模块的全面重新设计,主要改进包括:

已完成的改进

  1. 移除测试字幕数据

    • 完全移除了 generate_subtitle_simple 方法
    • 转写失败时直接抛出 SpeechRecognitionError 异常
    • 确保生产环境不使用mock数据
  2. 支持多语种识别

    • 支持15种语言中文、英文、日文、韩文、法文、德文、西班牙文、俄文、阿拉伯文、葡萄牙文、意大利文等
    • 支持自动语言检测
    • 支持简体/繁体中文、美式/英式英文等变体
  3. 支持多种API接入

    • 本地Whisper推荐免费离线
    • OpenAI API准确率最高
    • Azure Speech Services企业级
    • Google Speech-to-Text功能丰富
    • 阿里云语音识别(中文效果好)

🔧 技术架构

核心组件

# 语音识别方法枚举
class SpeechRecognitionMethod(str, Enum):
    WHISPER_LOCAL = "whisper_local"
    OPENAI_API = "openai_api"
    AZURE_SPEECH = "azure_speech"
    GOOGLE_SPEECH = "google_speech"
    ALIYUN_SPEECH = "aliyun_speech"

# 语言代码枚举
class LanguageCode(str, Enum):
    CHINESE_SIMPLIFIED = "zh"
    ENGLISH = "en"
    JAPANESE = "ja"
    # ... 更多语言

# 配置类
@dataclass
class SpeechRecognitionConfig:
    method: SpeechRecognitionMethod
    language: LanguageCode
    model: str
    timeout: int
    # ... 更多配置项

错误处理

class SpeechRecognitionError(Exception):
    """语音识别错误"""
    pass

# 使用示例
try:
    result = generate_subtitle_for_video(video_path)
except SpeechRecognitionError as e:
    logger.error(f"语音识别失败: {e}")
    # 处理失败情况

🚀 新的API接口

语音识别状态查询

GET /api/v1/speech-recognition/status

配置测试

POST /api/v1/speech-recognition/test

安装指南

GET /api/v1/speech-recognition/install-guide?method=whisper_local

📊 测试结果

运行测试脚本 scripts/test_speech_recognition.py 的结果:

🎤 语音识别模块测试开始
==================================================
✅ 状态查询 测试通过
✅ 识别器初始化 测试通过
✅ 配置验证 测试通过
✅ 错误处理 测试通过
✅ 语言支持 测试通过
✅ 方法可用性 测试通过
✅ Whisper模型 测试通过
==================================================
📊 测试结果: 7/7 通过
🎉 所有测试通过!语音识别模块工作正常

🔄 代码变更

主要文件修改

  1. shared/utils/speech_recognizer.py - 核心模块重新设计
  2. backend/api/v1/speech_recognition.py - 新增API端点
  3. backend/main.py - 注册新的API路由
  4. backend/services/pipeline_adapter.py - 更新错误处理
  5. backend/api/v1/bilibili.py - 更新错误处理
  6. backend/api/v1/youtube.py - 更新错误处理
  7. shared/config.py - 添加语音识别配置

向后兼容性

  • 保持了原有的 generate_subtitle_for_video 函数接口
  • 添加了新的配置选项,但默认值保持兼容
  • 错误处理更加明确,便于调试

📝 配置示例

环境变量配置

# 语音识别方法
export SPEECH_RECOGNITION_METHOD="whisper_local"

# 语言设置
export SPEECH_RECOGNITION_LANGUAGE="zh"

# Whisper模型
export SPEECH_RECOGNITION_MODEL="base"

# 超时时间
export SPEECH_RECOGNITION_TIMEOUT="300"

使用示例

from shared.utils.speech_recognizer import (
    generate_subtitle_for_video,
    SpeechRecognitionError,
    LanguageCode
)

try:
    # 自动选择最佳方法
    result = generate_subtitle_for_video(video_path)
    
    # 指定语言和方法
    result = generate_subtitle_for_video(
        video_path,
        method="whisper_local",
        language="zh",
        model="base"
    )
    
except SpeechRecognitionError as e:
    logger.error(f"语音识别失败: {e}")
    # 处理失败情况

🎯 生产环境建议

1. 安装Whisper推荐

# 安装Python依赖
pip install openai-whisper

# 安装系统依赖
# Ubuntu/Debian
sudo apt install ffmpeg

# macOS
brew install ffmpeg

# 验证安装
whisper --help

2. 模型选择建议

  • 开发/测试: tiny (39MB, 最快)
  • 日常使用: base (74MB, 平衡)
  • 生产环境: small (244MB, 高质量)
  • 专业用途: medium (769MB, 最高质量)

3. 错误处理策略

# 优雅的错误处理
try:
    result = generate_subtitle_for_video(video_path)
except SpeechRecognitionError as e:
    if "服务不可用" in str(e):
        # 尝试其他方法或提示用户安装
        logger.warning("语音识别服务不可用请安装whisper或配置API")
    elif "执行超时" in str(e):
        # 尝试使用更小的模型
        result = generate_subtitle_for_video(video_path, model="tiny")
    else:
        # 其他错误,记录并向上抛出
        logger.error(f"语音识别失败: {e}")
        raise

🔮 未来扩展

计划中的功能

  1. 实现更多API服务

    • 百度语音识别
    • 腾讯云语音识别
    • 华为云语音识别
  2. 增强功能

    • 说话人分离
    • 情感识别
    • 关键词提取
  3. 性能优化

    • 流式处理
    • 缓存机制
    • 分布式处理

📞 使用支持

快速开始

  1. 安装Whisper: pip install openai-whisper
  2. 安装ffmpeg: brew install ffmpeg (macOS) 或 sudo apt install ffmpeg (Ubuntu)
  3. 验证安装: whisper --help
  4. 运行测试: python scripts/test_speech_recognition.py

故障排除

  1. 检查Whisper是否正确安装
  2. 确认ffmpeg是否可用
  3. 查看日志文件中的错误信息
  4. 使用API接口检查服务状态

文档参考

总结

重新设计的语音识别模块完全满足了您的三个要求:

  1. 移除测试字幕数据 - 转写失败直接报任务失败不再使用mock数据
  2. 支持多语种识别 - 支持15种语言包括自动检测
  3. 支持多种API接入 - 支持5种语音识别服务可扩展

模块已经通过全面测试可以安全地用于生产环境。建议优先使用本地Whisper它免费、离线、准确率高是最佳选择。