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 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(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")

NOTE: currently only work if the frames shape is a multiple of 32.

:warning: 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:

import  video_reader as vr

videoname = "/path/to/your/video.mp4"
chunk_size = 800 # adjust to fit within your memory limit
video_length = int(vr.get_shape(videoname)[0])

for i in range(0, video_length, chunk_size):
    end = min(i + chunk_size, video_length)
    frames = vr.decode_fast(
        videoname,
        start_frame=i,
        end_frame=end,
        threads=8 # adjust to whatever works best for your use case
        # you can also set threads=0 to let ffmpeg auto detect
    )
    # do something with this chunk of 800 `frames`

: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* 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 11.3s 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.

: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.9.tar.gz (28.7 kB view details)

Uploaded Source

Built Distributions

video_reader_rs-0.1.9-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (11.7 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.9-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl (11.7 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.9-cp312-none-win_amd64.whl (671.4 kB view details)

Uploaded CPython 3.12 Windows x86-64

video_reader_rs-0.1.9-cp312-cp312-manylinux_2_28_x86_64.whl (11.7 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.9-cp312-cp312-macosx_11_0_arm64.whl (723.5 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

video_reader_rs-0.1.9-cp312-cp312-macosx_10_12_x86_64.whl (805.8 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

video_reader_rs-0.1.9-cp311-none-win_amd64.whl (671.4 kB view details)

Uploaded CPython 3.11 Windows x86-64

video_reader_rs-0.1.9-cp311-cp311-manylinux_2_28_x86_64.whl (11.7 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.9-cp311-cp311-macosx_11_0_arm64.whl (723.4 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

video_reader_rs-0.1.9-cp311-cp311-macosx_10_12_x86_64.whl (806.5 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

video_reader_rs-0.1.9-cp310-none-win_amd64.whl (671.8 kB view details)

Uploaded CPython 3.10 Windows x86-64

video_reader_rs-0.1.9-cp310-cp310-manylinux_2_28_x86_64.whl (11.7 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.9-cp310-cp310-macosx_11_0_arm64.whl (723.8 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

video_reader_rs-0.1.9-cp39-none-win_amd64.whl (672.2 kB view details)

Uploaded CPython 3.9 Windows x86-64

video_reader_rs-0.1.9-cp39-cp39-manylinux_2_28_x86_64.whl (11.7 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

video_reader_rs-0.1.9-cp39-cp39-macosx_11_0_arm64.whl (724.1 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

video_reader_rs-0.1.9-cp38-none-win_amd64.whl (672.1 kB view details)

Uploaded CPython 3.8 Windows x86-64

video_reader_rs-0.1.9-cp38-cp38-manylinux_2_28_x86_64.whl (11.7 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.28+ x86-64

File details

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

File metadata

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

File hashes

Hashes for video_reader_rs-0.1.9.tar.gz
Algorithm Hash digest
SHA256 8c5e66b0d186997c454943e79fc22be653ff479f6520eee3d24a29b5e00f6ea0
MD5 831659b12d8a377bd373a05de1593baf
BLAKE2b-256 1f35845bef22fdc0ddc4f7eec8231d8e44bff1522bf2a7794e57a17ae726154f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2fae3358173606a30b2fd7411406fe9c91a17c5aa8216d898b1154b31cb998a3
MD5 0cf16ce694cb4c5029bb663d9edaf420
BLAKE2b-256 6825c17c04b0510b456c55fac2d2ce528533eb6f089773216ba213bf40f65683

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 443ff955244db338bfd9b8f12e3d5bda92bf6a5f632b69793a23d95c82e37e16
MD5 bd5cc681ffdff57f103f5dbefe465a66
BLAKE2b-256 8b872f94537259701099a8708947b017351fb67158bf83afc4ef46890f688ba7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 910595c3830077ad6a67015b1ee736ceefdd8a048a561366b4d4c445e3557800
MD5 7cb3097ac67a4b27fa4dce4211e437ed
BLAKE2b-256 e561f16353d0626369f9e4a40cfcf8a898848329181f5ffc10fc916781b6fe00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8464761b1eb47e419994f00af642fde3208707b0fea0f35ff5a816801acf6279
MD5 d5a25f509938915183dd8e5a0246b349
BLAKE2b-256 549d365235c7706c690855f95f151660c5bd4bcce0c790cf3e5d2a162f39d50a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6d131a7407883227958af7be7daf2120f55a847e9937385737bcfcf41b5a26a7
MD5 eca8580434e433f32707a599f232d027
BLAKE2b-256 f6fa302ac4121cbe00e3ac5bc87fb3651ff7ae2bb20bca2b0bfee671d5de2b40

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ba04886c041d988e3f955df344ce096e61fe84e6e7d01d00885118d92f482a5a
MD5 f45be3417f6bea7284c2c0530c6cdb52
BLAKE2b-256 90d8a1460a99b2655785c7fcaf008bacbdfdab2138d5c0c86279d16219479dc3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 55d1d18b916e3f079726b9958470b77c411d87b4ecd0400af5426c33e50f31c1
MD5 3953def77e0a5de4728ebfb0760e3397
BLAKE2b-256 f9727c3d660fb3773e645fa9d2293fffe4d85913b87e4e41aad6a4faf8b8df50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 429fdf4aef4bf2677995fb99a44987d2f02b3bc84b77bb3605fd9812c32b37aa
MD5 99cd4170cf375747f9983a3745a36d66
BLAKE2b-256 605d2432edef9e4a9ffae0bc62bd4039b7b5da7e4faf12b66d21d81ede707048

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2d9181abf229eab304237f6f409bdc5e1f36a18f596b92241ac6e009355db8f7
MD5 8d37eb7b5877494a80ac29a8812b1d62
BLAKE2b-256 b7c26ba3528a075fcfe6536f187f94423c2fc837dba0bf0c9b04bd951df6e7a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 95a5d2ba8b1d94e22b93d7fda9885bf2accb6236cbd3e6d002a09b6c102a3f27
MD5 dc159363f66887ee9b8e0bc4ccac493e
BLAKE2b-256 469d3d72617062be4ac8d89f31c4fc7cfc7a2d42f07573119c17164da2f63a62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 074c95b26409cec0be53ccc0bfa49bd29c5ca3531efa5eaa81041b834cbd3726
MD5 c71690d87a90642f44d5c8e1facba0c6
BLAKE2b-256 33ebfda5500b41a4d69e89ec7a64e221a59e32bf59171dcfa685d6d6d26b0cf7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7a2e9749ef4023f60598d1cb632f075a4215d1b4b18d68a17295015e91d9ea6f
MD5 87f252b1b3a0f7522c1daa3b3f06ca49
BLAKE2b-256 763088eb5f2bc41eafb776054f3605f83c3136bbbb20e90fd8e802fc57fdb3ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 25635c66120e99f1c045891ceec4aa6de386239872cbca9fd5e7a22a24c0d666
MD5 d78788eea1653ec64f57520867382a24
BLAKE2b-256 895b1a2270596b6ed3abcfb57b8615ab18ea88e684493a10c5f83074c148bdcd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 1c34b4b40ef4d339f99b4d12fe39787c979994ec3bd6bff437c24f5e4b531d0b
MD5 7a2223d3e9c24c7eb92c8e3280633258
BLAKE2b-256 8ab95c04833badb6ba07b0ae15d2af025871cb63c5d561ab0a7950aff4c4cbc1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3a1fcfceb6be6cd9635297a11a39987b91caeec3de061e10ccdd9ff70f66669a
MD5 50be12824fe0776bfa84f8bd139d25b1
BLAKE2b-256 f4abfb4d8df592d8d38c71f9b5704329f5de9f5063423873309b0ce0d525d5c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 74e709b810cb04af2a257d6031ee3f3c8370b9569df491704e85e1250af75288
MD5 905f2a36dbc404e994b0b4f5bad5d8c2
BLAKE2b-256 8fafdd1af0036e95205287f3797118fa8be17c61ab6a9e0e9352840ad5919070

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 3f44c766b1538b87d88b4685a0a4fd7dc2277d55b6be78f4b01e6fd58cb496ef
MD5 2f35b3afadca4b6db5a14232a92fca8e
BLAKE2b-256 e266a30ee8bf8bf112304d31143b6713f321eeac9cc5a380bb7784081aa68564

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.9-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 38e59e3e3afa33266ed9e56f372ffa78cfcbce67aa991be26ba22b3769d85557
MD5 64ba54bfe2758b441ddde857f9024c2f
BLAKE2b-256 1409befcb02415b4acf661de6cdcb16921f1394b781cb70e4ff27d452716677e

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