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.

⚠️ 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:

from video_reader import PyVideoReader

vr = PyVideoReader(filename)
# or if you want to resize and use a specific number of threads
vr = PyVideoReader(filename, threads=8, resize_shorter_side=480)
# similar but by resizing longer side
vr = PyVideoReader(filename, threads=8, resize_longer_side=640)
# or to use GPU decoding:
vr = PyVideoReader(filename, device='cuda')

# decode all frames from the video
frames = vr.decode()
# or decode a subset of frames
frames = vr.decode(start_frame=100, end_frame=300, compression_factor=0.5)
  • 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).

# this method has the same arguments as decode()
frames = vr.decode_gray()

If we only need a sub-clip of the video we can use the get_batch function:

frames = vr.get_batch(indices)
  • indices: list of indices of the frames to get
  • 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()

Or get a dict with information about the video, returned as Dict[str, str]

info_dict = vr.get_info()
print(info_dict)
# example output:
# {'color_space': 'BT709', 'aspect_ratio': 'Rational(1/1)', 'color_xfer_charac': 'BT709', 'codec_id': 'H264', 'fps_rational': '0/1', 'width': '1280', 'vid_ref': '1', 'duration': '148.28736979166666', 'height': '720', 'has_b_frames': 'true', 'color_primaries': 'BT709', 'chroma_location': 'Left', 'time_base': '0.00006510416666666667', 'vid_format': 'YUV420P', 'bit_rate': '900436', 'fps': '33.57669643068823', 'start_time': '0', 'color_range': 'MPEG', 'intra_dc_precision': '0', 'frame_count': '4979'}

⚠️ Dealing with High Res videos

If you are dealing with High Resolution videos such as HD, UHD etc. We recommend using vr.decode_fast() which has the same arguments as vr.decode() but will return a list of frames. It uses async conversion from yuv420p to RGB to speed things up.

If you have some memory limitations that wont let you decode the entire video at once, you can decode by chunk like so:

from video_reader import PyVideoReader

videoname = "/path/to/your/video.mp4"
vr = PyVideoReader(videoname)

chunk_size = 800 # adjust to fit within your memory limit
video_length = vr.get_shape()[0]

for i in range(0, video_length, chunk_size):
    end = min(i + chunk_size, video_length)
    frames = vr.decode_fast(
        start_frame=i,
        end_frame=end,
    )
    # do something with this chunk of 800 `frames`

🧪 Experimental support for Hardware Acceleration

You need to install video-reader-rs from source by cloning this repo and running maturin develop -r or maturin develop -r --features ffmpeg_6_0 if you have ffmpeg >= 6.0. Your ffmpeg installation should have support for cuda. Check with ffmpeg -version | grep cuda for example.

from video_reader import PyVideoReader

videoname = "/path/to/your/video.mp4"
vr = PyVideoReader(videoname, device='cuda')

You can also pass your own ffmpeg filter if you feel adventurous enough. For example, this would be the default filter used when specifying devide='cuda' and resize_shorter_side=512.

vr = PyVideoReader(videoname, device='cuda', filter='scale_cuda:h=512:w=-1:passthrough=0,hwdownload,format=nv12', resize_shorter_side=512)

In theory any hwaccel should work if you provide the correct filters, ie qsv, vaapi, vdpau, etc. It has not been tested though. Feel free to report.

Another example with VAAPI hardware acceleration:

vr = PyVideoReader(videoname, device='vaapi', filter='hwmap,format=nv12')

🚀 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* vr.decode vr.decode_fast
f 1.0 65s 18s 9.3s 6.2s
f 0.5 33.96s 14.6s 5.5s 4.2s
f 0.25 7.16s 14.03s 4.2s 3.8s
f 0.25, r 512 6.5s 13.3s 3.92s 3.5s
f 0.25, g 20.2s 25.7s 6.6s N/A

* decord was tested on a machine with more RAM and CPU cores because it was crashing on the laptop with only 15Gb. See below.

💥 Crash test

Tested on a laptop with 15Gb of RAM, with ubuntu 22.04 and python 3.10. Run this script:

from video_reader import PyVideoReader
from time import time

def bench_video_decode(filename, compress_factor, resize):
    start =  time()
    vr = PyVideoReader(filename, resize_shorter_side=resize, threads=0)
    vid = vr.decode(compression_factor=compress_factor)
    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)

