If we are using copyts parameter it is not possible to inserting key frames same way as hlsenc requests, this will lead to different hls segments length.

-copyts is required for long period audio/video sync

Small modification to hlsenc.c is also required and will arrive in next patch.

--
Miroslav Slugeň









>From 90d0636ea6cd34c9b51e4b568bb9e8aa461fa615 Mon Sep 17 00:00:00 2001
From: Miroslav Slugen <thunde...@email.cz>
Date: Sun, 12 Feb 2017 19:08:27 +0100
Subject: [PATCH 1/1] ffmpeg: add new forced_keyframes option -hls:time for
 inserting keyframes exactly athlsenc position compatible with copyts

---
 ffmpeg.c | 19 +++++++++++++++++++
 ffmpeg.h |  4 ++++
 2 files changed, 23 insertions(+)

diff --git a/ffmpeg.c b/ffmpeg.c
index 06570c0..6ba5771 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -1237,6 +1237,20 @@ static void do_video_out(OutputFile *of,
             }
 
             ost->forced_keyframes_expr_const_values[FKF_N] += 1;
+        } else if (ost->forced_keyframes_hls_interval) {
+            if (in_picture->pts != AV_NOPTS_VALUE) {
+                int64_t pts_out = av_rescale_q(in_picture->pts, enc->time_base, ost->st->time_base);
+
+                if (ost->forced_keyframes_hls_start_pts == AV_NOPTS_VALUE) {
+                    ost->forced_keyframes_hls_start_pts = pts_out;
+                }
+
+                if (av_compare_ts(pts_out - ost->forced_keyframes_hls_start_pts, ost->st->time_base, ost->forced_keyframes_hls_end_pts, AV_TIME_BASE_Q) >= 0) {
+                    ost->forced_keyframes_hls_number++;
+                    ost->forced_keyframes_hls_end_pts+= ost->forced_keyframes_hls_interval;
+                    forced_keyframe = 1;
+                }
+            }
         } else if (   ost->forced_keyframes
                    && !strncmp(ost->forced_keyframes, "source", 6)
                    && in_picture->key_frame==1) {
@@ -3224,6 +3238,11 @@ static int init_output_stream_encode(OutputStream *ost)
                 ost->forced_keyframes_expr_const_values[FKF_N_FORCED] = 0;
                 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] = NAN;
                 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] = NAN;
+            } else if (!strncmp(ost->forced_keyframes, "hls:", 4)) {
+                ost->forced_keyframes_hls_interval = atof(ost->forced_keyframes+4) * AV_TIME_BASE;
+                ost->forced_keyframes_hls_number = 1;
+                ost->forced_keyframes_hls_start_pts = AV_NOPTS_VALUE;
+                ost->forced_keyframes_hls_end_pts = ost->forced_keyframes_hls_interval;
 
             // Don't parse the 'forced_keyframes' in case of 'keep-source-keyframes',
             // parse it only for static kf timings
diff --git a/ffmpeg.h b/ffmpeg.h
index 458bb8a..85a8f18 100644
--- a/ffmpeg.h
+++ b/ffmpeg.h
@@ -478,6 +478,10 @@ typedef struct OutputStream {
     char *forced_keyframes;
     AVExpr *forced_keyframes_pexpr;
     double forced_keyframes_expr_const_values[FKF_NB];
+    int forced_keyframes_hls_interval;
+    int64_t forced_keyframes_hls_number;
+    int64_t forced_keyframes_hls_start_pts;
+    int64_t forced_keyframes_hls_end_pts;
 
     /* audio only */
     int *audio_channels_map;             /* list of the channels id to pick from the source stream */
-- 
2.1.4

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Reply via email to