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)
# alternatively one can iterate over frames
for frame in vr:
    # do something with a single frame
    print("top left red pixel value:", frame[0, 0, 0])
  • 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.7.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.7-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.7-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.7-cp313-cp313-win_amd64.whl (63.0 MB view details)

Uploaded CPython 3.13Windows x86-64

video_reader_rs-0.2.7-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.7-cp313-cp313-macosx_14_0_arm64.whl (33.1 MB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

video_reader_rs-0.2.7-cp313-cp313-macosx_13_0_x86_64.whl (38.4 MB view details)

Uploaded CPython 3.13macOS 13.0+ x86-64

video_reader_rs-0.2.7-cp312-cp312-win_amd64.whl (63.0 MB view details)

Uploaded CPython 3.12Windows x86-64

video_reader_rs-0.2.7-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.7-cp312-cp312-macosx_14_0_arm64.whl (33.1 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

video_reader_rs-0.2.7-cp312-cp312-macosx_13_0_x86_64.whl (38.4 MB view details)

Uploaded CPython 3.12macOS 13.0+ x86-64

video_reader_rs-0.2.7-cp311-cp311-win_amd64.whl (63.0 MB view details)

Uploaded CPython 3.11Windows x86-64

video_reader_rs-0.2.7-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.7-cp311-cp311-macosx_14_0_arm64.whl (33.1 MB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

video_reader_rs-0.2.7-cp311-cp311-macosx_13_0_x86_64.whl (38.4 MB view details)

Uploaded CPython 3.11macOS 13.0+ x86-64

video_reader_rs-0.2.7-cp310-cp310-win_amd64.whl (63.0 MB view details)

Uploaded CPython 3.10Windows x86-64

video_reader_rs-0.2.7-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.7-cp39-cp39-win_amd64.whl (63.0 MB view details)

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9manylinux: glibc 2.28+ x86-64

File details

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

File metadata

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

File hashes

Hashes for video_reader_rs-0.2.7.tar.gz
Algorithm Hash digest
SHA256 2ee086f0f3d32b969b9fd12ece1060d8128afc44a97be2e237e6f1f41e2d6b7b
MD5 3261851edd9fc6c93e057196f1f4c33d
BLAKE2b-256 1cbbe663e8dd1ac7f041c702af42789c1f9db4634da11d37d25b1494d6b3fdc3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7665a1e44daf75951f2407cade313cdd76974d1cac0b10bff3962a3a9ab961cb
MD5 a080ad66080ddf547223cfed99ba5970
BLAKE2b-256 e810c37dd1f24748651e431ae12ac5564c3ffe945175d860c9113df690c80ff6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 59aabd754e0968dac42d8b31c31af3026e35c8668ee6b9a4fcc655402fcf8e73
MD5 748252c39dd6951dfc5dfd63450850ca
BLAKE2b-256 d3f9569e688a5a8a519c64c5df37de273227e4b19f45328b710ead1c4b103c27

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 69096329777aba6a2bfce7a2fdeeac409966efbe50ca627f4dc1c236bcc9885d
MD5 a1792565fb170a43635df4b170e96e5a
BLAKE2b-256 31c2fecbe6719ff850b9f4160073f2439b2b60f3f299fbd590a38dbff9ce5bb8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 365b6343c68ee3d1d73f6f74e119dc28a8ab799549f02c98b03ebb8064ce34cc
MD5 21b74a74fcc63390bd75661c4762eaf7
BLAKE2b-256 266c8bcff6f6824d64a8267471eb6155df0a24a57e5d6b4b34e01291ad242f7a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 337e01c575c5cfed8a1d3d4d4a934f74fa3cacd3aca15094ed48960fadc2e9ca
MD5 45e28676d06ef5dcaf9ad78dd0d9a6ce
BLAKE2b-256 ae301b1cd45edb99d359ce6c575252bc13d1300c252c61c820785ade6085bdc9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 2a4365f40f5d14f54be1068f8f3f6b03c28290089e41f493fc771296ae0969fb
MD5 50479e7e60ec66a94f56cf3dcadc1059
BLAKE2b-256 1436d172502332a6d3f8474df0d59176513a22e2873d3cd89d2db7efffe47247

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 03de01fe86833579ab2a67ca42265fa498dbb1160b720101a592e7dc06fc67d2
MD5 06e2791708a83c43560e9b3c21dab43a
BLAKE2b-256 ef11605658bc3717da2cf641f979f0b45b23faed7c5300560ca1f73b8d8cef0c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9a413bda1312fdb13e8d5c7f10994d3dbb16ef49a01c1cb1404a0e9d6c4f6c1f
MD5 c82cf451863cd5fe76efe7c1a3907a03
BLAKE2b-256 974efe28f494cf03c429e2c81cedd657fab1c345417e4cc06f4395e1d8104030

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 9fde6c500d64a65593d8f53dbd25fac6672aedf17d9d08051642549876afc86b
MD5 f8a7a8ad73adc4644b460aa04741fac1
BLAKE2b-256 79b3ef5b7f5be5f40e3aa3bed94cfb1d5e7cc0cbe76c7419f4b934097a5e611e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp312-cp312-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 b72bf6e079ae05636ac2adb984433c76efe468c589d4eee8702f5433628e9d89
MD5 c49f85e7645c9ce7dd6b3704ed072c99
BLAKE2b-256 a9b5a0c258372fa1945297f5871805c49d97bf1e23718c5d97b55f6e04336db4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d58841e93d5273f6f8a0914ceb7196b6dedbe99927a9a573b76fa285576abf43
MD5 9c436fe6824bc0ab7092ffc6e1d4216e
BLAKE2b-256 cb7c3f6b061113a79acd81b5458703edd2c6d3cdb0232d9027311c74942d5432

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3eec84a879bc45b7eb2a0c2ddc37347aea0a65125788436365798b65c43c3503
MD5 54bcfda851502e903061f86e3493ef87
BLAKE2b-256 474390acacdb3d592888fcbf0fa1f0316116b7e8231e0e92bd5aac0bc385611f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 2b60b79e7bf9bb1e79f215e16d3777186466b530c80fb0952586aa21bb8d107b
MD5 839aa310aac8e93b83f397e9c0191066
BLAKE2b-256 312e38a8f8e81ee39c2f9a5790c66fe143f1a12aaac4fb8479552e6ba48bafc2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp311-cp311-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 7d6b56a059474b853820b8161df7247b817b0904b8f43fdd46cede903548a4f3
MD5 0a40b8ef9e577d448cb0f92d566a4b8e
BLAKE2b-256 b6b702f824d7e84457101107d972523effe337ea247b2e7e3d21b5731979932e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d06ca3996bca96f83a5a118c2f9f75ee4fc674bbd77f783c85c9a58076b1134f
MD5 5a9e4cfb0102d55819755bdacfbaa562
BLAKE2b-256 7d69f0b4fa5ff1c7e2353247ff378b8b0ed38c67f4c957a834a0d09e2ff930fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4f077bb55ed9c90b15715f86c1dc6b6aade733922abba23a7476ee96991f81d1
MD5 114994c365fb946356bea01e3ae46585
BLAKE2b-256 ea5e3196bcc497ee84c155a706d8d3805a71bd6b85dab4c7c272217c37ca1c0b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 d91ff5a1ff3eaf85cbc8d22f63bc149fd7a5ef5f7d396411f212724d88eceff6
MD5 4217a27aee5c848b5c187af80adb4caa
BLAKE2b-256 3acd7e5fbae3aa48f3d289b7b180eee8b190e9fc78069e603360a9298b4e9b11

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.7-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8e9c6381992719fb4a164244e07640fa25370f9a680fa70e8b00759b1f833f4f
MD5 ce528be6ad76e9ea2f6d18b7268f5042
BLAKE2b-256 9ccbd2a2bc236388ac8c9559ae4938924707aeefab4cfc66ddef832cdf8ff158

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