Skip to content

Setting packet duration #32

@nathanvdh

Description

@nathanvdh

Hi, I'm learning to use FFmpeg and I have a question regarding this packet duration calculation:
pkt->duration = stream->time_base.den / stream->time_base.num / stream->avg_frame_rate.num * stream->avg_frame_rate.den;
https://github.com/Raveler/ffmpeg-cpp/blob/master/source/ffmpeg-cpp/ffmpeg-cpp/Muxing/VideoOutputStream.cpp#L82

I have also experienced the incorrect total duration/frame rate with h264/mp4, if packet duration is not set.

When encoding, I have just set packets' duration to 1 as they come out of the encoder (fair enough for CFR content) and then call av_packet_rescale_ts() to rescale it to the stream timebase.

Your calculation appears to be (1/Tb) * (1/Fr), where Tb is the stream timebase and Fr is the stream average framerate.
This will be constant for CFR streams, and equivalent to setting the duration to 1 before the rescale. So what is the benefit of doing this more complex calculation? If the stream is VFR isn't this duration pretty much always wrong? Isn't it kind of just a guess of the average frame duration so far/

Also, wouldn't it be better to use the AVRational math functions to do this calculation to prevent overflow and other potential issues.
e.g.:
pkt->duration = av_mul_q(av_inv_q(stream->time_base), av_inv_q(stream->avg_frame_rate));

I'd appreciate if you could offer any insight into this, and maybe explain why this line of code is the way it is.

Thank you!
-Nathan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions