import requests from datetime import datetime def call_asr_api(): api_url = "http://localhost:7777/transcriptions" audio_file = r"G:\Work_data\workstation\Forwork-voice-txt\FireRedASR\new_pyannote\可以.mp3" # 生成输出文件名(使用时间戳) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") output_file = f"transcription_{timestamp}.txt" try: with open(audio_file, 'rb') as f: files = {'file': f} response = requests.post(api_url, files=files) result = response.json() print("完整响应:", result) # 检查响应状态 if result.get('code') == 200: transcription_data = result['data']['transcription'] statistics = result['data']['statistics'] # 保存到txt文件 with open(output_file, 'w', encoding='utf-8') as txt_file: for segment in transcription_data: content = segment['content'] txt_file.write(content + '\n') print(f"\n转录结果已保存到: {output_file}") print(f"总片段数: {statistics['total_segments']}") print(f"处理时间: {statistics['processing_time']}秒") # 控制台仍然显示详细信息 print(f"\n转录结果 ({len(transcription_data)} 个片段):") print("-" * 50) for segment in transcription_data: start_sec = segment['start'] / 1000 end_sec = segment['end'] / 1000 content = segment['content'] seg_type = segment.get('segment_type', 'unknown') confidence = segment.get('confidence', 0) print(f"[{start_sec:.1f}s - {end_sec:.1f}s] ({seg_type}) 置信度:{confidence:.3f}") print(f" {content}") print() else: print(f"API调用失败: {result.get('message')}") except FileNotFoundError: print(f"音频文件不存在: {audio_file}") except requests.exceptions.RequestException as e: print(f"网络请求失败: {e}") except Exception as e: print(f"发生错误: {e}") if __name__ == "__main__": call_asr_api()