├── 768x576.avi ├── main.cpp ├── presentation └── Intro.png └── readme.txt /768x576.avi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MicrocontrollersAndMore/OpenCV_3_Play_Video_File_Cpp/5f08a5b43337055e623dd83cb03f908ab651cdea/768x576.avi -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | // PlayVideoFileCpp.sln 2 | // main.cpp 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include // it may be necessary to change or remove this line if not using Windows 10 | 11 | /////////////////////////////////////////////////////////////////////////////////////////////////// 12 | int main(void) { 13 | 14 | cv::VideoCapture capVideo; 15 | 16 | cv::Mat imgFrame; 17 | 18 | capVideo.open("768x576.avi"); 19 | 20 | if (!capVideo.isOpened()) { // if unable to open video file 21 | std::cout << "\nerror reading video file" << std::endl << std::endl; // show error message 22 | _getch(); // it may be necessary to change or remove this line if not using Windows 23 | return(0); // and exit program 24 | } 25 | 26 | if (capVideo.get(CV_CAP_PROP_FRAME_COUNT) < 1) { 27 | std::cout << "\nerror: video file must have at least one frame"; 28 | _getch(); 29 | return(0); 30 | } 31 | 32 | capVideo.read(imgFrame); 33 | 34 | char chCheckForEscKey = 0; 35 | 36 | while (capVideo.isOpened() && chCheckForEscKey != 27) { 37 | 38 | cv::imshow("imgFrame", imgFrame); 39 | 40 | // now we prepare for the next iteration 41 | 42 | if ((capVideo.get(CV_CAP_PROP_POS_FRAMES) + 1) < capVideo.get(CV_CAP_PROP_FRAME_COUNT)) { // if there is at least one more frame 43 | capVideo.read(imgFrame); // read it 44 | } 45 | else { // else 46 | std::cout << "end of video\n"; // show end of video message 47 | break; // and jump out of while loop 48 | } 49 | 50 | chCheckForEscKey = cv::waitKey(1); // get key press in case user pressed esc 51 | 52 | } 53 | 54 | if (chCheckForEscKey != 27) { // if the user did not press esc (i.e. we reached the end of the video) 55 | cv::waitKey(0); // hold the windows open to allow the "end of video" message to show 56 | } 57 | // note that if the user did press esc, we don't need to hold the windows open, we can simply let the program end which will close the windows 58 | 59 | return(0); 60 | } 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /presentation/Intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MicrocontrollersAndMore/OpenCV_3_Play_Video_File_Cpp/5f08a5b43337055e623dd83cb03f908ab651cdea/presentation/Intro.png -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | The video pretty much explains it all: 2 | https://www.youtube.com/watch?v=VE-1UarzE40 --------------------------------------------------------------------------------