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

Uploaded CPython 3.14Windows x86-64

webcodecs_py-2025.1.1-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.1-cp314-cp314-manylinux_2_38_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.38+ ARM64

webcodecs_py-2025.1.1-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.1-cp314-cp314-manylinux_2_35_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.35+ ARM64

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

Uploaded CPython 3.14macOS 15.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

webcodecs_py-2025.1.1-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.1-cp313-cp313-manylinux_2_38_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.38+ ARM64

webcodecs_py-2025.1.1-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.1-cp313-cp313-manylinux_2_35_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.35+ ARM64

webcodecs_py-2025.1.1-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.1-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for webcodecs_py-2025.1.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 4ff27c861fac8a00c599986e5ff0829a5b013372d2493220c26b391498369170
MD5 57048dd4df7b28a0700dd74c2e3f7905
BLAKE2b-256 024d9e7814670b825c23f5afbdbe7443abc84f19719c40d5d679c7d74be4fcfc

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.1-cp314-cp314-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 547ace4c8e3d99fd54f75c514bab5bb435a92a486496a294611994a3e16e9d1f
MD5 99a71bb4c0cce9681647f186e29671ef
BLAKE2b-256 92c5467edda02edae944621a52ddaed28a9dd934f3f313832cdef021b34cda3a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.1-cp314-cp314-manylinux_2_38_aarch64.whl
Algorithm Hash digest
SHA256 4f5e141a7132e5705221036b5a450f14df0ca6817dadff560d72fb1179cca5b9
MD5 6a1cf4a337749583081e1b721dfa807f
BLAKE2b-256 eba91c0ac0d120fe54eb8f953cdc00e7a14ba591d2c48dcad336f836b9230d00

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.1-cp314-cp314-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 554de07c5dd3a4687cf45adb872afb36d9d7da95c773d4d88b8e78807ffaf82d
MD5 4e14ecd617229bf0aef1e97def2196ed
BLAKE2b-256 99618b2403e902060ab9d01c8e70502977c05c1084a9da44c822ee7503e27239

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.1-cp314-cp314-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 c92f806a1e4fc7c0d01c5ef6d1d966624d5476c73cf24d5820bf8cc98f2adf28
MD5 c13cea995da7412ffe5c4dc2d281eb99
BLAKE2b-256 864fab4c8cd0573259eb972bf695f50fb0f66169ab2bb4974d84264cf5d01156

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.1-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f65d5201bb5b018a0fe17540c0ec00c4f1ed29af850371d2697daee0ea909e49
MD5 4a59b7f25469104ea6110148c14cc03c
BLAKE2b-256 a62186f1613cb399ec7c3f19325608c989bb46ff96791d60ed0ffd59eadb8566

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 68ac070c6084a9e3349f4d1e344fbc8d01f44cf6533ab5e152019edf5cb4702c
MD5 669d3c0af1923fd7df1726de7d2e62da
BLAKE2b-256 ab37d4bcc3beec249d81a7d1969dcffb900ca15d1d2a60b09a2e78cebe164a56

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.1-cp313-cp313-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 2f16dd01a429b82a88e8d2eae2547a693abd8cc876f87fffc14ac7bdc1648700
MD5 1e461bd1f20a5f2a4d647d05c4f7a22e
BLAKE2b-256 e617c5657c3e7ac43cf06e113932c563b1139ea7b9d2cf2729abc93663bdb386

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.1-cp313-cp313-manylinux_2_38_aarch64.whl
Algorithm Hash digest
SHA256 c3993339655d4cec50533b343ed3ca5f87bc7daf66a0d07f0201532f1609eae7
MD5 ab9e25ac02fe0d4dcbbc872e3301e3f4
BLAKE2b-256 55523f93531cf44235855d9b8283f216d7eee3967776bbebeaf777d0d786d26c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.1-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 b32bb74a1b71e7d459f2c9b6fbcefe651a975bcf390397a339dd8fbb7d955044
MD5 74fb6203ab82e06974a6ca65900d721a
BLAKE2b-256 e6d91f17bd0e63f8dc9ab5937fb75d6a8f0bdbce058926347a4cb32995156851

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.1-cp313-cp313-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 f69135e6d5bafec5d316b5611363c3493424012ad59a10a9d5031dba3121ef65
MD5 841214bd52213df6c291becb4048adc0
BLAKE2b-256 2898985aaeb54940067faa2ab622cbcead3a8fd024f2a638758ca0782f2d4eda

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for webcodecs_py-2025.1.1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 0502c4dc7a07138a1a7620e02abfad2f7329b5a81f4b85127973981106275065
MD5 5d89da99355da50e526ba88cb099c1ac
BLAKE2b-256 a0a4b7294748a62ac081ce71b3b12d69cf555b6d31031230a169024007f91b3c

See more details on using hashes here.

Provenance

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