Shortcuts

VideoReader

class mmcv.video.VideoReader(filename, cache_capacity=10)[source]

Video class with similar usage to a list object.

This video wrapper class provides convenient apis to access frames. There exists an issue of OpenCV’s VideoCapture class that jumping to a certain frame may be inaccurate. It is fixed in this class by checking the position after jumping each time. Cache is used when decoding videos. So if the same frame is visited for the second time, there is no need to decode again if it is stored in the cache.

Examples

>>> import mmcv
>>> v = mmcv.VideoReader('sample.mp4')
>>> len(v)  # get the total frame number with `len()`
120
>>> for img in v:  # v is iterable
>>>     mmcv.imshow(img)
>>> v[5]  # get the 6th frame
current_frame()[source]

Get the current frame (frame that is just visited).

Returns:

If the video is fresh, return None, otherwise return the frame.

Return type:

ndarray or None

cvt2frames(frame_dir, file_start=0, filename_tmpl='{:06d}.jpg', start=0, max_num=0, show_progress=True)[source]

Convert a video to frame images.

Parameters:
  • frame_dir (str) – Output directory to store all the frame images.

  • file_start (int) – Filenames will start from the specified number.

  • filename_tmpl (str) – Filename template with the index as the placeholder.

  • start (int) – The starting frame index.

  • max_num (int) – Maximum number of frames to be written.

  • show_progress (bool) – Whether to show a progress bar.

property fourcc

“Four character code” of the video.

Type:

str

property fps

FPS of the video.

Type:

float

property frame_cnt

Total frames of the video.

Type:

int

get_frame(frame_id)[source]

Get frame by index.

Parameters:

frame_id (int) – Index of the expected frame, 0-based.

Returns:

Return the frame if successful, otherwise None.

Return type:

ndarray or None

property height

Height of video frames.

Type:

int

property opened

Indicate whether the video is opened.

Type:

bool

property position

Current cursor position, indicating frame decoded.

Type:

int

read()[source]

Read the next frame.

If the next frame have been decoded before and in the cache, then return it directly, otherwise decode, cache and return it.

Returns:

Return the frame if successful, otherwise None.

Return type:

ndarray or None

property resolution

Video resolution (width, height).

Type:

tuple

property vcap

The raw VideoCapture object.

Type:

cv2.VideoCapture

property width

Width of video frames.

Type:

int