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)

# 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'}

We can encode the video with h264 codec

from video_reader import save_video
save_video(frames, "video.mp4", fps=15, codec="h264")

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

⚠️ 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`

🚀 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.2.tar.gz (30.8 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.2-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (11.7 MB view details)

Uploaded PyPymanylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.2-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.2-cp313-cp313-macosx_11_0_arm64.whl (754.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

video_reader_rs-0.2.2-cp313-cp313-macosx_10_12_x86_64.whl (845.3 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

video_reader_rs-0.2.2-cp312-cp312-win_amd64.whl (711.4 kB view details)

Uploaded CPython 3.12Windows x86-64

video_reader_rs-0.2.2-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.2-cp312-cp312-macosx_11_0_arm64.whl (754.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

video_reader_rs-0.2.2-cp312-cp312-macosx_10_12_x86_64.whl (845.6 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

video_reader_rs-0.2.2-cp311-cp311-win_amd64.whl (711.3 kB view details)

Uploaded CPython 3.11Windows x86-64

video_reader_rs-0.2.2-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.2-cp311-cp311-macosx_11_0_arm64.whl (754.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

video_reader_rs-0.2.2-cp311-cp311-macosx_10_12_x86_64.whl (845.2 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

video_reader_rs-0.2.2-cp310-cp310-win_amd64.whl (711.6 kB view details)

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.2-cp39-cp39-win_amd64.whl (711.9 kB view details)

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9manylinux: glibc 2.28+ x86-64

video_reader_rs-0.2.2-cp38-cp38-win_amd64.whl (711.7 kB view details)

Uploaded CPython 3.8Windows x86-64

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

Uploaded CPython 3.8manylinux: glibc 2.28+ x86-64

File details

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

File metadata

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

File hashes

Hashes for video_reader_rs-0.2.2.tar.gz
Algorithm Hash digest
SHA256 7a39af6c192e675ea6489355bb8b85f8731913163a993a03bbdd6f63daa66fb2
MD5 9c16f9c0f231b2dd16be42200cb1f62c
BLAKE2b-256 4966cc0967e4ed4102dfbe6946a618b951d99374c9fde00158015cd357c95ae5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c08c98ca0e79bed8d9e0dca430b40be37ca684cfa7f0f47b3bb0f735d8e4371f
MD5 ae184be14bc1e97d5a22fdf3504b812f
BLAKE2b-256 351a213482249cd8d004cbc2f904590a32fe15488498337289a7a632dd593213

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0f214c415ec2e786abee772a292c2414b3a40ec66599c977ea40a0e9cb95cd5e
MD5 961b0394dc5608e23327a3caf8052b2d
BLAKE2b-256 d037634ee30ed7100ed5cebfa116428360bd6ea3f682b2a77e8d13324d400eb9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 70708fc06800f19b2c6f31a5461d0ab50251bd6b3996f6a563f618ced527809d
MD5 12a450742209ddc501a7c2ed5dda477f
BLAKE2b-256 d298b8a7fdab5ce90a8a76abe762ffa3f298f2914e869583cdc311f4bd228ffc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 78e1a5112af6638910cd8880f53551d21ebf8094c4874694cb7902d520027901
MD5 7eef95f24bbd99cf3b2e3c9d3cd5b468
BLAKE2b-256 a936601fbbaeaa87ba0fd2b21f0c1ff93e1819e23db082fb73debb1b2f4e2a41

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 977cffd4d736162f352ee647fbf76c1f66312e2d18fd6a042369ef4bda9feec2
MD5 27741a262d9675daf7d95b07a271cb8d
BLAKE2b-256 c06c963a6bee4b1fbb996e133353954f8c4abf75b3d6ca053a7da686f122c1d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b7174e5c30ffe26166c45eaa9070ef22ea2da59aefa89d493652480c39e5426b
MD5 8b187c1b1150654a8adf3d3c0e86f13f
BLAKE2b-256 c97777bba2fe9cd1fda624cacd455d1ff555464c13d7582432ee2aff8ac32732

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e6b6067ba7a77a578b50aef15f934b412382c37573c39ebaf1e8e83046657db3
MD5 9855b2ec7ea66667301a23c352c5e3bf
BLAKE2b-256 2f9c1403084f4401dcf084d24f19e76aebaea50c404163aafd92dd8a75d34784

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a896d8fd72781e46b592529a2a0dca734b684477df6e5d6bfc7b04dd102ec789
MD5 f109984f7e4de85ca3426b53fec04846
BLAKE2b-256 f9939496f9b9eb53a6f3e749b69b0382d80ea1d4eb2bae17f4a91aff5e9eab7c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 9f9bc58670a0399d84d62e0dae6806bca7d2a2a8cb1a03357ad3ce68efad954c
MD5 f871f2255eb927b4a56573d5105f3cd4
BLAKE2b-256 41d697a89c251ad026fc660462f202eb11e1b2cdebf47dfc24201433f1735175

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 22b9abad03b9ab120eed5e605fad2a2b7fdb21ac6fbc12789629d1140faf38b4
MD5 f50ecb889b7720ac4cc4d2554ab9bd78
BLAKE2b-256 2ff4395529611a52761c3aa1149b2d5f50aa1670e96127ecc2e7dcbba98cc58e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cbc25b3ac57047aa9ac0be0978ca8bcfff467978e8625c67814134a0479f6300
MD5 67783e4d0652a404eda467bad4024354
BLAKE2b-256 7b72eb1c04dcd92209f149ec4023b2d3987581aea86513f36f403e2c23d53843

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 16b6713da5f7097600013eefa20d9bb89eab73f3a9fe0a7e2a026ea225b840e6
MD5 7c9c3f77f4471a1c98b757da876d3726
BLAKE2b-256 f4c13430e49697c0b127f20ca7fc92c09e7f8047a77f2ebf0fdc7f31a4387c4f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 fd2fec6fb32f8c1d380c63fe1d68bd8f20c7b8871fec1ad6d5bd6c4863d7cc0d
MD5 f3d760acc3f570d200b1824f42d9aca4
BLAKE2b-256 eb5586505d4226e0ceae96f9ae33f2665ae624bd7f2278bd3de7ee51cf42764c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 894e055db1e3dbfd4665ed42a85b755cdbd6f63e483dd8150188b629fa9139d4
MD5 6c63f5915348b3c983e578d366dad34f
BLAKE2b-256 9dc249005a02e971d9d59e400db552345f2d39b442ed2d54189653802be69a99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 768d499efab2e316f9c68d8f15bbef16cf81af353a82b1f9ef205095d75e0799
MD5 6077476914248736fb65a4a16ded18b5
BLAKE2b-256 149eaa26adac6463b946049d69e96089de724daf3e53f543b0b2dfeadcadb81f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dbffe5c7124b8cad0d3582e5fc9835ad9b3ff92726d913e6552fac637c9f6d94
MD5 bb4e6d5106decc5da291d37a64a43fa8
BLAKE2b-256 f385d61b3fd213147bdee47d3774549c7afa7ee99c7592d541073bb00c951c73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 51f3f64cd9c9d2a15be424eeb3e21a2b8751cbe77c7cdd12d378ea21a5de5542
MD5 ad2410505b46a94a7e5b1f64654a2751
BLAKE2b-256 0e8598cc9068e9453ce7b20e2d952fc4eb9fa69d901997ff16980c5f7007de0b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.2-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 acce1cf550aca8b47533899b89dad2d9ba1c7294de4db37af56d0c2164383e0b
MD5 aa227771fca7c7b189f0ab76fd24b523
BLAKE2b-256 ed864f232a599d9238a4104c7122bae77bafa2bee49d59f53e0b6a01757ccf7c

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