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,
}

# with 文で AudioData を使用(自動的に close される)
with AudioData(init) as audio_data:
    encoder.encode(audio_data)

encoder.flush()

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

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,
}

# with 文で VideoFrame を使用(自動的に close される)
with VideoFrame(frame_data, init) as frame:
    encoder.encode(frame, {"keyFrame": True})

encoder.flush()

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

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

Uploaded CPython 3.14Windows x86-64

webcodecs_py-2025.2.0.dev1-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.dev1-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.dev1-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.dev1-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.dev1-cp314-cp314-macosx_15_0_arm64.whl (3.3 MB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

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

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 d7cd7664606e013d249334a9a93a4b9dc9baada37745bdf043811858314ed228
MD5 b2ba906c96f55f5f8f8b23a871646c6e
BLAKE2b-256 b70481cb5a101646ae8a62d020f2ec04f7cda6d42b54fb25cfbba65181e84553

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev1-cp314-cp314-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 b65ae366288e6a95925d629d1ac51043740c7a97a8b19edc0e3f643dfae3e4dd
MD5 a55b499c3a55e527bd3a6df221487aac
BLAKE2b-256 56a83e726a71f0557b39a688b2fbe8c71a28df0673ec553f14bef34acfb855f9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev1-cp314-cp314-manylinux_2_38_aarch64.whl
Algorithm Hash digest
SHA256 d597a3dda41bd0f5d1ed01b0eb41941004a7444781d8108e9f6bc2fa8df85044
MD5 bd4250a6ca0378c2e581261a971484e8
BLAKE2b-256 cc9487cf812497b6ba0c1b2b2c43b6ae8f505b54055d593a7e883a29a827faa7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev1-cp314-cp314-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 4c17ff42b35cc27b87374d16a23c604d2db1c217d50605cea2df7d838b938870
MD5 0957bee21dd3977ec8e4bb8ae80148f9
BLAKE2b-256 e05553babd5f864b9531434201dd8558d6af4eefdebee96e4f243f28e22ca008

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev1-cp314-cp314-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 655e1b55800a64841546988b0fa28052df9f976d40c64aafa3194adb37c2c97f
MD5 8f419a7fc1d562c2f1026b6941020aa3
BLAKE2b-256 39d9524f9925cc43a13634a3071bdd167637ef09a849c6e84e4d6a3bdbbe79e8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev1-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 404d93b8d9440e1e949e157dc54454b05658c3e5ba9de49709b42f65b575272d
MD5 ec1d164d60cc9450042be5c2919127ff
BLAKE2b-256 e9d0918fccc0da3010a87272a9618596806d5edeaa7f984db81e1de75f20b278

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 8a6b9b02cc9bdb3d7382226e5b2c65fe594179d41d5b51f5915ec2ed0f0c7fad
MD5 392c2e07dffa7ce32eafed290d8a7864
BLAKE2b-256 9ba45dd38c12684ff5dd34e8c09036b157fcb86bfa427d635fd00c7cab41073c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev1-cp313-cp313-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 52d77c52efdbb93dff1ec92d1b760fb80f456d58b9f5759f4413c5db5be042cb
MD5 605b1df2f615e503fb89faba4cc09de4
BLAKE2b-256 90b06146b19de4769793bef2fa8f2f6645365380c5bd85e565962e77cbd33196

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev1-cp313-cp313-manylinux_2_38_aarch64.whl
Algorithm Hash digest
SHA256 4462f2ae997db8cf34b5ab032083329508889b2a71a0e1f43396ce2f112211ee
MD5 a779845171cdf078a8c3bc6f040d744a
BLAKE2b-256 66db411f97cfcd84ea076305f83909b76702ceae30d014a68a3020de35be01a6

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev1-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 f389f9f29e5ed67508b09f0ca0e35e1bddd216ca623ada2653d2d2b46f3fe211
MD5 686ca21672c0831435eb72f6a7482746
BLAKE2b-256 ee679ebe9166d33b88b35192118f55f842f819b37c31ca040be3ed4c73256515

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev1-cp313-cp313-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 79d57859ab9318a76ae6c78b7a17848891d6e4426927281bd7a16e4f398f2429
MD5 c240eafe6455f8f6db4d1e3a2f01d554
BLAKE2b-256 f2fd2d6e547b4621bb8f6346481041a60bbde9a1d3e1a07fbff4f6c3b95068c4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.2.0.dev1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 8d23f7973e432bc2f0e883a22b3958a923c4eb6fd213d518929076e10089cc50
MD5 935b716c4d7f3d5b5aa14a8a5153b2bf
BLAKE2b-256 2f5c3c3eae2d3bcc50f182dc965c8eeaeb4213ece7133fea3abc0306ab2ad507

See more details on using hashes here.

Provenance

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