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.

⚠️ 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=512)
# 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.3.tar.gz (34.3 kB 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.3-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.3-cp313-cp313-win_amd64.whl (724.8 kB view details)

Uploaded CPython 3.13Windows x86-64

video_reader_rs-0.2.3-cp313-cp313-manylinux_2_28_x86_64.whl (11.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.3-cp313-cp313-macosx_11_0_arm64.whl (775.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

video_reader_rs-0.2.3-cp313-cp313-macosx_10_12_x86_64.whl (868.1 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

video_reader_rs-0.2.3-cp312-cp312-win_amd64.whl (724.9 kB view details)

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.3-cp312-cp312-macosx_11_0_arm64.whl (775.5 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

video_reader_rs-0.2.3-cp312-cp312-macosx_10_12_x86_64.whl (868.2 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

video_reader_rs-0.2.3-cp311-cp311-win_amd64.whl (724.9 kB view details)

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.3-cp311-cp311-macosx_11_0_arm64.whl (775.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

video_reader_rs-0.2.3-cp311-cp311-macosx_10_12_x86_64.whl (868.1 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

video_reader_rs-0.2.3-cp310-cp310-win_amd64.whl (725.1 kB view details)

Uploaded CPython 3.10Windows x86-64

video_reader_rs-0.2.3-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.3-cp39-cp39-win_amd64.whl (725.7 kB view details)

Uploaded CPython 3.9Windows x86-64

video_reader_rs-0.2.3-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.3-cp38-cp38-win_amd64.whl (725.7 kB view details)

Uploaded CPython 3.8Windows x86-64

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

File metadata

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

File hashes

Hashes for video_reader_rs-0.2.3.tar.gz
Algorithm Hash digest
SHA256 4e95df1fa17c0b9972f91a95f05b6cfc14fa9a33b7539bba800b3aa0778152a5
MD5 26c4e97c1eda314b5f8527eaeb67679a
BLAKE2b-256 641951b8006f2acc6a117588ad21fc49cb2b57a443e6d640820c47c47e9c2956

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 763dd790e682f0d433cc3f5bd7502a7c147f3aa26987c52cd8b01d2b53ef8caf
MD5 ecb8969b88c760c9ee61847624131057
BLAKE2b-256 76dec4ef8d88b3b10916b116b47b494a09b10b8c74f7b0bc4b2e25440cba4be5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 26a445ccfc1bd0e6f43070ef2687a620cb28371cbba7beaf3909a7f26b6feb11
MD5 0add22b7db121c627b34c8e6bd72505f
BLAKE2b-256 ec5bbdd21c7014fca99d306c3df76dff4809142ffe03e6011fa7b88aa46b5eb9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dbc82682614f4deb058fee128d538c1031254a43709009f184f7dc3cba29ff6f
MD5 7a12baa1136aae73f52042415d3c1d73
BLAKE2b-256 2108916d7c54ffe046df3415e77f74aa36e32933869a163bc750d4ab12975a8d

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.3-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 294317bc1f857d4538e75936ee72a31dc2f15d0469276c3d88a86b1a23cc4fc2
MD5 338a91fb3350b739b3337b27f36db29f
BLAKE2b-256 2e740ef8c3382f3f7e840388b1647da4146dca4245b2a94c49c2e78200c21df0

See more details on using hashes here.

File details

Details for the file video_reader_rs-0.2.3-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c8c57d703bcdd7dd3b9c9fdd3fd30a0af0d07606450c16e343a21607b8a1cda5
MD5 ed0038ce32028eb747411d971d0c2918
BLAKE2b-256 218ced0ae1d44d4e37649d5cc792732b447fd9af66fdd635077a6cf6dcdb5f91

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a5c7f87f661650b6a775c6f91ade3a4b40ab326aac49de12e070f84f394fecb3
MD5 3da5e378b9e61ef2ef3a8e69d3067d35
BLAKE2b-256 e4e1fff8aac6ecbd93c7f66eb54686510d5150e9376f81448b7715a642e3ac36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6bbb517274ae9e93b4ca5118704108245859ff7a9066af8c33c06455f3618b8d
MD5 69c3fbb25d6d0854035b64ee61955817
BLAKE2b-256 0f5c16ef3435f15e32f7fab10f1d9f2d1bc6e847ca2f5491e2fa9a5fe222fb0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8e274f12f35f23d4cd352014db0ef8d91cca482ae66bce60dfc8e08ac01427e6
MD5 051f807451e551377a079a4806885559
BLAKE2b-256 da20432c4739b99efc0a52b9db87f168c62ed23ad7b737a66366c30c96e94cc1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a8bdee382f3cdb19a86e956e1902d46c0b4de8e387167e0cee8d6c565dd33b01
MD5 1865088cf4ed8eb791104c640d66f799
BLAKE2b-256 7201954a1d082531f8e803507ca0581f96a1a17bc2d6ac538e42362762178bf3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 57a1eb48c96cb58571622cb06588c7a7276a397d7f9ecaccec7ee73347d484fc
MD5 f20f142d84ff4a81d94af9a9e8dd08c7
BLAKE2b-256 c0cd65d540ef44b8bad69518c47e1dbc36adf4b0049e719a1ed7d0b13484a073

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 44f1212640c9acbfb7159cd74e15ec40910afd8b270bd4f876991fc287726741
MD5 3b2ee464fd0aa4fff7f73ba72f6e10ae
BLAKE2b-256 f62489fde8415148c1e487436f74835200b209b954156c60460033509297c6bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8e9543ca28b0306778a9c910a77634f274623bd6e8c894c12890d8b6256ec546
MD5 c510a771a6cce845966daf40d8857022
BLAKE2b-256 5c031720bd90b5d9e93f29d3e8a1d81fa5ce89278e0d7e6cb933029e83ceea9e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e5e506b2f4509fb81af87e381ee0adce497dbe691c27ac7700ce8de51fa2d07e
MD5 9b0013fdbac02218e52ef50af7c8a28f
BLAKE2b-256 2b78b725846d2f97b68b22759622a8f944dc58a55cb6cb8fb2b993aff74d1e28

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 cdc6262a284bbdceec68968a83986aef533a31c3e854e367654bbf7c312ebfb8
MD5 1a744697577ebc480b3727a22fb22507
BLAKE2b-256 6abd7dbd79c38026183972edc97be53d85a05df2be268552c895a765c32ba7e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 301949c934a3d117bc6205de3938a23d879431f684777198290a1d3390f2c331
MD5 4a1ea26ff48f6aa8e02f0ea324721d29
BLAKE2b-256 0feb6232aa39b026b81e181c36778533a620fa6a94f1a2d175127287a0a7ac7c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 ba1e7f3d6d69bb73a8464d5d3b64731ceb2031a88df6cd977f83006d1f192a3d
MD5 85eef63f9640f0d01dacfc343c270298
BLAKE2b-256 8cbad8d9d2895677bc64a78427f3796d225286679826e92fefbabd2975e1b7a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 be268b86e107d1f7b9e851d7346f26ed074a21b32ad96c421351c524a4537fcd
MD5 3a7eca2293ae34c8dbb70ef1ef7702b2
BLAKE2b-256 628670f29b4d397f1dd8f3b716228ebd69d82bc3ba104ebb3acdc83f5e0c5ea8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 45d5a9d000a043c04c9e27f74b30b8933afaf8c53792588b923e46c6d08932aa
MD5 c7559f6d878b9c22f70dcacc7ea0548f
BLAKE2b-256 332f60b1905c8eafec3aedc6d5f0c39c10d46c83b611c34ab32ff718a3768dd9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.3-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 723d8481888d91ba120e19cf0dcdca3bafd9ac254b32a0b3ab88adf79909556b
MD5 1e1b49ecacdd8c8f705390ff70da3a7d
BLAKE2b-256 e48c40314ceb14cd83348ddb7ba05a50ce63d8ec12189af71022ba4e20ce08e8

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