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, 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 = video_reader.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 = 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.
  • 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.3.tar.gz (23.8 kB view details)

Uploaded Source

Built Distributions

video_reader_rs-0.1.3-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.3-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.3-cp312-none-win_amd64.whl (699.6 kB view details)

Uploaded CPython 3.12 Windows x86-64

video_reader_rs-0.1.3-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.3-cp312-cp312-macosx_11_0_arm64.whl (792.0 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

video_reader_rs-0.1.3-cp312-cp312-macosx_10_12_x86_64.whl (835.9 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

video_reader_rs-0.1.3-cp311-none-win_amd64.whl (700.1 kB view details)

Uploaded CPython 3.11 Windows x86-64

video_reader_rs-0.1.3-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.3-cp311-cp311-macosx_11_0_arm64.whl (792.1 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

video_reader_rs-0.1.3-cp311-cp311-macosx_10_12_x86_64.whl (835.9 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

video_reader_rs-0.1.3-cp310-none-win_amd64.whl (700.1 kB view details)

Uploaded CPython 3.10 Windows x86-64

video_reader_rs-0.1.3-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.3-cp310-cp310-macosx_11_0_arm64.whl (792.6 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

video_reader_rs-0.1.3-cp39-none-win_amd64.whl (700.5 kB view details)

Uploaded CPython 3.9 Windows x86-64

video_reader_rs-0.1.3-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.3-cp39-cp39-macosx_11_0_arm64.whl (792.4 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

video_reader_rs-0.1.3-cp38-none-win_amd64.whl (699.8 kB view details)

Uploaded CPython 3.8 Windows x86-64

video_reader_rs-0.1.3-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.3.tar.gz.

File metadata

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

File hashes

Hashes for video_reader_rs-0.1.3.tar.gz
Algorithm Hash digest
SHA256 90ccc0c1c85f17286fdf493242684e2308ec573e57f86064af35ac40f7bd9dff
MD5 5909fadea22b10e08f0a50e2e127c26e
BLAKE2b-256 b873533004de1b00394550de97c491888ce7671239446009c51857451db19530

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5d27e639d86a79a478badaf5da104afe510cc3e4d7eda4d0f8f29ed69fffb225
MD5 57079f6a5fde69445b4958967620ba65
BLAKE2b-256 3dc6afe9cc2e88ca3061e08dfafc3a88772086a52c0ae97a3cc0e41bd4e190b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ce9d161fa5764a4eec0f5334e410fab33cadd2fbb38218eed9f2343882a716fe
MD5 6c21e4777a22cd37abc3bbb945dc08e4
BLAKE2b-256 009ce8477098f77717d7bfe2a6a0a7849fc82d12f84786a7982b90ac312c9640

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 2644fc61471a90a0fb29bbdd7ce47f66f340e43ba586404eeda88694c143d4f8
MD5 da813efac5020abfc62008a9858af30d
BLAKE2b-256 bbce41c76abe8addcf78504c2777e18fb92bd9f6c72ac66dcd6ca78658576bae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9ec5339e64de65b6562bdfbf7403df81d2c515bd02868964bfe05f2d087995ac
MD5 068acca9a3a90633791b4b68d188a063
BLAKE2b-256 d404c71a8a012829207c59f688c1551ee0a7bb8297b7811510c50464c9005b3e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4cd5d21d0b783654f72fcb3b5443ff609287e5bb9d8554fef0b93bc1b63193b5
MD5 c3eefc751a662f5ab036ca73617302ba
BLAKE2b-256 4a3a93dfdd86ecd7767c2770f86d40f6278421126ddbc1753540213c57c83624

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a17203453d802d3c3d30ae74736ba31e199f749c92de5709e51aa890279e98d5
MD5 e3ac952fd6950150a878be355951cdbb
BLAKE2b-256 027e83e27c4328e9e56909502f6eae0f8092e130ef111fdd73d6f358e723ea89

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 e762d9f35188eefd60c7521827c95984aa744768a02913a546bc449d9c485a62
MD5 0891806d5bd5155daf7bb807a4abbd9e
BLAKE2b-256 d994fae4adecff76f655f27933325aa02b938453d86a9ba4d3f145301163d6e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 574a34a4903ded4d52551bba6282bfdfdde9ccaef226c6441f10d1cc89748efc
MD5 b9b06f1a988a492be5c195ce90d7ea09
BLAKE2b-256 9899b3d6066284f42c9131374915102cee6920ffa41cc1cd9fdf9e824224a32d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 16f2e780d8ff540e905617674dc7d92286d7fa1ec0472f62aff66ac802d7e406
MD5 a9d87f211dd0a4a124c979856965e2e2
BLAKE2b-256 a30c32ff32476c1c761e72da414406be498c9aab7d90adb789e763fb4a2b1fe8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 97a55093b6f42f71311bb73d8581638422feab01a840f97aa27047f7d61c0edf
MD5 6a35a600579044c00d096a666f34f9fe
BLAKE2b-256 fe5c681ea34e3c0728cb8802f30d730bc932bc1cdf14ef3dff2549a88331cb0b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 a4e75491ba7972d540df36a4c9af98edc8c05e15dffafdf07d9982bd8dbe27cc
MD5 0c236658e3cc119cd0bbdb2976c4ecbf
BLAKE2b-256 ac9248a9a509cc20d87f61f78bea6e8d2642f6b4aa99ce6aafdf18ab955efb65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 95527be6ee304515e29d1568ce42c84680ce92f5c552f21492f26a4e9cc84bf5
MD5 11231568a5b924cad5ced320ce2974e4
BLAKE2b-256 cc0fea32c88143b18a53c2b5a3f9680b5e840165b7c317035b16034537b67b83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 98e52a7d8973799a2b7ddb9f4f1d6f924d8d7b27c88cc1fef8f954b1dd4c87db
MD5 4cb2238e4298a82799208c6ff30d8c6e
BLAKE2b-256 c6d6db7ac6bffa717534cab5cb2fd3a80511c9ea7eee4209000b5a58bf9c8aca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 8ed79f999a4c4f51e26e30f353fe58e536a1923c6daa59b9543461466611c1e7
MD5 1d8ac1db05d4ea150e07c467adc66db6
BLAKE2b-256 d0f9049df065d9bf9139647632b84ff8b9d9862dd371b9b3133a2fdc768f69c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 591a209e60011c026331631d36e11cf78b376fb024883efc0a22535519640efc
MD5 dd8d5ee6d12d1e2ddf41c0b60b6ebaba
BLAKE2b-256 d500c0d201d80adeaedc56ba48606d91673a512e8403b8ffa76a1652d6b50c6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2202da86f34ba56db88e295a6afbea65312e604c6989791457192681e9710051
MD5 99e9223ed60fd9618804340146551fea
BLAKE2b-256 edde9b243e5f832c23e3a5ce67a8125f1574cac8859acf2e6df3fe7c76204a39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 d85104c0f9e91f4933e10c898155ab204810177dd5505d035d0b5822452d5a10
MD5 b11a2a4dc425bd7361a46b61aaee0147
BLAKE2b-256 dcd049ab8874ca8360c38af252fda9dacf0ec5bad230d3e68656425726bfd837

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.1.3-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3b70e2c4219ecfc1062f1d04f7134ec2af99ab0b73bdf9e4ef4f0a58b9dfaaac
MD5 3a4e0453a367ff6a7e2b48065981aed0
BLAKE2b-256 672fc4b4362d44e2839885b320384fadfdd785372fbacc78f30f792eb698cc02

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