Skip to main content

No project description provided

Project description

Video Reader RS

A python module to decode videos based on rust ffmpeg-next, with a focus on ML use cases.

Why yet another library based on ffmpeg ?

When training ML models on videos, it is usefull to load small sub-clips of videos. So decoding the entire video is not necessary.

The great decord library seems to be unmaintained, while having a few issues. The main one (for us) is bad memory management, which makes it crash on large videos. Indeed it allocates memory for the whole video when instantiating a VideoReader object. While in fact you might want to only get a few frames from this video.

So we took great inspiration from this library to rewrite the get_batch function using ffmpeg-next rust bindings. We also added the decode function which is usefull for decoding the entire video or for temporally reducing it using a compression_factor. Option to resize the video while decoding is also added.

NOTE: other functionalities of decord are not implemented (yet?).

Benchmark indicates that video_reader-rs is performing equally or better than decord, while using less memory. At least on the intended ML uses cases where video resolution remains reasonable, eg not 4K videos.

Installation

Install via pip

pip install video-reader-rs

Should work with python >= 3.8 on recent linux x86_64, macos and windows.

Manual installation

You need to have ffmpeg installed on your system. Install maturin:

pip install maturin

Activate a virtual-env where you want to use the video_reader library and build the library as follows:

maturin develop --release

maturin develop builds the crate and installs it as a python module directly in the current virtualenv. the --release flag ensures the Rust part of the code is compiled in release mode, which enables compiler optimizations.

:warning: If you are using a version of ffmpeg >= 6.0 you need to enable the ffmpeg_6_0 feature:

maturin develop --release --features ffmpeg_6_0

Usage

Decoding a video is as simple as:

import video_reader
frames = video_reader.decode(filename, resize, compression_factor, threads, start_frame, end_frame)
  • filename: path to the video file to decode
  • resize: optional resizing for the video.
  • compression_factor: temporal sampling, eg if 0.25, take 25% of the frames, evenly spaced.
  • threads: number of CPU cores to use for ffmpeg decoding, 0 means auto (let ffmpeg pick the optimal number).
  • start_frame - Start decoding from this frame index
  • end_frame - Stop decoding at this frame index

Returns a numpy array of shape (N, H, W, C).

We can do the same thing if we want grayscale frames, and it will retun an array of shape (N, H, W).

frames = video_reader.decode_gray(filename, resize, compression_factor, threads, start_frame, end_frame)

If we only need a sub-clip of the video we can use the get_batch function:

frames = video_reader.get_batch(filename, indices, threads=0, resize_shorter_side=None, with_fallback=False)
  • filename: path to the video file to decode
  • indices: list of indices of the frames to get
  • threads: number of CPU cores to use for ffmpeg decoding, currently has no effect as get_batch does not support multithreading. (NOTE: it is still as fast as decord from our benchmarking)
  • resize_shorter_side: optional resizing for the video.
  • with_fallback: False by default, if True will fallback to decoding without seeking (ie slower) if suspicious metadata is detected in the video, eg multiple key frames have pts <= 0, first key frames duration <= 0, etc. This might be usefull if your application requires you to be 100% sure you get the exact frames you asked for.

We can also get the shape of the raw video

(n, h, w) = video_reader.get_shape(filename)

We can encode the video with h264 codec

video_reader.save_video(frames, "video.mp4", fps=15, codec="h264")

Performance comparison

Decoding a video with shape (2004, 1472, 1472), using a compression factor of 0.25

Without resizing

using OpenCV

Video shape after loading: (501, 1472, 1472), took 52.05 sec.

Using decord

Video shape after loading: (501, 1472, 1472), took 17.20 sec.

video_reader decode

Video shape after loading: (501, 1472, 1472), took 9.32 sec.

With resizing to 512 while decoding

using OpenCV

Video shape after loading: (501, 512, 512), took 52.49 sec.

using decord

Video shape after loading: (501, 512, 512), took 13.91 sec.

using video_reader decode

Video shape after loading: (501, 512, 512), took 5.13 sec.

Crash test

Tested on a laptop with 15Gb of RAM, with ubuntu 22.04 and python 3.10. Run this script:

import video_reader
from time import time

def bench_video_decode(filename, compress_factor, resize):
    start =  time()
    vid = video_reader.decode(filename, resize_shorter_side=resize, compression_factor=compress_factor, threads=0)
    duration = time() - start
    print(f"Duration {duration:.2f}sec")
    return vid

