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.

Installation

Install maturin on your machine

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)
  • filename: path to the video file to decode
  • resize: optional resizing for the video. If the value is bigger than the actual video size, no resizing will be done
  • 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).

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)

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. If the value is bigger than the actual video size, no resizing will be done
  • 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)

TODO

  • publish on pypi for easy install

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

Uploaded Source

Built Distributions

video_reader_rs-0.1.0-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.0-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.0-pp38-pypy38_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.0-cp312-none-win_amd64.whl (698.7 kB view details)

Uploaded CPython 3.12 Windows x86-64

video_reader_rs-0.1.0-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.0-cp312-cp312-macosx_11_0_arm64.whl (791.4 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

video_reader_rs-0.1.0-cp312-cp312-macosx_10_12_x86_64.whl (835.1 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

video_reader_rs-0.1.0-cp311-none-win_amd64.whl (699.2 kB view details)

Uploaded CPython 3.11 Windows x86-64

video_reader_rs-0.1.0-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.0-cp311-cp311-macosx_11_0_arm64.whl (791.5 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

video_reader_rs-0.1.0-cp311-cp311-macosx_10_12_x86_64.whl (835.0 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

video_reader_rs-0.1.0-cp310-none-win_amd64.whl (699.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

video_reader_rs-0.1.0-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.0-cp310-cp310-macosx_11_0_arm64.whl (791.9 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

video_reader_rs-0.1.0-cp39-none-win_amd64.whl (699.6 kB view details)

Uploaded CPython 3.9 Windows x86-64

video_reader_rs-0.1.0-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.0-cp39-cp39-macosx_11_0_arm64.whl (791.5 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

video_reader_rs-0.1.0-cp38-none-win_amd64.whl (698.9 kB view details)

Uploaded CPython 3.8 Windows x86-64

video_reader_rs-0.1.0-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.0.tar.gz.

File metadata

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

File hashes

Hashes for video_reader_rs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c3de7f589689719376602151dcec2a7190197fb665519998a24c5aff704cbb3b
MD5 aaf3eeef94daccf3d028e1e28b83de26
BLAKE2b-256 acf32d7c7427308067f2904ab8d336572b0d3cc6dec82510880518bc3ed1d0b5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 85bb20268dd069ebec87493293e18485f329e483f557fafdd84faa83bf52de92
MD5 0a881c909485e98cf96b44c17caaf973
BLAKE2b-256 6ec53635ff2ccd8e3318f8f3a3e67dba2f4bfad830aa59007443fe1771329360

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 09cf6823e17da99720bf9a823fa3f260fa65a7bc593227267768aaa76726677b
MD5 0047d914242908ec7b53bd3dbf5248bc
BLAKE2b-256 7ceea873afb619afbb1326ef841145a6c881959c52d821b8758d99fa7ebca072

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.1.0-pp38-pypy38_pp73-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-pp38-pypy38_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9b124a2753940c2c9beea321f61ccfd4b5b5cf76188681e849861120bf86b7a4
MD5 af4f76d88b7c0e214406c5c7648b022d
BLAKE2b-256 a0ae46759e78fcc34509cc9fe8e2b4d9d6b9221f9ed429e0c744f222abbe84fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 d05632d489fdaf232ba9fab560f463f304cb9b4627070ea3b095dde9182a4bf3
MD5 12c8231638675f80b6c02d4b051b097f
BLAKE2b-256 00730bfe7a4e139776d5e153574d80494460d234515eaff4f83d1993cf2260e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7115686b8e385b396aa1d0909b72776336501c717cd3b94c77a96e40acfde45d
MD5 c3e9739c2fb0cb1c5f431d67493075cf
BLAKE2b-256 f25f803e58d049402d7012c2ec06e27be08291eec639d9316dffeea7ed10f6cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 848adcae960052075e242e4eeaa4c343591e75a77e9ef7d34c533a01ebc15715
MD5 562e6d8a12b6349c8415aabfab8a8726
BLAKE2b-256 403093af74839f27a81a88e6fbe2e0027c543ca0149e44ceaa70cdcceabaa814

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8aa70653d6cf75f7cfe465efea656a7ccc3f35f49a2649d778727a4299f59ac6
MD5 74561ac2a106f1b8c57a26acc52476d7
BLAKE2b-256 98412b61c67ffc4864f6bcecdaf31db8e2cb7ce93e59a7d6bb3ab769f6da17cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 a35ef73895c555424a3df2fe9c8dc421fdda67c1f1b626816bd83cb5bb958598
MD5 b80eed622912a193f2da3c3890f64713
BLAKE2b-256 d5ea04e6eb14460f052cfb0d940320f517a4532c1d87d54b59062097793c8145

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6d54edcf2214079a4a2c79059fb8c5f8e01acf3b2ba4289cc3de797d2c7ff2c2
MD5 d61e21d3a564635694024159c0b50af0
BLAKE2b-256 4b36bb962b3ad18a044689eeed2162890447977b2ad8d7abfccfeedb2fd78881

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f60a7ae8cc8e4d4c9f34da2a813636286247777c9d48f394dd5d1716db35ee7b
MD5 0a876c74a684d5ca8c70d23ce61c3ecb
BLAKE2b-256 73b9dea0202fc58f6fc11a425333ffdcca672a33b29c638b95dfd58df58b5745

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7aa7780110a31aea45d317a1c3bbd723bc813503439009b67a4cbcd8d5ee3fe3
MD5 531de149bbbf655439488d70c410b13d
BLAKE2b-256 124d0a269ecd229e64b4e345e34adda855a12b252bce04359477dd42f9d9badd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 934794278743914149b9f0b3428c6a93ec0c20f1f9dea7ef54980b0ad9991968
MD5 5f7740c979e8c30d1d0140a68ccd774d
BLAKE2b-256 72c05d95c92cbdedbf1322bb49205eb2049eeacd6b7cb7cca5fafbbdc9ecf6f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 de6529ee6e8eeed1c24f42e0cb7db841f58245181a6af566e7757297d7f3286a
MD5 dcd152b1e690c6e6270ae2a77d474574
BLAKE2b-256 ea0cd575fb54213798f0e59f0c4784ae114a2f5c22dde568da80f6928477ca92

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a8ae558892d3a5fd2a1fd5e2e60b098bcb4738957a36042a4bfd06e60958fe06
MD5 93a75843cb17be0bc1f6800e64c540e6
BLAKE2b-256 4f0ecf47f206838f4664c3c7ddfb5821eadccd19dd78782fcd5bd35d0bf4d75c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 58a44567a2b697045d07672252958370ed3b91faae6f86985fb0419cb1c7f47e
MD5 1352ef25b124a9e16423aab86d59a0c9
BLAKE2b-256 f3f835f23b7e2bfd923ecd0c7577b04db13f760a2e2c7f6f34428ace982accc3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 22322395e6746dbe49e1753f04290429bb77130ce86dbfe15e0a84824f4b3215
MD5 2ffdda15b73267a69bd4cb101b3c5d50
BLAKE2b-256 efd17d57a351cb313c23352dd0c3da4e04f562081653251f192632158054856f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 662c92ff7813ff15d03cfc9253da8092a07d564e247cf0ce5a3e6232ad1a6a11
MD5 9473358be7f943bce774bdd2aa9df887
BLAKE2b-256 af748513a8c54392d126b8f84ea31dd1af5cf1fc920ab3e977bb8029910a1c2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 cd360fb7afcc3fccfb1a8431e122c15a2e7d567727ece29e5d18fa49f6e1daaa
MD5 e9a69af48f35625b1351ff98713dd406
BLAKE2b-256 3597f432a6eba5e529cb3dea942a91690d4b0873027230b5abead3ac05a8846a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.0-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2b73b23fbe8a0b52c353c2d2da08eeb886b37c50cc7a354299a0d33cf5265833
MD5 27c9d68a0c50f071fa128bbe066c5613
BLAKE2b-256 34e3328a40209a14cfb0518c9c22fe9eaebe23081daf9f9bd32ae6d7c96ae6cf

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