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 with image loading support
pip install mediaref
# With video support (adds PyAV for video frame extraction)
pip install mediaref[video]
Usage
from mediaref import MediaRef, batch_decode
# 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 decoding with automatic caching (requires [video] extra)
refs = [MediaRef(uri="video.mp4", pts_ns=int(i*1e9)) for i in range(10)]
frames = batch_decode(refs) # Default: PyAV decoder
# Use TorchCodec decoder for GPU acceleration (requires torchcodec>=0.4.0)
frames = batch_decode(refs, decoder="torchcodec")
# Use batch decoding strategy (PyAV only)
from mediaref.video_decoder import BatchDecodingStrategy
frames = batch_decode(refs, strategy=BatchDecodingStrategy.SEQUENTIAL)
# 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 Imageembed_as_data_uri(format="png", quality=None) -> str- Encode to data URIresolve_relative_path(base_path, allow_nonlocal=False) -> MediaRef- Resolve relative pathsvalidate_uri() -> bool- Check if URI exists (local files only)model_dump() -> dict- Serialize to dictmodel_dump_json() -> str- Serialize to JSONmodel_validate(data) -> MediaRef- Deserialize from dictmodel_validate_json(json_str) -> MediaRef- Deserialize from JSON
Functions
batch_decode(refs, strategy=None, decoder="pyav", **kwargs) -> list[np.ndarray]- Batch decode using optimized batch decoding APIrefs: List of MediaRef objects to decodestrategy: Batch decoding strategy (PyAV only):SEPARATE,SEQUENTIAL, orSEQUENTIAL_PER_KEYFRAME_BLOCKdecoder: Decoder backend ("pyav"or"torchcodec")
cleanup_cache()- Clear video container cache (PyAV only)
Video Decoders (requires [video] extra)
PyAVVideoDecoder(source)- PyAV-based decoder with batch decoding strategies- Supports batch decoding strategies:
SEPARATE,SEQUENTIAL,SEQUENTIAL_PER_KEYFRAME_BLOCK - CPU-based decoding using FFmpeg
- Automatic container caching with reference counting
- Supports batch decoding strategies:
TorchCodecVideoDecoder(source)- TorchCodec-based decoder for GPU acceleration- Requires
torchcodec>=0.4.0(install separately) - GPU-accelerated decoding with CUDA support
- Does not support batch decoding strategies (parameter ignored)
- Requires
Decoder Comparison:
| Feature | PyAVVideoDecoder | TorchCodecVideoDecoder |
|---|---|---|
| Batch decoding strategies | ✅ Full support | ❌ Not supported (ignored) |
| GPU acceleration | ❌ CPU only | ✅ CUDA support |
| Backend | PyAV (FFmpeg) | TorchCodec (FFmpeg) |
| Installation | pip install mediaref[video] |
pip install torchcodec>=0.4.0 |
When to use:
- Use
PyAVVideoDecoder(default) for fine-grained control over batch decoding strategies - Use
TorchCodecVideoDecoderfor GPU-accelerated decoding when processing large batches
Design Notes
- Video container caching: Uses reference counting with LRU eviction (default: 10 containers)
- MCAP file path resolution: Detects
.mcapsuffix and uses parent directory as base path - Garbage collection: Triggered every 10 PyAV operations to handle FFmpeg reference cycles
- Cache size: Configurable via
AV_CACHE_SIZEenvironment variable - Lazy loading: Video dependencies only imported when needed (not at module import time)
Acknowledgments
The video decoder interface design references TorchCodec's API design.
Dependencies
Core dependencies (automatically installed):
pydantic>=2.0- Data validation and serialization (requires Pydantic v2 API)numpy- Array operationsopencv-python- Image loading and color conversionpillow>=9.4.0- Image loading from various sourcesrequests>=2.32.2- HTTP/HTTPS URL loadingloguru- Logging (disabled by default for library code)
Optional dependencies:
[video]extra:av>=15.0(PyAV for video frame extraction)- TorchCodec:
torchcodec>=0.4.0(install separately for GPU-accelerated decoding)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mediaref-0.2.0.tar.gz.
File metadata
- Download URL: mediaref-0.2.0.tar.gz
- Upload date:
- Size: 39.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92585d9fdd7b7933425fdb1f40fca26d89011eceb07fb69ab2d3403538bfde8e
|
|
| MD5 |
38c1b8e6f6f9780c595b75ac61850b80
|
|
| BLAKE2b-256 |
bdd0901aef6cd1352604187c7ba0571c06fed62040ab7072a2df5afda3381156
|
File details
Details for the file mediaref-0.2.0-py3-none-any.whl.
File metadata
- Download URL: mediaref-0.2.0-py3-none-any.whl
- Upload date:
- Size: 29.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f40993e170b015db768efdf6b71061e32e880437a61348d08a3d91384e38dba
|
|
| MD5 |
5a7f05b528399e32780548073451d3d0
|
|
| BLAKE2b-256 |
5fe450ed8a512b1b01517b6f262acb7655dffbd7ba02428d63f14f8c321c84ee
|