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.

:bulb: 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.

:hammer_and_wrench: 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

:computer: Usage

Decoding a video is as simple as:

import video_reader as vr
frames = vr.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 = vr.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 = vr.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) = vr.get_shape(filename)

Or get a dict with information about the video, returned as Dict[str, str]

info_dict = vr.get_info(filename)
print(info_dict["fps"])

We can encode the video with h264 codec

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

:rocket: Performance comparison

Decoding a video with shape (2004, 1472, 1472, 3). Tested on a laptop (12 cores Intel i7-9750H CPU @ 2.60GHz), 15Gb of RAM with Ubuntu 22.04.

Options:

  • f: compression factor
  • r: resize shorter side
  • g: grayscale
Options OpenCV decord* video_reader
f 0.5 33.96s 14.6s 26.76s
f 0.25 7.16s 14.03s 6.73s
f 0.25, r 512 6.49s 13.33s 3.92s
f 0.25, g 20.24s 25.67s 14.11s

* decord was tested on a machine with more RAM and CPU cores because it was crashing on the laptop with only 15Gb. See below.

:boom: Crash test

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

import video_reader as vr
from time import time

def bench_video_decode(filename, compress_factor, resize):
    start =  time()
    vid = vr.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)
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)

:stars: 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 and for the code snippet to convert ffmpeg frames to ndarray ;-)

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.7.tar.gz (24.7 kB view details)

Uploaded Source

Built Distributions

