Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Dave Johansen <davejohansen@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Dave Johansen <davejohansen@gmail.com>
Subject: [FFmpeg-devel] [PATCH 4/4] avformat/hlsenc: Add second_level_segment_microsecond for using %%f to specify microseconds of time in segment filename
Date: Thu, 26 Oct 2023 21:59:41 -0600
Message-ID: <20231027035941.23491-4-davejohansen@gmail.com> (raw)
In-Reply-To: <20231027035941.23491-1-davejohansen@gmail.com>

---
 libavformat/hlsenc.c | 51 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 48 insertions(+), 3 deletions(-)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 93c47b631b..f613e35984 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -103,6 +103,7 @@ typedef enum HLSFlags {
     HLS_SECOND_LEVEL_SEGMENT_INDEX = (1 << 8), // include segment index in segment filenames when use_localtime  e.g.: %%03d
     HLS_SECOND_LEVEL_SEGMENT_DURATION = (1 << 9), // include segment duration (microsec) in segment filenames when use_localtime  e.g.: %%09t
     HLS_SECOND_LEVEL_SEGMENT_SIZE = (1 << 10), // include segment size (bytes) in segment filenames when use_localtime  e.g.: %%014s
+    HLS_SECOND_LEVEL_SEGMENT_MICROSECOND = (1 << 15), // include microseconds of localtime in segment filenames when use_localtime  e.g.: %%f
     HLS_TEMP_FILE = (1 << 11),
     HLS_PERIODIC_REKEY = (1 << 12),
     HLS_INDEPENDENT_SEGMENTS = (1 << 13),
@@ -496,7 +497,7 @@ static int replace_str_data_in_filename(char **s, const char *filename, char pla
     return found_count;
 }
 
-static int replace_int_data_in_filename(char **s, const char *filename, char placeholder, int64_t number)
+static int replace_int_data_in_filename_forced(char **s, const char *filename, char placeholder, int64_t number, int forced_digits)
 {
     const char *p;
     char c;
@@ -521,6 +522,9 @@ static int replace_int_data_in_filename(char **s, const char *filename, char pla
                 nd = nd * 10 + *(p + addchar_count) - '0';
                 addchar_count++;
             }
+            if (forced_digits > nd) {
+                nd = forced_digits;
+            }
 
             if (*(p + addchar_count) == placeholder) {
                 av_bprintf(&buf, "%0*"PRId64, (number < 0) ? nd : nd++, number);
@@ -544,6 +548,11 @@ static int replace_int_data_in_filename(char **s, const char *filename, char pla
     return found_count;
 }
 
+static int replace_int_data_in_filename(char **s, const char *filename, char placeholder, int64_t number)
+{
+    return replace_int_data_in_filename_forced(s, filename, placeholder, number, 0);
+}
+
 static void write_styp(AVIOContext *pb)
 {
     avio_wb32(pb, 24);
@@ -1020,6 +1029,20 @@ static int sls_flags_filename_process(struct AVFormatContext *s, HLSContext *hls
             }
             ff_format_set_url(vs->avf, filename);
         }
+        if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_MICROSECOND) {
+            char *filename = NULL;
+            double mod_res;
+            if (replace_int_data_in_filename_forced(&filename, vs->avf->url,
+                                             'f',  1000000 * modf(vs->curr_prog_date_time, &mod_res), 6) < 1) {
+                av_log(hls, AV_LOG_ERROR,
+                       "Invalid second level segment filename template '%s', "
+                       "you can try to remove second_level_segment_microsecond flag\n",
+                       vs->avf->url);
+                av_freep(&filename);
+                return AVERROR(EINVAL);
+            }
+            ff_format_set_url(vs->avf, filename);
+        }
     }
     return 0;
 }
@@ -1043,6 +1066,11 @@ static int sls_flag_check_duration_size_index(HLSContext *hls)
                "second_level_segment_index hls_flag requires strftime to be true\n");
         ret = AVERROR(EINVAL);
     }
+    if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_MICROSECOND) {
+        av_log(hls, AV_LOG_ERROR,
+               "second_level_segment_microsecond hls_flag requires strftime to be true\n");
+        ret = AVERROR(EINVAL);
+    }
 
     return ret;
 }
@@ -1063,12 +1091,17 @@ static int sls_flag_check_duration_size(HLSContext *hls, VariantStream *vs)
                "second_level_segment_size hls_flag works only with file protocol segment names\n");
         ret = AVERROR(EINVAL);
     }
