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.2.0.dev0-cp314-cp314-win_amd64.whl (5.5 MB view details)

Uploaded CPython 3.14Windows x86-64

webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp314-cp314-manylinux_2_38_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.38+ ARM64

webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp314-cp314-manylinux_2_35_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.35+ ARM64

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

Uploaded CPython 3.14macOS 15.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp313-cp313-manylinux_2_38_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.38+ ARM64

webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp313-cp313-manylinux_2_35_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.35+ ARM64

webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 6bd4b8af41df90b09b74ad671d6c0f2cbc7d13b93d5eba85dcd0820f2fdba520
MD5 cf322ce1dbbef1dbfc64ed76fdf403fc
BLAKE2b-256 a4196c92cd34c5158b0d6ef9de9cf7f39672541a011a6346279143de1673ba6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp314-cp314-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev0-cp314-cp314-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 51e94bcad7b917ab0fe2f96d78d2bb5c27f74690d8cf0ea6414b6f79c013826a
MD5 c4f76cdd9941ff8d18e7ea66b1c9fd4d
BLAKE2b-256 2cdc9dc139d581113347d4ae66fa857a38d23dfb2818edce28dc840922660ab3

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp314-cp314-manylinux_2_38_aarch64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev0-cp314-cp314-manylinux_2_38_aarch64.whl
Algorithm Hash digest
SHA256 218a5b014fbe808624b5e2dc5e03cdb50c4727b98bf496d4b0cd61d166052fe0
MD5 636848b18bd01b52fde3e0e3c615dd89
BLAKE2b-256 6c6787e835c4106fa82aa482be6fa60bca6547d6b7a54c6cf0c6f014e22abace

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp314-cp314-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev0-cp314-cp314-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 47b91dbdb87a08655e0f65f89c841de94529ea89ee03a2a2a635524039fa6e54
MD5 39d0e338d2379d9f16bba3ecc6296795
BLAKE2b-256 5788b2e67c2ef5dace4554594909b5165631b58ab428b15d1c12171eec802df1

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp314-cp314-manylinux_2_35_aarch64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev0-cp314-cp314-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 34e62534d6490c8448be5c1a8a324d9a06a5d61f020233a03806a00c319d56c6
MD5 7a94bbb75454e54af6d62dbdc4c38fb1
BLAKE2b-256 739327987640c2a99513895d65626aba81bfa69f4430bd0f4fab7341e8614f27

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev0-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 0aaa174bb62b3d707bf270011702ecff7e63d60ba6eb9b3f0a079d464aed8f06
MD5 ec93dfa1d0d26e8af66923b841120b14
BLAKE2b-256 4b4c403806ad16094ed75888618c0939dd78ac94f28ab1022a3e633d4a2a408e

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 4a3485a1cf1114ca319bffba311717cebd1316ceeab2530a9f970b473d18b87a
MD5 d5157f2efe6d9762812dcf2a3a47364d
BLAKE2b-256 b8852a2f67579b3333ac087940f0a4a148065af3e1efbae847fc30e7f9431bb5

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp313-cp313-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev0-cp313-cp313-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 99e34de5fdd5c8a976c67f6b8483c010d291e17e582425cb0ea5c4b5c42709cb
MD5 000648d7ec50590524a03ae783fe3a58
BLAKE2b-256 8d0e5cf9f8678ee0411d056b1b1f8a053aa42d04759fb3068d654f7c98702fcb

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp313-cp313-manylinux_2_38_aarch64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev0-cp313-cp313-manylinux_2_38_aarch64.whl
Algorithm Hash digest
SHA256 28a34ad55da052884c32d5e61aeac45773d706d498ce5b9c1a14b13116b80f16
MD5 f75618731bc4ca51a9d3fd319491449a
BLAKE2b-256 55249e6aa44c732937f13ff5914a2412c5597680d65c93e62fd665f8385c863e

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp313-cp313-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev0-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 fbcf0539914e5a0aaa65bbabcfd98c1e089a52a873043e80aeb44b54af5f3e27
MD5 c9d9955867b67077588bde54a3c708a2
BLAKE2b-256 0e0989166ff00c093a22356f76c066e511b2025dd9513b13bf1055066d448d49

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp313-cp313-manylinux_2_35_aarch64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev0-cp313-cp313-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 824d2249aa9d59f29550e05521f815ea79e3fbda017f547c8adc8e8ee4903b0a
MD5 d02c10aa1b050211c8b439291ac702cd
BLAKE2b-256 b98b9a434ff7c5137b9bd626a9d6286891c1cd78d8867829415840c7e13305cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.2.0.dev0-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.2.0.dev0-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev0-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f61b31efa71a69299fba0059cc742dfcf7079cc29a216eaac619d2ff93c270e5
MD5 5438a668dff2e4b938f6e933e2a8fc1c
BLAKE2b-256 e59d6118d39c1e59941432a90c14b5be177c09f952697fa1aa056d49d35833bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for webcodecs_py-2025.2.0.dev0-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