FFmpeg C API HLS real time timestamps

Viewed 30

I'm using the FFmpeg C API in C++ to read from a HLS stream. I need to know the real time of each AVPacket. I can extract the pts using AVPacket::pts but that is relative to the start of the stream.

This is how the .m3u8 file looks:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-ALLOW-CACHE:NO
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:0

#EXT-X-PROGRAM-DATE-TIME:2022-09-07T14:01:56.612+02:00
#EXTINF:10.322561783,
1662552116.ts
#EXT-X-PROGRAM-DATE-TIME:2022-09-07T14:02:06.935+02:00
#EXTINF:10.320075936,
1662552126.ts

...

The .m3u8 file contains an accurate EXT-X-PROGRAM-DATE-TIME, but how can I extract the one of the currently playing segment?

Alternatively, the file name of each .ts file is the unix timestamp in seconds. Can I extract that somehow?

If none of those are possibly, is it possible to control the exact number of preloaded segments? I know the (approximate) segment length is 10 seconds so I could just do the following when receiving the first AVPacket:

start_time = current_time - segment_count * segment_length`
start_pts = first_av_packet.pts

And then to get the time of a later AVPacket, I could do:

packet_time = start_time + new_packet.pts - start_pts

This wouldn't give the same accuracy since the segments are not exactly the same length, but that is okay.

0 Answers
Related