[Libav-user] AVPacket duration

LUKY CMUKY luky.cmuky at tiscali.cz
Tue Oct 22 16:52:55 EEST 2019

Hi all
 I'm struggling to understand how to set AVPacket::duration field. I have done loads of googling, but never found any working answer. When leaving the field with default 0, which is what it is after avcodec_receive_packet() call, for 30 FPS video I get 34.28 reported (by ffprobe as well as Windows file properties tab). All time-stamps printed by "ffprobe -show_frames" however perfectly correspond with 30 FPS.
 The official documentation says that AVPacket::duration should be set to next_pts - this_pts (https://ffmpeg.org/doxygen/2.8/structAVPacket.html#af151ba1967c37038088883cd544eeacd [https://ffmpeg.org/doxygen/2.8/structAVPacket.html#af151ba1967c37038088883cd544eeacd]), so I take that it means to set the duration of given AVPacket when a next one is received such as
 thisPacket->duration = nextPacket->pts - thisPacket->pts;
 However, when I made a circular AVPacket pool and implemented this, I got 34.28 FPS reported again. Strangely, when the duration is simply hard-coded to 1 it all works:
 void EncodeFrame(AVFormatContext* formatContext, AVCodecContext* encodingContext, AVStream* stream, AVFrame* frame, AVPacket* packet)
 {   int ret = avcodec_send_frame(encodingContext, frame);
   while (ret >= 0)
     ret = avcodec_receive_packet(encodingContext, packet);
     if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
     if (ret < 0)
     packet->duration = 1;
     av_packet_rescale_ts(packet, encodingContext->time_base, stream->time_base);
     ret = av_write_frame(formatContext, packet);
 The encodingContext->time_base is 1/30 and stream->time_base is 1/15360.
 Whilst hard-coding the duration = 1 makes the FPS reported correctly, it just can't be right, because hard-coding a value for all possible scenario would mean that the field is unnecessary and also it would be in conflict with the documentation.
 Please, can anybody advise?
 Thanks a lot!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20191022/508e6a86/attachment-0001.html>

More information about the Libav-user mailing list