[libav-api] FLV Variable Frame Rate

Mark Kenna mark.kenna at sureviewsystems.com
Fri May 27 17:48:07 CEST 2011

On 27/05/2011 15:05, Ronald S. Bultje wrote:
> Hi Mark,
> On Fri, May 27, 2011 at 9:56 AM, Mark Kenna
> <mark.kenna at sureviewsystems.com>  wrote:
>> On 27/05/2011 14:51, Ronald S. Bultje wrote:
>>> On Fri, May 27, 2011 at 9:49 AM, Mark Kenna
>>> <mark.kenna at sureviewsystems.com>    wrote:
>>>> I'm having a hard time getting my head around encoding variable frame
>>>> rate
>>>> containers in LibAV. I have taken the 'output_example.c' program and
>>>> modified it to encode to FLV. What I would like to know is how I would
>>>> modify the 'output_example.c' program to write variable time stamp values
>>>> into the output file. For my project I will be taking a streaming input
>>>> source (which will not have time-stamp information) and will need to
>>>> create
>>>> my own timestamp values (I guess from the delta value between the last
>>>> received frame and the current frame?).
>>>> I have read that you need to set the time base value to:
>>>>         c->time_base.num=1;
>>>>         c->time_base.den=1000;
>>>> But I am not sure exactly what I should be setting the encoded packet's
>>>> PTS
>>>> values to?
>>> This means "each pts value has a fractional unit of 1/1000th", or in
>>> other words, millisecond precision. So if your timestamps are 10ms,
>>> 22ms, 38ms, then pts would be 10, 22 and 38.
>>> Ronald
>> Wow! That was quick - thanks!
>> So after encoding the frame and calculating what the pts value should be
>> (based on the delay between the last frame and this one) I would do:
>>             pkt.stream_index= st->index;
>>             pkt.data= video_outbuf;
>>             pkt.size= out_size;
>>             pkt.pts= previousPacketPts + deltaDelay;
>>             /* write the compressed frame in the media file */
>>             ret = av_write_frame(oc,&pkt);
>> If that makes any sense?
> That works, if you're not using B-frames.
> If you're using B-frames, I'd suggest using
> AVCodecContext->coded_frame->pts as a way to set/get the PTS of
> encoded frames before/after encoding. ffmpeg.c uses this also.
> Ronald
Hi Ronald

That actually works perfectly!

I set up and start a timer when I get the first frame of video, then 
before every encode I do:

             frame->pts = _stopWatch->ElapsedMilliseconds;

             /* encode the image */
             out_size = avcodec_encode_video(c, video_outbuf, 
video_outbuf_size, frame);

And as I am not using B-Frames it works perfectly. I have not tried (and 
will not try) to use this technique with P-Frames.

Thanks for all your help,

More information about the libav-api mailing list