Skip to content
Related Articles

Related Articles

OpenCV C++ Program to play a video

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 11 Jul, 2022
View Discussion
Improve Article
Save Article

The following is the explanation to the C++ code to play a video in C++ using the tool OpenCV.
Things to know:
(1) The code will only compile in Linux environment.
(2) To run in windows, please use the file: ‘play_video.o’ and run it in cmd. However if it does not run(problem in system architecture) then compile it in
windows by making suitable and obvious changes to the code like: Use
<iostream.h> in place of <iostream>.
(3) Compile command: g++ -w vid.cpp -o vid `pkg-config –libs opencv`
(4) Run command: ./vid
(5) Please make sure that the video : “Bumpy.mp4” is in the same location.
Before you run the code, please make sure that you have OpenCV installed on your system.
Code Snippet:

#include "opencv2/highgui/highgui.hpp"
// highgui - an interface to video and image capturing.

// The header files for performing input and output.

using namespace cv;
// Namespace where all the C++ OpenCV functionality resides.

using namespace std;
// For input output operations.

int main()
    VideoCapture cap("Bumpy.mp4");
    // cap is the object of class video capture that tries to capture Bumpy.mp4
    if ( !cap.isOpened() )  // isOpened() returns true if capturing has been initialized.
        cout << "Cannot open the video file. \n";
        return -1;

    double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
    // The function get is used to derive a property from the element.
    // Example:
    // CV_CAP_PROP_POS_MSEC :  Current Video capture timestamp.
    // CV_CAP_PROP_POS_FRAMES : Index of the next frame.

    namedWindow("A_good_name",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
    // first argument: name of the window.
    // second argument: flag- types: 
    // WINDOW_NORMAL : The user can resize the window.
    // WINDOW_AUTOSIZE : The window size is automatically adjusted to fitvthe displayed image() ), and you cannot change the window size manually.
    // WINDOW_OPENGL : The window will be created with OpenGL support.

        Mat frame;
        // Mat object is a basic image container. frame is an object of Mat.

        if (! // if not success, break loop
        // read() decodes and captures the next frame.
            cout<<"\n Cannot read the video file. \n";

        imshow("A_good_name", frame);
        // first argument: name of the window.
        // second argument: image to be shown(Mat object).

        if(waitKey(30) == 27) // Wait for 'esc' key press to exit

    return 0;

About the Author: 

Aditya Prakash is an undergraduate student at Indian Institute Adityaof Information Technology, Vadodara. He primarily codes in C++. The motto for him is: So far so good. He plays cricket, watches superhero movies, football and is a big fan of answering questions.

If you also wish to showcase your blog here, please see GBlog for guest blog writing on GeeksforGeeks.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!