瀏覽代碼

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

master
lanvent 1 年之前
父節點
當前提交
76a81d5360
共有 2 個檔案被更改,包括 31 行新增3 行删除
  1. +7
    -3
      channel/wechatcom/wechatcomapp_channel.py
  2. +24
    -0
      voice/audio_convert.py

+ 7
- 3
channel/wechatcom/wechatcomapp_channel.py 查看文件

@@ -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


+ 24
- 0
voice/audio_convert.py 查看文件

@@ -92,6 +92,7 @@ def any_to_amr(any_path, amr_path):
audio = AudioSegment.from_file(any_path)
audio = audio.set_frame_rate(8000) # only support 8000
audio.export(amr_path, format="amr")
return audio.duration_seconds * 1000


def sil_to_wav(silk_path, wav_path, rate: int = 24000):
@@ -101,3 +102,26 @@ def sil_to_wav(silk_path, wav_path, rate: int = 24000):
wav_data = pysilk.decode_file(silk_path, to_wav=True, sample_rate=rate)
with open(wav_path, "wb") as f:
f.write(wav_data)


def split_audio(file_path, max_segment_length_ms=60000):
"""
分割音频文件
"""
audio = AudioSegment.from_file(file_path)
audio_length_ms = len(audio)
if audio_length_ms <= max_segment_length_ms:
return [file_path]
segments = []
for start_ms in range(0, audio_length_ms, max_segment_length_ms):
end_ms = min(audio_length_ms, start_ms + max_segment_length_ms)
segment = audio[start_ms:end_ms]
segments.append(segment)
file_prefix = file_path[: file_path.rindex(".")]
format = file_path[file_path.rindex(".") + 1 :]
files = []
for i, segment in enumerate(segments):
path = f"{file_prefix}_{i+1}" + f".{format}"
segment.export(path, format=format)
files.append(path)
return files

Loading…
取消
儲存