Extract all the frames in a video

Hey,

I recently needed to extract all the frames from a video and thought that maybe others would find it useful as well. This is assuming one has OpenCV installed.

int main()
{
VideoCapture video(videopath);
if (!video.isOpened())
return -1;
Mat im;
int fileNumber = 0;
string filename = imagepath;
string fileformat = ".jpg";
while (video.read(im))
{
string filenumber = to_string(fileNumber);
string fullFileName = filename + filenumber + fileformat;
fileNumber++;
imwrite(fullFileName, im);
}
return 0;
}

This code will read the video from the videopath and extract all of its image and save it to the imagepath. All the frames will be numbered from 0 to n where n is the number of frames in the video. You can tinker around the code for your purpose. If you like it do leave a comment. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *