From 685c0a106a6a6c57a073aee7b31e453f1f96f9ef Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sat, 25 Jan 2025 20:18:40 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20use=20pysilk=20instead=20of=20pilk=20?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E6=9E=84=E5=BB=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/utils/tencent_record_helper.py | 46 +++++++++++++-------- requirements.txt | 2 +- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/astrbot/core/utils/tencent_record_helper.py b/astrbot/core/utils/tencent_record_helper.py index 386b9b0ea..897e57906 100644 --- a/astrbot/core/utils/tencent_record_helper.py +++ b/astrbot/core/utils/tencent_record_helper.py @@ -2,29 +2,41 @@ import wave from io import BytesIO async def tencent_silk_to_wav(silk_path: str, output_path: str) -> str: - import pilk + import pysilk with open(silk_path, "rb") as f: - pcm_path = f"{output_path}.pcm" - pilk.decode(silk_path, pcm_path) + input_data = f.read() + if input_data.startswith(b'\x02'): + input_data = input_data[1:] + input_io = BytesIO(input_data) + output_io = BytesIO() + pysilk.decode(input_io, output_io, 24000) + output_io.seek(0) + with wave.open(output_path, 'wb') as wav: + wav.setnchannels(1) + wav.setsampwidth(2) + wav.setframerate(24000) + wav.writeframes(output_io.read()) - with open(pcm_path, "rb") as pcm: - with wave.open(output_path, 'wb') as wav: - wav.setnchannels(1) - wav.setsampwidth(2) - wav.setframerate(24000) - wav.writeframes(pcm.read()) - return output_path async def wav_to_tencent_silk(wav_path: str, output_path: str) -> int: '''返回 duration''' - import pilk - - # wav to pcm + import pysilk + with wave.open(wav_path, 'rb') as wav: - pcm_path = f"{wav_path}.pcm" - with open(pcm_path, "wb") as f: - f.write(wav.readframes(wav.getnframes())) + wav_data = wav.readframes(wav.getnframes()) + wav_data = BytesIO(wav_data) + output_io = BytesIO() + pysilk.encode(wav_data, output_io, 24000, 24000) + output_io.seek(0) - return pilk.encode(pcm_path, output_path, pcm_rate=24000, tencent=True) \ No newline at end of file + # 在首字节添加 \x02,去除结尾的\xff\xff + silk_data = output_io.read() + silk_data_with_prefix = b'\x02' + silk_data[:-2] + + # return BytesIO(silk_data_with_prefix) + with open(output_path, "wb") as f: + f.write(silk_data_with_prefix) + + return 0 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 5a3e75ee1..432d5bb19 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,4 +17,4 @@ pyjwt apscheduler docstring_parser aiodocker -pilk \ No newline at end of file +silk-python \ No newline at end of file