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 and macos.

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.6.tar.gz (2.4 MB 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.6-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.6-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.6-cp313-cp313-win_amd64.whl (62.9 MB view details)

Uploaded CPython 3.13Windows x86-64

video_reader_rs-0.2.6-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.6-cp313-cp313-macosx_14_0_arm64.whl (33.0 MB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

video_reader_rs-0.2.6-cp313-cp313-macosx_13_0_x86_64.whl (38.3 MB view details)

Uploaded CPython 3.13macOS 13.0+ x86-64

video_reader_rs-0.2.6-cp312-cp312-win_amd64.whl (62.9 MB view details)

Uploaded CPython 3.12Windows x86-64

video_reader_rs-0.2.6-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.6-cp312-cp312-macosx_14_0_arm64.whl (33.0 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

video_reader_rs-0.2.6-cp312-cp312-macosx_13_0_x86_64.whl (38.3 MB view details)

Uploaded CPython 3.12macOS 13.0+ x86-64

video_reader_rs-0.2.6-cp311-cp311-win_amd64.whl (62.9 MB view details)

Uploaded CPython 3.11Windows x86-64

video_reader_rs-0.2.6-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.6-cp311-cp311-macosx_14_0_arm64.whl (33.0 MB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

video_reader_rs-0.2.6-cp311-cp311-macosx_13_0_x86_64.whl (38.3 MB view details)

Uploaded CPython 3.11macOS 13.0+ x86-64

video_reader_rs-0.2.6-cp310-cp310-win_amd64.whl (62.9 MB view details)

Uploaded CPython 3.10Windows x86-64

video_reader_rs-0.2.6-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.6-cp39-cp39-win_amd64.whl (62.9 MB view details)

Uploaded CPython 3.9Windows x86-64

video_reader_rs-0.2.6-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.6-cp38-cp38-win_amd64.whl (62.9 MB view details)

Uploaded CPython 3.8Windows x86-64

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

File metadata

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

File hashes

Hashes for video_reader_rs-0.2.6.tar.gz
Algorithm Hash digest
SHA256 1211fde97587e22c94cb7c5bf25f78dfb7421b4ba1935c2e97e8686cdb3870c9
MD5 e6b7b437a3b716173735e30e625eac09
BLAKE2b-256 ae6f728296b12140db7d14c915505dbb212018ff8ad2c30d60614ba59bf3499d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 27466ea0150a83d901525c43b1aee91bb6f700f5d295bdd5b8cfc4effde7d572
MD5 4b09542b99721ca29785f0827c5d016a
BLAKE2b-256 5a45e461c95f7663f714b47c6a111846c351402c8985a371d2501bc6c70a1afe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b3f66c4c9f3ae4b5d43fded630195efbf664436b8fbe1d1f923abcd6ec925954
MD5 8098765364f7e7b9568e0006780d6cea
BLAKE2b-256 fd0f6c0c36abdc515214994040bd0417e206a3ee44fef224eab5e386f03e8331

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ebc2f642a8e6a70636ffa818fb53407db0ed32603b47c1d25450df12df1a729d
MD5 e3f9c2f6d933a1424a17372ace825593
BLAKE2b-256 0ad092655c7cc018602682503a3fc6ef38ec8bc6bdfd24751043234dc88fd2e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8a1a2c5912e738a5ad60eed7a13d7592bbb6ddaa0b26f99d4b97afa84b60a7ad
MD5 dea9a2bc8fdcd1f1ca9002106ea38988
BLAKE2b-256 2b77b60f36169908f57293929a4f7b4c621a1794577fe6619fa67e6a65d6d7de

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.6-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 8afe597bc550d3869575b77a0b9cbc41d3e62c6aa230bf05179dd74a20704433
MD5 b0e16118419783724557893872463f88
BLAKE2b-256 6d7dcb5a0835b93370e7d97a37b2b0dec6234f296f756cf5eff36a0add72c2aa

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.6-cp313-cp313-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 b4d1c7817ce4bd2458a5d04ab6bcf854187f09331306452add124db449cfa255
MD5 3a307c31fa8b07b2c05e0232eebe8373
BLAKE2b-256 b0cd7adaacb428bfd373a93dc0a4fd696d07afec0e9de89c86027d59fb73d403

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 cf02d1c225631c507a84a2e7cad13b541e674ed8b1684fe36685f5c3fb938620
MD5 904c7e93e46074aa90b281d8ffbafc39
BLAKE2b-256 c660aa872019210b8d65b5e2dabc882ab72b818a20c2b24cc83a494f7e89337a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d778e24f66f13785576cf332e38fe405846d9a93136e08edc2cea967c0f500da
MD5 b54072e3fd2d4605aedfef2b56337848
BLAKE2b-256 d791e983f2c38526d0f755b1b2f5b31635a974e7c0039c9977c159e16fbd5cf6

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.6-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 16c9ddea9cb2cbc645c08d105d8531d1cad9d0d08665b5b56e555a2d40f5d372
MD5 877f20454cf98480329c302dd14a0b33
BLAKE2b-256 7684861d66e5ebe084d3c35cca290db0036e827b67a69038ad17d8c8951f68c6

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.6-cp312-cp312-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp312-cp312-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 b9f8227805a5d3b84f13c0e108bd7fc2c592b614caecab5a7823bc3897f9132b
MD5 1cfd7279369826d561953a356f3b2bf3
BLAKE2b-256 b671eedf1af2302e6bcf5bc34cba34d796b49bc55dd3cdcee36cefa0b953a8b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 850892a129b8b0d0762bdfe9de8200be6cd38a7682c5ed5eaf9c158edbdcbb8c
MD5 8d938f9111ed39cd4f28341458171a3c
BLAKE2b-256 f1f1f0b6e5af6aad3bf0c3321cd0f80de9d2317548590bc9efe8545e67f9064c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7322e0e96ec3326a8b22b0797bff7de995bec7554d1af4af793e315ecf14e050
MD5 15c43ad0f252a0ff3ff7757b4c94fe52
BLAKE2b-256 3ccaa8040fd8d97817321062cb2b36233f37cbc58b19e3384e4781a93c929ce9

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.6-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 c14510d07d82a1e28f9f96f26ef78ee7e5124a54bb346cf9eec1b75aac5b4481
MD5 ce02da852a1a4171a51b49e53bb3092f
BLAKE2b-256 57b9c071f01fb9b484d8113d63338b16e7338665b9ae78d80ed21ac3326a1aec

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.6-cp311-cp311-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp311-cp311-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 f6553403966c9e366159ac089e271baf22271b1536475c0d836aeaf386bd7d0d
MD5 7b04ce237ed4c7bbad7a37e4869105b2
BLAKE2b-256 9de899aed082fa9bc9273fd5227cb67b63bdff17111831cec5609b55a1af7653

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4bc38fca03c11504844be2ba008714ed1bc19232b8ddf4dc1721cf5429340ec1
MD5 4c0c84ae6aab74fb85efd42526119723
BLAKE2b-256 6ed43974752194349fb4857641272c554686043a98173f738bc818a4749bbb72

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b3abd618ba034165de5e615ee8b07d33cfe950ab73efa67d780adb22036c59b6
MD5 4f20a7fe44a16cf9c4f5036fcdacf449
BLAKE2b-256 c6d970e762c82d0d5d42bcfb25a34510f26ddb49c7f75b84e2e5225fd6373694

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 d4907836d253cc9757aa2b41b76ce2b7b685a389e60d0a47c0a615580572908d
MD5 c2d5b71ba41ef49f95aea6d3f525e2cf
BLAKE2b-256 3eda56159456e5a0f5e0d98cf6d17f08600dba0f260d0dc53befe675ee356fc4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 737572aa39a53660c09c647790a0d39009062703c93a9b1f3e5001c77d852c37
MD5 dc7726743ccda27155bf2bc913ce7f8c
BLAKE2b-256 ac087b84aa6f2c4253037fa2df2a316d067facfc0c2b6b24f2d00eb6828cceab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 606c78e60666e1da54c895f9e5fca0d1ff119977a8d3fff7d64bb159ed293f8c
MD5 ecc76ea0706609e6a0f36fb17f864f9f
BLAKE2b-256 6e1bdf2a3e6f89c8866719e3aabc69592a749500e1e834d5cb3a4d3774a554dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.6-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c8f204186f8b71829e6a14d33652cc0f51ed390c478018a97539fb440e605648
MD5 ff085bf244f8dd8c4419453278be9e8f
BLAKE2b-256 a0ee8302b2c96a9593db58c3c36aa3ea94a26fa051e9a135c222e63cb614c46a

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