Skip to main content

Add your description here

Project description

MediaRef

Pydantic-based media reference for images and video frames. Supports file paths, URLs, data URIs, and video timestamps. Designed for dataset metadata and lazy loading.

Installation

# Core package (MediaRef definition only)
pip install -e .

# With loader support (includes batch loading, video decoders)
pip install -e ".[loader]"

Usage

from mediaref import MediaRef, load_batch

# Reference creation - supports multiple URI schemes
MediaRef(uri="image.png")                              # Local file
MediaRef(uri="https://example.com/image.jpg")          # Remote URL
MediaRef(uri="video.mp4", pts_ns=1_000_000_000)        # Video frame at 1.0s
MediaRef(uri="data:image/png;base64,...")              # Embedded data URI

# Loading
ref.to_rgb_array()                                     # Returns (H, W, 3) numpy array
ref.to_pil_image()                                     # Returns PIL.Image

# Batch loading with automatic caching (default: PyAV decoder)
refs = [MediaRef(uri="video.mp4", pts_ns=i*1e9) for i in range(10)]
frames = load_batch(refs)                              # Reuses video container

# Use TorchCodec decoder for GPU acceleration (requires torchcodec>=0.4.0)
frames = load_batch(refs, decoder="torchcodec")

# Embedding
data_uri = ref.embed_as_data_uri(format="png")         # Encode to data URI
MediaRef(uri=data_uri)                                 # Create from data URI

# Path resolution for MCAP/rosbag datasets
ref = MediaRef(uri="relative/video.mkv", pts_ns=123456)
ref.resolve_relative_path("/data/recording.mcap")      # Returns absolute path

# Serialization (Pydantic-based)
ref.model_dump()                                       # {'uri': '...', 'pts_ns': ...}
ref.model_dump_json()                                  # '{"uri":"...","pts_ns":...}'
MediaRef.model_validate(data)                          # From dict
MediaRef.model_validate_json(json_str)                 # From JSON string

API Reference

MediaRef(uri: str, pts_ns: int | None = None)

Properties: is_embedded, is_video, is_remote, is_local, is_relative_path

Methods:

  • to_rgb_array(**kwargs) -> np.ndarray - Load as RGB array (H, W, 3)
  • to_pil_image(**kwargs) -> PIL.Image - Load as PIL Image
  • embed_as_data_uri(format="png", quality=None) -> str - Encode to data URI
  • resolve_relative_path(base_path, allow_nonlocal=False) -> MediaRef - Resolve relative paths
  • validate_uri() -> bool - Check if URI exists (local files only)
  • model_dump() -> dict - Serialize to dict
  • model_dump_json() -> str - Serialize to JSON
  • model_validate(data) -> MediaRef - Deserialize from dict
  • model_validate_json(json_str) -> MediaRef - Deserialize from JSON

Functions

  • load_batch(refs: list[MediaRef], **kwargs) -> list[np.ndarray] - Batch load with caching
  • cleanup_cache() - Clear video container cache

Video Decoders (requires [loader] extra)

  • PyAVVideoDecoder(video_path) - PyAV-based decoder with TorchCodec-compatible interface
  • TorchCodecVideoDecoder(video_path) - TorchCodec-based decoder (requires torchcodec>=0.4.0)

Design Notes

  • Video container caching uses reference counting with LRU eviction (default: 10 containers)
  • MCAP file path resolution: detects .mcap suffix and uses parent directory as base
  • Garbage collection triggered every 10 PyAV operations to handle reference cycles
  • Cache size configurable via AV_CACHE_SIZE environment variable

Acknowledgments

The video decoder interface design references TorchCodec's API design.

Dependencies

Core: pydantic>=2.0 (requires Pydantic v2 API)

Loader (optional): numpy, opencv-python, pillow, av, requests

The loader dependencies use stable APIs with no version constraints. Install with pip install mediaref[loader] to enable batch loading and video decoding.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mediaref-0.1.0.tar.gz (24.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mediaref-0.1.0-py3-none-any.whl (25.9 kB view details)

Uploaded Python 3

File details

Details for the file mediaref-0.1.0.tar.gz.

File metadata

  • Download URL: mediaref-0.1.0.tar.gz
  • Upload date:
  • Size: 24.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.5

File hashes

Hashes for mediaref-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e53f9de63cd0cea7f6c6c0e099ab63fd50934cf6c96421520368ca0432bfa78c
MD5 a93b5b6cddb2abc04efe393571496d5b
BLAKE2b-256 328698384f71d123a089428627c999609225fa38bdbc765e6f58f33826dec526

See more details on using hashes here.

File details

Details for the file mediaref-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mediaref-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 25.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.5

File hashes

Hashes for mediaref-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1b8ee31724479850fc5adb812ffcb1bb39fbc9255d596ab12e8728b5917469bc
MD5 6f10562756afc9cc4ba22d9a65c38fb4
BLAKE2b-256 395f5b10e6227ee7bb5486a7c69a36692ed0fdc72cba0bc40b392e5bbfd3c1cf

See more details on using hashes here.

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