video_reader_rs-0.1.7-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (11.6 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.7-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl (11.6 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.7-cp312-none-win_amd64.whl (591.0 kB view details)

Uploaded CPython 3.12 Windows x86-64

video_reader_rs-0.1.7-cp312-cp312-manylinux_2_28_x86_64.whl (11.6 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.7-cp312-cp312-macosx_11_0_arm64.whl (639.8 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

video_reader_rs-0.1.7-cp312-cp312-macosx_10_12_x86_64.whl (711.9 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

video_reader_rs-0.1.7-cp311-none-win_amd64.whl (591.2 kB view details)

Uploaded CPython 3.11 Windows x86-64

video_reader_rs-0.1.7-cp311-cp311-manylinux_2_28_x86_64.whl (11.6 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.7-cp311-cp311-macosx_11_0_arm64.whl (640.2 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

video_reader_rs-0.1.7-cp311-cp311-macosx_10_12_x86_64.whl (712.4 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

video_reader_rs-0.1.7-cp310-none-win_amd64.whl (591.4 kB view details)

Uploaded CPython 3.10 Windows x86-64

video_reader_rs-0.1.7-cp310-cp310-manylinux_2_28_x86_64.whl (11.6 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.7-cp310-cp310-macosx_11_0_arm64.whl (640.6 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

video_reader_rs-0.1.7-cp39-none-win_amd64.whl (591.7 kB view details)

Uploaded CPython 3.9 Windows x86-64

video_reader_rs-0.1.7-cp39-cp39-manylinux_2_28_x86_64.whl (11.6 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.7-cp39-cp39-macosx_11_0_arm64.whl (641.1 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

video_reader_rs-0.1.7-cp38-none-win_amd64.whl (591.4 kB view details)

Uploaded CPython 3.8 Windows x86-64

video_reader_rs-0.1.7-cp38-cp38-manylinux_2_28_x86_64.whl (11.6 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.28+ x86-64

File details

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

File metadata

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

File hashes

Hashes for video_reader_rs-0.1.7.tar.gz
Algorithm Hash digest
SHA256 5ad44876c37d268b36c1c4d82fe5dd0c29ef5731191c4e9f51ea8e786aac678c
MD5 56e713fc492ac123cc212e02d05f5d38
BLAKE2b-256 44f453b0f15e76d0c173fc0a7c7ac1cbdf3f5d89ba0752be63c790bab3b06b94

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5bc6287de964581e7b3884bd50f050a7ea129e056d4b16e49db110d257be0701
MD5 53c296a2a3a5d34e07aeafd1d9e1196e
BLAKE2b-256 15db4f03e74115906281952447eb24e372eb96a04a45337c14917a4997f058d6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 24bba322bac5e13a62cb8e5888789a905b16c5dbde295fcfe1f06610a52051de
MD5 aa74064c734f163bf4c316f116445def
BLAKE2b-256 16146e4b73a767dbdaa8f9bcbe72ea0b494cfab2c022123327d467b001e1b102

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 110cbd78f5c4995df13cf20dcaf6fab1fb5a93a4232571cf805eb9d827061f08
MD5 7963392084a7c96220fef58145b4c39b
BLAKE2b-256 9c8a5ebf1bc9632711fbf680e3ac8512a2d5a97c2273f1b3acfb8494b9ec9bef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 46c0d1601dc7d1a2e51314c9542393a99c49b008403051c62a210f993bf36436
MD5 6dbb4ce44415753e75e109f0330f9f2d
BLAKE2b-256 4c93b41f52312f6a6720eb3f428d4957cc09455f4e2ff3c88daf3f171d0c5596

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c88ce8f4ab524ad7a25389aac77e74aea46c19a4985939cf9f976799a7ee8769
MD5 8ada75b89ed1cd65c87a872558686637
BLAKE2b-256 21ae771f116d65cfe331864814b86215b2fa8554a04762065dcc71d1ea9e263c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 90769515dd861f489c41f74ac8ede24780f278948ceb0c24441d1eac3e3a8281
MD5 182f9f2cc5a838d2707f221b4afeb8ca
BLAKE2b-256 cd5a2c975b69e74f4caa612e8ad83242bb772d0f8f1b312eba3d58c43cc295b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 7fbfdd9e06189378bb07d2fa3f57266e496fa04ce5cca78143806457e808d52e
MD5 e9f0b5207bf66a21a1dabf7ddbb4553b
BLAKE2b-256 4649deb7cb865e2701e9f1aa7cc0656b9e0c374340fb5a3b60bfc69874e211d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4b8f060394a4510b0f21faeeda1a384b164f9707251d6979e53aaf8bcaa3d3bc
MD5 37d01308af9452a35542ce1c78e04083
BLAKE2b-256 4728bec3e71cfee41df24046bb9fac0cbd09e3dc00b2e00af14ed77d4be5a297

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bd4991a9a32583fa522ae6a1f0dac1cfee0861f175156e0fa4b55c72b4b02edc
MD5 c8f5c3c7ec100306b937b30a6018070c
BLAKE2b-256 a9aa1eebf325e45e3a572f5dc8347a278e90aef2697d3e29d1b421c2dd478043

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d614cdae7d6e5f69a29b4390a484c815e6475e6a6520d8b81676adb5cb396291
MD5 60c031d05860059e5beab402cf4549b4
BLAKE2b-256 0033d1aa5ceba9c441225930d68c29d1f8d4598d5a6152c9ee087ca5f2fb2540

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 e5d31422a4d65de30cf4098625138c880c07759efaf48bbb9861d158e5cdc8b8
MD5 85cfd795c9233fcd228401bf57577186
BLAKE2b-256 e41e699a8770c16b6c5bfee0e1d4e8ccf85a4d4b669b84305723f5014fb8fce0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bbaacbe84ba8583915d0867847cec230a1522a27d0bec785bdf92ff38ab4a837
MD5 ce4f49e63d9fca896277c28f6caf278e
BLAKE2b-256 b55d27dd29261649dfba829a0d2694c7e6dc2055dbbca552c89c560833e804f0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f8808cac0eaed8cba4ef080688c9adab9ad57d4a3e05dc37421caa7d10f265a8
MD5 1fd67c2602e5f027eb5968b561509712
BLAKE2b-256 a82e5880eb4da7867577c47bccd841cac7125b0161068b8f8515eeb23122b49f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 01eb43b3f971b239d91934821a448d856300113ead3c04c316786105178f9695
MD5 735eb48c06ddab79096ca194366a8139
BLAKE2b-256 695981608f73adc26afef0464a2d766bb680931e1cd6718154e6585df5457617

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5fb3b82383adc1fab068d499f99513acfacb354da424aa5b700ce61b6da83c5b
MD5 a62462c725cc2b7a661be3139f8caff6
BLAKE2b-256 8efb4a3640d4bc88e0100529469f348cc9784ee58a7a663b77f6d75698c802a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e8355b179abbbd42ef9cc87cce6d77f9892fbb919bece0725eb1d101d8343c52
MD5 9888a42a729a280276e88256125d6c7b
BLAKE2b-256 41478a61ef7b244047e5fadb5804da26e14359f4764ad9fd1ad19db97077612c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 a5f3c18634dc45e83ee5b56586c862a246491748dae7cf78f22478f9c6b695ac
MD5 b28e3a61d17eb93d762b219ea1db0040
BLAKE2b-256 7cb456426512a022c425f1d15b5e1e2bddea7816c6963e7d87f6e4057d7067e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.7-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 63ac1e78e10268e79e3ef7ad0f3b369d95ce87bd3ae3505b1813aebd119f0f98
MD5 5dd0552ab97ad6ce92243e340dab0b65
BLAKE2b-256 90881808ab1f9efe35c2c3883eb7011934a48ddaab4bbd29de19cb254c06f530

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