Unable to read video files

Hello All,

I have installed the opencv 4.5.0 and VS2022 professional on windows 10 Enterprise system.
I am able to read the image (.jpg) file successfully, so I hope the include and library settings are set proper in VS.
Trying to run the small code to check if the video reads from the .mp4 file, I am unable to view the video file read, when I try to hit the debugger button I am getting the error message mentioned below with text version,

[ INFO:0] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\videoio_registry.cpp (191) 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 (370) cv::impl::getPluginCandidates Found 3 plugin(s) for FFMPEG
[ INFO:0] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (175) cv::impl::DynamicLib::libraryLoad load C:\Naveen\projects\ocv\packages\opencv\build\x64\vc15\bin\opencv_videoio_ffmpeg450_64d.dll => OK
[ INFO:0] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (236) cv::impl::PluginBackend::PluginBackend Video I/O: loaded plugin ‘FFmpeg OpenCV Video I/O plugin’
[ INFO:1] global C:\build\master_winpack-build-win64-vc15\opencv\modules\videoio\src\backend_plugin.cpp (181) cv::impl::DynamicLib::libraryRelease unload C:\Naveen\projects\ocv\packages\opencv\build\x64\vc15\bin\opencv_videoio_ffmpeg450_64d.dll

C:\Naveen\projects\ocv\repo\DMS\x64\Debug\DMS.exe (process 2940) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

here is my code:

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

#include <iostream>
using namespace cv;
using namespace std;

int main()
{
	auto path = "videos/ESB_ITI.mp4";
	VideoCapture cap(path);
	Mat img;

	while (true)
	{
		cap.read(img);

		imshow("video", img);
		waitKey(1);

		return 0;
	}
}

Could anyone help me out! Thanks in advance.

hehe, check your code again, the

return 0;

line should go after the loop

also, you must check, if the capture returned something valid
(the last frame of a video file will be empty() !)

if ( ! cap.read(img) )
    break;

That was my mistake :grinning:! thanks for noticing and helping me out.
Its working well now!