""" 完整测试 DashScope ASR 批量识别 按照官方文档要求测试 """ import os import sys import time import logging # 添加 lover 目录到路径 sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'lover')) from lover.config import settings from lover.oss_utils import upload_audio_file, delete_audio_file import dashscope from dashscope.audio.asr import Transcription logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def create_test_audio(): """创建一个简单的测试音频(PCM 格式)""" # 生成 1 秒的 16kHz 单声道 PCM 数据 # 简单的正弦波 import struct import math sample_rate = 16000 duration = 2 # 2 秒 frequency = 440 # A4 音符 samples = [] for i in range(sample_rate * duration): # 生成正弦波 value = int(32767 * 0.3 * math.sin(2 * math.pi * frequency * i / sample_rate)) samples.append(struct.pack('= max_wait: print(f"\n⏰ 等待超时({max_wait}秒)") return False except Exception as e: print(f"❌ ASR 调用失败: {e}") import traceback traceback.print_exc() return False finally: # 清理 OSS 文件 print(f"\n🗑️ 清理 OSS 文件...") try: delete_audio_file(file_url) print(f"✅ 文件已删除") except Exception as e: print(f"⚠️ 删除失败: {e}") print(f"\n" + "=" * 60) print("🎉 DashScope ASR 测试完成!") print("=" * 60) print(f"\n📚 官方文档要求总结:") print(f" 1. ✅ 文件必须通过 HTTPS URL 访问") print(f" 2. ✅ 支持的格式: WAV, MP3, PCM 等") print(f" 3. ✅ 推荐采样率: 16kHz") print(f" 4. ✅ 推荐声道: 单声道") print(f" 5. ⚠️ 音频必须包含有效语音内容") return True if __name__ == "__main__": success = test_dashscope_asr() sys.exit(0 if success else 1)