decord icon indicating copy to clipboard operation
decord copied to clipboard

VideoReader: Fastest way to get all the frames?

Open ggregoire opened this issue 1 year ago • 2 comments

Hi there,

I'm using get_batch to get all the frames from a 1 min video and I wanted to ask you if there is a fastest method to achieve this? I read the code from video_reader.py and I don't think so, but I just wanted to confirm.

Here is my code:

vr = VideoReader(video, ctx=gpu(0))
n_frames = len(vr) # 1800
frames_indexes = list(range(0, n_frames)) # [0, ..., 1799]
frames = vr.get_batch(frames_indexes).asnumpy()

Thank you.

ggregoire avatar May 23 '23 23:05 ggregoire

It's not faster but it is more concise:

vr = VideoReader(video, ctx=gpu(0))
frames = vr[:].asnumpy()

Also do you really need ctx=gpu(0) ? Isn't numpy always on cpu anyway ?

gcanat avatar Jun 27 '23 20:06 gcanat

I'm not a numpy expert but we see our script appearing in nvidia-smi when we use ctx=gpu(0) so I guess it works.

The weird thing though: when we hash the frames, the hashes are different when we use ctx=cpu(0) and ctx=gpu(0).

For instance:

vr_gpu = VideoReader(video, ctx=gpu(0))
vr_cpu = VideoReader(video) # defaults to ctx=cpu(0)
hash(vr_gpu[0].asnumpy()) == hash(vr_cpu[0].asnumpy()) # => False

ggregoire avatar Jul 04 '23 00:07 ggregoire