MATLAB Answers

Problems obtaining the correct number of frames in video file

조회 수: 31(최근 30일)
K E 26 Sep 2012
I am having trouble using VideoReader to determine the correct number of frames in a video file,
videoObj = VideoReader('2_1107.mpg') ;
Warning: Unable to determine the number of frames in this file.
lastFrame = read(videoObj, inf)
Error using VideoReader/read (line 86)
The frame index requested is beyond the end of the file.
The following code returns a different number of frames each time it's run, e.g. 164, 263, 257.
videoObj = VideoReader('2_1107.mpg');
nFrame = 0 ;
isGood = true ;
while isGood
nFrame = nFrame + 1 ;
thisFrame = read(videoObj, nFrame) ;
isGood = false ;
nFrame = nFrame - 1 ; % Don't include in the count if fails
If VideoReader skipping or repeating frames on each pass through the file, how can I get the correct number of frames? More info: problem is present for both R2012a and R2012b run on Windows 7 (32-bit versions), video format is MPEG-2, size of the video is 65706 KB, and it's a variable frame rate file.

채택된 답변

Ryan G
Ryan G 27 Sep 2012
Have you tried something like this:
xyloObj = VideoReader('xylophone.mpg');
xylDat = read(xyloObj);
For this example, which is the mpg in the link provided, the final value in size is the number of frames. If this does not help, can you do a:
and post the results? I would be interested to see what data is missing.
  댓글 수: 7
K E 28 Sep 2012
Thanks again Ryan. You really helped me. I ended up using another free converter, ffmpeg, to convert from MPEG-2 to MPEG-1 using the following command which I could incorporate into a MATLAB script,
ffmpeg -i myMpeg2File.mpg -target ntsc-vcd -y myMpeg1File.mpg
64-bit VideoReader has no problems reading the MPEG-1 file and counts the same number of frames each time my code snippet runs. It takes a lot longer to count the frames now, so I think before it failed part way through the file.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by