python silk voice library
Low maintenance activity. Last release over 3 years ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install pilkpilk
python silk codec binding 支持微信语音编解码
pilk: python + silk
关联项目: weixin-wxposed-silk-voice
安装
pip install pilk
介绍与说明
SILK 是一种语音编码格式,由 Skype 公司研发,网上可找到的最新版本是 2012 发布的。
SILK 原始代码已上传到 Release , 包含规范文档
Tencent 系语音支持来自 silk-v3-decoder
Release 中也包含 silk-v3-decoder 重编译的 x64-win 版本,支持中文,源代码
SILK 编码格式 和 Tencent 系语音的关系:
此处 Tencent 系语音,仅以微信语音为例
- 标准 SILK 文件以
b'#!SILK_V3'开始,以b'\xFF\xFF'结束,中间为语音数据 - 微信语音文件在标准 SILK 文件的开头插入了
b'\x02',去除了结尾的b'\xFF\xFF',中间不变
已下统称为语音文件
语音数据
语音数据分为很多个独立 frame,每个 frame 开头两字节存储剩余 frame 数据的大小,每个 frame 默认存储 20ms 的音频数据
据此可写出获取 语音文件 持续时间(duration) 的函数(此函数 pilk 中已包含)
def get_duration(silk_path: str, frame_ms: int = 20) -> int:
"""获取 silk 文件持续时间,单位:ms"""
with open(silk_path, 'rb') as silk:
tencent = False
if silk.read(1) == b'\x02':
tencent = True
silk.seek(0)
if tencent:
silk.seek(10)
else:
silk.seek(9)
i = 0
while True:
size = silk.read(2)
if len(size) != 2:
break
size = size[0] + size[1] << 8
if not tencent and size == 0xffff:
break
i += 1
silk.seek(silk.tell() + size)
return i * frame_ms