Skip to main content

Library for simple video reading and writing

Project description

vrw: A Simple Library for Video Reading and Writing

PyPI

vrw is a lightweight Python library for reading and writing video files with slicing support. It provides a simple interface for handling video frames using NumPy arrays and supports multiple backends like OpenCV and PyAV.


Installation

To install vrw, use the following command:

pip install vrw

Usage

Reading Videos

The VideoReader class allows you to read video files and access frames with slicing and indexing.

from vrw import VideoReader

# Open a video file
vr = VideoReader("video.mp4")

# Access video properties
print(vr.shape)  # (N, H, W, C): N frames, H height, W width, C channels
print(vr.fps)    # Frames per second
print(vr.dtype)  # Data type of the frames

# Access frames using slicing
print(vr[:, 0].shape)         # (N, W, C): All frames, first row
print(vr[::2, :, -1].shape)   # (N // 2, H, C): Every second frame, last column
print(vr[..., 0].shape)       # (N, H, W): All frames, first channel
print(vr[[1, 3, 3, 7], :42].shape)  # (4, 42, W, C): Specific frames, first 42 rows

# Reading grayscale frames
vr_gray = VideoReader("video.mp4", to_gray=True)
print(vr_gray.shape)  # (N, H, W): Grayscale frames

Note that advanced indexing involving the 0th axis has not been implemented yet.

Writing Videos

The VideoWriter class allows you to write video files frame by frame.

Writing RGB Frames

from vrw import VideoWriter
import numpy as np

# Write RGB frames to a video file
with VideoWriter("video.mp4", fps=30) as vw:
    for i in range(256):
        im = np.full((256, 256, 3), i, dtype=np.uint8)
        im[..., 0], im[..., 1] = np.mgrid[:256, :256]
        vw.write(im)

Writing Grayscale Frames

# Write grayscale frames to a video file
with VideoWriter("video.mp4", fps=30) as vw:
    for i in range(256):
        vw.write(np.full((256, 256), i, dtype=np.uint8))

Using the PyAV Backend

The PyAV backend allows you to use advanced codecs (e.g., libx264) for video writing. Ensure that FFmpeg is installed and built with the required codecs.

# Use the PyAV backend with a specific codec
with VideoWriter("video.mp4", fps=30, backend="pyav", codec="libx264") as vw:
    for i in range(256):
        im = np.full((256, 256, 3), i, dtype=np.uint8)
        im[..., 0], im[..., 1] = np.mgrid[:256, :256]
        vw.write(im)

Backends

vrw supports the following backends

  • OpenCV: Default backend for reading and writing videos.
  • PyAV: Alternative backend for advanced codec support (requires FFmpeg built with the necessary codecs).

To specify a backend, use the backend parameter (e.g., backend="cv2" or backend="pyav") when creating a VideoReader or VideoWriter instance.

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

vrw-0.2.0.tar.gz (339.4 kB view details)

Uploaded Source

Built Distribution

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

vrw-0.2.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file vrw-0.2.0.tar.gz.

File metadata

  • Download URL: vrw-0.2.0.tar.gz
  • Upload date:
  • Size: 339.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for vrw-0.2.0.tar.gz
Algorithm Hash digest
SHA256 21f5c1913c1a3c4c6f3456e4ebc29106a9a4b0d2291ec979a2ee03e14794ebcb
MD5 fa3de48e12074ce8ead94fb490d95630
BLAKE2b-256 4c2b745f8b1aedebd5467dd3512b877609e8bf49138c64b521a6d843d6356a5f

See more details on using hashes here.

File details

Details for the file vrw-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: vrw-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for vrw-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df06d1907b85f3d7e1900d2021cdd7b293aaf6488253f014a0c57fec7ededfc0
MD5 712556219a9dd1e5b8b050be31693c4b
BLAKE2b-256 295955dddfff500eb93660daa75ddc88cca7d2dfaddba955198b3d125276b761

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