Question

Please, this is not duplicate of similar posts!

I want to find and to decode Nth frame, for example 7th frame.

As I understood, using time_base I can calculate how many ticks is each frame and by multiplying it with 7 we will get position of 7th frame. To calculate the ticks I do

AVStream inStream = getStreamFromAVFormatContext();

int fps = inStream->r_frame_rate.num;
AVRational timeBase = inStream->time_base;

int ticks_per_frame = (1/fps) / timeBase;
int _7thFramePos = ticks_per_frame * 7;
  1. Did I calculated correctly position of 7th frame? If I did, so to go to that frame I just do av_seek_frame(pFormatCtx, -1, _7thFramePos, AVSEEK_FLAG_ANY), right? What happens if the 7th frame was P-Frame or B-Frame, how I decode it?
  2. I noticed that the calculated value differs from inStream->codec->ticks_per_frame, why? Shouldn't they be the same? What is the difference?
Was it helpful?

Solution

This post explains the issue nicely. http://www.hackerfactor.com/blog/index.php?/archives/307-Picture-Go-Back.html

[1] comment for AVStream structure clearly mentions that "r_frame_rate" is a guess and may not be accurate, because even if I have frame-rate of (say) 25fps, in term of base_time I may have 24 or 26 frames in a second.

[2] To find the exact frame number you need to decode frame from the start and keep a counter, but that is very in-efficient, this can be optimized for some file-formats like MP4 where information about every frame is present in file-header.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top