diff mbox

[FFmpeg-devel,v1] lavf/img2enc: add support for option strftime_source

Message ID 20190415064302.8693-1-junli1026@gmail.com
State New
Headers show

Commit Message

Jun Li April 15, 2019, 6:43 a.m. UTC
Currently the strftime option generate timestamp based on generation
time. The new option would calcualte timestamp from source's
start_realtime and pkt->pts, try to generate a timestamp matches the
source starting time.
---
 doc/muxers.texi       |  4 ++++
 libavformat/img2enc.c | 39 +++++++++++++++++++++++++++++++++++++++
 libavformat/rtsp.c    |  1 +
 3 files changed, 44 insertions(+)

Comments

Jun Li April 17, 2019, 9:46 p.m. UTC | #1
On Sun, Apr 14, 2019 at 11:43 PM Jun Li <junli1026@gmail.com> wrote:

> Currently the strftime option generate timestamp based on generation
> time. The new option would calcualte timestamp from source's
> start_realtime and pkt->pts, try to generate a timestamp matches the
> source starting time.
> ---
>  doc/muxers.texi       |  4 ++++
>  libavformat/img2enc.c | 39 +++++++++++++++++++++++++++++++++++++++
>  libavformat/rtsp.c    |  1 +
>  3 files changed, 44 insertions(+)
>
> diff --git a/doc/muxers.texi b/doc/muxers.texi
> index 83ae017d6c..58d2d0ee7c 100644
> --- a/doc/muxers.texi
> +++ b/doc/muxers.texi
> @@ -1190,6 +1190,10 @@ If set to 1, the filename will always be
> interpreted as just a
>  filename, not a pattern, and the corresponding file will be continuously
>  overwritten with new images. Default value is 0.
>
> +@item strftime_source
> +If set to 1, expand the filename with date and time information from
> source.
> +Useful when input is live stream like rtsp.
> +
>  @item strftime
>  If set to 1, expand the filename with date and time information from
>  @code{strftime()}. Default value is 0.
> diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
> index bec4bf81dd..8ba95ee3d5 100644
> --- a/libavformat/img2enc.c
> +++ b/libavformat/img2enc.c
> @@ -45,6 +45,7 @@ typedef struct VideoMuxData {
>      int frame_pts;
>      const char *muxer;
>      int use_rename;
> +    int strftime_source;
>  } VideoMuxData;
>
>  static int write_header(AVFormatContext *s)
> @@ -96,6 +97,7 @@ static int write_packet(AVFormatContext *s, AVPacket
> *pkt)
>              struct tm *tm, tmpbuf;
>              time(&now0);
>              tm = localtime_r(&now0, &tmpbuf);
> +
>              if (!strftime(filename, sizeof(filename), img->path, tm)) {
>                  av_log(s, AV_LOG_ERROR, "Could not get frame filename
> with strftime\n");
>                  return AVERROR(EINVAL);
> @@ -105,6 +107,42 @@ static int write_packet(AVFormatContext *s, AVPacket
> *pkt)
>                  av_log(s, AV_LOG_ERROR, "Cannot write filename by pts of
> the frames.");
>                  return AVERROR(EINVAL);
>              }
> +        } else if (img->strftime_source) {
> +            int64_t start_realtime = s->start_time_realtime;
> +            time_t sec = 0;
> +            struct tm *tm;
> +
> +            if (start_realtime == AV_NOPTS_VALUE) {
> +                // The value is not passed from input context to output,
> try to find in metadata.
> +                AVDictionaryEntry *entry = av_dict_get(s->metadata,
> "start_realtime", NULL, 0);
> +                if (entry) {
> +                    av_log(s, AV_LOG_INFO, "Get start time from metadata
> start_realtime: %s\n", entry->value);
> +                    start_realtime = strtoll(entry->value, NULL, 0);
> +                } else {
> +                    if (ff_parse_creation_time_metadata(s,
> &start_realtime, 0) != 0)
> +                        av_log(s, AV_LOG_INFO, "Use creation_time as
> start_timestamp.\n");
> +                }
> +
> +                if (start_realtime == 0 || start_realtime ==
> AV_NOPTS_VALUE) {
> +                    av_log(s, AV_LOG_WARNING, "Could not get
> start_realtime from source, set value to now.\n");
> +                    time(&sec);
> +                    s->start_time_realtime = sec * AV_TIME_BASE;
> +                } else {
> +                    s->start_time_realtime = start_realtime;
> +                }
> +            }
> +
> +            if (!sec) {
> +                int64_t offset = av_rescale_q(pkt->pts,
> s->streams[0]->time_base, AV_TIME_BASE_Q);
> +                int64_t timestamp = start_realtime + offset;
> +                sec = timestamp / AV_TIME_BASE;
> +            }
> +
> +            tm = gmtime(&sec);
> +            if (!strftime(filename, sizeof(filename), img->path, tm)) {
> +                av_log(s, AV_LOG_ERROR, "Could not get frame filename
> with strftime\n");
> +                return AVERROR(EINVAL);
> +            }
>          } else if (av_get_frame_filename2(filename, sizeof(filename),
> img->path,
>                                            img->img_number,
>
>  AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0 &&
> @@ -215,6 +253,7 @@ static const AVOption muxoptions[] = {
>      { "strftime",     "use strftime for filename", OFFSET(use_strftime),
> AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
>      { "frame_pts",    "use current frame pts for filename",
> OFFSET(frame_pts),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
>      { "atomic_writing", "write files atomically (using temporary files
> and renames)", OFFSET(use_rename), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
> ENC },
> +    { "strftime_source", "use strftime for filename and timestamp(UTC)
> from source if exists", OFFSET(strftime_source), AV_OPT_TYPE_BOOL, { .i64 =
> 0 }, 0, 1, ENC },
>      { NULL },
>  };
>
> diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
> index 8349840c96..16bb143ca6 100644
> --- a/libavformat/rtsp.c
> +++ b/libavformat/rtsp.c
> @@ -2253,6 +2253,7 @@ redo:
>                                          (uint64_t)
> rtpctx->st->time_base.num * 1000000,
>
> rtpctx->st->time_base.den);
>                      }
> +                    av_dict_set_int(&s->metadata, "start_realtime",
> s->start_time_realtime, 0);
>                  }
>              }
>              if (ret == -RTCP_BYE) {
> --
> 2.17.1
>

Ping. :)
Carl Eugen Hoyos April 17, 2019, 10:11 p.m. UTC | #2
2019-04-15 8:43 GMT+02:00, Jun Li <junli1026@gmail.com>:
> Currently the strftime option generate timestamp based on generation
> time. The new option would calcualte timestamp from source's
> start_realtime and pkt->pts, try to generate a timestamp matches the
> source starting time.

> diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
> index 8349840c96..16bb143ca6 100644
> --- a/libavformat/rtsp.c
> +++ b/libavformat/rtsp.c
> @@ -2253,6 +2253,7 @@ redo:
>                                          (uint64_t)
> rtpctx->st->time_base.num * 1000000,
>
> rtpctx->st->time_base.den);
>                      }
> +                    av_dict_set_int(&s->metadata, "start_realtime",
> s->start_time_realtime, 0);

Is this change related?

Carl Eugen
Jun Li April 18, 2019, 12:02 a.m. UTC | #3
On Wed, Apr 17, 2019 at 3:11 PM Carl Eugen Hoyos <ceffmpeg@gmail.com> wrote:

> 2019-04-15 8:43 GMT+02:00, Jun Li <junli1026@gmail.com>:
> > Currently the strftime option generate timestamp based on generation
> > time. The new option would calcualte timestamp from source's
> > start_realtime and pkt->pts, try to generate a timestamp matches the
> > source starting time.
>
> > diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
> > index 8349840c96..16bb143ca6 100644
> > --- a/libavformat/rtsp.c
> > +++ b/libavformat/rtsp.c
> > @@ -2253,6 +2253,7 @@ redo:
> >                                          (uint64_t)
> > rtpctx->st->time_base.num * 1000000,
> >
> > rtpctx->st->time_base.den);
> >                      }
> > +                    av_dict_set_int(&s->metadata, "start_realtime",
> > s->start_time_realtime, 0);
>
> Is this change related?
>
> Carl Eugen
> _______________________________________________
> 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".


