Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg c/c++ get frame count or timestamp and fps

Tags:

c++

c

ffmpeg

I am using ffmpeg to decode a video file in C. I am struggling to get either the count of the current frame I am decoding or the timestamp of the frame. I have read numerous posts that show how to calculate an estimated frame no based on the fps and frame timestamp, however I am not able to get either of those.

What I need: fps of video file, timestamp of current frame or frame no(not calculated)

What I have: I am able to get the time of the video using

pFormatCtx->duration/AV_TIME_BASE

I am counting the frames currently as I process them, and getting a current frame count, this is not going to work longterm though. I can get the total frame count for the file using

pFormatCtx->streams[currentStream->videoStream]->nb_frames

I have read this may not work for all streams, although it has worked for every stream I have tried.

I have tried using the time_base.num and time_base.den values and packet.pts, but I can't make any sense of the values that I am getting from those, so I may just need to understand better what those values are.

Does anyone know of resources that show examples on how to get this values?

like image 316
broschb Avatar asked Jan 26 '12 06:01

broschb


1 Answers

This url discusses why the pts values may not make sense and how to get sensible ones: An ffmpeg and SDL Tutorial by Dranger

Here is an excerpt from that link, which gives guidance on exactly what you are looking for in terms of frame numbers and timestamps. If this seems useful to you then you may want to read more of the document for a fuller understanding:

So let's say we had a movie, and the frames were displayed like: I B B P. Now, we need to know the information in P before we can display either B frame. Because of this, the frames might be stored like this: I P B B. This is why we have a decoding timestamp and a presentation timestamp on each frame. The decoding timestamp tells us when we need to decode something, and the presentation time stamp tells us when we need to display something. So, in this case, our stream might look like this:

PTS:    1 4 2 3
DTS:    1 2 3 4
Stream: I P B B

Generally the PTS and DTS will only differ when the stream we are playing has B frames in it.

When we get a packet from av_read_frame(), it will contain the PTS and DTS values for the information inside that packet. But what we really want is the PTS of our newly decoded raw frame, so we know when to display it.

Fortunately, FFMpeg supplies us with a "best effort" timestamp, which you can get via, av_frame_get_best_effort_timestamp()

like image 166
Beel Avatar answered Oct 21 '22 18:10

Beel