import requests from pathlib import Path # 配置 API_URL = "http://localhost:7777/transcriptions" SEGMENT_FOLDER = Path("segmented_audio/1762410009_21d79e67") OUTPUT_TXT = "transcription_result.txt" # 获取所有音频文件 audio_files = sorted(SEGMENT_FOLDER.glob("segment_*.wav")) print(f"找到 {len(audio_files)} 个音频片段\n") # 逐个识别 all_texts = [] for audio_file in audio_files: print(f"识别: {audio_file.name} ...", end=" ") with open(audio_file, "rb") as f: files = {"file": (audio_file.name, f, "audio/wav")} response = requests.post(API_URL, files=files) result = response.json() if result["code"] == 200: transcriptions = result["data"]["transcription"] print(f"✓ ({len(transcriptions)} 段)") # 合并该音频片段的所有文本 segment_text = " ".join([trans["content"] for trans in transcriptions if trans["content"]]) if segment_text: all_texts.append(segment_text) else: print(f"✗ 失败: {result['message']}") # 保存到txt文件 with open(OUTPUT_TXT, "w", encoding="utf-8") as f: f.write("\n".join(all_texts)) print(f"\n{'=' * 60}") print(f"✓ 转录完成!") print(f" 总片段数: {len(audio_files)}") print(f" 识别段落: {len(all_texts)}") print(f" 保存文件: {OUTPUT_TXT}") print(f"{'=' * 60}") # 预览前3段 if all_texts: print("\n前3段预览:") for i, text in enumerate(all_texts[:3], 1): print(f"{i}. {text}")