Thanks Carl for review !
Yes, it is related. The value is passed into metadata so that image2enc to
get it during muxing.
The change on image2enc relies on metadata "start_realtime" and
"creation_time".

Best Regards,
Jun
Carl Eugen Hoyos April 18, 2019, 1:23 a.m. UTC | #4
2019-04-18 2:02 GMT+02:00, Jun Li <junli1026@gmail.com>:
> On Wed, Apr 17, 2019 at 3:11 PM Carl Eugen Hoyos <ceffmpeg@gmail.com> wrote:
>
>> 2019-04-15 8:43 GMT+02:00, Jun Li <junli1026@gmail.com>:
>> > Currently the strftime option generate timestamp based on generation
>> > time. The new option would calcualte timestamp from source's
>> > start_realtime and pkt->pts, try to generate a timestamp matches the
>> > source starting time.
>>
>> > diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
>> > index 8349840c96..16bb143ca6 100644
>> > --- a/libavformat/rtsp.c
>> > +++ b/libavformat/rtsp.c
>> > @@ -2253,6 +2253,7 @@ redo:
>> >                                          (uint64_t)
>> > rtpctx->st->time_base.num * 1000000,
>> >
>> > rtpctx->st->time_base.den);
>> >                      }
>> > +                    av_dict_set_int(&s->metadata, "start_realtime",
>> > s->start_time_realtime, 0);
>>
>> Is this change related?

