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

Uploaded Source

Built Distributions

video_reader_rs-0.1.6-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.6-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.6-cp312-none-win_amd64.whl (632.2 kB view details)

Uploaded CPython 3.12 Windows x86-64

video_reader_rs-0.1.6-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.6-cp312-cp312-macosx_11_0_arm64.whl (639.9 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

video_reader_rs-0.1.6-cp312-cp312-macosx_10_12_x86_64.whl (714.7 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

video_reader_rs-0.1.6-cp311-none-win_amd64.whl (632.4 kB view details)

Uploaded CPython 3.11 Windows x86-64

video_reader_rs-0.1.6-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.6-cp311-cp311-macosx_11_0_arm64.whl (640.3 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

video_reader_rs-0.1.6-cp311-cp311-macosx_10_12_x86_64.whl (715.0 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

video_reader_rs-0.1.6-cp310-none-win_amd64.whl (632.5 kB view details)

Uploaded CPython 3.10 Windows x86-64

video_reader_rs-0.1.6-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.6-cp310-cp310-macosx_11_0_arm64.whl (640.8 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

video_reader_rs-0.1.6-cp39-none-win_amd64.whl (632.8 kB view details)

Uploaded CPython 3.9 Windows x86-64

video_reader_rs-0.1.6-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.6-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.6-cp38-none-win_amd64.whl (632.5 kB view details)

Uploaded CPython 3.8 Windows x86-64

video_reader_rs-0.1.6-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.6.tar.gz.

File metadata

  • Download URL: video_reader_rs-0.1.6.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.6.tar.gz
Algorithm Hash digest
SHA256 68fe0dbddabc7823fc0d713ff604ef7c1ff88e9d79682b6ed0a7aae51bb838fc
MD5 cc2c63bd4dedb623cbe697a8cb661cb0
BLAKE2b-256 4b4c7f394ebe3fcd86ea04fb7e6fce957d2a7f11d8448ede08b10ec385383445

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c41698da48dd7a1ab6198b7aaeb0574e7b2f86984a9bf07094cc3c53743042f4
MD5 756f2ba52562d13c09ba3bfcedb22b8b
BLAKE2b-256 e3b2d028b47653e02d2a90b7d5a995bd964f86074313a10ee9eff88f6d318309

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a856afa38ac573fe4a8cc31bc4e2ea75e6153a53fd7c724a74a41abce3dd5f61
MD5 ca7ec55c6d0778b5aabc073ac30c9b8d
BLAKE2b-256 fdb2ae8be2cf12ab97236089d660b813a5c7f6786ad9de78b2bd4fb2e4d80a20

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 dfc2e9be13c5c3e076d008460121de634455c898dc74db7447821ebd73dc1cc3
MD5 80754ff8d7fed92db2a1e2039bbec1a8
BLAKE2b-256 588431371ee6d393fb1c3ef7cb96ce3a801a8585d73d459ce08b602d9c0f0d64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0f14e6d9801ba19de3435502355a97c80e70d2a8abb70112ef8cd2727c1a6248
MD5 709b2258b50bbb44c6d428787eacc90d
BLAKE2b-256 7f03d2f22a9f2ebc8d9c2d334c75c25e9b56be34f909005d2141b9427fffc027

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3613f3020cb59cf81a2ed756d3c0f241d3b29dd1690e5308bc0077a13c090d24
MD5 9dfa0e871bdddb953699460009902a0f
BLAKE2b-256 cd68092763d0092d7ea89742fd798f5908ca327fb0b4a57c146c878b07f4ebb8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a1e9f8c8e484bec8cf27048e0ea683fb24af7a368e25b80d3d7478873defb5e2
MD5 dac2ba045903aa28065710e0ae0d6dc0
BLAKE2b-256 bcccb68a40ab63b58ea747948478afdeb21cc1250f7595d7f22e71d16ab111b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 1a564646cf831c639edc5d93c6261b3d350316dfa0392d93a31d1e12e74c4834
MD5 c561612b59e19d0b31c523cd17c45782
BLAKE2b-256 4d105ba61c339fbefde845dc272ec270311445629cbe994ecb595177753f35d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e81ef424edc25f289d5e2cf2c7f2a59af2c2c3bbbed842431137a0b1faf24933
MD5 89af85286f06c835a04c1563b1480470
BLAKE2b-256 46310d6747c104e7141ef4d4b24519688be9ef40eab03475825d000cde6ec69c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7bbe0530bb0311a7d8cc7b115b0a50374ffbe002ef7530088e4cac09d262b96b
MD5 e74a34d2dd54e7b73d9e3ef42f1168ac
BLAKE2b-256 8bc5777594b39ac4e76d7a06541bd71d104a533bee605f2d88b5659a8bf3b715

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 41ec1bb83300aab8f5de7f28477513c122d6791f267b036bc2549ab9fca92559
MD5 6f230e18d4a3700d831faf8bb1c6473d
BLAKE2b-256 939e96d437216eb026e6f924a28a7907ffc1a03617eaa3d00deef2329afd1782

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 7fab3af686f6eae91443da523d76a31aab735f50afb3883f8d492fe8125465a6
MD5 341826cb8ee58943ad2d3fae0e6505ed
BLAKE2b-256 d6f5fa41db6ce28bdfa6a61c8c0954cc0cb53d9459508e857ac5f032c759ed64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 184d99097ce801380801a1d94d41e50358d6ef2e1924d3a6e27ebc9280e830e6
MD5 a8aa268209a85d9fa4186c94b55c9845
BLAKE2b-256 c7b4da8b1bfee40ad0e6a67a1f1be11f7ec17825709d6207ced855f92ea18977

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d36ba5e112129e97af223660993e679724419bfd491add2c357c73ab4fa4c64f
MD5 a20052c5f35ec9873500699cabd55be9
BLAKE2b-256 2dc42577b8b79e0cbc2ceba4ed38d933f3284ead2dd965e8a105563a119a4c80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 b398ba58550d82e50bdaed1daab4a10dff1bd4c116bfcc070476872fcd43aa76
MD5 24db8e947399167b59206ecebf3c2b6e
BLAKE2b-256 e41d2587387f554917944de58e0d51d698391033401b8faef6b0ab6887e84cea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fe7435be119fe34bd9f97095294afe5337a897bbe11085a2cd0a060bab9b64fb
MD5 1138ff6f568c92be79c95ec20590cce4
BLAKE2b-256 8bc8286dc838543d74a57471fd75c0023e69ea814105e19f533216cec207e059

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 041345e89e2d33bae06c3e3ed15b81fd65ea61f10abd24e8c78c3c96ea66759d
MD5 6a5b1e77ec0b155e7f7e76ca8074d43c
BLAKE2b-256 795fb78b1f4e2475aed4f215d1a0db2525913f277930d978938b1176ff10ec82

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 fc7e5c6ad1fc2ba0e89941e2ac33b638e6e3be3fa5d15bc97c2ee517651b5ff2
MD5 acda6cd98d66b7a6dc379a69c54f52cd
BLAKE2b-256 c9e90f8b4553ae6dcc743954a72f7dde973a1703c1d1f65037d2bd07e79f7d2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.6-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e075f4c2d27329ec34586d7a011efffdf4e346a47ddedbcdaf4b90ed5a46c4a8
MD5 d129a39eee3b5d323d6436d398e0d160
BLAKE2b-256 440c57a091746c5e04b69cd4a191bce6d0a28d0f2471b8aa8038ba891e362d3b

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