Extracting and Saving Video Frames using OpenCV-Python

In this tutorial, I will show you how to extract and save frames from a video file. Prerequisites are Chapter 1 and 2.

Steps:

  1.  Open the Video file or camera using cv2.VideoCapture()
  2.  Read frame by frame
  3.  Save each frame using cv2.imwrite()
  4.  Release the VideoCapture and destroy all windows

Now, Let’s code

If you don’t have any video, no need to worry. Open the camera instead of the file using cv2.VideoCapture(0) and start extracting frames.

Use: We can perform a number of operations on these frames like crop, flip, reverse etc. save them into a list and iterate over them to get cropped/flipped/reversed video. (See How you can make Video from images)

Hope you enjoy reading. If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time

29 thoughts on “Extracting and Saving Video Frames using OpenCV-Python

  1. Anonymous

    Please, add termination condition in case of video file. Otherwise it will continue to extract frames from video infinitely.

    Reply
  2. praveen

    How do I set the parameter for the number of frames to be extracted? I only want to extract every 50th frame

    Reply
    1. kang & atul Post author

      To extract every 50th frame you can do something like this:

      Reply
  3. Fred

    I would like to capture more than one frame but the program hangs for me after I press “s”. I’m using windows 8.1. Any clues?

    Reply
  4. Satya

    I want to implement the above program, but at different time slots. For example, in a video of 20 mins I want to take at particular intervals such as at 2 mins, 3:50, 5, 10…etc. How can I modify and implement it ?

    Reply
  5. Sziroky Piotr

    Hey, my video has 30fps and it last 1 minute 23 seconds i run this code everything gone well but i just wondering why it extracts 2497 images when 30 fps*83sec = 2490 hope somebody solve this riddle. Thanks 😀

    Reply
  6. amna

    can anyone tell me after extract the frames how we cluster that frames i mean that characters in seperate folders

    Reply

Leave a Reply