|
1 | | -"""阿里百炼 TTS 引擎:内置音色列表、_FileCallback 回调类、基于 tts_v2 的音频合成。""" |
| 1 | +"""阿里百炼 TTS 引擎:内置音色列表、_FileCallback 回调类、基于 tts_v2 的音频合成""" |
2 | 2 |
|
3 | 3 | import os |
| 4 | +from pathlib import Path |
4 | 5 | import threading |
5 | 6 | import traceback |
6 | 7 | from typing import Optional |
@@ -230,15 +231,15 @@ class _FileCallback(ResultCallback): # type: ignore[misc] |
230 | 231 | """将百炼 WebSocket 流式音频分片写入目标文件,并在完成时发出事件信号。""" |
231 | 232 |
|
232 | 233 | def __init__(self, output_path: str) -> None: |
233 | | - self.output_path = output_path |
| 234 | + self.output_path = Path(output_path) |
234 | 235 | self.file = None |
235 | 236 | self.error_message: Optional[str] = None |
236 | 237 | self.total_bytes: int = 0 |
237 | 238 | self.events: list[str] = [] |
238 | 239 | self.done_event = threading.Event() |
239 | 240 |
|
240 | 241 | def on_open(self) -> None: |
241 | | - self.file = open(self.output_path, 'wb') |
| 242 | + self.file = self.output_path.open('wb') |
242 | 243 | print(f'[百炼TTS] 连接建立,输出文件:{self.output_path}') |
243 | 244 |
|
244 | 245 | def on_complete(self) -> None: |
@@ -327,7 +328,8 @@ def _call() -> None: |
327 | 328 | raise RuntimeError('百炼 TTS 在 30 秒内未完成,可能是网络超时或服务阻塞') |
328 | 329 | if callback.error_message: |
329 | 330 | raise RuntimeError(f'百炼 TTS 回调报错:{callback.error_message}') |
330 | | - if not os.path.exists(path) or os.path.getsize(path) == 0: |
| 331 | + output_path = Path(path) |
| 332 | + if not output_path.exists() or output_path.stat().st_size == 0: |
331 | 333 | event_tail = callback.events[-3:] if callback.events else [] |
332 | 334 | raise RuntimeError( |
333 | 335 | f'百炼 TTS 输出文件为空;累计字节={callback.total_bytes};最近事件={event_tail}' |
|
0 commit comments