feat(wechatcomapp): add support for splitting long audio files

This commit is contained in:
lanvent
2023-04-27 22:47:50 +08:00
parent 3314b05648
commit 76a81d5360
2 changed files with 31 additions and 3 deletions

View File

@@ -19,7 +19,7 @@ from common.log import logger
from common.singleton import singleton
from common.utils import compress_imgfile, fsize, split_string_by_utf8_length
from config import conf, subscribe_msg
from voice.audio_convert import any_to_amr
from voice.audio_convert import any_to_amr, split_audio
MAX_UTF8_LEN = 2048
@@ -66,8 +66,12 @@ class WechatComAppChannel(ChatChannel):
file_path = reply.content
amr_file = os.path.splitext(file_path)[0] + ".amr"
any_to_amr(file_path, amr_file)
response = self.client.media.upload("voice", open(amr_file, "rb"))
logger.debug("[wechatcom] upload voice response: {}".format(response))
files = split_audio(amr_file, 60000)
if len(files) > 1:
logger.info("[wechatcom] voice too long, split into {} parts".format(len(files)))
for path in files:
response = self.client.media.upload("voice", open(path, "rb"))
logger.debug("[wechatcom] upload voice response: {}".format(response))
except WeChatClientException as e:
logger.error("[wechatcom] upload voice failed: {}".format(e))
return