Files
Fast-Whisper-MCP-Server/audio_processor.py
BigUncleHomePC 9d22de2ac9 refactor(whisper_server): 重构代码以模块化转录功能
将转录核心逻辑拆分为独立模块(transcriber.py、model_manager.py、audio_processor.py、formatters.py),提升代码可维护性和复用性。删除main.py文件,优化依赖管理并更新requirements.txt和pyproject.toml。
2025-03-22 05:26:17 +08:00

67 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
音频处理模块
负责音频文件的验证和预处理
"""
import os
import logging
from typing import Union, Any
from faster_whisper import decode_audio
# 日志配置
logger = logging.getLogger(__name__)
def validate_audio_file(audio_path: str) -> str:
"""
验证音频文件是否有效
Args:
audio_path: 音频文件路径
Returns:
str: 验证结果,"ok"表示验证通过,否则返回错误信息
"""
# 验证参数
if not os.path.exists(audio_path):
return f"错误: 音频文件不存在: {audio_path}"
# 验证文件格式
supported_formats = [".mp3", ".wav", ".m4a", ".flac", ".ogg", ".aac"]
file_ext = os.path.splitext(audio_path)[1].lower()
if file_ext not in supported_formats:
return f"错误: 不支持的音频格式: {file_ext}。支持的格式: {', '.join(supported_formats)}"
# 验证文件大小
try:
file_size = os.path.getsize(audio_path)
if file_size == 0:
return f"错误: 音频文件为空: {audio_path}"
# 大文件警告超过1GB
if file_size > 1024 * 1024 * 1024:
logger.warning(f"警告: 文件大小超过1GB可能需要较长处理时间: {audio_path}")
except Exception as e:
logger.error(f"检查文件大小失败: {str(e)}")
return f"错误: 检查文件大小失败: {str(e)}"
return "ok"
def process_audio(audio_path: str) -> Union[str, Any]:
"""
处理音频文件,进行解码和预处理
Args:
audio_path: 音频文件路径
Returns:
Union[str, Any]: 处理后的音频数据或原始文件路径
"""
# 尝试使用decode_audio预处理音频以处理更多格式
try:
audio_data = decode_audio(audio_path)
logger.info(f"成功预处理音频: {os.path.basename(audio_path)}")
return audio_data
except Exception as audio_error:
logger.warning(f"音频预处理失败,将直接使用文件路径: {str(audio_error)}")
return audio_path