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

Uploaded Source

Built Distributions

video_reader_rs-0.1.2-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.2-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.2-cp312-none-win_amd64.whl (699.3 kB view details)

Uploaded CPython 3.12 Windows x86-64

video_reader_rs-0.1.2-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.2-cp312-cp312-macosx_11_0_arm64.whl (791.8 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

video_reader_rs-0.1.2-cp312-cp312-macosx_10_12_x86_64.whl (835.6 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

video_reader_rs-0.1.2-cp311-none-win_amd64.whl (699.8 kB view details)

Uploaded CPython 3.11 Windows x86-64

video_reader_rs-0.1.2-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.2-cp311-cp311-macosx_11_0_arm64.whl (791.8 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

video_reader_rs-0.1.2-cp311-cp311-macosx_10_12_x86_64.whl (835.6 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

video_reader_rs-0.1.2-cp310-none-win_amd64.whl (699.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

video_reader_rs-0.1.2-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.2-cp310-cp310-macosx_11_0_arm64.whl (792.3 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

video_reader_rs-0.1.2-cp39-none-win_amd64.whl (700.1 kB view details)

Uploaded CPython 3.9 Windows x86-64

video_reader_rs-0.1.2-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.2-cp39-cp39-macosx_11_0_arm64.whl (792.0 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

video_reader_rs-0.1.2-cp38-none-win_amd64.whl (699.5 kB view details)

Uploaded CPython 3.8 Windows x86-64

video_reader_rs-0.1.2-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.2.tar.gz.

File metadata

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

File hashes

Hashes for video_reader_rs-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0f7be3bcaefb9a2db8d3a36d8e7f8e4afb8fdabd9887dc33c67c162b0003ff3f
MD5 6c9a28ba04fac2491c75e3d834975c9a
BLAKE2b-256 eeb4b5c14f2d2e06d2dfdc8e4fb8eb61187eaae2ab4655aeeb9172a3e3230b75

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9607b08bbbf05d0598f180ed3bd6feece9d1e667993d2331fc67229cd2e6e52a
MD5 ea38dfa9c1768dc735b6ed577cbb903e
BLAKE2b-256 1461fae5f63fdf2465680baffeab23b8464fc45e58a48b46c0b77c40d6345000

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2c82dd5b1630c3b6a21d9a1fd8229904a6444e15c7713062ef8c90bcb5b6a782
MD5 ba56a18c845261eb68d450658da3834d
BLAKE2b-256 8434fbaa88776433f74e95c48d7ba37b827a431b6b1cd205829da1b87bd35383

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 eef7c1904bd8a65da92df39f1e96e505575d022bb79ade109e2a3d602e9630ba
MD5 83f9e002728d60c7d09ddee52a08bf38
BLAKE2b-256 7a082f29e6e0f78c23f6646cb36e722ea8faf7964c0795b7a06f874ede595817

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 92d04adf739b324357159939cf4db529ec8dbcc4cffb7907a479109592356963
MD5 0d8ef03a61c82c316af24b600b99ab73
BLAKE2b-256 a85b46ed25e937f61f3f376ca1f9e2a3aba713e887a79654a2e9f7c673c98835

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 06bf76a906ed7820bd85eb70012365fbd1c5adb1783930e77ca708e562e32a5a
MD5 551d497af43c891642b230f577b6e785
BLAKE2b-256 cbd657e642b7180335e24d6dfa53c12a47a7fe34635d83b6a3e9a8c1afaa1db0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 42244c878ff5d1d3b9f54954aedc025bf946b24d3695f140d6f45b7b44c870d0
MD5 6e8e640ff0046dcabac166bb12e648af
BLAKE2b-256 0eebf75f12038af64864968a1f3b7fec847a00e2a157f0f82353644fe8a69f37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 c15e4bc1ec0f087a7923fd3bcd8b92b89e0fa3e423e1b218bfef72180ac898f2
MD5 4ac03b13ccbbc2347979202660629b32
BLAKE2b-256 94b8533ecc3272780d5be3d9a760ba9f24bc381c79394e517dd50ebb72ea7fb4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b82e1e6f5ead060f0c47855ddd96418bd28d850175f294d8f2432ef353a64152
MD5 153ac19e9501f1122b36f93a9cf07913
BLAKE2b-256 992fc6bbbfd86995c342c5386c9f42c69b2391dac2b625bdf520ecabd9063cf9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5c5a5ed04f0f9045cb84bbb2a614bd983293e49883d4bb4f00f58d84b26e6ee8
MD5 580841940506a08e64ff66cdedb71d17
BLAKE2b-256 fc6ae0c580e4f64c51cf879fad86259533f14928b594c949b5dda8c10493eeb3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0ca8432e552e8e5e745bae3898a394e1f04a5431c2db4c2777bfb7baa5e323a6
MD5 dd948fb50e93f41cf910d42be02701ac
BLAKE2b-256 0fddde30e04a8a779a96c67557acded83eeebab5502bccf209a283b318f5a190

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 4b61d507498fa3d2b89d731d8921c065857dac61b2411e12544353209738f5ae
MD5 cc7b3c2639f9abeb6749db51762240ed
BLAKE2b-256 6c21d96460495112c268c159c8440b962dfc654d7b4a0754c8adf913617058bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d105bc632e7179dd151111737e50691f8ad55895c0c4920172611fc754673ed5
MD5 a842645f0fba5f69ba50db6f3378935d
BLAKE2b-256 e25a9b67e5f3c2bfdc944ef4699a2e59a44ff8a2e214d35723f1b3b789e88e2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f953144a3aa137a69071a149ecc4c69df96fcdd96a8aeefd5df6c47feef28305
MD5 975f286230d229526975ac10e55d3bad
BLAKE2b-256 a2cf4e8720a76159adbfe5c63bb190590a6adbc0bf8f16f9e2afbe3d8793e93e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 d5b7325eb5a8916890a3593efbf43518e816440883acddddb0e6a5ce8c460b7b
MD5 f782e5fe2bb7fb8afa0a5d24a33eee13
BLAKE2b-256 65d7d733f1c0fc90a42d2a5c065f2ca63f196cb91f3ced80d6ae4ddc8e14b2ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d2543a5e03c8ea2271b0688aec6ffe129d24662695addfeb683cec25540d345b
MD5 c003be8590c017671ebb4636a5decf84
BLAKE2b-256 eed157b618ff54e19f6bbc38889dc53dcbbc5cc8cdcba3eb0b3b922839a8fac4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ad41876efe8692d2579320b0e27db016b005d807e652f4b9bf6e65b48a461793
MD5 3e74602f212549821284f6e34c22d498
BLAKE2b-256 b57fc39b1c24e03f653033ef274e1a78c3d622a0bd6e0ba8543ae6ad4a00755f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 9db39ca55f2ef4ffdf289a0b54de91d0ca446a488b3fb84667f6e597b3d6f992
MD5 175711af1f63ccd40df1f34e4bb95c9b
BLAKE2b-256 4f4e3895f15b58c8648019f7f9a91993190df6563b288b809d8fe013f05f4403

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.2-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e87a4648055f29dcb7f710b11c3fe5d0b551f9f1f06ee4b97b7a279e5c5ba9b0
MD5 eb922f5fa7b28b5b0345b87596d7a040
BLAKE2b-256 742a123eaf9e93b0d185e63b5274b6c0e7e1342955a4908d21ef1632a44f8ef5

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