> Thanks Carl for review !

> Yes, it is related.

But the change should be separate unless something gets broken.

Carl Eugen
Jun Li April 19, 2019, 1:06 a.m. UTC | #5
On Wed, Apr 17, 2019 at 6:23 PM Carl Eugen Hoyos <ceffmpeg@gmail.com> wrote:

> 2019-04-18 2:02 GMT+02:00, Jun Li <junli1026@gmail.com>:
> > On Wed, Apr 17, 2019 at 3:11 PM Carl Eugen Hoyos <ceffmpeg@gmail.com>
> wrote:
> >
> >> 2019-04-15 8:43 GMT+02:00, Jun Li <junli1026@gmail.com>:
> >> > Currently the strftime option generate timestamp based on generation
> >> > time. The new option would calcualte timestamp from source's
> >> > start_realtime and pkt->pts, try to generate a timestamp matches the
> >> > source starting time.
> >>
> >> > diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
> >> > index 8349840c96..16bb143ca6 100644
> >> > --- a/libavformat/rtsp.c
> >> > +++ b/libavformat/rtsp.c
> >> > @@ -2253,6 +2253,7 @@ redo:
> >> >                                          (uint64_t)
> >> > rtpctx->st->time_base.num * 1000000,
> >> >
> >> > rtpctx->st->time_base.den);
> >> >                      }
> >> > +                    av_dict_set_int(&s->metadata, "start_realtime",
> >> > s->start_time_realtime, 0);
> >>
> >> Is this change related?
>
> > Thanks Carl for review !
>
> > Yes, it is related.
>
> But the change should be separate unless something gets broken.
>
> Carl Eugen
> _______________________________________________
> 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".


Thanks Carl for review. Updated the version here:
https://patchwork.ffmpeg.org/patch/12805/
I re-thinked about the feature, it can be and should be separated into two
different tasks, one for img2 genrating timestamp from output timebase, the
other for passing input start_time_realtime to output. They actually donot
have direct connections.

Best Regards,
Jun
diff mbox

Patch

diff --git a/doc/muxers.texi b/doc/muxers.texi
index 83ae017d6c..58d2d0ee7c 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -1190,6 +1190,10 @@  If set to 1, the filename will always be interpreted as just a
 filename, not a pattern, and the corresponding file will be continuously
 overwritten with new images. Default value is 0.
 
