Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronizing FFMPEG video frames using PTS

I'm attempting to synchronize the frames decoded from an MP4 video. I'm using the FFMPEG libraries. I've decoded and stored each frame and successfully displayed the video over an OPENGL plane.

I've started a timer just before cycling through the frames; the aim being to synchronize the Video correctly. I then compare the PTS of each frame against this timer. I stored the PTS received from the packet during decoding.

What is displayed within my application does not seem to play at the rate I expect. It plays faster than the original video file would within a media player.

I am inexperienced with FFMPEG and programming video in general. Am I tackling this the wrong way?

Here is an example of what I'm attempting to do

            FrameObject frameObject = frameQueue.front();

            AVFrame frame = *frameObject.pFrame;

            videoClock += dt;

            if(videoClock >= globalPTS)
            {
                //Draw the Frame to a texture
                DrawFrame(&frame, frameObject.m_pts);

                frameQueue.pop_front();

                globalPTS = frameObject.m_pts;
            }

Please note I'm using C++, Windows, Opengl, FFMPEG and the VS2010 IDE

like image 535
DundeeDave Avatar asked Feb 12 '23 01:02

DundeeDave


1 Answers

First off, Use int64_t pts = av_frame_get_best_effort_timestamp(pFrame) to get the pts. Second you must make sure both streams you are syncing use the same time base. The easiest way to do this is convert everything to AV_TIME_BASE_Q. pts = av_rescale_q ( pts, formatCtx->streams[videoStream]->time_base, AV_TIME_BASE_Q ); In this format, pts is in nanoseconds.

like image 50
szatmary Avatar answered Feb 16 '23 03:02

szatmary