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=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)
  • 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.5.tar.gz (36.0 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.5-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.5-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.5-cp313-cp313-win_amd64.whl (746.0 kB view details)

Uploaded CPython 3.13Windows x86-64

video_reader_rs-0.2.5-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.5-cp313-cp313-macosx_11_0_arm64.whl (781.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

video_reader_rs-0.2.5-cp313-cp313-macosx_10_12_x86_64.whl (883.8 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

video_reader_rs-0.2.5-cp312-cp312-win_amd64.whl (746.5 kB view details)

Uploaded CPython 3.12Windows x86-64

video_reader_rs-0.2.5-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.5-cp312-cp312-macosx_11_0_arm64.whl (781.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

video_reader_rs-0.2.5-cp312-cp312-macosx_10_12_x86_64.whl (884.4 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

video_reader_rs-0.2.5-cp311-cp311-win_amd64.whl (748.3 kB view details)

Uploaded CPython 3.11Windows x86-64

video_reader_rs-0.2.5-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.5-cp311-cp311-macosx_11_0_arm64.whl (784.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

video_reader_rs-0.2.5-cp311-cp311-macosx_10_12_x86_64.whl (887.6 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

video_reader_rs-0.2.5-cp310-cp310-win_amd64.whl (748.4 kB view details)

Uploaded CPython 3.10Windows x86-64

video_reader_rs-0.2.5-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.5-cp39-cp39-win_amd64.whl (748.7 kB view details)

Uploaded CPython 3.9Windows x86-64

video_reader_rs-0.2.5-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.5-cp38-cp38-win_amd64.whl (748.7 kB view details)

Uploaded CPython 3.8Windows x86-64

video_reader_rs-0.2.5-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.5.tar.gz.

File metadata

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

File hashes

Hashes for video_reader_rs-0.2.5.tar.gz
Algorithm Hash digest
SHA256 75534bedf922c4b11ccaa9f9b82fad22d4b832ba3e04aee32f9fd33ddfdbba51
MD5 4400ecf40eba3f69f851e09564e95bec
BLAKE2b-256 9fc7812e0947e2639e76985c416c8316774735d11df42118cb0f53aa26658b22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c31186d152ca8e87470efd00843f83a9ea0a52a0e2637a6aa60c5f8a512a4f96
MD5 e6178e2af75b81d969a9bfca24ba26e6
BLAKE2b-256 d46484cdb97103271b5162df72344d9018d28fd0cca1d98ae4b4c14ae77ac35d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5775cd999c78dae541cd17aab35a8876150edcec11960ade87b07f8e37bfec55
MD5 6054a5180f0ad7a903937e95f3ba0cba
BLAKE2b-256 355d12637652f60dc5bdc285a1496e51255b1a4200276a64f35e374b23c66865

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 daa4a4def72a566842e82111172abed7a4154cec1fa7d6b84558b05bc7bcad13
MD5 d1ea03e1e1bdc4c880612ffc5383d635
BLAKE2b-256 2bc0dc6f44011e0a6e20d370217ecd408cdb6b61e390b2e46e788d73c424f542

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 847fa5edce2ca44e2f6150509fa575088a8d7f2c7a440abee406528cdea29bab
MD5 985343c24cd8cd8704fda6b43a41e2ec
BLAKE2b-256 f4115204d88370211d1820d86686546d4af2b69727bf07823bfc87e0050f4411

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d9ceadf4f12fc9a977665d1c96d9a809285157c624c5f96e75370348ca757592
MD5 3b35ab9458154b636b1fa4b79b7d5146
BLAKE2b-256 aa3b1e1745f00f01258dd11c489f53281bf8a76952167bdf97c8e2302aebc4a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5488da964b6006193d2aa7a057a9c7ad897db834e17162a2122c8b9aba3e173f
MD5 9e9b613cd677e1a447a5dc58f28aadc4
BLAKE2b-256 092e31331658fed402d1f39d6683977e3e9ce1f25d2f8b859b33be0c48304bca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 07847bc69fdd6ad3ef990a4eef8ff8f2417e97960e08fb0a2ca71bc542ec8ec3
MD5 8937da6153fb4379d0cb8fc228b610cc
BLAKE2b-256 6a3e7946447ab0ed3fb7160d43da63db2e9ca0d3a98e8a1e469eb56f1e0c2281

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8d38d9a792780e64dd4293f9d8ead5deca11180bd859707888797f0f3936034a
MD5 1b61687352e93b7b6af893233aefa763
BLAKE2b-256 27bd79aebd87d8d112ab7097a74ad688ea3bbb9328524140e667d9c00cb316cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 535d97e7984b935d2b3b01acca8b592ca00b2e845497ca066f51d66280b38583
MD5 789bb3650aa15ea52ab389018eb21bea
BLAKE2b-256 03f6244ad2db685801b95aef33e53d4dc4638a882d3e82aa6297e9de976f42a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 58225d57576a8a549747e11670ff1e0a564a50f38b0ee79c3276092fb2774e3c
MD5 596681787d1ab694031882705fd24aa7
BLAKE2b-256 68ceacd98535255aada459d7f0d026efdabae2e67637412eee808f138f3efb08

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c92779d59f58911b62efd3b336b364ed2fdecbf496b0b25ecf0e252231451c25
MD5 78aa944d2005be6068d9019e11aa45f7
BLAKE2b-256 5f30a44822d655069626c75fcb601793231e96d871992b27b1cc1f40b4fc9083

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cb55ac23a7e7b262907a97982a28342abc09f36777fa7a1575158c30570941c5
MD5 928dc84504209c90305bf9b8feabe7cd
BLAKE2b-256 7980ee93710e4e2cb51ff62cda11c03d7d05b0f0353678b86e9464291f4dc2c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dff94112a61f45c6254912df89f22b89c517d2d84b1dae9e79ba601924007f71
MD5 8c6f8ef0ce1a17895f597600ce12bab2
BLAKE2b-256 70b291152714d31653d6777695fae1d52beb8b2082198caa942027169c7490fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 eabd65a019ab3e5c4a0816daa5842ee9f1934a457799a64f899ce511acf3693d
MD5 529100ae611a1062ccb3558bc7072457
BLAKE2b-256 5b247c4ee62acfc353a0a3e31bd709ca60064c95f6ce13ed9123a1d98fae4f24

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 73a44f391adcd54541936f03491796af036508869866c92d107e7ae009104948
MD5 0f808ae9c684d644fd66244041888369
BLAKE2b-256 7970bef552dea0cd1849ac31f64e7b0112f512698f1ac763e91cf4215a71d6e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8b19a669573697832f7a1e9f8712eccb968e23417122ea7d5380ffba90fb777b
MD5 c689dcaf261ed7f310f9ba10244f413b
BLAKE2b-256 c0946a0164baf09e6665814daed8eb1b01ff056f5494ab38c60e7f9dd4916a4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 27de77ce5d85387616dc6947cc7db56162dd35d0fef60f507cb336c5e48b1d40
MD5 743b6fe3a2843b9be8fc2d7fdb61b4d3
BLAKE2b-256 46c7971db6629ef6133ecea31b4ed7f7d5c601606eb6749052bbbf413c24cab7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 50f33239f04b8640bf0e462ef54c6aeff55a86385e876be928f5e82ecef80f3f
MD5 6ea94726592a54bf22e3e47876fb4ade
BLAKE2b-256 a528df760e2dddf535917dd0f46e7b88fc5d4ff7e43b369eebac1dded7983f67

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 67a398e64f689c0c6d9e97650d5ab810b6b1c320674efb5174ee52de25c428b4
MD5 19a7542fbc46becc70628305c3e2248e
BLAKE2b-256 309244d0832db8f1fec0b07feafb3b4fe8890c88e0905decfde2b0c95df6bda0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for video_reader_rs-0.2.5-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b9ee686ecf0e6b163f48867c47365fb68a1ff318377060256d944d1030663ac9
MD5 2c4d6e83497128652e79230ef71eb839
BLAKE2b-256 cd2e14cfe3b65c107738d3b1e8e6a9f9ac2c4f126cafedf2f1a18db75d8a2788

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