Skip to main content

Cross-platform UVC camera library binding for Python

Project description

uvc-py

PyPI SPEC 0 — Minimum Supported Dependencies image License

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 をお読みください。

uvc-py について

UVC (USB Video Class) カメラからの映像キャプチャを行う Python ライブラリです。

  • numpy.ndarray での映像フレーム取得
  • ネイティブバッファへの直接アクセス (macOS: CVPixelBufferRef / PyCapsule)
  • デバイス接続/切断コールバック (macOS / Linux)
  • Python Free Threading 対応

対応フォーマット

  • NV12
  • YUY2

将来サポート予定

  • MJPEG

対応プラットフォーム

  • macOS 26 arm64
  • macOS 15 arm64
  • Ubuntu 24.04 x86_64
  • Ubuntu 24.04 arm64
  • Ubuntu 22.04 x86_64
  • Ubuntu 22.04 arm
OS API
macOS AVFoundation
Linux V4L2
Windows Media Foundation

将来対応予定

  • Windows Server 2025 x86_64
  • Windows 11 x86_64

対応 Python

  • 3.14
  • 3.14t
  • 3.13
  • 3.12

インストール

uv add uvc-py

使い方

デバイス一覧の取得

import uvc

devices = uvc.list_devices()
for device in devices:
    print(f"[{device.index}] {device.name}")

映像キャプチャ

import uvc

with uvc.open(0) as device:
    device.start(1920, 1080, 30, capture_format=uvc.Format.NV12)

    frame = device.get_frame()
    if frame is not None:
        y, uv = frame.to_nv12()
        print(f"Y: {y.shape}")
        print(f"UV: {uv.shape}")

デバイス接続/切断コールバック

import uvc
import time

def on_connected():
    print("デバイスが接続されました")

def on_disconnected():
    print("デバイスが切断されました")

with uvc.open(0, on_connected=on_connected, on_disconnected=on_disconnected) as device:
    device.start(1920, 1080, 30, capture_format=uvc.Format.NV12)

    while True:
        frame = device.get_frame()
        if frame is not None:
            print(f"Frame: {frame.width}x{frame.height}")
        time.sleep(0.033)

raw-player との連携

import uvc
from raw_player import VideoPlayer

with uvc.open(0) as device:
    device.start(1920, 1080, 30, capture_format=uvc.Format.NV12)

    player = VideoPlayer(width=1920, height=1080)
    player.play()

    while player.is_open:
        if not player.poll_events():
            break

        frame = device.get_frame()
        if frame is not None:
            y, uv = frame.to_nv12()
            player.enqueue_video_nv12(y, uv, frame.timestamp)

    player.close()

webcodecs-py との連携

import uvc
from webcodecs import VideoEncoder, VideoFrame, VideoPixelFormat

def on_output(chunk):
    print(f"Encoded: {chunk.byte_length} bytes")

def on_error(error):
    print(f"Error: {error}")

with uvc.open(0) as device:
    device.start(1920, 1080, 30, capture_format=uvc.Format.NV12)

    encoder = VideoEncoder(on_output, on_error)
    encoder.configure({
        "codec": "avc1.640028",
        "width": 1920,
        "height": 1080,
        "bitrate": 5_000_000,
        "framerate": 30,
    })

    frame = device.get_frame()
    if frame is not None:
        y, uv = frame.to_nv12()
        with VideoFrame(
            y,
            uv,
            {
                "format": VideoPixelFormat.NV12,
                "coded_width": frame.width,
                "coded_height": frame.height,
                "timestamp": frame.timestamp,
            },
        ) as video_frame:
            encoder.encode(video_frame, {"key_frame": True})

    encoder.flush()
    encoder.close()

PyCapsule

PyCapsule は Python C API の仕組みで、C/C++ のポインタを Python オブジェクトとして安全に受け渡しできます。

uvc-py では frame.native_buffer() で macOS の CVPixelBufferRef を PyCapsule として取得できます。 これにより他の C 拡張モジュールが CVPixelBufferRef を直接扱えます。

[!NOTE]

  • Linux / Windows では native_buffer() は None を返します

raw-player との連携

native_buffer() を渡すことで CVPixelBufferRef を直接扱えます。

import uvc
from raw_player import VideoPlayer

with uvc.open(0) as device:
    device.start(1920, 1080, 30, capture_format=uvc.Format.NV12)

    player = VideoPlayer(width=1920, height=1080)
    player.play()

    while player.is_open:
        if not player.poll_events():
            break

        frame = device.get_frame()
        if frame is not None:
            player.enqueue_video_nv12(frame.native_buffer(), frame.timestamp)

    player.close()

