Skip to main content

transform audio file to silk

Project description

Graiax-silkcoder

现在版本:pypi
这,是一个Python的silk转码器
通过将kn007/silk-v3-decoder通过简单的封装制成

安装

从 PyPI

# 如果需要转换非wav的音频文件,则需要自行安装ffmpeg
pip install graiax-silkcoder
# 也可以通过下面的方式使用imageio-ffmpeg中的ffmpeg
pip install graiax-silkcoder[ffmpeg]
#  在 0.3.0 后,可以通过以下方式使用libsndfile来解析音频
pip install graiax-silkcoder[libsndfile]

注: 假设你是Windows用户,安装时出现了error: Microsoft Visual C++ 14.0 is required:
请安装Microsoft C++ Build Tools

从 conda-forge

conda install graiax-silkcoder -c conda-forge
# 如果需要 ffmpeg,可以一并从 conda-forge 安装
conda install ffmpeg -c conda-forge

Q&A

ImportError:DLL load failed while importing _silkv3:找不到指定的模块

相关issue: #23

现在本库已经通过 Github Actions 来预编译 whl 了,出现这种问题一般不是说没有编译。
如果遇到这种问题,请在这里下载最新版本的 C++ Redistributable

IOS 音频问题

IOS 的音频解码器因为某些特性,只支持解码 25kbps 以下 的音频。
所以在 0.2.6 中,我们新增了一个 ios_adaptive 参数(默认为 False)。
当为 True 时,将把自适应最高码率限制在 24kbps 以下(一般是限制在 100kbps 以下)

ffmpeg 转换成 aac 格式的问题

因为 graiax-silkcoder 全程采用 PIPE 的形式跟 ffmpeg 传输,
所以假设你想要将 silk 转码成 aac 的时候,就会出现一些问题。
解决方法如下

await silkcoder.async_decode("a.silk", "a.m4a", audio_format="adts")

注:ADTS 是 AAC 音频的传输流格式

自定义ffmpeg_path

可能有一些用户会想要自定义ffmpeg的路径 你可以使用以下方法解决:

from graiax import silkcoder
silkcoder.set_ffmpeg_path("./ffmpeg")

CLI(0.2.0新增)

使用办法

# 其他参数与encode / decode 保持一致
python -m graiax.silkcoder encode -i "a.wav" "a.silk"
python -m graiax.silkcoder decode -i "a.silk" "a.wav"

ffmpeg 还是 libsndfile

在该项目最开始的时候,就有人吐槽过:为了简简单单的音频转换去下载一个大的离谱的 ffmpeg,这也太麻了吧。
(注:虽然说 ffmpeg 可以通过 disable 一大堆不必要视频/滤镜库来达到减小体积的目的,但是这需要自己编译,对小白挺不友好的)

所以,从 0.3.0 开始,开始增加了通过 libsndfile 来使用解析音频。

libsndfile 是一款广泛用于读写音频文件的C语言库, 他支持包括 flac, ogg, opus, mp3[1]等多种格式。

因为

使用方法

Tips:
因为同步和异步的区别只有前面是否有一个 async_
所以下面我们就只拿同步方法距离了

编码

你可以传入 pathlike、str、bytes 作为你的输入

from io import BytesIO
from pathlib import Path
from graiax import silkcoder

data: bytes = silkcoder.encode("a.wav")
data: bytes = silkcoder.encode(Path("a.wav"))
data: bytes = silkcoder.encode(Path("a.wav").read_bytes())
data: bytes = silkcoder.encode(BytesIO(Path("a.wav").read_bytes()))

它也能输出到 filelike、bytes

from io import BytesIO
from pathlib import Path
from graiax import silkcoder

data: bytes = silkcoder.encode("a.wav")
silkcoder.encode("a.wav", "a.silk")
silkcoder.encode("a.wav", Path("a.silk"))
silkcoder.encode("a.wav", BytesIO())

它能做到截取一部分来编码

from graiax import silkcoder

#从最开始截取 5s
silkcoder.encode("a.wav", "a.silk", t=5)
#从第 10s 开始截取 5s
silkcoder.encode("a.wav", "a.silk", ss=10, t=5)

你可以指定你的编码器

from graiax import silkcoder
from graiax.silkcoder import Codec

silkcoder.encode("a.mp3", "a.silk", codec = Codec.libsndfile)
silkcoder.encode("a.mp3", "a.silk", codec = Codec.ffmpeg)

在 ffmpeg 模式下,你甚至可以直接传入 ffmpeg 参数

from graiax import silkcoder

# 虽然 -vn 是可有可无,但我想不出其他例子了
silkcoder.encode("a.mp4", "a.silk", codec = Codec.ffmpeg,
                 ffmpeg_para = ["-vn"])

你还可以指定输出 silk 的码率大小

from graiax import silkcoder

# 默认状态下将会将尝试将目标语音大小限制在980kb上下
silkcoder.encode("a.wav", "a.silk", rate = 70000)

解码