vid = bench_video_decode("sample.mp4", 0.25, 1500)
print("video shape:", vid.shape)

# Terminal output:
# Duration 4.81sec
# video shape: (501, 1472, 1472, 3)

And then run this script:

from decord import VideoReader

vr = VideoReader("sample.mp4")

# Terminal output:
# terminate called after throwing an instance of 'std::bad_alloc'
#  what():  std::bad_alloc
# [1]    9636 IOT instruction (core dumped)

Credits

  • decord for showing how to get_batch efficiently.
  • ffmpeg-next for the Rust bindings to ffmpeg.
  • video-rs for the nice high level api which makes it easy to encode videos.

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

video_reader_rs-0.1.4.tar.gz (23.8 kB view details)

Uploaded Source

Built Distributions

video_reader_rs-0.1.4-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.4-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.4-cp312-none-win_amd64.whl (699.6 kB view details)

Uploaded CPython 3.12 Windows x86-64

video_reader_rs-0.1.4-cp312-cp312-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.4-cp312-cp312-macosx_11_0_arm64.whl (792.1 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

video_reader_rs-0.1.4-cp312-cp312-macosx_10_12_x86_64.whl (836.1 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

video_reader_rs-0.1.4-cp311-none-win_amd64.whl (700.3 kB view details)

Uploaded CPython 3.11 Windows x86-64

video_reader_rs-0.1.4-cp311-cp311-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.4-cp311-cp311-macosx_11_0_arm64.whl (792.1 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

video_reader_rs-0.1.4-cp311-cp311-macosx_10_12_x86_64.whl (836.1 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

video_reader_rs-0.1.4-cp310-none-win_amd64.whl (700.3 kB view details)

Uploaded CPython 3.10 Windows x86-64

video_reader_rs-0.1.4-cp310-cp310-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.4-cp310-cp310-macosx_11_0_arm64.whl (792.7 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

video_reader_rs-0.1.4-cp39-none-win_amd64.whl (700.7 kB view details)

Uploaded CPython 3.9 Windows x86-64

video_reader_rs-0.1.4-cp39-cp39-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.4-cp39-cp39-macosx_11_0_arm64.whl (792.4 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

video_reader_rs-0.1.4-cp38-none-win_amd64.whl (700.0 kB view details)

Uploaded CPython 3.8 Windows x86-64

video_reader_rs-0.1.4-cp38-cp38-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.28+ x86-64

File details

Details for the file video_reader_rs-0.1.4.tar.gz.

File metadata

  • Download URL: video_reader_rs-0.1.4.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.6.0

File hashes

Hashes for video_reader_rs-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d1887808a878aff7ab6c727011ae81c70206724db6ea32189048b9f80e47160b
MD5 f6c034883ff9a54666b1a847b26545f6
BLAKE2b-256 493057a522aa2cbca359f8cdce05ad9a9c721ca9fca5edf9c0ae1490d29913bb

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1d31a55df84074aec465042ef64301a4f9125f464e22d345c537cb7210b853be
MD5 376eb8ee267afaabcddcdf3609bafa9a
BLAKE2b-256 7ea084afd8af6971f98e6b0ad282af7426e6de31e78878ccfe9c659c6a63e206

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 968f43d5a0775edc3c37440189312b3690556207ee65ce65c1d7476afee4c59f
MD5 7ac2ffa91f46e436ba0d79dfd540ae3c
BLAKE2b-256 b41fb1b3554c7a187f8c234609973a9b7b1973c65d9516272919cd1113d11fc5

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp312-none-win_amd64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 169bd347d7856bc6315b88267e9108038ee9418fd9bfde38ec3cbd27a675616e
MD5 27cc67b7f0852452f697f382aa2dcc04
BLAKE2b-256 95121a553b74702419570e06e2bc1c953b67466890a718255a042e4831c0010e

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 338b7ffcc59ed8117c27461458be1f87d0ab29a6f3fc29b2db5d87d10ec95c77
MD5 a6253ee672e36cda737054621f36994c
BLAKE2b-256 ffc390e61cfed03744df36d7f382bcc99e3e1cde0fe7be7011a922c2bcfbaa87

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b81d3f3b25cf0a5f70d546e002467012227ff96cb203f11c2f037b86f399d425
MD5 2d970cda9a0dcbd5e5d09674416cc47e
BLAKE2b-256 80763adc487ffef6b97f54d0357aea1ac05ca2b5fdc807bbb7062736c11c80d3

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 baf649378744a1b69ef7b2d8872edbe33f8b480e88f49679adba9bb9de59592a
MD5 ff48c260b8e3c2871afd883fe0d5bfbb
BLAKE2b-256 5dab9382894c52ebbdc452dc8e916684005bf493dd840e8c392efcb49e35e91a

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp311-none-win_amd64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 2514cd35de4c893d0ed9342f65d9fe257610380dbc0ac90ea277269c7be8239f
MD5 a32451722d2f2b5c02a2685d2d4017ef
BLAKE2b-256 84b0d53af7aa28855c474c1024c89b6e0d79ef829ee0bdbc2ff9d9a3cf4f6770

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 861641e20881dfbe3167b684d36556ef25cae9f4c614d0a708f3e210c9d401f0
MD5 181e5fe6eff69650e9ea44df0afeea88
BLAKE2b-256 817ef1fc8ba3641a423aa37ae83ba23f4aa86af51704b4f252a2e55a7c625c8e

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4ae77bd9df8971878b3254b29fb793e65b967d30675819b0cda135f988ee7f38
MD5 979bf45f05c10928e5fdf78be5d7e1f6
BLAKE2b-256 2ed27377799b2f14ffe7e7245586352f5f709bcc9bfb98080f789878b69ab006

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6fc72292312aa7c3b42e737862de2e38865a34db1403140254f0a58075703d96
MD5 b437e0db9b87b4a487227e5ea768d55f
BLAKE2b-256 ad87a89f0762183f93799431313c2cc5b19dfad040187755daf5e830c2d7e6aa

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 d12286eb3526ff60fce497a888051fc04bd962ccd9e1ad077d52b60859384412
MD5 313a177a4e82571222018202f9cdc61c
BLAKE2b-256 37621e3b5ffd52ae860ba87f213198d7d82d6e1b8070e930f0e3539dec6eef00

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e9aee07b40e42e890e02a6e4caf4759564999856fd4bb515af4047888892b97a
MD5 10ce6fac5d44f3e9f6f91e81c7aceb17
BLAKE2b-256 a476f87ec4973ac460aa0c41b1f71320ead9230c0e0df538ab708bd7fcf183d6

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 749ea68b9d69e87e4ce58af12ce4f5c41a61f5473a0e9fa608d5ddee98c2eb35
MD5 05cf609f518487eed5922701a3f49095
BLAKE2b-256 2134d2568515419d455a4d23b822a1ab9c6524fef0ed98b6cab3dc3a37e6d311

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp39-none-win_amd64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 e8806bb8d045257a8e38d161eaabc860b6f75290250fcab96a406282140fa20d
MD5 df6c719cdfbe27e88f70da4a3f410cc8
BLAKE2b-256 860c23d0efff15eb69950ad75b35dd75f1585fc39fcc13586cf345e14039e056

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ed1c5efc689b032c44b01ce56b7eef40b85aca5fb3790e093778de324b397693
MD5 df4a7867f1db620c88391438b6f6e4c9
BLAKE2b-256 14a18596dfbda8cab8431c11bff078fa60cf7a7562715978f09d80eb223760c9

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2365103566e91267011ed6aab1035fcf88e733f6982aa963265626c34ea5d16e
MD5 a30490464132779454969bd327151b8b
BLAKE2b-256 109cbe6f7501857fb9f08c88bb727e4eb408a3f1bcb71a2c2d6ab4a149cf59d6

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp38-none-win_amd64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 3496be0e5b19297ece53f20771a043bd08b231512d07a308b2f854a2d840f00e
MD5 65cebd7a264126b3bdf06ee3f1a6aec3
BLAKE2b-256 be0e641762b3857355e68cbf76f70613419e47f2b44a3892ce7de6b4d519fc1e

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.4-cp38-cp38-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.4-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dde6bce84e123ae89e3dbff16652d2b2c107e9d454e15de6116cd7d5dd403dac
MD5 33f60092ba0890ae9d0c112029206d74
BLAKE2b-256 3fe4927b87d8eb43a60c842794ca90ed98c4f7fecbc9fdf7f26eb777e1898273

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page