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 iterating over all packets of the video and only decoding the frames that match in indices. It is safer to use when the video contains B-frames and you really need to get the frames exactly corresponding to the given indices. It can also be faster in some use cases if you have many cpu cores available.

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")

NOTE: currently only work if the frames shape is a multiple of 32.

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

Uploaded Source

Built Distributions

video_reader_rs-0.1.8-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.8-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.8-cp312-none-win_amd64.whl (595.1 kB view details)

Uploaded CPython 3.12 Windows x86-64

video_reader_rs-0.1.8-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.8-cp312-cp312-macosx_11_0_arm64.whl (642.9 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

video_reader_rs-0.1.8-cp312-cp312-macosx_10_12_x86_64.whl (716.9 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

video_reader_rs-0.1.8-cp311-none-win_amd64.whl (595.1 kB view details)

Uploaded CPython 3.11 Windows x86-64

video_reader_rs-0.1.8-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.8-cp311-cp311-macosx_11_0_arm64.whl (643.3 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

video_reader_rs-0.1.8-cp311-cp311-macosx_10_12_x86_64.whl (717.7 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

video_reader_rs-0.1.8-cp310-none-win_amd64.whl (595.3 kB view details)

Uploaded CPython 3.10 Windows x86-64

video_reader_rs-0.1.8-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.8-cp310-cp310-macosx_11_0_arm64.whl (643.6 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

video_reader_rs-0.1.8-cp39-none-win_amd64.whl (595.6 kB view details)

Uploaded CPython 3.9 Windows x86-64

video_reader_rs-0.1.8-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.8-cp39-cp39-macosx_11_0_arm64.whl (644.0 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

video_reader_rs-0.1.8-cp38-none-win_amd64.whl (595.4 kB view details)

Uploaded CPython 3.8 Windows x86-64

video_reader_rs-0.1.8-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.8.tar.gz.

File metadata

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

File hashes

Hashes for video_reader_rs-0.1.8.tar.gz
Algorithm Hash digest
SHA256 016cca3190932b9fd2fc4a567c805b1f62bbd23217a1f6ef4da461e8076202e9
MD5 0f6337d177f56c32cbdfe2e49762f79a
BLAKE2b-256 17b47ebefa1386d39263867ab7f14d5630e9c7fdb309e17b3bcad1cda55feb83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 54ca98736969365d46c7e0f7d27542020e5bffd02cdf9c5385790e9b5cfd2cf8
MD5 502e989cc5d952fa9b22c0ae2b941cbf
BLAKE2b-256 8aa7a4cf4709e95d1903013384b16cf20dc5f85d111950ae2097d4382f510c5f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f42636cc72e7ee832fa89555f168dbb1f05277458cfc3b08925d75b16dc7dbdd
MD5 d7051146deb484c87a1b46d9dd55c7a4
BLAKE2b-256 400863ba1d0db192f5c13fb4ad7197df0124cb70fae59b3823d590ed70eb9234

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 cf068390153a4e8d5459b96c6b7e21c82c119692d8569c3abeeec5c5b4b0c8b0
MD5 6c734f60d7b62819c435068bc02bc151
BLAKE2b-256 947764a1a78e910ab50ad26bf60511ad0ff37826d7de12eeff996182bcce4a50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1af650f4f7d1b250ae2df2ea98179fbaa03ef9c400f59c52ca8a8c58dddc5166
MD5 52d477dc575c7738523254dc3e1bb4ad
BLAKE2b-256 38620b45e9fecfac8ec93d2f0f06da584ba386db09040acdf127198ab75014bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7f1c73a472db9fb29699fcb256194c89ac7e82a18fa6d0a59a2ed6a1e6c2389d
MD5 19eb6e25f876d25547f74ba4d06f30eb
BLAKE2b-256 f29f8e3a6c36dc41cf7e6002c45752b70d853bc12fd4a0684b4d864fd3569fe4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 22733d8dd512160b0f3915112e93e649ae3bd390ba94202b6ff5a7d63187a844
MD5 468a00d0ef676f04cb0938d66675365f
BLAKE2b-256 bdaab7adb172940d37f5b4f49fc74ad6f5b0a29c875d5f1b0457e819fda04748

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 143713887e4faa5481c16f64e4dfab73b43d481103b04d579a0b8bf7fbba506e
MD5 7f3f464dac91fb749ea9321b8a4bd811
BLAKE2b-256 07c85592bd1a54e868cb1199e0210bd693b8cf53f13d5148a2222b86a71469eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 68c5c9082f41f4968003e5665a564dcefeb2862aeab532a1e0ddfeb18ef1b476
MD5 e3c7552a387b27c74f0199eb76a9896e
BLAKE2b-256 42a4f1de61558915357a45766c5eba9498393b995465875ea91aaf90186c76ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bfd0e7e1ee48f1863b644f79a298cd072884b88869fabac516a7b97731467bcc
MD5 53a48588bfd2646bb45351624d525460
BLAKE2b-256 392fe209b146a4f8e417bc59ede65c52ff017ba4abf2939a667d2b3f16b63c88

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 073ec03eeb8fbb4866bc3893198e422d4b9ac104afc94cd1392317556f0d8033
MD5 212fef3029472f8efde3d232647b1d77
BLAKE2b-256 acd37f0878465d6666f0af468d7c1cbfb97a851d4aeafd786f591ba13cf382d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 2060d1a5fa4216d9a37c0f5a809a8366f83131655bd0c3b86b1e37c1de19553c
MD5 aa93b7bd608c0c5e8a485150fe09799e
BLAKE2b-256 24e50921ad84c66f1a202f6fab062a40e925853e1ca3bbb1f69e37f4920afde2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8e26101856926408d3ce8dfcb125fceda0da1b71085beb6365c124becfa36238
MD5 cefe3bad2c0c36ada63e08a14d1d3baf
BLAKE2b-256 d2febd56ddae16cb98c7085b9b498345d3797122e47264db8dd06487c89cad99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9c571c08d2f949f427a78ae13bf7aa62c57fb35bd6ea08af301ad06f0b6f6549
MD5 3085c01ade9b34eeb01ab831794a24b9
BLAKE2b-256 a8378620837dd543e041fa46be79a9ac49002289ee8df4ca4d412c00880b9def

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 99f6085bbc9e85f7996419fe96f7afdb6f662b9e06fad7b3edf9b49fbe2196d8
MD5 d213744d01b9a9a237b292f1982c9162
BLAKE2b-256 9db141523e843c376f38f380746719601d75b62f9a86d22b1262926d3ec402a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 43bf9a3ea18c61732bed6c8632569886f33296a5670eb867bb44d04ee452f459
MD5 a5b847e89f376c8d9a2048b07a48fd15
BLAKE2b-256 dd29c6a6329cf6a5cc4779d14953f4108446d4b6034ca02a97c8dbbbf076aa87

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6df0c5f5f93591c7631c2a358871e53e5efa58fb637b5b5f47ffbf83cdd1f0c0
MD5 6360c48a4576c815ad137c36a5e228f2
BLAKE2b-256 34d08ea8e1e32cdbd38665d11bf62a3f82d56b5044429f4155a00ba7c7a79f7b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 e8632e253c5844c6dbe8872a97f164b60b49c3b01ad5311cec2aae615eca75e4
MD5 fe92aff341ac846d41c3b3fe49e26398
BLAKE2b-256 6862a38d9df9b45fb76cd4eac6468232e373fc95c9d0390cef5bfaf353f649dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.8-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9480307b7d55957d2bd61614528b034fcb4b51ce252007ff360bc38e20894c7b
MD5 8d230ab87698ef6bc078dccc7c3dae04
BLAKE2b-256 120785e5b6fd051d2b3de3824f0f09033af8295e9606e292c75e4228a9c8292a

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