pilk


Namepilk JSON
Version 0.2.4 PyPI version JSON
download
home_pagehttps://github.com/foyoux/pilk
Summarypython silk voice library
upload_time2023-05-26 03:32:34
maintainerfoyou
docs_urlNone
authorfoyou
requires_python>=3.6
license
keywords silk voice python extension wechat qq tencent xposed c/c++
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pilk

python silk codec binding 支持微信语音编解码

pilk: python + silk

关联项目: [weixin-wxposed-silk-voice](https://github.com/foyoux/weixin-wxposed-silk-voice)

## 安装

[![python version](https://img.shields.io/pypi/pyversions/pilk)](https://pypi.org/project/pilk/)  [![downloads](https://static.pepy.tech/personalized-badge/pilk?period=total&units=international_system&left_color=black&right_color=orange&left_text=Downloads)](https://pepy.tech/project/pilk)

```bash
pip install pilk
```

## 介绍与说明

[**SILK**](https://en.wikipedia.org/wiki/SILK) 是一种语音编码格式,由 [**Skype**](https://en.wikipedia.org/wiki/Skype_Technologies)
公司研发,网上可找到的最新版本是 2012 发布的。

**SILK** 原始代码已上传到 [Release](https://github.com/foyoux/pilk/releases/tag/v0.0.1) , 包含规范文档

**Tencent** 系语音支持来自 [silk-v3-decoder](https://github.com/kn007/silk-v3-decoder)

[Release ](https://github.com/foyoux/pilk/releases/tag/v0.0.1)
中也包含 [silk-v3-decoder](https://github.com/kn007/silk-v3-decoder) 重编译的 **x64-win**
版本,支持中文,[源代码](https://github.com/foyoux/silk-codec)

### **SILK** 编码格式 和 **Tencent** 系语音的关系:

> 此处 **Tencent** 系语音,仅以微信语音为例

1. 标准 **SILK** 文件以 `b'#!SILK_V3'` 开始,以 `b'\xFF\xFF'` 结束,中间为语音数据
2. 微信语音文件在标准 **SILK** 文件的开头插入了 `b'\x02'`,去除了结尾的 `b'\xFF\xFF'`,中间不变

> 已下统称为语音文件

### 语音数据

语音数据分为很多个独立 **frame**,每个 **frame** 开头两字节存储剩余 **frame** 数据的大小,每个 **frame** 默认存储 **20ms** 的音频数据

据此可写出获取 **语音文件** 持续时间(duration) 的函数(此函数 **pilk** 中已包含)

```python
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
```

根据 **SILK** 格式规范,**frame_ms** 可为 `20, 40, 60, 80, 100`

## 快速入门

> 详情请在 IDE 中查看 API 文档注释

在使用 **pilk** 之前,你还需清楚 **音频文件 `mp3, aac, m4a, flac, wav, ...`** 与 **语音文件** 之间的转换是借助 [**PCM raw
data**](https://en.wikipedia.org/wiki/Pulse-code_modulation) 完成的

具体转换关系:音频文件 ⇔ PCM ⇔ 语音文件

1. 音(视)频文件 ➜ PCM
   > 借助 ffmpeg,你当然需要先有 [ffmpeg](https://www.ffmpeg.org/download.html)
    ```bat
    ffmpeg -y -i <音(视)频输入文件> -vn -ar <采样率> -ac 1 -f s16le <PCM输出文件>
    ```
    1. `-y`: 可加可不加,表示 <PCM输出文件> 已存在时不询问,直接覆盖
    2. `-i`: 没啥好说的,固定的,后接 <音(视)频输入文件>
    3. `-vn`: 表示不处理视频数据,建议添加,虽然不加也不会处理视频数据(视频数据不存在转PCM的说法),但可能会打印警告
    4. `-ar`: 设置采样率,可选的值是 [8000, 12000, 16000, 24000, 32000, 44100, 48000], 这里你可以直接理解为声音质量
    5. `-ac`: 设置声道数,在这里必须为 **1**,这是由 **SILK** 决定的
    6. `-f`: 表示强制转换为指定的格式,一般来说必须为 **s16le**, 表示 `16-bit short integer Little-Endian data`
    7. example1: `ffmpeg -y -i mv.mp4 -vn -ar 44100 -ac 1 -f s16le mv.pcm`
    8. example2: `ffmpeg -y -i music.mp3 -ar 44100 -ac 1 -f s16le music.pcm`


2. PCM ➜ 音频文件
    ```bat
    ffmpeg -y -f s16le -i <PCM输入文件> -ar <采样率> -ac <声道数> <音频输出文件>
    ```
    1. `-f`: 这里必须为 `s16le`, 同样也是由 **SILK** 决定的
    2. `-ar`: 同上
    3. `-ac`: 含义同上,值随意
    4. `<音频输出文件>`: 扩展名要准确,没有指定格式时,**ffmpeg** 会根据给定的输出文件扩展名来判断需要输出的格式
    5. example3: `ffmpeg -y -f s16le -i test.pcm test.mp3`

> ffmpeg 也可以使用 python ffmpeg binding 替换,推荐 [PyAV](https://github.com/PyAV-Org/PyAV) 大家自行研究,这里不再啰嗦。

讲完了 音频文件 ⇔ PCM,接下来就是用 **pilk** 进行 PCM ⇔ 语音文件 互转

### silk 编码

```python
import pilk

# pcm_rate 参数必须和 使用 ffmpeg 转 音频 到 PCM 文件时,使用的 `-ar` 参数一致
# pcm_rate 参数必须和 使用 ffmpeg 转 音频 到 PCM 文件时,使用的 `-ar` 参数一致
# pcm_rate 参数必须和 使用 ffmpeg 转 音频 到 PCM 文件时,使用的 `-ar` 参数一致
duration = pilk.encode("test.pcm", "test.silk", pcm_rate=44100, tencent=True)

print("语音时间为:", duration)
```

### silk 解码

```python
import pilk

# pcm_rate 参数必须和 使用 ffmpeg 转 音频 到 PCM 文件时,使用的 `-ar` 参数一致
duration = pilk.decode("test.silk", "test.pcm")

print("语音时间为:", duration)
```

## 使用 Python 转任意媒体文件到 SILK

使用 [pudub](https://github.com/jiaaro/pydub) 依赖 [ffmpeg](https://www.ffmpeg.org/)

```python
import os, pilk
from pydub import AudioSegment


def convert_to_silk(media_path: str) -> str:
    """将输入的媒体文件转出为 silk, 并返回silk路径"""
    media = AudioSegment.from_file(media_path)
    pcm_path = os.path.basename(media_path)
    pcm_path = os.path.splitext(pcm_path)[0]
    silk_path = pcm_path + '.silk'
    pcm_path += '.pcm'
    media.export(pcm_path, 's16le', parameters=['-ar', str(media.frame_rate), '-ac', '1']).close()
    pilk.encode(pcm_path, silk_path, pcm_rate=media.frame_rate, tencent=True)
    return silk_path
```

使用 [pyav](https://github.com/PyAV-Org/PyAV) **推荐**

```python
import os

import av

import pilk


def to_pcm(in_path: str) -> tuple[str, int]:
    """任意媒体文件转 pcm"""
    out_path = os.path.splitext(in_path)[0] + '.pcm'
    with av.open(in_path) as in_container:
        in_stream = in_container.streams.audio[0]
        sample_rate = in_stream.codec_context.sample_rate
        with av.open(out_path, 'w', 's16le') as out_container:
            out_stream = out_container.add_stream(
                'pcm_s16le',
                rate=sample_rate,
                layout='mono'
            )
            try:
               for frame in in_container.decode(in_stream):
                  frame.pts = None
                  for packet in out_stream.encode(frame):
                     out_container.mux(packet)
            except:
               pass
    return out_path, sample_rate


def convert_to_silk(media_path: str) -> str:
    """任意媒体文件转 silk, 返回silk路径"""
    pcm_path, sample_rate = to_pcm(media_path)
    silk_path = os.path.splitext(pcm_path)[0] + '.silk'
    pilk.encode(pcm_path, silk_path, pcm_rate=sample_rate, tencent=True)
    os.remove(pcm_path)
    return silk_path
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/foyoux/pilk",
    "name": "pilk",
    "maintainer": "foyou",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "yimi.0822@qq.com",
    "keywords": "silk,voice,python,extension,wechat,qq,tencent,xposed,c/c++",
    "author": "foyou",
    "author_email": "yimi.0822@qq.com",
    "download_url": "https://files.pythonhosted.org/packages/66/bb/938dd697b6bc2d851ffec4ffe82ed20078e58bd0ef049e84f4d038d6f991/pilk-0.2.4.tar.gz",
    "platform": null,
    "description": "# pilk\r\n\r\npython silk codec binding \u652f\u6301\u5fae\u4fe1\u8bed\u97f3\u7f16\u89e3\u7801\r\n\r\npilk: python + silk\r\n\r\n\u5173\u8054\u9879\u76ee: [weixin-wxposed-silk-voice](https://github.com/foyoux/weixin-wxposed-silk-voice)\r\n\r\n## \u5b89\u88c5\r\n\r\n[![python version](https://img.shields.io/pypi/pyversions/pilk)](https://pypi.org/project/pilk/)  [![downloads](https://static.pepy.tech/personalized-badge/pilk?period=total&units=international_system&left_color=black&right_color=orange&left_text=Downloads)](https://pepy.tech/project/pilk)\r\n\r\n```bash\r\npip install pilk\r\n```\r\n\r\n## \u4ecb\u7ecd\u4e0e\u8bf4\u660e\r\n\r\n[**SILK**](https://en.wikipedia.org/wiki/SILK) \u662f\u4e00\u79cd\u8bed\u97f3\u7f16\u7801\u683c\u5f0f\uff0c\u7531 [**Skype**](https://en.wikipedia.org/wiki/Skype_Technologies)\r\n\u516c\u53f8\u7814\u53d1\uff0c\u7f51\u4e0a\u53ef\u627e\u5230\u7684\u6700\u65b0\u7248\u672c\u662f 2012 \u53d1\u5e03\u7684\u3002\r\n\r\n**SILK** \u539f\u59cb\u4ee3\u7801\u5df2\u4e0a\u4f20\u5230 [Release](https://github.com/foyoux/pilk/releases/tag/v0.0.1) , \u5305\u542b\u89c4\u8303\u6587\u6863\r\n\r\n**Tencent** \u7cfb\u8bed\u97f3\u652f\u6301\u6765\u81ea [silk-v3-decoder](https://github.com/kn007/silk-v3-decoder)\r\n\r\n[Release ](https://github.com/foyoux/pilk/releases/tag/v0.0.1)\r\n\u4e2d\u4e5f\u5305\u542b [silk-v3-decoder](https://github.com/kn007/silk-v3-decoder) \u91cd\u7f16\u8bd1\u7684 **x64-win**\r\n\u7248\u672c\uff0c\u652f\u6301\u4e2d\u6587\uff0c[\u6e90\u4ee3\u7801](https://github.com/foyoux/silk-codec)\r\n\r\n### **SILK** \u7f16\u7801\u683c\u5f0f \u548c **Tencent** \u7cfb\u8bed\u97f3\u7684\u5173\u7cfb\uff1a\r\n\r\n> \u6b64\u5904 **Tencent** \u7cfb\u8bed\u97f3\uff0c\u4ec5\u4ee5\u5fae\u4fe1\u8bed\u97f3\u4e3a\u4f8b\r\n\r\n1. \u6807\u51c6 **SILK** \u6587\u4ef6\u4ee5 `b'#!SILK_V3'` \u5f00\u59cb\uff0c\u4ee5 `b'\\xFF\\xFF'` \u7ed3\u675f\uff0c\u4e2d\u95f4\u4e3a\u8bed\u97f3\u6570\u636e\r\n2. \u5fae\u4fe1\u8bed\u97f3\u6587\u4ef6\u5728\u6807\u51c6 **SILK** \u6587\u4ef6\u7684\u5f00\u5934\u63d2\u5165\u4e86 `b'\\x02'`\uff0c\u53bb\u9664\u4e86\u7ed3\u5c3e\u7684 `b'\\xFF\\xFF'`\uff0c\u4e2d\u95f4\u4e0d\u53d8\r\n\r\n> \u5df2\u4e0b\u7edf\u79f0\u4e3a\u8bed\u97f3\u6587\u4ef6\r\n\r\n### \u8bed\u97f3\u6570\u636e\r\n\r\n\u8bed\u97f3\u6570\u636e\u5206\u4e3a\u5f88\u591a\u4e2a\u72ec\u7acb **frame**\uff0c\u6bcf\u4e2a **frame** \u5f00\u5934\u4e24\u5b57\u8282\u5b58\u50a8\u5269\u4f59 **frame** \u6570\u636e\u7684\u5927\u5c0f\uff0c\u6bcf\u4e2a **frame** \u9ed8\u8ba4\u5b58\u50a8 **20ms** \u7684\u97f3\u9891\u6570\u636e\r\n\r\n\u636e\u6b64\u53ef\u5199\u51fa\u83b7\u53d6 **\u8bed\u97f3\u6587\u4ef6** \u6301\u7eed\u65f6\u95f4(duration) \u7684\u51fd\u6570\uff08\u6b64\u51fd\u6570 **pilk** \u4e2d\u5df2\u5305\u542b\uff09\r\n\r\n```python\r\ndef get_duration(silk_path: str, frame_ms: int = 20) -> int:\r\n    \"\"\"\u83b7\u53d6 silk \u6587\u4ef6\u6301\u7eed\u65f6\u95f4\uff0c\u5355\u4f4d\uff1ams\"\"\"\r\n    with open(silk_path, 'rb') as silk:\r\n        tencent = False\r\n        if silk.read(1) == b'\\x02':\r\n            tencent = True\r\n        silk.seek(0)\r\n        if tencent:\r\n            silk.seek(10)\r\n        else:\r\n            silk.seek(9)\r\n        i = 0\r\n        while True:\r\n            size = silk.read(2)\r\n            if len(size) != 2:\r\n                break\r\n            size = size[0] + size[1] << 8\r\n            if not tencent and size == 0xffff:\r\n                break\r\n            i += 1\r\n            silk.seek(silk.tell() + size)\r\n        return i * frame_ms\r\n```\r\n\r\n\u6839\u636e **SILK** \u683c\u5f0f\u89c4\u8303\uff0c**frame_ms** \u53ef\u4e3a `20, 40, 60, 80, 100`\r\n\r\n## \u5feb\u901f\u5165\u95e8\r\n\r\n> \u8be6\u60c5\u8bf7\u5728 IDE \u4e2d\u67e5\u770b API \u6587\u6863\u6ce8\u91ca\r\n\r\n\u5728\u4f7f\u7528 **pilk** \u4e4b\u524d\uff0c\u4f60\u8fd8\u9700\u6e05\u695a **\u97f3\u9891\u6587\u4ef6 `mp3, aac, m4a, flac, wav, ...`** \u4e0e **\u8bed\u97f3\u6587\u4ef6** \u4e4b\u95f4\u7684\u8f6c\u6362\u662f\u501f\u52a9 [**PCM raw\r\ndata**](https://en.wikipedia.org/wiki/Pulse-code_modulation) \u5b8c\u6210\u7684\r\n\r\n\u5177\u4f53\u8f6c\u6362\u5173\u7cfb\uff1a\u97f3\u9891\u6587\u4ef6 \u21d4 PCM \u21d4 \u8bed\u97f3\u6587\u4ef6\r\n\r\n1. \u97f3(\u89c6)\u9891\u6587\u4ef6 \u279c PCM\r\n   > \u501f\u52a9 ffmpeg\uff0c\u4f60\u5f53\u7136\u9700\u8981\u5148\u6709 [ffmpeg](https://www.ffmpeg.org/download.html)\r\n    ```bat\r\n    ffmpeg -y -i <\u97f3(\u89c6)\u9891\u8f93\u5165\u6587\u4ef6> -vn -ar <\u91c7\u6837\u7387> -ac 1 -f s16le <PCM\u8f93\u51fa\u6587\u4ef6>\r\n    ```\r\n    1. `-y`: \u53ef\u52a0\u53ef\u4e0d\u52a0\uff0c\u8868\u793a <PCM\u8f93\u51fa\u6587\u4ef6> \u5df2\u5b58\u5728\u65f6\u4e0d\u8be2\u95ee\uff0c\u76f4\u63a5\u8986\u76d6\r\n    2. `-i`: \u6ca1\u5565\u597d\u8bf4\u7684\uff0c\u56fa\u5b9a\u7684\uff0c\u540e\u63a5 <\u97f3(\u89c6)\u9891\u8f93\u5165\u6587\u4ef6>\r\n    3. `-vn`: \u8868\u793a\u4e0d\u5904\u7406\u89c6\u9891\u6570\u636e\uff0c\u5efa\u8bae\u6dfb\u52a0\uff0c\u867d\u7136\u4e0d\u52a0\u4e5f\u4e0d\u4f1a\u5904\u7406\u89c6\u9891\u6570\u636e\uff08\u89c6\u9891\u6570\u636e\u4e0d\u5b58\u5728\u8f6cPCM\u7684\u8bf4\u6cd5\uff09\uff0c\u4f46\u53ef\u80fd\u4f1a\u6253\u5370\u8b66\u544a\r\n    4. `-ar`: \u8bbe\u7f6e\u91c7\u6837\u7387\uff0c\u53ef\u9009\u7684\u503c\u662f [8000, 12000, 16000, 24000, 32000, 44100, 48000], \u8fd9\u91cc\u4f60\u53ef\u4ee5\u76f4\u63a5\u7406\u89e3\u4e3a\u58f0\u97f3\u8d28\u91cf\r\n    5. `-ac`: \u8bbe\u7f6e\u58f0\u9053\u6570\uff0c\u5728\u8fd9\u91cc\u5fc5\u987b\u4e3a **1**\uff0c\u8fd9\u662f\u7531 **SILK** \u51b3\u5b9a\u7684\r\n    6. `-f`: \u8868\u793a\u5f3a\u5236\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u683c\u5f0f\uff0c\u4e00\u822c\u6765\u8bf4\u5fc5\u987b\u4e3a **s16le**, \u8868\u793a `16-bit short integer Little-Endian data`\r\n    7. example1: `ffmpeg -y -i mv.mp4 -vn -ar 44100 -ac 1 -f s16le mv.pcm`\r\n    8. example2: `ffmpeg -y -i music.mp3 -ar 44100 -ac 1 -f s16le music.pcm`\r\n\r\n\r\n2. PCM \u279c \u97f3\u9891\u6587\u4ef6\r\n    ```bat\r\n    ffmpeg -y -f s16le -i <PCM\u8f93\u5165\u6587\u4ef6> -ar <\u91c7\u6837\u7387> -ac <\u58f0\u9053\u6570> <\u97f3\u9891\u8f93\u51fa\u6587\u4ef6>\r\n    ```\r\n    1. `-f`: \u8fd9\u91cc\u5fc5\u987b\u4e3a `s16le`, \u540c\u6837\u4e5f\u662f\u7531 **SILK** \u51b3\u5b9a\u7684\r\n    2. `-ar`: \u540c\u4e0a\r\n    3. `-ac`: \u542b\u4e49\u540c\u4e0a\uff0c\u503c\u968f\u610f\r\n    4. `<\u97f3\u9891\u8f93\u51fa\u6587\u4ef6>`: \u6269\u5c55\u540d\u8981\u51c6\u786e\uff0c\u6ca1\u6709\u6307\u5b9a\u683c\u5f0f\u65f6\uff0c**ffmpeg** \u4f1a\u6839\u636e\u7ed9\u5b9a\u7684\u8f93\u51fa\u6587\u4ef6\u6269\u5c55\u540d\u6765\u5224\u65ad\u9700\u8981\u8f93\u51fa\u7684\u683c\u5f0f\r\n    5. example3: `ffmpeg -y -f s16le -i test.pcm test.mp3`\r\n\r\n> ffmpeg \u4e5f\u53ef\u4ee5\u4f7f\u7528 python ffmpeg binding \u66ff\u6362\uff0c\u63a8\u8350 [PyAV](https://github.com/PyAV-Org/PyAV) \u5927\u5bb6\u81ea\u884c\u7814\u7a76\uff0c\u8fd9\u91cc\u4e0d\u518d\u5570\u55e6\u3002\r\n\r\n\u8bb2\u5b8c\u4e86 \u97f3\u9891\u6587\u4ef6 \u21d4 PCM\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u7528 **pilk** \u8fdb\u884c PCM \u21d4 \u8bed\u97f3\u6587\u4ef6 \u4e92\u8f6c\r\n\r\n### silk \u7f16\u7801\r\n\r\n```python\r\nimport pilk\r\n\r\n# pcm_rate \u53c2\u6570\u5fc5\u987b\u548c \u4f7f\u7528 ffmpeg \u8f6c \u97f3\u9891 \u5230 PCM \u6587\u4ef6\u65f6\uff0c\u4f7f\u7528\u7684 `-ar` \u53c2\u6570\u4e00\u81f4\r\n# pcm_rate \u53c2\u6570\u5fc5\u987b\u548c \u4f7f\u7528 ffmpeg \u8f6c \u97f3\u9891 \u5230 PCM \u6587\u4ef6\u65f6\uff0c\u4f7f\u7528\u7684 `-ar` \u53c2\u6570\u4e00\u81f4\r\n# pcm_rate \u53c2\u6570\u5fc5\u987b\u548c \u4f7f\u7528 ffmpeg \u8f6c \u97f3\u9891 \u5230 PCM \u6587\u4ef6\u65f6\uff0c\u4f7f\u7528\u7684 `-ar` \u53c2\u6570\u4e00\u81f4\r\nduration = pilk.encode(\"test.pcm\", \"test.silk\", pcm_rate=44100, tencent=True)\r\n\r\nprint(\"\u8bed\u97f3\u65f6\u95f4\u4e3a:\", duration)\r\n```\r\n\r\n### silk \u89e3\u7801\r\n\r\n```python\r\nimport pilk\r\n\r\n# pcm_rate \u53c2\u6570\u5fc5\u987b\u548c \u4f7f\u7528 ffmpeg \u8f6c \u97f3\u9891 \u5230 PCM \u6587\u4ef6\u65f6\uff0c\u4f7f\u7528\u7684 `-ar` \u53c2\u6570\u4e00\u81f4\r\nduration = pilk.decode(\"test.silk\", \"test.pcm\")\r\n\r\nprint(\"\u8bed\u97f3\u65f6\u95f4\u4e3a:\", duration)\r\n```\r\n\r\n## \u4f7f\u7528 Python \u8f6c\u4efb\u610f\u5a92\u4f53\u6587\u4ef6\u5230 SILK\r\n\r\n\u4f7f\u7528 [pudub](https://github.com/jiaaro/pydub) \u4f9d\u8d56 [ffmpeg](https://www.ffmpeg.org/)\r\n\r\n```python\r\nimport os, pilk\r\nfrom pydub import AudioSegment\r\n\r\n\r\ndef convert_to_silk(media_path: str) -> str:\r\n    \"\"\"\u5c06\u8f93\u5165\u7684\u5a92\u4f53\u6587\u4ef6\u8f6c\u51fa\u4e3a silk, \u5e76\u8fd4\u56desilk\u8def\u5f84\"\"\"\r\n    media = AudioSegment.from_file(media_path)\r\n    pcm_path = os.path.basename(media_path)\r\n    pcm_path = os.path.splitext(pcm_path)[0]\r\n    silk_path = pcm_path + '.silk'\r\n    pcm_path += '.pcm'\r\n    media.export(pcm_path, 's16le', parameters=['-ar', str(media.frame_rate), '-ac', '1']).close()\r\n    pilk.encode(pcm_path, silk_path, pcm_rate=media.frame_rate, tencent=True)\r\n    return silk_path\r\n```\r\n\r\n\u4f7f\u7528 [pyav](https://github.com/PyAV-Org/PyAV) **\u63a8\u8350**\r\n\r\n```python\r\nimport os\r\n\r\nimport av\r\n\r\nimport pilk\r\n\r\n\r\ndef to_pcm(in_path: str) -> tuple[str, int]:\r\n    \"\"\"\u4efb\u610f\u5a92\u4f53\u6587\u4ef6\u8f6c pcm\"\"\"\r\n    out_path = os.path.splitext(in_path)[0] + '.pcm'\r\n    with av.open(in_path) as in_container:\r\n        in_stream = in_container.streams.audio[0]\r\n        sample_rate = in_stream.codec_context.sample_rate\r\n        with av.open(out_path, 'w', 's16le') as out_container:\r\n            out_stream = out_container.add_stream(\r\n                'pcm_s16le',\r\n                rate=sample_rate,\r\n                layout='mono'\r\n            )\r\n            try:\r\n               for frame in in_container.decode(in_stream):\r\n                  frame.pts = None\r\n                  for packet in out_stream.encode(frame):\r\n                     out_container.mux(packet)\r\n            except:\r\n               pass\r\n    return out_path, sample_rate\r\n\r\n\r\ndef convert_to_silk(media_path: str) -> str:\r\n    \"\"\"\u4efb\u610f\u5a92\u4f53\u6587\u4ef6\u8f6c silk, \u8fd4\u56desilk\u8def\u5f84\"\"\"\r\n    pcm_path, sample_rate = to_pcm(media_path)\r\n    silk_path = os.path.splitext(pcm_path)[0] + '.silk'\r\n    pilk.encode(pcm_path, silk_path, pcm_rate=sample_rate, tencent=True)\r\n    os.remove(pcm_path)\r\n    return silk_path\r\n```\r\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "python silk voice library",
    "version": "0.2.4",
    "project_urls": {
        "Download": "https://github.com/foyoux/pilk/releases",
        "Homepage": "https://github.com/foyoux/pilk"
    },
    "split_keywords": [
        "silk",
        "voice",
        "python",
        "extension",
        "wechat",
        "qq",
        "tencent",
        "xposed",
        "c/c++"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4668bb558c4c48bccb12f8bbd0d55196cbc5e8aace33aa80828a189b0515eabc",
                "md5": "9ffcea6c15ed5f9293bb353f2fcc843e",
                "sha256": "1e9d4a4f9dbc28f8b753babb70533007043b01cb7005915c956e670613ddaa2b"
            },
            "downloads": -1,
            "filename": "pilk-0.2.4-cp310-cp310-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "9ffcea6c15ed5f9293bb353f2fcc843e",
            "packagetype": "bdist_wheel",
            "python_version": "cp310",
            "requires_python": ">=3.6",
            "size": 126431,
            "upload_time": "2023-05-26T03:32:22",
            "upload_time_iso_8601": "2023-05-26T03:32:22.493204Z",
            "url": "https://files.pythonhosted.org/packages/46/68/bb558c4c48bccb12f8bbd0d55196cbc5e8aace33aa80828a189b0515eabc/pilk-0.2.4-cp310-cp310-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ff0680cac61bc7f791bcaae552ba90fb868f7af7503ef1c74e423cc20fca1a53",
                "md5": "73134994069886d1e6807bd7c679ec7f",
                "sha256": "a6692096607e8d77d348aeec00df633788c85b527aa83cbd803ddf508936db7f"
            },
            "downloads": -1,
            "filename": "pilk-0.2.4-cp311-cp311-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "73134994069886d1e6807bd7c679ec7f",
            "packagetype": "bdist_wheel",
            "python_version": "cp311",
            "requires_python": ">=3.6",
            "size": 127024,
            "upload_time": "2023-05-26T03:32:24",
            "upload_time_iso_8601": "2023-05-26T03:32:24.524390Z",
            "url": "https://files.pythonhosted.org/packages/ff/06/80cac61bc7f791bcaae552ba90fb868f7af7503ef1c74e423cc20fca1a53/pilk-0.2.4-cp311-cp311-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "eb59dc9575e72d6c879aca267101884ba13b589dc4102622bb1bc87909c78a24",
                "md5": "3b0691ce9bab6485b94316b0d9b35ebe",
                "sha256": "df3a219ae1f1cd4a640d3e6570b53616df7150a331a143246b46d49f3232e11e"
            },
            "downloads": -1,
            "filename": "pilk-0.2.4-cp36-cp36m-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "3b0691ce9bab6485b94316b0d9b35ebe",
            "packagetype": "bdist_wheel",
            "python_version": "cp36",
            "requires_python": ">=3.6",
            "size": 133434,
            "upload_time": "2023-05-26T03:32:26",
            "upload_time_iso_8601": "2023-05-26T03:32:26.098502Z",
            "url": "https://files.pythonhosted.org/packages/eb/59/dc9575e72d6c879aca267101884ba13b589dc4102622bb1bc87909c78a24/pilk-0.2.4-cp36-cp36m-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2f810300f145b50d2e10883ca092aa5d6e8118c9a7a046d3bc822a1e668084cd",
                "md5": "d1509f23085f9796b2310008ad6bc253",
                "sha256": "b9b40f49e0dedc1cb5501e13ed17b3bdaa7c6733bd871a22a063e7aa472cd2ef"
            },
            "downloads": -1,
            "filename": "pilk-0.2.4-cp37-cp37m-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "d1509f23085f9796b2310008ad6bc253",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.6",
            "size": 126422,
            "upload_time": "2023-05-26T03:32:27",
            "upload_time_iso_8601": "2023-05-26T03:32:27.800948Z",
            "url": "https://files.pythonhosted.org/packages/2f/81/0300f145b50d2e10883ca092aa5d6e8118c9a7a046d3bc822a1e668084cd/pilk-0.2.4-cp37-cp37m-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "38e86d29300efd4d6614bd77019bdd7871de0d27d0b935e21bcd1c6d41c99696",
                "md5": "780b399b1d5674411996e2c04c193dba",
                "sha256": "163af75478a7560b40549e7b0c58c83f9b88399deb3f301f6f763a617db0ae92"
            },
            "downloads": -1,
            "filename": "pilk-0.2.4-cp38-cp38-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "780b399b1d5674411996e2c04c193dba",
            "packagetype": "bdist_wheel",
            "python_version": "cp38",
            "requires_python": ">=3.6",
            "size": 126420,
            "upload_time": "2023-05-26T03:32:29",
            "upload_time_iso_8601": "2023-05-26T03:32:29.808479Z",
            "url": "https://files.pythonhosted.org/packages/38/e8/6d29300efd4d6614bd77019bdd7871de0d27d0b935e21bcd1c6d41c99696/pilk-0.2.4-cp38-cp38-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c009b2db1a9b3a6ae898fb5d13fd600113f8f80bec480f6503d766d582b38157",
                "md5": "3c08ba22ce7e44cf1ee37481a1708ab7",
                "sha256": "3730c51fc0bf96214df98006d3f0822a17c1b634506c801e66dc025bb41a2664"
            },
            "downloads": -1,
            "filename": "pilk-0.2.4-cp39-cp39-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "3c08ba22ce7e44cf1ee37481a1708ab7",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": ">=3.6",
            "size": 126426,
            "upload_time": "2023-05-26T03:32:31",
            "upload_time_iso_8601": "2023-05-26T03:32:31.769853Z",
            "url": "https://files.pythonhosted.org/packages/c0/09/b2db1a9b3a6ae898fb5d13fd600113f8f80bec480f6503d766d582b38157/pilk-0.2.4-cp39-cp39-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "66bb938dd697b6bc2d851ffec4ffe82ed20078e58bd0ef049e84f4d038d6f991",
                "md5": "d65845b1fffef26198e1151fa3e0dc40",
                "sha256": "d4a1bcf93dc6ef5e95e0cfd728ed4ef4d49f9c0476d70816fecbe456cc762e7f"
            },
            "downloads": -1,
            "filename": "pilk-0.2.4.tar.gz",
            "has_sig": false,
            "md5_digest": "d65845b1fffef26198e1151fa3e0dc40",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 226451,
            "upload_time": "2023-05-26T03:32:34",
            "upload_time_iso_8601": "2023-05-26T03:32:34.133356Z",
            "url": "https://files.pythonhosted.org/packages/66/bb/938dd697b6bc2d851ffec4ffe82ed20078e58bd0ef049e84f4d038d6f991/pilk-0.2.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-26 03:32:34",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "foyoux",
    "github_project": "pilk",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pilk"
}
        
Elapsed time: 0.21781s