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

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

Uploaded Source

Built Distributions

video_reader_rs-0.1.10-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.10-cp312-none-win_amd64.whl (696.8 kB view details)

Uploaded CPython 3.12 Windows x86-64

video_reader_rs-0.1.10-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.10-cp312-cp312-macosx_11_0_arm64.whl (747.8 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

video_reader_rs-0.1.10-cp312-cp312-macosx_10_12_x86_64.whl (834.9 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

video_reader_rs-0.1.10-cp311-none-win_amd64.whl (696.8 kB view details)

Uploaded CPython 3.11 Windows x86-64

video_reader_rs-0.1.10-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.10-cp311-cp311-macosx_11_0_arm64.whl (748.3 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

video_reader_rs-0.1.10-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.10-cp310-none-win_amd64.whl (697.0 kB view details)

Uploaded CPython 3.10 Windows x86-64

video_reader_rs-0.1.10-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.10-cp310-cp310-macosx_11_0_arm64.whl (748.5 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

video_reader_rs-0.1.10-cp39-none-win_amd64.whl (697.6 kB view details)

Uploaded CPython 3.9 Windows x86-64

video_reader_rs-0.1.10-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.10-cp39-cp39-macosx_11_0_arm64.whl (749.2 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

video_reader_rs-0.1.10-cp38-none-win_amd64.whl (697.3 kB view details)

Uploaded CPython 3.8 Windows x86-64

video_reader_rs-0.1.10-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.10.tar.gz.

File metadata

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

File hashes

Hashes for video_reader_rs-0.1.10.tar.gz
Algorithm Hash digest
SHA256 52629d902c49ee4c6683a50fb5042dedca0f45d0a6b6114b98853be87c2615c1
MD5 bd8c1f57ae2b9fdfab3079041284e28c
BLAKE2b-256 93225d121e7182374656ac518fd3cf1f9a40f1576f5fec3fd755fb2aeb9ec476

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 95e5ec02c4b48c3dc3bb4e102add07956c8ee0880d0250718a42c37f2e93c936
MD5 cc210ab08f7c053ecfde3d82f10fc9a4
BLAKE2b-256 02907aa8fa9f3147a37a305d894ac8562e1bd06ee948324c30487be0a260802d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 2feda72f5e288280175e9de09d24ab2078fdd5b41b71bf06020628462b497db5
MD5 4941783143228300f65b84992257c3e1
BLAKE2b-256 ae70d177a4731052a942892e72f1914c7fa486c84f38d2b913516f6f3ac46b91

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e0c4100e8c273cfe4796fcf4eb9c1b0bfc1bac797277c092c9a97840b9f3703f
MD5 d555c25bb47be6bc6e506dd6624358cb
BLAKE2b-256 5eae4041f6100faec74b9b630adcae71c7af7fc19efe345512b339a48bf3d761

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ea339b1f9196319484fba6fb8d2a4c79eb23176a3bf82d3aa3ba3ca2703c21a0
MD5 94bf1335fa3566c315d3ed10d5b4ec3c
BLAKE2b-256 c3a4bc6327f3e7bec4129098d3597b6f991003ecd7eec0280f5bf2742b35cf05

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 67a97343c2777c6aa3ed5bebf89258f08afb3ab9ab4a2f9fbe5f78fa354dbbd5
MD5 8a08e53fb714a775cc9daf5958ed6168
BLAKE2b-256 23d3b634d89f7d75d4e6b71e8908eeaba977ecbd67855f80776e8215bbb915e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 7ce8a66a82e0bca60863c37bd17f343ce4155f9c829403b81c45ed3af48e6f9b
MD5 56588e76f5bd6d4cd41ce7d21cd9ab02
BLAKE2b-256 c68fee647a0fb26368c9cef79bcf55d60d40f46e21edbd05d601426dc06bb8da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 35735c54a805ddc843855173162ae34773d548de849d8e55fc639a539aa2dfd0
MD5 4103e9e97991bcb952dc7556aab3cc00
BLAKE2b-256 bb467becf93b078ed6093e3423e40d921c4e62c356846f5a8f9ec840d73c47af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 96b4697b7a22c39ca0386993b07cdc21fe7a7002b8c560e18a4db37738de49fd
MD5 d9da806c0c6171a77152939fdf638113
BLAKE2b-256 7ec35dfbf8a4510b762db44baf4f195442255db480fcb5f72693036481384274

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b81a326624f639ee239beb42e4af219c891ad27b0da05577d64ea6ba4a700d4c
MD5 a1d8a4043ec913df55d819e876698768
BLAKE2b-256 84f6901c296219a47f4cc863349f45a99699eb2258c29fde2692d6196d1aaccc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 099da626bf3d00534a9cc68d5319cb1aec059de1e1d2fda65959c1bfdb692b36
MD5 1bf2758ab48843f12b1f784c72b6f150
BLAKE2b-256 e300f9c6a0032113502908052d006b2396f701bc16dc3365354ff1efcb83c432

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ecb55ec19388c80f6c55e49900b3b8d2a26cec9195bfcd50640420a15577904e
MD5 bb11e18970284fe90e6fc859c3a92954
BLAKE2b-256 486765e958d136c5996896455bb6925452f55699a77b234614f86eb95a812b40

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d51450c94c8b3d29ec0001160df9f77bef34c9f4031853deec5c8d18ea30c9ae
MD5 d7a21d446618756ff09ea28c25ff86ad
BLAKE2b-256 1920fec55be9e89da342ca2a856339a541993d279f66ea3e74835933a217cfdd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 e721d3dafd3df5ad5ea3561f5886b736780db9a4e8162d6f1129d132f45d46ba
MD5 47db79b5069ecefe00f68b95ad741f6b
BLAKE2b-256 d49c6eddd1b00d8f55979a6b79542e93195a9bd82d3cc5c45949c6d32aeeeeb5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5b0d75e2460e25b1b77015d40d59d7c1cb27b0a92daa9668dc55662a0b6e1d6a
MD5 1ce40ed1f760feb3b869f2f66b548225
BLAKE2b-256 c9c7c06ccf04c77e44f3b74cc1a9fd2114590bb99e4b8456e30f3d9115024b00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d9535805dd0780480f0334a0497ab272ef54e211337145a2396a0c9a113c3325
MD5 d96855559e9a6067afeb508a3b682dcd
BLAKE2b-256 e1b0b38afc96faf6a91a49f74f6e21fe26969ea544131cd9aa9e9681361ac612

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 e3cdace04998ca63fa4ef8205567a70be6e6711521ff75bc262c15cb7f1531c1
MD5 2fa8765229452d90a0a17b2b92908b84
BLAKE2b-256 4887022941928ef95ada9e1125661040e001825aea48d9d19bf1563d41c9812d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.10-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e4658364233e949817f5ecd6fe32df3c38fafd8540723e68c18b2562c06a5ded
MD5 b2360492312db7b8c4f93a5040302c9b
BLAKE2b-256 b79c3c84832248f8cbeb58d79a6ff5ceb4d4919116c4a4b814ad3173ce8e981b

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