webcodecs-py との連携

native_buffer() を渡すことで CVPixelBufferRef を直接扱えます。

import uvc
from webcodecs import VideoEncoder, VideoFrame, VideoPixelFormat

def on_output(chunk):
    print(f"Encoded: {chunk.byte_length} bytes")

def on_error(error):
    print(f"Error: {error}")

with uvc.open(0) as device:
    device.start(1920, 1080, 30, capture_format=uvc.Format.NV12)

    encoder = VideoEncoder(on_output, on_error)
    encoder.configure({
        "codec": "avc1.640028",
        "width": 1920,
        "height": 1080,
        "bitrate": 5_000_000,
        "framerate": 30,
    })

    frame = device.get_frame()
    if frame is not None:
        with VideoFrame(
            frame.native_buffer(),
            {
                "format": VideoPixelFormat.NV12,
                "coded_width": frame.width,
                "coded_height": frame.height,
                "timestamp": frame.timestamp,
            },
        ) as video_frame:
            encoder.encode(video_frame, {"key_frame": True})

    encoder.flush()
    encoder.close()

API リファレンス

モジュール関数

関数 説明
list_devices() 利用可能な UVC デバイスの一覧を取得
open(index_or_info, on_connected, on_disconnected) デバイスを開く

open の引数

  • index_or_info: デバイスインデックス(int)または DeviceInfo
  • on_connected: デバイス接続時のコールバック (macOS / Linux)
  • on_disconnected: デバイス切断時のコールバック (macOS / Linux)

Device

UVC デバイスを操作するクラス。コンテキストマネージャとして使用可能。

with uvc.open(0) as device:
    device.start(1920, 1080, 30, capture_format=uvc.Format.NV12)
メソッド 説明
start(width, height, fps, capture_format, output_format) キャプチャ開始
stop() キャプチャ停止
get_frame() フレーム取得(None の場合あり)
get_supported_formats() サポートされているフォーマット一覧を取得
プロパティ 説明
is_running キャプチャ中かどうか
info デバイス情報(DeviceInfo)

start の引数

  • width: 映像幅
  • height: 映像高さ
  • fps: フレームレート
  • capture_format: キャプチャフォーマット(Format)
  • output_format: 出力フォーマット(省略時は capture_format と同じ)

Frame

キャプチャしたフレームを表すクラス。

メソッド 説明
to_nv12() NV12 フォーマット: (y, uv) タプルを返す
to_yuy2() YUY2 フォーマット: (H, W, 2) を返す
to_rgb() RGB フォーマット: (H, W, 3) を返す
to_rgba() RGBA フォーマット: (H, W, 4) を返す
native_buffer() ネイティブバッファを PyCapsule として取得
プロパティ 説明
width フレーム幅
height フレーム高さ
format フォーマット(Format)
timestamp タイムスタンプ(マイクロ秒)

to_nv12 の戻り値

  • y: Y プレーン(uint8、shape: (H, W))
  • uv: UV インターリーブプレーン(uint8、shape: (H/2, W))

DeviceInfo

デバイス情報を表すクラス。

プロパティ 説明
name デバイス名
unique_id ユニーク ID
index デバイスインデックス

FormatInfo

フォーマット情報を表すクラス。

プロパティ 説明
width 映像幅
height 映像高さ
fps フレームレート
format フォーマット(Format)

Format

映像フォーマットを表す列挙型。

説明
NV12 NV12(Y + UV インターリーブ)
YUY2 YUY2(パックド YUV)
RGB RGB
RGBA RGBA
MJPEG Motion JPEG(将来サポート予定)

ビルド

make develop

サンプルの実行

uv sync --only-group example
make develop
# デバイス一覧を表示
uv run examples/capture.py --list-devices

# 720p 30fps でキャプチャ
uv run examples/capture.py --resolution 720p --fps 30

# NV12 フォーマットでキャプチャ
uv run examples/capture.py --capture-format nv12

# raw-player で映像を表示
uv run examples/capture.py --player

# 100 フレームだけキャプチャ
uv run examples/capture.py --frames 100

# 10 秒間キャプチャ
uv run examples/capture.py --duration 10

Discord

  • サポートはしません
  • アドバイスします
  • フィードバック歓迎します

最新の状況などは Discord で共有しています。質問や相談も Discord でのみ受け付けています。

https://discord.gg/shiguredo

バグ報告

Discord へお願いします。

ライセンス

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.

