优化发语音
This commit is contained in:
@@ -332,21 +332,23 @@ class MessageMixin(WechatAPIClientBase):
|
||||
voice_byte = f.read()
|
||||
else:
|
||||
raise ValueError("voice should be str, bytes, or path")
|
||||
|
||||
voice_type = 0
|
||||
# get voice duration and b64
|
||||
if format.lower() == "amr":
|
||||
audio = AudioSegment.from_file(BytesIO(voice_byte), format="amr")
|
||||
voice_base64 = base64.b64encode(voice_byte).decode()
|
||||
elif format.lower() == "wave":
|
||||
audio = AudioSegment.from_file(BytesIO(voice_byte), format="wav").set_channels(1)
|
||||
audio = AudioSegment.from_file(BytesIO(voice_byte), format="wav").set_channels(2)
|
||||
audio = audio.set_frame_rate(self._get_closest_frame_rate(audio.frame_rate))
|
||||
voice_base64 = base64.b64encode(
|
||||
await pysilk.async_encode(audio.raw_data, sample_rate=audio.frame_rate)).decode()
|
||||
voice_type = 4
|
||||
elif format.lower() == "mp3":
|
||||
audio = AudioSegment.from_file(BytesIO(voice_byte), format="mp3").set_channels(1)
|
||||
audio = AudioSegment.from_file(BytesIO(voice_byte), format="mp3").set_channels(2)
|
||||
audio = audio.set_frame_rate(self._get_closest_frame_rate(audio.frame_rate))
|
||||
voice_base64 = base64.b64encode(
|
||||
await pysilk.async_encode(audio.raw_data, sample_rate=audio.frame_rate)).decode()
|
||||
voice_type = 4
|
||||
else:
|
||||
raise ValueError("format must be one of amr, wav, mp3")
|
||||
|
||||
@@ -362,7 +364,7 @@ class MessageMixin(WechatAPIClientBase):
|
||||
# }
|
||||
async with aiohttp.ClientSession() as session:
|
||||
json_param = {"Wxid": self.wxid, "ToWxid": wxid, "Base64": voice_base64, "VoiceTime": duration,
|
||||
"Type": format_dict[format]}
|
||||
"Type": voice_type}
|
||||
response = await session.post(f'http://{self.ip}:{self.port}/api/Msg/SendVoice', json=json_param)
|
||||
json_resp = await response.json()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user