🌠 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.2.4.tar.gz (34.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

video_reader_rs-0.2.4-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded PyPymanylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.4-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded PyPymanylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.4-cp313-cp313-win_amd64.whl (736.5 kB view details)

Uploaded CPython 3.13Windows x86-64

video_reader_rs-0.2.4-cp313-cp313-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.4-cp313-cp313-macosx_11_0_arm64.whl (776.0 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

video_reader_rs-0.2.4-cp313-cp313-macosx_10_12_x86_64.whl (875.0 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

video_reader_rs-0.2.4-cp312-cp312-win_amd64.whl (736.9 kB view details)

Uploaded CPython 3.12Windows x86-64

video_reader_rs-0.2.4-cp312-cp312-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.4-cp312-cp312-macosx_11_0_arm64.whl (776.1 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

video_reader_rs-0.2.4-cp312-cp312-macosx_10_12_x86_64.whl (875.3 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

video_reader_rs-0.2.4-cp311-cp311-win_amd64.whl (738.7 kB view details)

Uploaded CPython 3.11Windows x86-64

video_reader_rs-0.2.4-cp311-cp311-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.4-cp311-cp311-macosx_11_0_arm64.whl (777.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

video_reader_rs-0.2.4-cp311-cp311-macosx_10_12_x86_64.whl (877.4 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

video_reader_rs-0.2.4-cp310-cp310-win_amd64.whl (739.0 kB view details)

Uploaded CPython 3.10Windows x86-64

video_reader_rs-0.2.4-cp310-cp310-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.4-cp39-cp39-win_amd64.whl (739.4 kB view details)

Uploaded CPython 3.9Windows x86-64

video_reader_rs-0.2.4-cp39-cp39-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.4-cp38-cp38-win_amd64.whl (739.4 kB view details)

Uploaded CPython 3.8Windows x86-64

video_reader_rs-0.2.4-cp38-cp38-manylinux_2_28_x86_64.whl (11.8 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.28+ x86-64

File details

Details for the file video_reader_rs-0.2.4.tar.gz.

File metadata

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

File hashes

Hashes for video_reader_rs-0.2.4.tar.gz
Algorithm Hash digest
SHA256 580d7b542574bc70ee71bd0d02e1f79802407b28857f3c8f799e95716aa492d3
MD5 d0abdb5cabcf684b13c763ba6c4330a9
BLAKE2b-256 b297eee622a057d3334e785eb63870172960125b6a057347f1cfaba491d06e60

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.4-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f9ab89bda59ad2b2b5a5f6c73acdc6879b886514ac0bbbf4b3c3d6d57e8195ef
MD5 213c3843eea2615bcb24128cc583d6da
BLAKE2b-256 d6c6e5ca65f0e2e0b7aee0b57707ae0f9231525b6281e6d76f5049e30dff1f97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8e13be5b2a3799a6454bac86dbee9c9ad22f5011bc425507362d89c46cd3f61a
MD5 4d74e613ee6304ad60621bacee339954
BLAKE2b-256 bc6e64a337668d9a19f2469e8be3d1786c05da9f65c8c9c30b402fff72394785

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.4-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 829b6654c01306bafde26b83969913c6f38d4e46a040237b89a72847c425746e
MD5 848dff14b90ab7ff4cc31133c7da9c3d
BLAKE2b-256 c82e45cf79322d5b2d562eb8b0b04f1156751f5cdc633e987ee1860775dfb704

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.4-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 12b7594d67b3c49bdc9957523d35ae4e6705145d116651427d9fb9bfecb36b90
MD5 505e72d9e65457569d644c91dc568cf2
BLAKE2b-256 9c0111e9d8d8777de5b40bac2a56f2bb2122003755e3a93152f97f49d37319fc

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.4-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 863f8a7dd944ef8bfbec483628784fc4143f3e45193ddaa3c91e38ba9f807dff
MD5 939df32a14db70ac7ed61ab063a6b183
BLAKE2b-256 0d4f7e9ba7abdd794bb8e8985b5579e1911a4fb9bffbb1c5d99ec57b19395842

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.4-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f8dbcd8c8d8ac4e36e8529e7995ea21dcf1ba0bad22f4a48750a0987dd24a7bb
MD5 6ae690737e9f8e1f9f9162f946cbc15b
BLAKE2b-256 1e278cbe688981dba7b6f672e1780006673882a290489a3261303dbdc974f729

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.4-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 26eb9499311d21fcc0a29c4fdc62f2763d919245f3743af2a836d2c7a98bf225
MD5 ad1867accda085d2d114777f80405023
BLAKE2b-256 94e1daceb12df395051a1e2ee69f0af20c5608365149e42e01e499abfcc80626

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 731764c39b477e7c8c6cf2d9e6aebd394cc932ba2cf2b0d169da063f81ce4e0f
MD5 61574f9148092d204271ca100ec4f6e1
BLAKE2b-256 a8a471ef66f64b3d3473b61fd80132b45fd5907d9cd50ab08209e5ef88f786bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3aad2b1ade9396a7081d07d73e30169f65206aef5d1b195a114fa63b348f070e
MD5 52c2faa072d6597267e1bb98366c572a
BLAKE2b-256 73e5d98da546678e08a59dbf6af579cd09c5d9294911d6e77361577e7b005aeb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 93acdb47c8e08bc3fc1d6c03241f5b47e92573c10fc6cd105ed175b0fb8ca645
MD5 cd2042f34e9ca0699b2c5232a81ca61e
BLAKE2b-256 cab28505356652cbaf19fc310764f19427b4a5ab5849939ff081a227368f51ac

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.4-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1cb6111a9498e185e1901b936b167def69f6ee719fdd8db8370ff6469fdc5ff3
MD5 a23c047b3405aaefbb7586dd317a7ca2
BLAKE2b-256 048ea4be81a7e9a6acf05c6a17410709eb5a05d63a7fca5ab7bd2168d1ee37f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fb93dca336b2d76dbe06c80721857471c60ae2e5fad0772312ba1551aeed7e46
MD5 c2526dd2fc2420c4c96085e1cee3c08c
BLAKE2b-256 4a56ce2eeeeb1a92d04656047bd967e4ebcf6806df377e2172a2e554509d0487

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e039956d402c7f792eb15e3455cf7a09e9da62bd804cca897d025826a68f862c
MD5 af80b477f7a5a6631bd887393b5482ed
BLAKE2b-256 ea87f121f25756fc325a1eeb5629dcb3f08c04aa482b5f2e1151a5f21fb11729

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 19d4fbccd0333cde31356c17dc3143a210d4894af2d312a6b5930f189763de41
MD5 d8e306de55bf85a2acd696c7a1266e72
BLAKE2b-256 f5dd7586d4d4c8d9e7eec07762354b39f9236849f5c0c8b63d9ae8cbce64e6c3

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.4-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d40372e1b0b5aa2ab38e8f6ca5acb8411252665eb7268c50732e861f05075168
MD5 6efed244f18f91ba3d130c5481795239
BLAKE2b-256 d377ca3ce9824ad3e02d93118fe177e981722a3feba9bc1f04b4ca1dd3d9caca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 00b942e1ec1aa327b7157e54caad9c5130bbbe837446d279b37aa1d24ba5f07f
MD5 a2c8a24838a7bafefa27e678c0cc6ae6
BLAKE2b-256 a78ec3355d9095692ca9d39f30a829ab84250987185c6bb0e02656daf42d5792

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.4-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 004d4272606dae7a69998830e6261500d57fae7fb7d38711eaadcf7e81788436
MD5 9ef5fd9eb3484d165cf9681cf7205951
BLAKE2b-256 ed99ce70925af4ac7a54ad1d8cfa4513bab28e4989c5d3337c0ba8c98bfa7a5c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 629ab378e5c2fb907e3c9a4bf85cc386258552e4ef13511397e3a25525ea2ca3
MD5 4237584bd6428e7acd61a6d5a616da9c
BLAKE2b-256 68812df0dfc34454de2fe5988ce953b0241b69f665bb7fb3652dad30a188bf11

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.4-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 5849fcc2fbade351391c9b6e028e45b7cd5f126860b11a3ec89a642a619f538b
MD5 98774bf2468b1b92c8751e81c4600fc3
BLAKE2b-256 b75a74a8fa6a4464c006cbfe278984edb04a1ed597e2080ade1b9d5f959274a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.4-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 989c6704637df641bace399cfa0ff7bd8f420304be0d8b3d2da22cf8cbeccd57
MD5 f3892ab72bbbd72a24fcbd18681ca59d
BLAKE2b-256 a3cdc798e91eb28435fd487e1aa3d69964193366dace1bed49998e4f855f7d27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page