[Libav-user] Converting PTS to frame number

Michael Ivanov ivspcom at gmail.com
Wed Dec 18 10:36:19 EET 2024


Hi!
I am trying to figure out the right way to convert PTS during demuxing to
frame number.
I came up with the following code:

int64_t timestamp_to_frame(Demuxer* demuxer, int64_t pts)
 {
     AVStream* stream = demuxer->fmtc->streams[demuxer->iVideoStream];
     if (!stream || pts < 0)
     {
         fprintf(stderr, "Invalid stream or PTS.\n");
         return -1;
     }

     // Get the frame rate (FPS)
     const double fps = get_frame_rate(stream);//here returns 25.0

     // Stream time base (e.g., 1/25000 for my case)
     const AVRational timeBase = stream->time_base;

     // Calculate frame duration in seconds
     const double frameDurationInSeconds = 1.0 / fps;

     const int64_t ptsIncrement = (int64_t)((frameDurationInSeconds *
timeBase.den) * frameDurationInSeconds + 0.5);
     // Calculate the frame number by dividing PTS by the increment
     const  int64_t frameNumber = pts / ptsIncrement;
return frameNumber;

}

It works correctly, but I am not sure this is the right way to do that and
that it will work with any fps/time scale. Could someone review this code
and provide feedback, or an alternative way?

Much appreciated,
Mike.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20241218/e158496b/attachment.htm>


More information about the Libav-user mailing list