Skip to main content

webcodecs-py

Project description

webcodecs-py

[!CAUTION] webcodecs-py はまだ開発中であり、安定版ではありません。将来的に API が変更される可能性があります。 開発状況は 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、AV1、H.264、H.265 コーデックをサポート
    • H.264 と H.265 は macOS の VideoToolbox を利用
  • クロスプラットフォーム対応
    • macOS
    • Ubuntu
    • Windows

実装しない機能

  • ImageDecoder(画像デコード機能は PIL/Pillow や OpenCV を使用してください)

サンプルコード

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

Uploaded CPython 3.13Windows x86-64

webcodecs_py-2025.1.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.1.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.1.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.1.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.1.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.1.0.dev1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0.dev1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 4e214b6904170baf829f861bdf987e4ace413921c1937c00e9b30477e695761d
MD5 4c573b5abbf0b992c39e0c0855f0a477
BLAKE2b-256 bfeaee43971da261ddc6890dfaa77d49068759f3797474ee6fb765db83b3446a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0.dev1-cp313-cp313-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 31fc4c5f61091ab282680a92dfb89b677c33500e87574ebcb7b6d93fedee2bef
MD5 2c78673b1ae83656a837fad5ef26a89f
BLAKE2b-256 b813d0b94a947542f41f1acafea76db8a289b8a158b65611d9e6567654c9df5e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0.dev1-cp313-cp313-manylinux_2_38_aarch64.whl
Algorithm Hash digest
SHA256 95edee0276b5cd9e163f47fa04ff809957d9c008ef45ab24c925b4b65393e890
MD5 16b8aeb95b05b1414146cd9a4190ff18
BLAKE2b-256 8ccfd6ce39dc51c90175f15a9f1f6051c1932c30fe10ec9fef2b0439e3b5cf6e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0.dev1-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 9172218dd0c8c88d7e742d0279bb3adac3bb0fd753e9a2b487fc5bba9db487c1
MD5 9f279c8df3036d7190e401a0cbefdfb1
BLAKE2b-256 dbfbab4e710c4c0112dbcce52fb1f699e648182dfbb8606c9cf75cfa4406517c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0.dev1-cp313-cp313-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 0345e182824d6a99e44b67beabb152412c7cf896fd6bea80a1416d4960ab4bae
MD5 521428c3f8341b0f31be840c022130ad
BLAKE2b-256 abca892a5d68b14e3326df6820f5a150c506d5e3327c4422e434758a3d2179ff

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.0.dev1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 e48b218b8eafad8786abea81a391520781d7af273ddb753a80294df5a8be48c8
MD5 82c52017fc868d359ff173126e9d09d1
BLAKE2b-256 4f55a83b5f0a4a5ca1e2bafbf5fee25fe79dfbda084c6269faf4869496ffd172

See more details on using hashes here.

Provenance

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