Skip to main content

Library for simple video reading and writing

Project description

vrw: A Simple Library for Video Reading and Writing

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:

git clone https://github.com/tkclam/vrw
cd vrw
pip install .

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.1.0.tar.gz (3.8 MB view details)

Uploaded Source

Built Distribution

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

vrw-0.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: vrw-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.13

File hashes

Hashes for vrw-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4a3068142124a9c7fd67e5d3c7b661069845b0bbb635889ea6af738b98a5421b
MD5 c23af05e07202d89cba29113e05e7396
BLAKE2b-256 a9e04d3303ebc1e97942d73f593507d928b87f2560f60406a21461ff75e07817

See more details on using hashes here.

File details

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

File metadata

  • Download URL: vrw-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.13

File hashes

Hashes for vrw-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 16df51eaa2992a45ba0e00d9712db0700e9fa14f71bbee6871e894ccc78f67e4
MD5 df2d6de3529a334ca529ca27a59ed09e
BLAKE2b-256 18a66ff1c9ac78597eeeafb9b8e43f7d2a0c5d7599c2778b688c674e67e551ab

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