uvc_py-2026.1.0.dev0-cp314-cp314t-manylinux_2_34_x86_64.whl (96.4 kB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.34+ x86-64

uvc_py-2026.1.0.dev0-cp314-cp314t-manylinux_2_34_aarch64.whl (92.0 kB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.34+ ARM64

uvc_py-2026.1.0.dev0-cp314-cp314t-macosx_15_0_arm64.whl (81.0 kB view details)

Uploaded CPython 3.14tmacOS 15.0+ ARM64

uvc_py-2026.1.0.dev0-cp314-cp314-manylinux_2_34_x86_64.whl (94.3 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.34+ x86-64

uvc_py-2026.1.0.dev0-cp314-cp314-manylinux_2_34_aarch64.whl (88.7 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.34+ ARM64

uvc_py-2026.1.0.dev0-cp314-cp314-macosx_15_0_arm64.whl (77.3 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

uvc_py-2026.1.0.dev0-cp313-cp313-manylinux_2_34_x86_64.whl (94.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

uvc_py-2026.1.0.dev0-cp313-cp313-manylinux_2_34_aarch64.whl (88.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ ARM64

uvc_py-2026.1.0.dev0-cp313-cp313-macosx_15_0_arm64.whl (77.4 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

uvc_py-2026.1.0.dev0-cp312-cp312-manylinux_2_34_x86_64.whl (94.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

uvc_py-2026.1.0.dev0-cp312-cp312-manylinux_2_34_aarch64.whl (88.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ ARM64

uvc_py-2026.1.0.dev0-cp312-cp312-macosx_15_0_arm64.whl (77.5 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

File details

Details for the file uvc_py-2026.1.0.dev0-cp314-cp314t-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for uvc_py-2026.1.0.dev0-cp314-cp314t-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a5f1d80aae06f7d84f04ee0e5bd99c3d573a71d3e1202335946ebe0cc29a399a
MD5 86aaf40f091265cbfb3ed4f5b017ca8b
BLAKE2b-256 d272148bdb199020c96359d293402a6584093a7e9195f68c96d1db0d74753f55

See more details on using hashes here.

Provenance

The following attestation bundles were made for uvc_py-2026.1.0.dev0-cp314-cp314t-manylinux_2_34_x86_64.whl:

Publisher: wheel.yml on shiguredo/uvc-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 uvc_py-2026.1.0.dev0-cp314-cp314t-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for uvc_py-2026.1.0.dev0-cp314-cp314t-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 0078a47d6f1789574715d28e1988659cce911289ece755e65ab87bb6e301c21a
MD5 8f259d2298bf59b57a1e036ca32e8501
BLAKE2b-256 a5214807bb7bce85867aa3c2cea5bb9f6aa281c87214982ae0a15188a66f0b90

See more details on using hashes here.

Provenance

The following attestation bundles were made for uvc_py-2026.1.0.dev0-cp314-cp314t-manylinux_2_34_aarch64.whl:

Publisher: wheel.yml on shiguredo/uvc-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 uvc_py-2026.1.0.dev0-cp314-cp314t-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for uvc_py-2026.1.0.dev0-cp314-cp314t-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f2bf6965e46fb200a641a148a0beff44c6ffc6255ad52c7e9eb3ec0aa749e3c4
MD5 13c09a28c9a0859679b96a2bd10d5ea9
BLAKE2b-256 62a421492a43d4fff7ca06d340c427ffdc740700a9b3134929fc7cf398ee0a93

See more details on using hashes here.

Provenance

The following attestation bundles were made for uvc_py-2026.1.0.dev0-cp314-cp314t-macosx_15_0_arm64.whl:

Publisher: wheel.yml on shiguredo/uvc-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 uvc_py-2026.1.0.dev0-cp314-cp314-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for uvc_py-2026.1.0.dev0-cp314-cp314-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 3054fe05f12b184aa7515f43ebe0c3c17129be0a7d793a703ffaeab5c01a2845
MD5 44e16cc0af2e2845cf3491d2fd852b6a
BLAKE2b-256 92a1aa57086b793792bcf6c38956c8948806fdbb8a8939e0c9a2a2456504db3e

See more details on using hashes here.

Provenance

The following attestation bundles were made for uvc_py-2026.1.0.dev0-cp314-cp314-manylinux_2_34_x86_64.whl:

Publisher: wheel.yml on shiguredo/uvc-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 uvc_py-2026.1.0.dev0-cp314-cp314-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for uvc_py-2026.1.0.dev0-cp314-cp314-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 1bcf336b022f79634bfe478310605dd6ee7c6abe3131ac7e1c30d3736601046d
MD5 be0c4ffdf94bfbda060f0ea4a422f78d
BLAKE2b-256 5da9564be11336254491e3813e81e37093c9dea08833cf63edeeebb215b7c54f

See more details on using hashes here.

Provenance

The following attestation bundles were made for uvc_py-2026.1.0.dev0-cp314-cp314-manylinux_2_34_aarch64.whl:

Publisher: wheel.yml on shiguredo/uvc-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 uvc_py-2026.1.0.dev0-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for uvc_py-2026.1.0.dev0-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 da547b6dbb997cc0753959a4bc13325f66258bcf039aee911189c89a2087faa2
MD5 63377ba263a43e7ce70950e52dd66c8e
BLAKE2b-256 f3a95445a956dc7d9f94f9c9bcd56874b78b5245e65198008b6f18bda45e1691

See more details on using hashes here.

Provenance

The following attestation bundles were made for uvc_py-2026.1.0.dev0-cp314-cp314-macosx_15_0_arm64.whl:

Publisher: wheel.yml on shiguredo/uvc-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 uvc_py-2026.1.0.dev0-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for uvc_py-2026.1.0.dev0-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 f662facda624cf2bf70622b29acd543a3654e04d1a67658221a989c48e6b8d66
MD5 81521ef3a0279bbd910900c2c4864db6
BLAKE2b-256 d195c4e65298c4612abd14f59ba8bb076b853cde5bd30e976ee4d25545c8cdd5

See more details on using hashes here.

Provenance

The following attestation bundles were made for uvc_py-2026.1.0.dev0-cp313-cp313-manylinux_2_34_x86_64.whl:

Publisher: wheel.yml on shiguredo/uvc-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 uvc_py-2026.1.0.dev0-cp313-cp313-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for uvc_py-2026.1.0.dev0-cp313-cp313-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 219e0a2f9cf2b85ba7dd26979b1d328bfa4751f5c3aad63de46bd455ace658d4
MD5 152f169ea86565d51f3552075ac82bc6
BLAKE2b-256 3d142cd78ad967630b6ee78f44af854505aabd204e7c78f0609fec4b2bb3439b

See more details on using hashes here.

Provenance

The following attestation bundles were made for uvc_py-2026.1.0.dev0-cp313-cp313-manylinux_2_34_aarch64.whl:

Publisher: wheel.yml on shiguredo/uvc-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 uvc_py-2026.1.0.dev0-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for uvc_py-2026.1.0.dev0-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f89a4a71feb35fcceca9d750459a39991ca8d977dfa28e758bd909986f1e4765
MD5 1673d9c0bd363ce14d1ee20dc6719a4c
BLAKE2b-256 2ecf96a89dbc7016941dc007ebf8ec61d1824a0d4d9fc25449675dc9440eb677

See more details on using hashes here.

Provenance

The following attestation bundles were made for uvc_py-2026.1.0.dev0-cp313-cp313-macosx_15_0_arm64.whl:

Publisher: wheel.yml on shiguredo/uvc-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 uvc_py-2026.1.0.dev0-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for uvc_py-2026.1.0.dev0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a3512d3b463a6e0c83d2b0d826987a303d6921f392881959839116c890b67ca0
MD5 9c4ed998cedaee15df5b2220654878bd
BLAKE2b-256 0bf819b5c770d410df69aaa8783256e8187859fab4839febb8a83499fc691a9c

See more details on using hashes here.

Provenance

The following attestation bundles were made for uvc_py-2026.1.0.dev0-cp312-cp312-manylinux_2_34_x86_64.whl:

Publisher: wheel.yml on shiguredo/uvc-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 uvc_py-2026.1.0.dev0-cp312-cp312-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for uvc_py-2026.1.0.dev0-cp312-cp312-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 4b73589f85812554410876630dd5bd06d58a9405814ea04d435b154c9f14686b
MD5 9779643dd39d0adf189c31a93ae9fad1
BLAKE2b-256 80933c974efe5f8f5af4775c0062ed7498281c4bf771abfa413d7392286234c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for uvc_py-2026.1.0.dev0-cp312-cp312-manylinux_2_34_aarch64.whl:

Publisher: wheel.yml on shiguredo/uvc-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 uvc_py-2026.1.0.dev0-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for uvc_py-2026.1.0.dev0-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 00b447e96d8db565d22e7dd33ce1b947c40c777695b09aa7590b521ee903e7ad
MD5 d43432a768a4afd5acf11c3bf52e0d89
BLAKE2b-256 e972ea7fcecbb8d2fe6846522af0e8d23a8039068d5b468429efc6a154257556

See more details on using hashes here.

Provenance

The following attestation bundles were made for uvc_py-2026.1.0.dev0-cp312-cp312-macosx_15_0_arm64.whl:

Publisher: wheel.yml on shiguredo/uvc-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