The VideoCaputre just capture about half number of total frame

I use VideoCapture class in OpenCV 4.5.3 to capture a 1920*1080 MP4 50fps video . The total frame number of the video is 39100. However, when loading with VideoCapture, total frame number is just 19572. It occus error to load the frames more than 19572 using the read function. Could anyone help to solve this problem? My code in C++ pastes in following.
cv::VideoCapture vcTestVideoStream("…//…//…//…//…//testVideo//M_11272018094751_00000000000132 4_1_001_002-1.MP4");

double dVideoTN = vcTestVideoStream.get(cv::CAP_PROP_FRAME_COUNT);
double dFPS = vcTestVideoStream.get(cv::CAP_PROP_FPS);

double dWT = 1000.0/dFPS;
double dImageW = vcTestVideoStream.get(cv::CAP_PROP_FRAME_WIDTH);
double dImageH = vcTestVideoStream.get(cv::CAP_PROP_FRAME_HEIGHT);

cv::Mat matShowFrame;
cv::namedWindow("VideoWin");

for (int n = 0; n < dVideoTN; n++) {

	vcTestVideoStream.read(matShowFrame);

	cv::imshow("VideoWin", matShowFrame);
	
	cv::waitKey(dWT);
}
cv::destroyWindow("VideoWin");

read all frames (until not ret) and count them. what do you get?

CAP_PROP_FRAME_COUNT can be wrong sometimes.

what does ffprobe say about your video? is it interlaced? where is the video from? can you provide an example to reproduce the issue?

Thank you sending message to me. The followings are the information of video using ‘.\ffprobe.exe -show_packets’ command.
Metadata:
major_brand : MSNV
minor_version : 285212672
compatible_brands: MSNVisommp42
creation_time : 2018-11-27T12:44:51.000000Z
Duration: 00:13:02.89, start: 0.000000, bitrate: 18295 kb/s
Stream #0:00x1: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 192 kb/s (default)
Metadata:
creation_time : 2018-11-27T12:44:51.000000Z
handler_name : Sound Media Handler
vendor_id : [0][0][0][0]
Stream #0:10x2: Video: h264 (High) (avc1 / 0x31637661), yuv420p(top first), 1920x1080 [SAR 1:1 DAR 16:9], 18098 kb/s, 50 fps, 50 tbr, 60k tbn (default)
Metadata:
creation_time : 2018-11-27T12:44:51.000000Z
handler_name : Video Media Handler
vendor_id : [0][0][0][0]
encoder : AVC Coding

And I use the command ’ .\ffprobe.exe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1’ to count the frame number. However, it is just 19572 that is different from the number figured by that video time multiples the fps. It’s a strange thing.

I’ve experienced the same thing.

it’s probably interlaced, “50i”, not 50p.

maybe it’s 25p packed into interlaced fields (PsF). whether it is, you should be able to see from “combing” artefacts near horizontally moving edges. combing artefacts indicate real interlaced data and trivial de-interlacing. lack of combing implies either some smart de-interlacing or the material is 25 PsF.

I don’t think you can rely on “CAP_PROP_FRAME_COUNT” then. it’s probably reporting the number of fields instead of frames, but decoding gives you frames.

you can try to submit that as a bug. my impression is that the ffmpeg backend of OpenCV has no real maintainers. some people improve some aspects of it sometimes but such types of bugs are bound to stay until someone is really bothered and works on a fix themselves.

1 Like

I got this message. I can look the horizontal artefacts on image due to de-interlacing. I think it’s not a simple problem of OpenCV because I get wrong frame number using the ffprobe command. Thank your answer and I really appreciate your help.

Could you rerun your program using following environment variables and post the output to see what is really going on?

export OPENCV_LOG_LEVEL=VERBOSE
export OPENCV_FFMPEG_LOGLEVEL=56
export OPENCV_VIDEOIO_DEBUG=1
export OPENCV_VIDEOWRITER_DEBUG=1
export OPENCV_VIDEOCAPTURE_DEBUG=1
export OPENCV_FFMPEG_DEBUG=1

This would for example show if the driver changes framerate midst-operation (which can happen) and many other things.