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 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".