+    if ((hls->flags & HLS_SECOND_LEVEL_SEGMENT_MICROSECOND) && !segment_renaming_ok) {
+        av_log(hls, AV_LOG_ERROR,
+               "second_level_segment_microsecond hls_flag works only with file protocol segment names\n");
+        ret = AVERROR(EINVAL);
+    }
 
     return ret;
 }
 
 static void sls_flag_file_rename(HLSContext *hls, VariantStream *vs, char *old_filename) {
-    if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
+    if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION | HLS_SECOND_LEVEL_SEGMENT_MICROSECOND)) &&
         strlen(vs->current_segment_final_filename_fmt)) {
         ff_rename(old_filename, vs->avf->url, hls);
     }
@@ -1088,7 +1121,7 @@ static int sls_flag_use_localtime_filename(AVFormatContext *oc, HLSContext *c, V
         }
         ff_format_set_url(oc, filename);
     }
-    if (c->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) {
+    if (c->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION | HLS_SECOND_LEVEL_SEGMENT_MICROSECOND)) {
         av_strlcpy(vs->current_segment_final_filename_fmt, oc->url,
                    sizeof(vs->current_segment_final_filename_fmt));
         if (c->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {
@@ -1113,6 +1146,17 @@ static int sls_flag_use_localtime_filename(AVFormatContext *oc, HLSContext *c, V
             }
             ff_format_set_url(oc, filename);
         }
+        if (c->flags & HLS_SECOND_LEVEL_SEGMENT_MICROSECOND) {
+            char *filename = NULL;
+            if (replace_int_data_in_filename(&filename, oc->url, 'f', 0) < 1) {
+                av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
+                                        "you can try to remove second_level_segment_microsecond flag\n",
+                       oc->url);
+                av_freep(&filename);
+                return AVERROR(EINVAL);
+            }
+            ff_format_set_url(oc, filename);
+        }
     }
     return 0;
 }
@@ -3176,6 +3220,7 @@ static const AVOption options[] = {
     {"second_level_segment_index", "include segment index in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_INDEX }, 0, UINT_MAX,   E, "flags"},
     {"second_level_segment_duration", "include segment duration in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_DURATION }, 0, UINT_MAX,   E, "flags"},
     {"second_level_segment_size", "include segment size in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_SIZE }, 0, UINT_MAX,   E, "flags"},
+    {"second_level_segment_microsecond", "include microseconds of localtime in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_MICROSECOND }, 0, UINT_MAX,   E, "flags"},
     {"periodic_rekey", "reload keyinfo file periodically for re-keying", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_PERIODIC_REKEY }, 0, UINT_MAX,   E, "flags"},
     {"independent_segments", "add EXT-X-INDEPENDENT-SEGMENTS, whenever applicable", 0, AV_OPT_TYPE_CONST, { .i64 = HLS_INDEPENDENT_SEGMENTS }, 0, UINT_MAX, E, "flags"},
     {"iframes_only", "add EXT-X-I-FRAMES-ONLY, whenever applicable", 0, AV_OPT_TYPE_CONST, { .i64 = HLS_I_FRAMES_ONLY }, 0, UINT_MAX, E, "flags"},
-- 
2.39.2 (Apple Git-143)

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2023-10-27  4:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-27  3:59 [FFmpeg-devel] [PATCH 1/4] avformat/hlsenc: Add init_program_date_time so start time can be specified Dave Johansen
2023-10-27  3:59 ` [FFmpeg-devel] [PATCH 2/4] avformat/hlsenc: Add strftime_prog for using PROGRAM-DATE-TIME in the segment filename Dave Johansen
2023-10-27  3:59 ` [FFmpeg-devel] [PATCH 3/4] avformat/hlsenc: Fix name of flag in error message Dave Johansen
2023-10-27  7:58   ` Steven Liu
2023-10-27  3:59 ` Dave Johansen [this message]
2023-10-27 10:58 ` [FFmpeg-devel] [PATCH 1/4] avformat/hlsenc: Add init_program_date_time so start time can be specified epirat07
2023-10-27 21:28   ` [FFmpeg-devel] [PATCH] avformat/hlsenc: Handle when fractional seconds not set and error out when init_program_date_time can't be parsed Dave Johansen
2023-10-27 21:32   ` [FFmpeg-devel] [PATCH 1/4] avformat/hlsenc: Add init_program_date_time so start time can be specified David Johansen
2023-11-05  8:20     ` Marton Balint
2023-11-07  0:31       ` [FFmpeg-devel] [PATCH] Use existing function to parse time Dave Johansen
2023-11-08 23:45       ` [FFmpeg-devel] [PATCH 1/4] avformat/hlsenc: Add init_program_date_time so start time can be specified David Johansen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231027035941.23491-4-davejohansen@gmail.com \
    --to=davejohansen@gmail.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git