跟编码一样,你的输入和输出都支持 pathlike、str、bytes

在非 wave 模式下,你可以写 metadata

from graiax import silkcoder
from graiax.silkcoder import Codec

metadata = {"title": "xx群",
            "artist": "xx网友"}

# Tips: 如果你硬是选了 wave,他会忽略 metadata 参数而不是报错
silkcoder.decode("a.silk", "a.flac", 
                 codec = Codec.libsndfile,
                 metadata = metadata)

在 ffmpeg 模式下,你可以选择输出的码率(仅对于有损格式)
在 libsndfile 模式下,你可以选择输出的质量(vbr)(仅对于有损格式)

from graiax import silkcoder
from graiax.silkcoder import Codec

#ffmpeg 转换成 128kbps 的 mp3
silkcoder.decode("a.silk", "a.mp3", 
                 codec = Codec.ffmpeg,
                 rate = 128000)
#libsndfile 转换为 压缩率最大 的 flac (注,quality 参数只能在 0~ 1 )
silkcoder.decode("a.silk", "a.flac", 
                 codec = Codec.libsndfile,
                 quality = 1)

你甚至可以在 ffmpeg 模式下输入 ffmpeg 参数

from graiax import silkcoder

silkcoder.decode("a.silk", "a.mp3", ffmpeg_para = ["-ar", "44100"])

  1. graiax-silkcoderlibsndfile 的支持来源于第三方库 soundfile,而该库在 1.1.0 之前并不支持mp3、opus。
    假设你有相关需求,请到这里下载 pre-release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graiax-silkcoder-0.3.3.tar.gz (209.3 kB view details)

Uploaded Source

Built Distributions

graiax_silkcoder-0.3.3-cp310-cp310-win_amd64.whl (123.9 kB view details)

Uploaded CPython 3.10 Windows x86-64

graiax_silkcoder-0.3.3-cp310-cp310-win32.whl (106.0 kB view details)

Uploaded CPython 3.10 Windows x86

graiax_silkcoder-0.3.3-cp310-cp310-musllinux_1_1_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

