Skip to main content

webcodecs-py

Project description

webcodecs-py

PyPI image License Actions status

About Shiguredo's open source software

We will not respond to PRs or issues that have not been discussed on Discord. Also, Discord is only available in Japanese.

Please read https://github.com/shiguredo/oss/blob/master/README.en.md before use.

時雨堂のオープンソースソフトウェアについて

利用前に https://github.com/shiguredo/oss をお読みください。

webcodecs-py について

webcodecs-py は WebCodecs API API を Python で利用できるようにするライブラリです。

特徴

  • WebCodecs API の Python バインディング
  • Opus、FLAC、AAC、AV1、H.264、H.265 コーデックをサポート
    • AAC は macOS の AudioToolbox を利用
    • H.264 と H.265 は macOS の VideoToolbox を利用
  • クロスプラットフォーム対応
    • macOS
    • Ubuntu
    • Windows

開発状況は webcodecs-py 対応状況 をご確認ください。

実装しない機能

  • ImageDecoder: 画像デコード機能は実装対象外

    • Pillow や OpenCV を使用してください
  • CanvasImageSource: VideoFrame の CanvasImageSource コンストラクタはブラウザ固有機能のため実装対象外

サンプルコード

Opus オーディオエンコード

import numpy as np

from webcodecs import (
    AudioData,
    AudioDataInit,
    AudioEncoder,
    AudioEncoderConfig,
    AudioSampleFormat,
)

sample_rate = 48000
frame_size = 960  # 20ms @ 48kHz

# エンコーダを作成
encoded_chunks = []


def on_output(chunk):
    encoded_chunks.append(chunk)


def on_error(error):
    raise RuntimeError(f"エンコーダエラー: {error}")


encoder = AudioEncoder(on_output, on_error)
encoder_config: AudioEncoderConfig = {
    "codec": "opus",
    "sample_rate": sample_rate,
    "number_of_channels": 1,
    "bitrate": 64000,
}
encoder.configure(encoder_config)

# サイン波を生成してエンコード
t = np.linspace(0, frame_size / sample_rate, frame_size, dtype=np.float32)
audio_samples = (np.sin(2 * np.pi * 440 * t) * 0.5).reshape(frame_size, 1)

init: AudioDataInit = {
    "format": AudioSampleFormat.F32,
    "sample_rate": sample_rate,
    "number_of_frames": frame_size,
    "number_of_channels": 1,
    "timestamp": 0,
    "data": audio_samples,
}
audio_data = AudioData(init)
encoder.encode(audio_data)
encoder.flush()

print(f"エンコード完了: {len(encoded_chunks)} チャンク")

audio_data.close()
encoder.close()

AV1 ビデオエンコード

import numpy as np

from webcodecs import (
    LatencyMode,
    VideoEncoder,
    VideoEncoderConfig,
    VideoFrame,
    VideoFrameBufferInit,
    VideoPixelFormat,
)

width, height = 320, 240

# エンコーダを作成
encoded_chunks = []


def on_output(chunk):
    encoded_chunks.append(chunk)


def on_error(error):
    raise RuntimeError(f"エンコーダエラー: {error}")


encoder = VideoEncoder(on_output, on_error)
encoder_config: VideoEncoderConfig = {
    "codec": "av01.0.04M.08",
    "width": width,
    "height": height,
    "bitrate": 500_000,
    "framerate": 30.0,
    "latency_mode": LatencyMode.REALTIME,
}
encoder.configure(encoder_config)

# I420 フォーマットのテストフレームを作成
data_size = width * height * 3 // 2
frame_data = np.zeros(data_size, dtype=np.uint8)
init: VideoFrameBufferInit = {
    "format": VideoPixelFormat.I420,
    "coded_width": width,
    "coded_height": height,
    "timestamp": 0,
}
frame = VideoFrame(frame_data, init)

# エンコード
encoder.encode(frame, {"keyFrame": True})
encoder.flush()

print(f"エンコード完了: {len(encoded_chunks)} チャンク, {encoded_chunks[0].byte_length} bytes")

frame.close()
encoder.close()

インストール

uv add webcodecs-py

コーデック

Python

  • 3.14
  • 3.13

プラットフォーム

  • macOS 26 arm64
  • macOS 15 arm64
  • Ubuntu 24.04 LTS x86_64
  • Ubuntu 24.04 LTS arm64
  • Ubuntu 22.04 LTS x86_64
  • Ubuntu 22.04 LTS arm64
  • Windows 11 x86_64

ビルド

make develop

テスト

uv sync
make test

サンプル

uv sync --group example
make develop
uv run python examples/blend2d_to_mp4.py

ライセンス

Apache License 2.0

Copyright 2025-2025, Shiguredo Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

webcodecs_py-2025.1.0-cp314-cp314-win_amd64.whl (5.5 MB view details)

Uploaded CPython 3.14Windows x86-64

webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_38_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.38+ x86-64

webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_38_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.38+ ARM64

webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_35_x86_64.whl (4.5 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.35+ x86-64

webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_35_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.35+ ARM64

webcodecs_py-2025.1.0-cp314-cp314-macosx_15_0_arm64.whl (3.3 MB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

webcodecs_py-2025.1.0-cp313-cp313-win_amd64.whl (5.4 MB view details)

Uploaded CPython 3.13Windows x86-64

webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_38_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.38+ x86-64

webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_38_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.38+ ARM64

webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_35_x86_64.whl (4.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.35+ x86-64

webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_35_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.35+ ARM64

webcodecs_py-2025.1.0-cp313-cp313-macosx_15_0_arm64.whl (3.3 MB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

File details

Details for the file webcodecs_py-2025.1.0-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 bcaaa38d5da071a083eaefd7c5002faaf3aa7b288c308d11a2302ea94c7d3725
MD5 4b801d964479f7c2b7abb379849343ad
BLAKE2b-256 55f309e4ab416db65c54813bbcda27958d11cb872b914c1d4f1d80f93d9817cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.1.0-cp314-cp314-win_amd64.whl:

Publisher: wheel.yml on shiguredo/webcodecs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 3c7cf8a00df6b8023ca06f6cb58cfd3a0b7e45f8cdf69737f456cddd85dbc718
MD5 83e5d4116d04f709b81eff334ad14176
BLAKE2b-256 96c3440690edc5bb01172a517226d5539c116b89135afeb8f8f978e6a07d44d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_38_x86_64.whl:

Publisher: wheel.yml on shiguredo/webcodecs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_38_aarch64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_38_aarch64.whl
Algorithm Hash digest
SHA256 57922543fd7cb1de26583a62c966be7134e46fa02138f9cb603913067168d0e0
MD5 3a54adcd84a32c77491f27a00347c1a6
BLAKE2b-256 2f26bc968086edf5db1c566cfa0eadad9a2a165c66883f15d35dd6da0ac960c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_38_aarch64.whl:

Publisher: wheel.yml on shiguredo/webcodecs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 54168369e181c1c06e5aa5acf397231eba1788a4c70a7bf3d8d46799e56a3907
MD5 8af44545499a5a27ee20b06dfcc13627
BLAKE2b-256 682ad7fa8bbe63ffed85df14aaa5fcc4c18008fd41055c03d2a541de33dceafe

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_35_x86_64.whl:

Publisher: wheel.yml on shiguredo/webcodecs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_35_aarch64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 6b344b17b382b97ef56781d3f8a822f75330a4a4fa283f040ac727decf9dd5ee
MD5 dc667f3a9fb57fbb0d4e00b609e67923
BLAKE2b-256 9fb49451847c68069de59700241d68094c3160d782d9dc4b6f3104e57e066b64

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.1.0-cp314-cp314-manylinux_2_35_aarch64.whl:

Publisher: wheel.yml on shiguredo/webcodecs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file webcodecs_py-2025.1.0-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f0d3acb3bca73dd5216f83671d4a4d645256db29e34b06dd467a72287ed7f95c
MD5 45938523f53895954e1ef2d1a0bc1844
BLAKE2b-256 70f55597566598fd36038cf7457b1307cde626b2e753332a85074208826748d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.1.0-cp314-cp314-macosx_15_0_arm64.whl:

Publisher: wheel.yml on shiguredo/webcodecs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file webcodecs_py-2025.1.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6f82fb21cab09a05a7d2c9d46f518dca0241ba320fcc184a49b3d19382127432
MD5 923f10cca0c80f0cc234d43909514c9b
BLAKE2b-256 f7ae595afa5df6d593dfeb15a3fcf2b1d9283c2ea7bf5c29ac3cf0da48a2588e

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.1.0-cp313-cp313-win_amd64.whl:

Publisher: wheel.yml on shiguredo/webcodecs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 fa4d4c07b07b1b7b6a7788b408cb4c24c83bab9ed23528515b7292100b3b74dc
MD5 696c89605fa4a7c4abd64302f6fd28fa
BLAKE2b-256 6ccd483d786a4d3736fab60dba095623811e3283953a24e80b40123dc1597498

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_38_x86_64.whl:

Publisher: wheel.yml on shiguredo/webcodecs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_38_aarch64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_38_aarch64.whl
Algorithm Hash digest
SHA256 4afd0c2e4633cd7d1bca2358a1d488644d2f494ff2d625fb559d9e8f4b6ac8c2
MD5 8443aa22078e93786e781771e660a836
BLAKE2b-256 21f2afc58e2897ae2b83f3eb2b551fc56198867d11e85737a85a679ac2b7aa60

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_38_aarch64.whl:

Publisher: wheel.yml on shiguredo/webcodecs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 d0aafc559462e4cc8370290cd702e69704563e7152efbd4fd50d3db0eadfa850
MD5 8ddd66e4a50381e4fdebfed917fdafad
BLAKE2b-256 62355236b41e169ddada295c5c5b784e6eeb318f67f554d00fafeea0f82c251d

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_35_x86_64.whl:

Publisher: wheel.yml on shiguredo/webcodecs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_35_aarch64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 6b5f5c58598a84e9b3f238717f6051e33595da88491ad53065b8fd157c1d334c
MD5 367a2ef2d5cee43ede8572c7f7082dc8
BLAKE2b-256 9885394b15b6ad3f76d73fa8d9282123518482899f2b9fc121898f823ba379fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.1.0-cp313-cp313-manylinux_2_35_aarch64.whl:

Publisher: wheel.yml on shiguredo/webcodecs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file webcodecs_py-2025.1.0-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 ceddb99bf2384f91900afe4da720d9322d084d61c7c9a1b874ecc6f58db6a346
MD5 0945542db05d69ef2c71524eef613942
BLAKE2b-256 e3f8224344f8c37d9413805a42169590f64355381c529eb24dca10683781bf1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.1.0-cp313-cp313-macosx_15_0_arm64.whl:

Publisher: wheel.yml on shiguredo/webcodecs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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