Problems obtaining the correct number of frames in video file

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 ;
try
thisFrame = read(videoObj, nFrame) ;
catch
isGood = false ;
nFrame = nFrame - 1 ; % Don't include in the count if fails
end
end
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.

 Accepted Answer

Have you tried something like this:
xyloObj = VideoReader('xylophone.mpg');
xylDat = read(xyloObj);
size(xylDat)
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:
get(videoObj)
and post the results? I would be interested to see what data is missing.

7 Comments

Your suggestion results in 10X as many frames as my counting code above! What does this mean?
videoDat = read(videoObj) ;
Error using VideoReader/read (line 86)
Not enough memory available for 3089
frames.
get(videoObj)
General Settings:
Duration = 103.08
Name = 2_1107.mpg
Path = G:\Test\Camera2
Tag =
Type = VideoReader
UserData = []
Video Settings:
BitsPerPixel = 24
FrameRate = 0
Height = 480
NumberOfFrames = 3089
VideoFormat = RGB24
Width = 720
Are you using 64 or 32-bit MATLAB? How big is the video file? How much RAM do you have?
I have a feeling that the loop is running out of memory at different places and is thus returning different results.
EDIT: just noticed you said the file is ~65 MB. By comparison the xylo demo is 0.65 MB. However, reading in the xylo uses 32 MB! of ram. lets assume that your file uses 50X increase in memory vs file size. That would mean it uses over 3 gigs of ram to read it. Now, that ratio is probably not consistent across files being read, but I think you get the idea.
I am using 32-bit MATLAB because I don't have the 64-bit MPEG-2 codec installed, but sounds like I need 64-bit MATLAB due to the file size. I have 8GB RAM.
32-bit will limit what you can do for video processing due to size. Technically 32-bit should be able to process that script you wrote since it's not storing anything, however, if you've already pushed your memory towards the limit (with the read command) it can quickly crap out.
Although you have 8gigs, I think you may already be aware that 32-bit will limit the amount you can access. I can't remember the exact number but it's somewhere between maybe 2-3gigs.
Thanks so much Ryan. I really appreciate it since I am inching toward being able to use Matlab for the video analysis. An unfair follow-up: Do you happen to know about where to get a 64-bit MPEG-2 codec? Everything I found via web search & installed hasn't worked, probably because I don't know what I am doing. If this should be properly be posted as new question on Stack Overflow, please don't hesitate to say so.
I think you really have 2 options.
1) If you have access to the Computer Vision System Toolbox, this supports mpeg-2 using the command:
videoFReader = vision.VideoFileReader(FILENAME)
2) You can find a free converter to change the file from mpeg-2 to mpeg-1
Depending on what you're looking to do this might be ok.
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.

Sign in to comment.

More Answers (0)

Products

Asked:

K E
on 26 Sep 2012

Community Treasure Hunt

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

Start Hunting!