Skip to main content

Simple video reading

Project description

mvid

mvid is a simple library to access videos by frame index and return NumPy arrays.

import mvid

with mvid.Video("myvideo.mp4") as video:
    # get the number of frames
    print(len(video))

    # random access
    frame = video[57]  # frame is a NumPy array
    
    print(frame.shape)  # (H, W, 3)
    
    # iterate over all frames in the video
    for frame in video:
        pass

It uses PyAV (with minimal to no overhead) and abstracts away seeking logic for you.

We also give a simple Recorder to write from NumPy arrays

import numpy as np
import mvid

with mvid.Recorder("output.mp4", fps=50) as rec:
    
    # record 1 second of gray
    for _ in range(50):
        rec(128 * np.ones(1080, 1920, 3))

Installation

pip install mvid

It requires PyAV and NumPY.

How it works

Frame lookup is based on decoding from the nearest preceding keyframe up to the requested index. We determine that index using each frame’s timestamp together with the stream’s frame rate. This approach works well for videos with consistent timing metadata, but not all files follow those assumptions. Some containers use variable frame rates or contain incomplete or inconsistent timestamps. In those cases there is no reliable way to infer a stable frame index without first scanning every frame and assigning indices explicitly. Rather than performing that preprocessing step, we intentionally crash when encountering timing metadata that cannot be interpreted unambiguously.

Performance

Generally speaking, sequential access is as fast as possible thanks to PyAV. Check benchmark.py and compare with ffmpeg -i <my_video> -f null -. The benchmarking script will also try random access and various thread parameters so you can see what performance to expect.

There is overhead from conversion to NumPY arrays. We also provide a more "raw" AVVideo class that performs all the bookkeeping without NumPY conversion.

Related projects

TorchCodec is a more heavy-duty library that returns PyTorch tensors. It also has index-based access (among other options). It requires managing your installation of ffmpeg.

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

mvid-0.2.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

mvid-0.2.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mvid-0.2.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for mvid-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9859d12e67d1565e646e9ee66dab8869f2d2aab048fea895569ff7ccb0ce3ba3
MD5 beda2a6e6028c1b1d7e85372e17825ec
BLAKE2b-256 6a8fa7b95d6364fc97019c09948b7bc6827ee1a3d47b934b1f3d390c390e45e9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mvid-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for mvid-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 803ddfb58c9217994ab9b345ce537326219f852b0bab57b7f3bf7fa084a4bbde
MD5 8c2743e5620b2b10214c270acf61a0fd
BLAKE2b-256 4e31de77cc1b9508114152454fd4a94b72b32fec3ee743afd50f6b5e77550fc8

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