[FFmpeg-user] How to determine length of animation using C++ API?

Mark McKay mark at kitfox.com
Thu Apr 25 22:38:22 EEST 2019

I'm using the C++ API to implement a simple video player in Qt. I'd
like to be able to load a video and determine how many seconds long it
is. I thought I would be able to calculate this time by looking at
AVFormatContext->duration in the header and that

 double timeMillisec =
(double)AVFormatContext->duration / AV_TIME_BASE * 1000

in the example
file I'm using, I'm getting a value of AVFormatContext->duration =
81223333, so about 81 seconds.

However, if I scan through all my
packets to the end, my final packet has a presentation time of 3580928
and my codec context has a timebase of 1 / 50. I'm calculating the time
of the packet as 

 double packetTimeMillisec = packet.pts *
pCodecCtx->time_base.num / pCodecCtx->time_base.den

Am I doing this
right? How do I figure out how to synchronize these packets with my
clock and determine the total length of the animation from the header?


More information about the ffmpeg-user mailing list