Skip to content

Instantly share code, notes, and snippets.

@AruniRC
Created July 17, 2018 16:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AruniRC/3e6d0a777b0770ba301a87198fe81034 to your computer and use it in GitHub Desktop.
Save AruniRC/3e6d0a777b0770ba301a87198fe81034 to your computer and use it in GitHub Desktop.
Get number of frames using Scikit-video and Python
'''
Sometimes it is useful to know the total number of video frames before starting to iterate.
This is not possible using the default video reader in Sk-video.
The snippet below shows how to do this using the FFMPEG reader instead.
'''
if osp.exists(video_path):
vid_reader = skvideo.io.FFmpegReader(video_path) # NOTE: not the standard skvideo.io.vreader
else:
raise IOError('Path to video not found: \n%s' % video_path)
# Knowing number of frames from FFMPEG metadata w/o without iterating over all frames
(numframe, _, _, _) = vid_reader.getShape() # numFrame x H x W x channels
videogen = vid_reader.nextFrame() # generator for iterating over all video frames
@0HaNC
Copy link

0HaNC commented May 29, 2021

It's also important to call vid_reader.close() after the read process to prevent mem leak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment