Syncing decoded video using Ffmpeg

4.7k Views Asked by At

I am using Ffmpeg to decode and play video files. I have currently got the video playing and the audio playing at as fast as the CPU can decode and display them. The problem is that I want to play the video and audio in sync using the system clock.

I've searched around for some help but can't find anything substantial other than dranger's tutorial 05 but I don't really understand what he is doing because my program isn't written in the same way as his.

I am using mjpeg files and so the pts seems to be retrieved every single time a frame is decoded, I have multiplied the pts by the time_base as dranger does to get the value in seconds but the resolution seems to be only seconds and so I get the value "6" 25 times and then "7" 25 times as the video runs at 25 frames per second.

Is there not a more accurate value? Or way to get a more accurate value and if so, how would I go about syncing to this value? I am using SDL to display the value so can I just use a SDL_Delay() of the value I get?

Thanks for your time,

Infinitifizz

1

There are 1 best solutions below

2
On

To convert pts or dts to floating-point seconds, use av_q2d() on proper time_base:

// You got the context from open_input:
AVFormatContext *pFormatCtx;
avformat_open_input(&pFormatCtx, inputfilename, NULL, &format_opts);

// Get a stream from the context
AVStream pStream= pFormatCtx->streams[i];

// Convert packet time (here, dts) to seconds with:  
double seconds= (dts - pStream->start_time) * av_q2d(pStream->time_base);

// Or convert frame number to seconds with the codec context
AVCodecContext *pCodecCtx= pStream->pVideoStream->codec;
double seconds= framenumber * av_q2d(pCodecCtx->time_base);

This returns time-from-when-the-video-starts in seconds.