|
14 | 14 | from funasr import AutoModel
|
15 | 15 | from funasr.utils.postprocess_utils import rich_transcription_postprocess
|
16 | 16 |
|
| 17 | +import struct |
| 18 | + |
17 | 19 | TAG = __name__
|
18 | 20 | logger = setup_logging()
|
19 | 21 |
|
@@ -63,19 +65,40 @@ def __init__(self, config: dict, delete_audio_file: bool):
|
63 | 65 | )
|
64 | 66 |
|
65 | 67 | def save_audio_to_file(self, opus_data: List[bytes], session_id: str) -> str:
|
66 |
| - """将Opus音频数据解码并保存为WAV文件和Opus文件""" |
| 68 | + """将Opus音频数据解码并保存为WAV文件和标准的Ogg Opus文件""" |
67 | 69 | base_name = f"asr_{session_id}_{uuid.uuid4()}"
|
68 | 70 | wav_path = os.path.join(self.output_dir, f"{base_name}.wav")
|
69 | 71 | opus_path = os.path.join(self.output_dir, f"{base_name}.opus")
|
70 | 72 |
|
71 | 73 | # 保存标准的Ogg Opus文件
|
72 | 74 | try:
|
73 |
| - with pyogg.OggOpusWriter(opus_path, sample_rate=16000, channels=1) as writer: |
| 75 | + with open(opus_path, 'wb') as f: |
| 76 | + # Ogg header |
| 77 | + f.write(b'OggS\x00') # 捕获模式 |
| 78 | + f.write(b'\x02') # 版本 |
| 79 | + f.write(b'\x00') # header type |
| 80 | + |
| 81 | + # Opus header |
| 82 | + header = bytearray() |
| 83 | + header.extend(b'OpusHead') # Magic signature |
| 84 | + header.extend([1]) # 版本 |
| 85 | + header.extend([1]) # 声道数 |
| 86 | + header.extend(struct.pack('<H', 0)) # 预跳过采样数 |
| 87 | + header.extend(struct.pack('<I', 16000)) # 采样率 |
| 88 | + header.extend(struct.pack('<H', 0)) # 输出增益 |
| 89 | + |
| 90 | + # 写入头部长度 |
| 91 | + f.write(struct.pack('<I', len(header))) |
| 92 | + f.write(header) |
| 93 | + |
| 94 | + # 写入音频数据 |
74 | 95 | for packet in opus_data:
|
75 |
| - writer.write(packet) |
76 |
| - logger.bind(tag=TAG).info(f"已保存Opus文件: {opus_path}") |
| 96 | + f.write(struct.pack('<I', len(packet))) # 包长度 |
| 97 | + f.write(packet) # 包数据 |
| 98 | + |
| 99 | + logger.bind(tag=TAG).info(f"已保存Ogg Opus文件: {opus_path}") |
77 | 100 | except Exception as e:
|
78 |
| - logger.bind(tag=TAG).error(f"保存Opus文件失败: {e}", exc_info=True) |
| 101 | + logger.bind(tag=TAG).error(f"保存Ogg Opus文件失败: {e}", exc_info=True) |
79 | 102 |
|
80 | 103 | # 保存WAV文件的现有逻辑
|
81 | 104 | decoder = opuslib_next.Decoder(16000, 1)
|
|
0 commit comments