One limitation of OpenCV with video is that it is incapable of capturing any sound in the video. Therefore you have to use another library like FFmpeg to capture the sound from a video file or a microphone.
Play a Video File
In this section, I am going to show you how to play a video file. First, a VideoCapture object should be constructed by passing a valid location to a video file. Then the VideoCapture object should be read frame by frame. Finally those frames should be displayed in a window sequentially. Here is the sample OpenCV code to perform the above tasks.
//Uncomment the following line if you are compiling this code in Visual Studio //#include "stdafx.h" #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char* argv[]) { //open the video file for reading VideoCapture cap("D:/My OpenCV Website/A Herd of Deer Running.mp4"); // if not success, exit program if (cap.isOpened() == false) { cout << "Cannot open the video file" << endl; cin.get(); //wait for any key press return -1; } //Uncomment the following line if you want to start the video in the middle //cap.set(CAP_PROP_POS_MSEC, 300); //get the frames rate of the video double fps = cap.get(CAP_PROP_FPS); cout << "Frames per seconds : " << fps << endl; String window_name = "My First Video"; namedWindow(window_name, WINDOW_NORMAL); //create a window while (true) { Mat frame; bool bSuccess = cap.read(frame); // read a new frame from video //Breaking the while loop at the end of the video if (bSuccess == false) { cout << "Found the end of the video" << endl; break; } //show the frame in the created window imshow(window_name, frame); //wait for for 10 ms until any key is pressed. //If the 'Esc' key is pressed, break the while loop. //If the any other key is pressed, continue the loop //If any key is not pressed withing 10 ms, continue the loop if (waitKey(10) == 27) { cout << "Esc key is pressed by user. Stoppig the video" << endl; break; } } return 0; }
Explanation
Let's go through the above code line by line.
//#include "stdafx.h" #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std;
These are the include files and namespaces used in our program. If you need more explanation, please refer to Load & Display Image.
VideoCapture cap("D:/My OpenCV Website/A Herd of Deer Running.mp4");This is one of few constructors available in VideoCapture class. This constructor will open the video file and initialize the VideoCapture object for reading the video stream from the specified file.
The destructor of this class will de-allocate any associated memory with the opened video file. Therefore you don't need to de-allocate memory explicitly in your program.
if (cap.isOpened() == false) { cout << "Cannot open the video file" << endl; cin.get(); //wait for any key press return -1; }If the previous call to VideoCapture constructor is successful, cap.isOpened() function will return true. Otherwise it will return false. If the function return false, program will print a message to the console, wait for any key press and exit the program with the exit code of -1.
It is essential to check whether the VideoCapture object is initialized successfully before playing the video to avoid a possible crash.
//cap.set(CAP_PROP_POS_MSEC, 300);You may uncomment this line, if you want to start your video in the middle. You can call this function inside the while loop too.
bool VideoCapture::set(int propId, double value)
It is possible to change some properties of VideoCapture object with VideoCapture::set function. For supported properties, this function will return true. Otherwise it will return false.
It is possible to change some properties of VideoCapture object with VideoCapture::set function. For supported properties, this function will return true. Otherwise it will return false.
- propID - The property of the VideoCapture object which is to be changed. Most common properties which can be changed are
- CAP_PROP_POS_MSEC - Current position of the video file in milliseconds
- CAP_PROP_POS_FRAMES - Relative position of the frame to be captured next (Starting from 0, e.g. - 0, 1, 2, 3, ...)
- value - The new value for the specified property
double fps = cap.get(CAP_PROP_FPS); cout << "Frames per seconds : " << fps << endl;These 2 lines will obtain the frame rate of the video and print it in the console. You can call this function inside the while loop too.
double VideoCapture::get(int propId) const
This function returns the value of the specified property of the VideoCapture object. For unsupported properties, this function will return 0.
This function returns the value of the specified property of the VideoCapture object. For unsupported properties, this function will return 0.
- propId - This argument specify the property of the VideoCapture object which is to be obtained. Most common properties are
- CAP_PROP_POS_MSEC - Current position of the video file in milliseconds
- CAP_PROP_POS_FRAMES - Relative position of the frame to be captured next (Starting from 0, e.g. - 0, 1, 2, 3, ...)
- CAP_PROP_FRAME_WIDTH - Width of the frames in the video
- CAP_PROP_FRAME_HEIGHT - Height of the frames in the video
- CAP_PROP_FPS - Frame rate of the video
- CAP_PROP_FOURCC - 4-character code of the codec
- CAP_PROP_FRAME_COUNT - Total number of frames in the video file
String window_name = "My First Video"; namedWindow(window_name, WINDOW_NORMAL); //create a windowFirst line specify the name of the window. This name will be used to identify the window in the code later.
Second line will create a window passing the window name and the flag WINDOW_NORMAL. With this flag, created window become re-sizable. More information about this function can be found in Load & Display Image.
Mat frame; bool bSuccess = cap.read(frame); // read a new frame from videoThe 2nd line will read frames from the video file one by one inside the while loop and store it in the frame.
if (bSuccess == false) { cout << "Video is completed" << endl; break; }
When there are no more frames to be captured from the video file, cap.read(frame) function will return false. This if block is placed to break the while loop at the end of the video file when bSuccess become false.
This function should be followed by waitKey(int) function in order to provide sufficient time to paint and display the frame in the window for the specified time duration in milliseconds. If you do not call waitKey(int) function, the frame will not be displayed in the window.
In the above section, you have learned,
Video file can be played backwards too. But it is not as simple as you would think. Please refer to Play Video File Backwards, if you need the details.
In this section, you will learn how to capture and play a video from a camera/webcam connected to your personal computer.
imshow(window_name, frame);This line will display each frames of the video file in the window created in a previous step.
This function should be followed by waitKey(int) function in order to provide sufficient time to paint and display the frame in the window for the specified time duration in milliseconds. If you do not call waitKey(int) function, the frame will not be displayed in the window.
if (waitKey(10) == 27) { cout << "Esc key is pressed by user. Stoppig the video" << endl; break; }waitKey function waits for 10 milliseconds. If a key was pressed before the specified time, it returns the ASCII value of the pressed key. If that value is 27 (ASCII value of Esc key is 27), the program will execute inside the if block and break the while loop. If no key is pressed during the 10 ms, the function will return -1 and program will continue the while loop.
Summary
In the above section, you have learned,
- How to load a video from a file
- How to read each frames of the video one by one and display it in a window in a while loop
- How to break the while loop at the end of the video
- How to break the while loop if a specific key is pressed before the end of the video
Video file can be played backwards too. But it is not as simple as you would think. Please refer to Play Video File Backwards, if you need the details.
Capture and Play Video From Camera/Webcam
In this section, you will learn how to capture and play a video from a camera/webcam connected to your personal computer.
The main difference of this program against the above program is the use of the VideoCapture constructor. In this program, you just need to give the index of your camera/webcam to the constructor of the VideoCapture object instead of a video file name. Here is the sample OpenCV code.
//Uncomment the following line if you are compiling this code in Visual Studio //#include "stdafx.h" #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char* argv[]) { //Open the default video camera VideoCapture cap(0); // if not success, exit program if (cap.isOpened() == false) { cout << "Cannot open the video camera" << endl; cin.get(); //wait for any key press return -1; } double dWidth = cap.get(CAP_PROP_FRAME_WIDTH); //get the width of frames of the video double dHeight = cap.get(CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video cout << "Resolution of the video : " << dWidth << " x " << dHeight << endl; string window_name = "My Camera Feed"; namedWindow(window_name); //create a window called "My Camera Feed" while (true) { Mat frame; bool bSuccess = cap.read(frame); // read a new frame from video //Breaking the while loop if the frames cannot be captured if (bSuccess == false) { cout << "Video camera is disconnected" << endl; cin.get(); //Wait for any key press break; } //show the frame in the created window imshow(window_name, frame); //wait for for 10 ms until any key is pressed. //If the 'Esc' key is pressed, break the while loop. //If the any other key is pressed, continue the loop //If any key is not pressed withing 10 ms, continue the loop if (waitKey(10) == 27) { cout << "Esc key is pressed by user. Stoppig the video" << endl; break; } } return 0; }
Copy and paste above simple code snippet into your IDE and run it. Then you should see the camera output in an opened window.
Explanation
VideoCapture cap(0);
This line will initialize the VideoCapture object with the default camera and the default back-end.
VideoCapture::VideoCapture(int index)
This is one of few constructors available in VideoCapture class. This constructor initializes and opens the camera specified by the argument index.
You may pass 0 for the argument index to use the default camera connected to your computer. You may use a positive integer as the index, if your computer is attached to more than 1 camera.
The destructor of this class will de-allocated any associated memory with this object. Therefore you don't need to de-allocate memory explicitly in your program.
This is one of few constructors available in VideoCapture class. This constructor initializes and opens the camera specified by the argument index.
You may pass 0 for the argument index to use the default camera connected to your computer. You may use a positive integer as the index, if your computer is attached to more than 1 camera.
The destructor of this class will de-allocated any associated memory with this object. Therefore you don't need to de-allocate memory explicitly in your program.
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video cout << "Resolution of the video : " << dWidth << " x " << dHeight << endl;
1st line will obtain the width (in pixels) of frames of the camera output. Next line will obtain the height of the frames of the camera output. Then it prints the resolution of the camera output which is (width x height).
All other code lines are same as in the first program to play a video file.
Next Tutorial : Save Images & Videos to File
All other code lines are same as in the first program to play a video file.
Next Tutorial : Save Images & Videos to File
I have a problem with the second program when I do it simulate it shows me a white window I do not know why
ReplyDeleteI had same problem when I removed form while cycle this piece of program:
Deleteif (waitKey(10) == 27){
cout << "Esc is presse by user" << endl;
break;
}
so it looks like it is somehow bounded with wait..
Thankyou so much for the tutorial bro.
ReplyDeletethanks for article! Could you tell how to capture video with sound?
ReplyDeleteAlthough OpenCV does not support sound in a video, you can use other libraries like ffmpeg to play the sound while OpenCV displays the video frame by frame.
Deletei am getting the error unresolved externals.
ReplyDeletethis is the error
Severity Code Description Project File Line Suppression State
Error LNK1120 2 unresolved externals PLAY_VIDEO c:\users\ashish\documents\visual studio 2015\Projects\PLAY_VIDEO\x64\Debug\PLAY_VIDEO.exe 1
Error LNK2019 unresolved external symbol "public: virtual bool __cdecl cv::VideoCapture::read(class cv::debug_build_guard::_OutputArray const &)" (?read@VideoCapture@cv@@UEAA_NAEBV_OutputArray@debug_build_guard@2@@Z) referenced in function main PLAY_VIDEO c:\Users\ashish\documents\visual studio 2015\Projects\PLAY_VIDEO\PLAY_VIDEO\Source.obj 1
Error LNK2019 unresolved external symbol "void __cdecl cv::imshow(class cv::String const &,class cv::debug_build_guard::_InputArray const &)" (?imshow@cv@@YAXAEBVString@1@AEBV_InputArray@debug_build_guard@1@@Z) referenced in function main PLAY_VIDEO c:\Users\ashish\documents\visual studio 2015\Projects\PLAY_VIDEO\PLAY_VIDEO\Source.obj 1
You have to give link location to the opencv debug/release libraries in your visual studio properties. Please refer https://opencv-srf.blogspot.com/2017/11/install-opencv-with-visual-studio.html for more information.
Deletehow to read sequence of images like image1 image2 .....image500 from file saved in a directory...can any one tell please. Thanks
ReplyDeleteYou have to rename image1.jpg to image001, image2.jpg to image002.jpg, etc.
DeleteThen try the following code
VideoCapture cap("Fullpath/image%03d.jpg");
while (true)
{
Mat frame;
bool bSuccess = cap.read(frame);
if (bSuccess == false)
break;
if (waitKey(10) == 27)
break;
}
Using eclipse ide and trying "play video from.file or camera"
ReplyDeleteFacing build error, undefined reference error:
Undefined reference to cv:videocapture::videocapture(cv::string const&)
Undefined reference to cv::videocapture::isopened() const
Undefined reference to cv::videocapture::get(int)const
Undefined reference to cv::videocapture::read(cv::outputarray const&)
The error is resolved.
ReplyDeleteI am unable to play the video.It is coming false and printing "cannot open video file" on output terminal.
Please check whether the given filename exist. If it exists, what is your video file format?
DeleteThe file is existing in the path, the video format is mp4
ReplyDeleteIs your path separator /
DeleteYes,d:\\personal\\workspace\\firstvideo.mp4
ReplyDeleteTry replacing \\ with /
Deletein my case I used '\' as a path separator and when I changed it to '\\' it worked.
DeleteYeah Because in c++ string // means/
DeleteI tried many options like adding ffmpeg.dll into system path... Videocapture with opencv 3.4.0 is still not working.
ReplyDeleteIs any configuration am I still missing with? Can anyone help me out in resolving this issue.
The link to next tutorial is broken in this page. The tutorials are amazing, Just wanted to suggest this.
ReplyDeleteFixed it. Thanks Krithik
DeleteSimple worked in first attempt..... Thank you..........
ReplyDeleteThank you very much for sample of work with camera!
ReplyDeletei already confirm my path has correct but it prompt cannot open video file
ReplyDeleteIn second program while compiler throws error:
ReplyDeleteException thrown at 0x00007FFCB12BA388 (KernelBase.dll) in Open_CV.exe: WinRT originate error - 0xC00D36B3 : 'The stream number provided was invalid.'.
but it still works
This comment has been removed by the author.
ReplyDeleteHello.. I tried running the first program but I am getting the following error and the program quits.
ReplyDelete'Open_Video.exe' (Win32): Loaded 'C:\Opencv-3.4.1\opencv\build\x64\vc15\bin\opencv_world341d.dll'. Cannot find or open the PDB file.
Can someone please help me in resolving this.
GStreamer Plugin: Embedded video playback halted; module v4l2src0 reported: Internal data flow error.
ReplyDeleteOpenCV Error: Unspecified error (GStreamer: unable to start pipeline
) in cvCaptureFromCAM_GStreamer, file /opt/opencv/modules/videoio/src/cap_gstreamer.cpp, line 818
terminate called after throwing an instance of 'cv::Exception'
what(): /opt/opencv/modules/videoio/src/cap_gstreamer.cpp:818: error: (-2) GStreamer: unable to start pipeline
in function cvCaptureFromCAM_GStreamer
I am getting this error.
can anyone tell me how to change backgroud of a video
ReplyDeleteclarify the function of bool,isOpened()
ReplyDeleteI have got a problem running the second problem.
ReplyDeleteIt gives out the following output:
[ INFO:0] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\videoio_registry.cpp (187) cv::`anonymous-namespace'::VideoBackendRegistry::VideoBackendRegistry VIDEOIO: Enabled backends(7, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); DSHOW(960); CV_IMAGES(950); CV_MJPEG(940)
[ INFO:0] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (340) cv::impl::getPluginCandidates Found 2 plugin(s) for GSTREAMER
[ INFO:0] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (172) cv::impl::DynamicLib::libraryLoad load D:\OpenCV\build\x64\vc15\bin\opencv_videoio_gstreamer411_64.dll => FAILED
[ INFO:0] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (172) cv::impl::DynamicLib::libraryLoad load opencv_videoio_gstreamer411_64.dll => FAILED
And I didn't know how to solve, or find the missing dll file, how can I solve this?
I have the same problem , did you find a solution ?
DeleteI also have this problem. I am using the Windows 10 system, and finally found that I banned the use of the camera. Please check the permissions of the camera in the privacy settings.
Deletehow to set the roi at the webcam?
ReplyDeleteI cannot open my webcamera through OpenCV! Why is that?
ReplyDeletesame. Does anyone know?
Deletesame here
DeleteI had a few problems loading the video files, but it turned out it was just missing dlls. So I copied the all the ffmpeg dlls and dumped them into the source directory. This seemed to work out the issues for me.
ReplyDeleteI completely tried the above two programs in new virtual machine ubuntu 18.04. installed "sudo apt-get install libopencv-dev" , downloaded other required packages from "https://docs.opencv.org/4.0.1/d7/d9f/tutorial_linux_install.html". It Works for me.
ReplyDeleteyou can add camera device to virtual machine rather than Windows 10, by default camera is associated with Windows 10 Machine rather than Virtual machine, and check whether webcam is working before running the above webcam program with opencv. Thanks for Author for good explanation.
第一个示例,Ubuntu下显示视频无法打开,求解
ReplyDeleteWatch and Download world's famous Turkish action drama Kurulus Osman Season 3 in English on link below
ReplyDelete👇
Kurulus Osman Season 3
Kurulus Osman Season 3 Episode 1
On link below
Kurulus Osman Season 3 Episode 1
Crypto trading course
Join on link below
Crypto quantum leap
YouTube course
Be a professional YouTuber and start your carrier
Tube Mastery and Monetization by matt
Best product for tooth pain ,
Cavity ,
Tooth decay ,
And other oral issues
Need of every home
With discount
And digistore money back guarantee
Steel Bite Pro
ReplyDeleteAwesome.. geweldig...grymt bra...fantastisch
acheter permis de conduire en suisse
führerschein kaufen schweiz
rijbewijs Kopen België
koop rijbewijs online geregistreerd in de gemeente binnen 5 tot 7 werkdagen tegen een goedkope prijs zonder examens
ReplyDeleteRijbewijs Kopen België
Rijbewijs Kopen
Rijbewijs b Kopen
Rijbewijs Online Kopen
ReplyDeleteComprar Carta Condução
carta condução mota
kjøpe førerkort
buy UK driver's license
Comprar Carta de Condução
Carta de Condução Preços
Comprar Carta Condução Verdadeira
Comprar Carta de Mota
Führerschein in Deutschland im Datenbanksystem registriert ohne Prüfungen innerhalb von 5 bis 7 Werktagen kaufen
ReplyDeleteFührerschein kaufen
eu führerschein kaufen
registrierten Führerschein kaufen
echten führerschein kaufen
deutschen Führerschein kaufen
führerschein kaufen legal
ReplyDeletecomprare patente
acquisti la patente guida
come acquistare patente guida
kup prawo jazdy
Comprar Carta de Condução
acheter permis de conduire
köp körkort
acquiste patente di guida
Comprar Licencia de Conducir
ReplyDeleteComprar Carnet de Conducir
Comprar Licencia Conducir Legal
Comprar carnet de conducir
comprar licencias de conducir
comprar carnet conducir dgt
comprar licencia de conducir
Cum să cumpărați permisul de conducere online în 5 până la 7 zile fără examene
ReplyDeletecumpăra permis de conducere
cumpărați permisul conducere online
cumpara permis conducere romanesc
cumpăra permis conducere înregistrat
cumpăra permis conducere România
speak for you.
ReplyDeletecomprare patente di Guida
comprar carta de conducao
420Buds