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

Uploaded Source

Built Distributions

video_reader_rs-0.1.5-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.5-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.5-cp312-none-win_amd64.whl (740.3 kB view details)

Uploaded CPython 3.12 Windows x86-64

video_reader_rs-0.1.5-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.5-cp312-cp312-macosx_11_0_arm64.whl (826.1 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

video_reader_rs-0.1.5-cp312-cp312-macosx_10_12_x86_64.whl (881.7 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

video_reader_rs-0.1.5-cp311-none-win_amd64.whl (740.6 kB view details)

Uploaded CPython 3.11 Windows x86-64

video_reader_rs-0.1.5-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.5-cp311-cp311-macosx_11_0_arm64.whl (826.6 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

video_reader_rs-0.1.5-cp311-cp311-macosx_10_12_x86_64.whl (882.9 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

video_reader_rs-0.1.5-cp310-none-win_amd64.whl (740.5 kB view details)

Uploaded CPython 3.10 Windows x86-64

video_reader_rs-0.1.5-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.5-cp310-cp310-macosx_11_0_arm64.whl (826.8 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

video_reader_rs-0.1.5-cp39-none-win_amd64.whl (740.0 kB view details)

Uploaded CPython 3.9 Windows x86-64

video_reader_rs-0.1.5-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.5-cp39-cp39-macosx_11_0_arm64.whl (826.9 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

video_reader_rs-0.1.5-cp38-none-win_amd64.whl (740.3 kB view details)

Uploaded CPython 3.8 Windows x86-64

video_reader_rs-0.1.5-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.5.tar.gz.

File metadata

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

File hashes

Hashes for video_reader_rs-0.1.5.tar.gz
Algorithm Hash digest
SHA256 f154e6a2b76acb6aea4babd121e024ae3e05268fe832b35d36da6a204c951318
MD5 e110a13ba8674136e630b24571b1afe4
BLAKE2b-256 e1ef3d7d67ec1bac42126fafb10f9e768fc87c8148f17df21f84027ee5bfdd9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d8d9b2116285a40b71cfc6e21aac4b32c35b631631e35bc69e98d1d908a3b756
MD5 01762abfc0c3d9fcb5e1c16f473a0707
BLAKE2b-256 f251f5ca6bc6b71b6e78de0b8fbf01da2632f12a92ea5e8ecb863f7a6030d78c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 88f83c1d3265b334a157c8610378913ead083a02dfc91d96c1397ef8bac692f2
MD5 0fb3edb69220e092eff64b07472a55e4
BLAKE2b-256 5f20302432883b51584dac76f68ad0e36ebe5373041d657cd14b3fbd552c442c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 38e6ec2bc8ec283c3617824fe298d7c150e47e8d4fbd1457bfd7c9399b9a9d67
MD5 0d50744a9fa10cc92d5f3c9cd77ebc3b
BLAKE2b-256 c7e86a38ba67128751e8da30ade3648eb3313bfd2efec62717963f18ee1e4d66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7a938c4efe8351e91524c8590a94df833ca15717af28744c469bd00004e380de
MD5 07c77bb6be6e62d90c9fa5261019f7a0
BLAKE2b-256 7ccf45e7bcd261896520c9db46a84a6f0f481c9fefab7733d95f53715b5e8e7d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 09a9d76a6d17027529f636a47765f33267326ca25cc1b885bd237d45a1420b79
MD5 6367d058343826abcd42cea81ff5cbba
BLAKE2b-256 90b7a238500d2c5d7ff1a02c7feb4f0c1d410589406058cf9bfe11f375a5332a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 de683124cd7d18a5838c1e89f51275167d81858f82d70ab1fe46f244e1f8d800
MD5 c0913a66416a69ab26bb14db2dd6cd84
BLAKE2b-256 c9051efaf611e2258890c93786fa0a8097cf7ba36a90b4303d6bb9b934393582

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 5a74bc276021a857debbc87d6397b080ec9a5abf953affbb264cde48628377ae
MD5 ed7c9d9f5aad7f064dd137e0998de7b6
BLAKE2b-256 b3d7f1c61865fac34cb725162c220f225a6c49234b7f0866033484e94bd894ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4a2055068aa8f65b41841d03487188f4e8ead2a9491388397ba86778e68b997a
MD5 44f850dc486652ce28d382f962afe091
BLAKE2b-256 3395b23435696f521d7c60eabc25f7124878b028da496024b897c34e2170c187

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 43f90f7de7ed0b53b31206140796c5c286757de46870be55eed0a954409738a2
MD5 cfeb2cf42ebc89c2702cd21047640056
BLAKE2b-256 9872c443feea83e6105fb243ca04e462beccc2466d1ae48947c690f02ea77af3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4162b13ad2878de68e7c610891c605b42abdc776bd4ccf96cc03a2d0085b3e6e
MD5 ff58ef9f5b962aa707210621fc9412ef
BLAKE2b-256 d61894a4e3a19541cc0c9e7b06104316dd9fc19ac71e15f777e415ccaa41f806

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 610975723359f4c41d55965fff1a2b653165f5464c948569c14666fb209e8e61
MD5 72cf0df18b609798bb0fb083d50a32e4
BLAKE2b-256 67ad7557f41db450775dbc856611a35c1503b2601b07abb70934533462bf76a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5f75adc9bb08a203620f8f2ec6de8e8026fb857a74d31df57f023135505b087c
MD5 52d620b06d928a377753ef731b0dec19
BLAKE2b-256 4e793ab5d0c531dbff68367950cfda23d0f6e956bfa2da8451e1120a77ff4e7d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2bc9ee7384f48749594b03065c98431b02d96256faa8bc6df00f940ac5c78706
MD5 0166e934259326d53283e1d0cfbbe092
BLAKE2b-256 1ffc4cde572e5d5bb7eb0e126b19824bf1f201bab6aaba508b64c87782e2d5e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 e81a0ad5b0c549c7ee397c919b22ca1e8b51f2c83ef8bf2e55bc69dcbdf04a61
MD5 d437d3a3470aaba05e13c2e8f207fab6
BLAKE2b-256 c7933133dbc7dc3b2ae4ef20d602894a52b6a3d493b28601ef2a0dec0bb31453

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ae9fb1a63eeefaf0dc32972131e411c24aeab156f149530efe269cb4e4c06ee5
MD5 228e333023669801b4c24d986870d909
BLAKE2b-256 3258f2c97faa6b62e7c39bbfab1f351bf0f7098bfb4593342331fdcaee4a111a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a92c94b7aa3f752ffebb2917e861a751fdf89ef85f189de0e732470c2dc036f3
MD5 3aee677465ce36f1acab2c1ae3452a76
BLAKE2b-256 1ed196dd4f0feba6eca568e36c469690bdfcc2a578648fe391b2ec99d6d8ee22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 59561c58cd09496245626a57c12d1982fcd13e7ff99fffd7b3270c0cdbc922e5
MD5 94576a65cfb3e05b04a7b09204ff8a0a
BLAKE2b-256 fe7c3d82513abf4acef7d2b0fab12fd4a807b6122cc2b6ddd228c9dbfc43ceef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.5-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0406c9af64c6fd7aa41e51a384405261e406349057d7f6ad95dfcc954e0f5f81
MD5 241e8589d813d880305c7eb3168fc309
BLAKE2b-256 6ebca2afb92e9605a136f0688ccda19d882e67b0c72fff3a9f44c405932875c2

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