+@item strftime_source
+If set to 1, expand the filename with date and time information from source.
+Useful when input is live stream like rtsp.
+
 @item strftime
 If set to 1, expand the filename with date and time information from
 @code{strftime()}. Default value is 0.
diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
index bec4bf81dd..8ba95ee3d5 100644
--- a/libavformat/img2enc.c
+++ b/libavformat/img2enc.c
@@ -45,6 +45,7 @@  typedef struct VideoMuxData {
     int frame_pts;
     const char *muxer;
     int use_rename;
+    int strftime_source;
 } VideoMuxData;
 
 static int write_header(AVFormatContext *s)
@@ -96,6 +97,7 @@  static int write_packet(AVFormatContext *s, AVPacket *pkt)
             struct tm *tm, tmpbuf;
             time(&now0);
             tm = localtime_r(&now0, &tmpbuf);
+
             if (!strftime(filename, sizeof(filename), img->path, tm)) {
                 av_log(s, AV_LOG_ERROR, "Could not get frame filename with strftime\n");
                 return AVERROR(EINVAL);
@@ -105,6 +107,42 @@  static int write_packet(AVFormatContext *s, AVPacket *pkt)
                 av_log(s, AV_LOG_ERROR, "Cannot write filename by pts of the frames.");
                 return AVERROR(EINVAL);
             }
+        } else if (img->strftime_source) {
+            int64_t start_realtime = s->start_time_realtime;
+            time_t sec = 0;
+            struct tm *tm;
+
+            if (start_realtime == AV_NOPTS_VALUE) {
+                // The value is not passed from input context to output, try to find in metadata.
+                AVDictionaryEntry *entry = av_dict_get(s->metadata, "start_realtime", NULL, 0);
+                if (entry) {
+                    av_log(s, AV_LOG_INFO, "Get start time from metadata start_realtime: %s\n", entry->value);
+                    start_realtime = strtoll(entry->value, NULL, 0);
+                } else {
+                    if (ff_parse_creation_time_metadata(s, &start_realtime, 0) != 0)
+                        av_log(s, AV_LOG_INFO, "Use creation_time as start_timestamp.\n");
+                }
+
+                if (start_realtime == 0 || start_realtime == AV_NOPTS_VALUE) {
+                    av_log(s, AV_LOG_WARNING, "Could not get start_realtime from source, set value to now.\n");
+                    time(&sec);
+                    s->start_time_realtime = sec * AV_TIME_BASE;
+                } else {
+                    s->start_time_realtime = start_realtime;
+                }
+            } 
+            
+            if (!sec) {
+                int64_t offset = av_rescale_q(pkt->pts, s->streams[0]->time_base, AV_TIME_BASE_Q);
+                int64_t timestamp = start_realtime + offset;
+                sec = timestamp / AV_TIME_BASE;
+            }
+
+            tm = gmtime(&sec);
+            if (!strftime(filename, sizeof(filename), img->path, tm)) {
+                av_log(s, AV_LOG_ERROR, "Could not get frame filename with strftime\n");
+                return AVERROR(EINVAL);
+            }
         } else if (av_get_frame_filename2(filename, sizeof(filename), img->path,
                                           img->img_number,
                                           AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0 &&
@@ -215,6 +253,7 @@  static const AVOption muxoptions[] = {
     { "strftime",     "use strftime for filename", OFFSET(use_strftime),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
     { "frame_pts",    "use current frame pts for filename", OFFSET(frame_pts),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
     { "atomic_writing", "write files atomically (using temporary files and renames)", OFFSET(use_rename), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
+    { "strftime_source", "use strftime for filename and timestamp(UTC) from source if exists", OFFSET(strftime_source), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
     { NULL },
 };
 
diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index 8349840c96..16bb143ca6 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -2253,6 +2253,7 @@  redo:
                                         (uint64_t) rtpctx->st->time_base.num * 1000000,
                                                    rtpctx->st->time_base.den);
                     }
+                    av_dict_set_int(&s->metadata, "start_realtime", s->start_time_realtime, 0);
                 }
             }
             if (ret == -RTCP_BYE) {