graiax_silkcoder-0.3.3-cp310-cp310-musllinux_1_1_i686.whl (1.1 MB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

graiax_silkcoder-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (521.8 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

graiax_silkcoder-0.3.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (497.9 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

graiax_silkcoder-0.3.3-cp39-cp39-win_amd64.whl (123.9 kB view details)

Uploaded CPython 3.9 Windows x86-64

graiax_silkcoder-0.3.3-cp39-cp39-win32.whl (106.0 kB view details)

Uploaded CPython 3.9 Windows x86

graiax_silkcoder-0.3.3-cp39-cp39-musllinux_1_1_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

graiax_silkcoder-0.3.3-cp39-cp39-musllinux_1_1_i686.whl (1.1 MB view details)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

graiax_silkcoder-0.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (521.6 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

graiax_silkcoder-0.3.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (497.7 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

graiax_silkcoder-0.3.3-cp38-cp38-win_amd64.whl (123.9 kB view details)

Uploaded CPython 3.8 Windows x86-64

graiax_silkcoder-0.3.3-cp38-cp38-win32.whl (106.0 kB view details)

Uploaded CPython 3.8 Windows x86

graiax_silkcoder-0.3.3-cp38-cp38-musllinux_1_1_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

graiax_silkcoder-0.3.3-cp38-cp38-musllinux_1_1_i686.whl (1.1 MB view details)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

graiax_silkcoder-0.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (521.6 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

graiax_silkcoder-0.3.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (497.7 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

File details

Details for the file graiax-silkcoder-0.3.3.tar.gz.

File metadata

  • Download URL: graiax-silkcoder-0.3.3.tar.gz
  • Upload date:
  • Size: 209.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for graiax-silkcoder-0.3.3.tar.gz
Algorithm Hash digest
SHA256 36b2d5101daef113bb303e5fd781aadf335137919abd73de3ebadd89700b074b
MD5 4ba4282468c257913bca38af7ce5400e
BLAKE2b-256 6058464ced71900ed2f4ce2c63c7329fbb21e3b20b8da3e269ecd3b8b054e34d

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 285e99a651ba5093f12dc76aaab6671dbd32a8da815b06e63d534f4ce8f68030
MD5 fb27df5518587152f2133771047afb4c
BLAKE2b-256 efcf28003a6df154231f237d190bed7de6c1e8f2e0606b171b333e872dab78bd

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp310-cp310-win32.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 9b5dc62f54980da63fa52d346fcf15e8c4d66b726a9354e980e1defc82fa58e1
MD5 b42b305e55113d54347441ceb1263125
BLAKE2b-256 6eb26e6822abd1bce885e99145ba882437c94cb27040b9f0a424bb13bc21900c

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp310-cp310-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 9004bc78b82b7c92ad01cd7d93775f25fef68988c191af1643a90e02a3855a7c
MD5 473dd25bb608545d7ca1cd81b8bde580
BLAKE2b-256 7e9dbb118bff63d3c53c9dacaa38e167a9fdac281c3c6617bd44d2091c1cb280

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp310-cp310-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp310-cp310-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 2df49506737450c504570ead69951c47ec33a0cb437b00f15667e2d416fc335c
MD5 b16f31ee80b805e9d53f911552a65dd8
BLAKE2b-256 6cae624fa10dc044173fddb90c80e65096633b3c238a006893ce0679ea9dd522

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 33b4c037e538ec92ed0963346114324800e8d746d5de0fa691ddd1e173f9dae1
MD5 31d88f5b60c2848d9ec633083fd2a982
BLAKE2b-256 062a58e292d80aec7d0f677c26734f87724fadd0554eb117a2090a38259ab3a6

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 cd0e72e42b7d9cff20c02898a3fd9195d8a435001d6687fa3c5916c783d09f86
MD5 325f91927cd40db1316d4496acbc5806
BLAKE2b-256 8859ab2fc78475dd60e3f987ff18a894232a045e5588e987b1e3146832d7d2e1

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 964901c772a2530608dd05b174a4307d15d17461a141459e6575f980b9f37424
MD5 e5ae125b194ed96383081d500df1cbc4
BLAKE2b-256 31874e458d2f77bc38b9f6277b7622e33a40ec44a2b637308231aa17bcd35701

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp39-cp39-win32.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 d5ed2184436ff0b2eaaa609a3365d663e0c3ccf6cd5026ec1dc56144d2935cd2
MD5 b453d470df4b4d78e24cb23893649fed
BLAKE2b-256 c23a1812853691a1360f5feae8beebac7581269f25163cea5865d755a74e362b

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp39-cp39-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 84774e3fe849ad12f64350fca5570ff1f72abae0898c6f697b3e90a31a14664a
MD5 084ef2309b10e8ff8684ef116f713299
BLAKE2b-256 c33e85ec27724fd28df005b3a53d8604219e6b4084952486306845e2ddc3b049

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp39-cp39-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp39-cp39-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 279e347b280f10819956762a0637ddc342f204473f0dc5cd6ee1b932f0d3c7cf
MD5 316fa8ffac18757c57e7c65ae9f24daa
BLAKE2b-256 6af44070a7ed143c57103b017bf1551fda156957ce45cf7eff371ff0845369b1

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1067a820ccf0774c32c5f2a6cc1e437113b6c7278e930c06f17abb67ebf6ae40
MD5 02c15d5255137aefe0822632c9ceeb95
BLAKE2b-256 e5064a0ca61019d35380712d4bfa40044e75300a3ea5662e2b22173fe891abac

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 68c6ee96d6daf889c7d3ca67e5c7ad6de7808735245513d15d4ea71e3b70040e
MD5 ef85b3054d17fbed91f8a5e1af30cc57
BLAKE2b-256 86595c7b5660a73b1240ccd1ad8160a40ea2dbfd575d57c3edc0173d6e45ff1f

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 de271740b7627063cb0d40833f5ee51c6773871a6f5fff673e54f6e5f1d082be
MD5 ef864652b74bac11389bc5c222506037
BLAKE2b-256 13c49835a5534637000db09e1fe56e2e3c0d811c6c2f2db9feafd70a5be3be17

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp38-cp38-win32.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 9759959950a5f1f0d6e9be1925d420e6b632756f42635a2059a5eb310ca552b4
MD5 56b03051cf0f46b0873769ffa5fb0dce
BLAKE2b-256 f3921907117f30debd1d32be1dd91a0b32848c8aa661ff803f89a017209dea40

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp38-cp38-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 fda9a3d2d1fc50786e444e5609d480fc13b25c9fe325e0823253342514ae6ad9
MD5 184e5a6f95c5c7e66fc08931de4333a4
BLAKE2b-256 5ac0db6d21625e95ad84cb7bfb376173b51d44a17d35342c266f59eab68c3ead

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp38-cp38-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp38-cp38-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 fb075c9ff7d934f7b6946833bb5ccbf7c19a75955f55dd65331a25a89e77a5b9
MD5 7e8604c44aac2abe151128ae392092af
BLAKE2b-256 80efdc8100a56f27b4e91deabaf93478a81b5710fb04c0100f4596fa56984de2

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 80664ccf4187313eb1d2a1d976fe174faec714d51f61e6216d73a83af1434ed6
MD5 a89871a2592e6370967205c3deac1517
BLAKE2b-256 80d07b95c53c871b3fcfb32c3a4e4daba96be9964f5d3acd45e3d37d9caaa285

See more details on using hashes here.

File details

Details for the file graiax_silkcoder-0.3.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for graiax_silkcoder-0.3.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 b59b4fa9cbf21ab2dea28cf319bd3604ab54092f2ae4c9f4eed1d6e60ddc7e28
MD5 f58475e9286f2d8dedbaeb46a87c6a59
BLAKE2b-256 8f955c25412ae8a8a922d634bb1b570523dd3ea953418b510e016a3671b517fe

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page