tags:

views:

57

answers:

1

I need to know how to find the total number of frames in a video file using avcodec/avformat.

I have a project that I'm picking up from someone else who was using the ffmpeg libraries to decode video streams. I need to retrofit some functionality to seek around frame by frame, and so my first task is to simply figure out the total number of frames in the file. AVStream.nb_frames seems to be a reasonable place to look, but this is always 0 with all of the video files I've tried. Is deducing the total number of frames from AVFormatContex.duration the best way to go?

A: 

The only way to find the exact number of frames is to go through them all and count. I have worried about this many times, tried many different tools (including ffmpeg), and read a lot. Sorry, but in the general case there's no other way. Some formats just don't store this information, so you have to count.

balpha
Is calculating the number of frames from AVFormatContext.duration an ok approach, or is this just as shaky as nb_frames?
Boatzart
It's an okay approach, but don't expect to decode that exact number of frames when you actually do decode them. The longer the video is, the greater the deviation will probably be. Also remember that seeking might be just as flaky -- unless you have an index (either self-created by, again, looping through the file in advance, or provided by the container format), it's close to impossible to say "I need frame number 96215."
balpha