[FFmpeg-devel] [PATCH 1/1] avformat/dashenc: Added #EXT-X-PROGRAM-DATE-TIME to HLS playlists

joepadmiraal joep at exmachina.nl
Thu Feb 21 14:24:18 EET 2019


From: joepadmiraal <joep at groovytunes.nl>

---
 libavformat/dashenc.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 37a7547b12..d0d0d6f030 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -122,6 +122,7 @@ typedef struct DASHContext {
     int64_t last_duration;
     int64_t total_duration;
     char availability_start_time[100];
+    int64_t start_time_ms;
     char dirname[1024];
     const char *single_file_name;  /* file names as specified in options */
     const char *init_seg_name;
@@ -432,6 +433,8 @@ static void write_hls_media_playlist(OutputStream *os, AVFormatContext *s,
     const char *proto = avio_find_protocol_name(c->dirname);
     int use_rename = proto && !strcmp(proto, "file");
     int i, start_index, start_number;
+    time_t start_time_s = c->start_time_ms / 1000;
+    double prog_date_time = start_time_s;
 
     get_start_index_number(os, c, &start_index, &start_number);
 
@@ -464,16 +467,21 @@ static void write_hls_media_playlist(OutputStream *os, AVFormatContext *s,
     ff_hls_write_init_file(c->m3u8_out, os->initfile, c->single_file,
                            os->init_range_length, os->init_start_pos);
 
-    for (i = start_index; i < os->nb_segments; i++) {
+    for (i = 0; i < os->nb_segments; i++) {
         Segment *seg = os->segments[i];
-        ret = ff_hls_write_file_entry(c->m3u8_out, 0, c->single_file,
-                                (double) seg->duration / timescale, 0,
-                                seg->range_length, seg->start_pos, NULL,
-                                c->single_file ? os->initfile : seg->file,
-                                NULL);
-        if (ret < 0) {
-            av_log(os->ctx, AV_LOG_WARNING, "ff_hls_write_file_entry get error\n");
+        double duration = (double) seg->duration / timescale;
+        if (i >= start_index) {
+            double prog_date_time_copy = prog_date_time;
+            ret = ff_hls_write_file_entry(c->m3u8_out, 0, c->single_file,
+                                    duration, 0,
+                                    seg->range_length, seg->start_pos, NULL,
+                                    c->single_file ? os->initfile : seg->file,
+                                    &prog_date_time_copy);
+            if (ret < 0) {
+                av_log(os->ctx, AV_LOG_WARNING, "ff_hls_write_file_entry get error\n");
+            }
         }
+        prog_date_time += duration;
     }
 
     if (prefetch_url)
@@ -1583,9 +1591,12 @@ static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
         os->first_pts = pkt->pts;
     os->last_pts = pkt->pts;
 
-    if (!c->availability_start_time[0])
+    if (!c->availability_start_time[0]) {
+        int64_t start_time_us = av_gettime();
+        c->start_time_ms = start_time_us / 1000;
         format_date_now(c->availability_start_time,
                         sizeof(c->availability_start_time));
+    }
 
     if (!os->availability_time_offset && pkt->duration) {
         int64_t frame_duration = av_rescale_q(pkt->duration, st->time_base,
-- 
2.17.0



More information about the ffmpeg-devel mailing list