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

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

Uploaded Source

Built Distributions

video_reader_rs-0.1.1-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.1-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.1-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.1-cp312-none-win_amd64.whl (698.8 kB view details)

Uploaded CPython 3.12 Windows x86-64

video_reader_rs-0.1.1-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.1-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.1-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.1-cp311-none-win_amd64.whl (699.2 kB view details)

Uploaded CPython 3.11 Windows x86-64

video_reader_rs-0.1.1-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.1-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.1-cp311-cp311-macosx_10_12_x86_64.whl (835.1 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

video_reader_rs-0.1.1-cp310-none-win_amd64.whl (699.3 kB view details)

Uploaded CPython 3.10 Windows x86-64

video_reader_rs-0.1.1-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.1-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.1-cp39-none-win_amd64.whl (699.6 kB view details)

Uploaded CPython 3.9 Windows x86-64

video_reader_rs-0.1.1-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.1-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.1-cp38-none-win_amd64.whl (698.9 kB view details)

Uploaded CPython 3.8 Windows x86-64

video_reader_rs-0.1.1-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.1.tar.gz.

File metadata

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

File hashes

Hashes for video_reader_rs-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3f88d2c4d1977c803e2877a6b6850c0d610baa50312b1a988daa29bbfc4fa05b
MD5 ff670b182011a9198a9590fa02b56a78
BLAKE2b-256 19c7b28e4712721dc5e1df52149d6b91b5e83abb7e327b235231f46a057dae43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5899c46db14f206826c72527af097badc366f7baba56245967ba2406ff69bd05
MD5 e55124f78707443ba4aad4c726675189
BLAKE2b-256 007afb934915f2ecc9d44ce5aef7c43d1b2c0075413e07af8f8ed0dd138ec610

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2603b35ae9b4e45d4f92219f34b1dc9e1afb4f77b910f242eabd3ec7118f711e
MD5 28209481d11f42a59dd4635071ecc7b6
BLAKE2b-256 6aa8575cae9e3a0b526a7280a32426ff38ed95fb754512ba1a8695347ce25284

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-pp38-pypy38_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e42cc5c108d018f053e80b313753be5f3689a7086c4f577069569c19fde68064
MD5 d674d0ead9445eafda670044365f1403
BLAKE2b-256 437103354801f15337076339dcc7d2344531bc737180b23d8fb930a6a570b8ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 838f0d28fa35072d36e80fe91cf3508749f45f2c8d8b6b2b68601a41d4f51bdf
MD5 cfcbbdfaadcaece99ed66889098f1992
BLAKE2b-256 c4bacbf0be1deb249c600491f1cf294251569216204d686370bd6ca45e1ddd9f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 614f367972b5d2abbe3758bba901789dbbf13276c83f1b90d6863332a5dcfc3b
MD5 c81009e1067c46b9b0bf437966c75109
BLAKE2b-256 a2b9b509c56a5f749331fe0bcc413dd0b68ad3b12854f287d52f001368c017b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2920149226be2feadeff9a1621142ebc0a1ded71809e7fe169f13ce4dc5dc241
MD5 27de8b6d2b10418e304d3f6fbe214078
BLAKE2b-256 b81b7395ce4eedeefef3597f6571eea450f722c2d73a21b786e6d16470dc5753

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a95f0f83f2fa096ef3c15b4abc088d4204cc7e0bfe9ec2138b18938435340abe
MD5 2fa85387908839faf6162a1b4fd07648
BLAKE2b-256 23bd3e7bdbcb881483ee702651149e5aa57177a4a5c4ac1b3b2605908a875663

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 97f482469e1f0399c1cb1e9308d6b2fe6974958a3012a0f8cc33067b76fa92f3
MD5 e45258e0a5c1242551b5eafda93b01a6
BLAKE2b-256 b244683bb3d3f65a4fd2a5267af24ecae4cd7f4a8ccb9e02099f0ca6873765d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 98699fa7108c9b3e0ea6c16c73ec2e34ff6246d6e78ed67d18bb2d3e7f64b434
MD5 8d801d452c91b7034957ad16a4450566
BLAKE2b-256 7dde6c42766c62febcf524441fecc1fc8cf7ca6987be3d4e0a5932e1ba6f2e85

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9afc43ce9d905c1720dcfa3e8cc31066a6114092c5de2b4648b2b2005d46cb5f
MD5 b28ea11a6768cff6dba1d9ff4b3ee934
BLAKE2b-256 2c1055d1a5ddbfb26737dc5306c03388d54dfea263c13807b7c74aa472a1a2e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 611ed08c944fe66ec4a74f38c4e80a451912eca1880793f218868d3b67ae65f1
MD5 0bc5756218e4ee5412700cb0aaea809f
BLAKE2b-256 79f20dc09acbcbc902a7b3ebb2a818b64bb56cd253db5981e345f78c1febd574

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 9daf438df925ad7707a72c4c85f8d51126b5003a0d418f0f529c9af84d114ed3
MD5 9e1bc3d8cb06a028135faf0823fb3eca
BLAKE2b-256 7175478789e52f13dac13531c34fb21b54f857aa3662bcdf1a403f2da9531493

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e259bce65d34701e0fa58dc983b0c55202dc2edd0f23641997af1c6bb37e5dac
MD5 34dd1aec5496d87480d9a3eb6243df77
BLAKE2b-256 e7cd630551cbac5a5491bd5ab1b9a3b964948dcefc984323ba350323b2948733

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 78c52b4ef063649ffc930ac6b6d411eb76fbf51ab08e033e7741974ceb047070
MD5 e8ec1c5ad1405f98a2eea8fd7de352f8
BLAKE2b-256 79ed0226f60843387ef6e836566f9c7cc9dca91a927de94b4bf48d35bee2cb23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 1bd54d220de6a1559dbeebc57bc7747eee88f39cdce1dd4b78123404280e613c
MD5 b4358b2cf5a31e3fb4a0d750fdb063a0
BLAKE2b-256 68001741099da4b0de2168b995e7f9ef7681de236b2e870ecc2d9d1bd30b400c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d8bf0803babf9e8a07867d9177aa2460697b619cefcfaec8997abb73129ca62e
MD5 a737f54a9705018f050b23000212b413
BLAKE2b-256 6f644f894cab47ed0a8397dd1a7c9870cb6e9070eb1c4b2e5f752d36dec76982

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9cba800388d62bd865d3801b6715e5bef4b83fec397e0b9f5b49adb9a48b49b4
MD5 62d550e2d0e0a1f579295360f00062e8
BLAKE2b-256 dc4b096df0028bf585ceb27aab0833826da6ad04e1919196fef39115329e25c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 c4b189f750986d1a8a60a567114dd6ebcddbadd94a4b78026349fbc63c6d0ff7
MD5 ade87deea7fd6d927f9430c7b21a4755
BLAKE2b-256 a443c88fae27c6b27f36bf244fd18c8b8ca0b50357d7f7d52add112cbf158ba7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.1-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 63c2a09c1c16ea9cadeb16e8fa826e5e38d590e78b8138b8f2e471c21b547210
MD5 bb62b5bc49cfe1270b5872a7ba8608cf
BLAKE2b-256 4084707d71e5f4ca9f63438d5931a31022516270befd47f9d3f49dc412038d34

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