From patchwork Mon Apr 15 06:43:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Li X-Patchwork-Id: 12748 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 772A9448F8A for ; Mon, 15 Apr 2019 09:50:39 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 582E0689A11; Mon, 15 Apr 2019 09:50:39 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 97325680924 for ; Mon, 15 Apr 2019 09:50:32 +0300 (EEST) Received: by mail-pf1-f196.google.com with SMTP id y13so8073199pfm.11 for ; Sun, 14 Apr 2019 23:50:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=T7/ddFEXpzaOkfVJ3q6rvYc/u3HsySYAodFKAKDkcz4=; b=DLxzAnhhfzhBGfsfpfxWNZ8V0KTJiFXXtnpH5CBCyvaLi9sBA9JSlcyKYm/4XR7cnS 2C9EK3U4ae2xhBMLj4gUEanOnOdUnUsoahsIr0KD8VIyYa0Y47le3A0RgoGL6kFDaqcg l5tD80UtjBYrF9QGHsFn14G+93Bb29CQcLPtwkxDc/U+VhaUoXLpGwRJXjm/5DZvZ4hX rqFVH+T4sz8xV20tE2tQL6PU/l/YpkWzjDI+xYpQ+LK5XryAz4lN9E3gQ1yC5uwpawfM hgirRkpYIWNYZpgcSyODNq/y0mFYEzjokeyzuejLmC0Gb8LggwMZJl2Gy4bxzB+POzW3 bSJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=T7/ddFEXpzaOkfVJ3q6rvYc/u3HsySYAodFKAKDkcz4=; b=RaInnK1wo7aiebSck3PhkpO8rn75eaEFEcecfAyaqSE1hJqZ8Z4OC7i0woXbWiaiIn qwirtdaPMeocMNKL4Cpgs1B6fN2WLrviQBnJn80coitBXIC6BkVDcAwWcNiQVVwzUgXD irMhnpc7yrXQKeVSZxlWtc8Ut4i+PSXskLDJbV6tpmFqHZiV3YNnS0awnVu8ES2tRKcZ 165qvMYJ4dhqmRWMVJLXGROeCbKmz7uHffW4kB1D23ONBc3167tQnASoRxlQ+3/M5m8m JWPkC3PBTNZCSMBYcJmPePrCCz/p/ztMR19PDHEy+HBcI53TyMO3VwGynZ0nN3UWACwd OSQA== X-Gm-Message-State: APjAAAUGN1zxZ9e4RlMK2xaygJFxKxr096O+lfA5HQzYhFHw2H4RvOaa yaaZ/cGMVa2n7hOA5kvJmCdvEv88 X-Google-Smtp-Source: APXvYqzrkU0EImKxUfos16P7QgAFcFc0zxfAG3xRO+GQFlX3wobqbDpHhNXKjEWSykFPKhvVpRR+Fw== X-Received: by 2002:a62:4602:: with SMTP id t2mr73093578pfa.26.1555310591564; Sun, 14 Apr 2019 23:43:11 -0700 (PDT) Received: from jun-ubuntu.corp.microsoft.com ([2001:4898:80e8:b:1883:4623:1301:5a3c]) by smtp.googlemail.com with ESMTPSA id p128sm111120535pfp.30.2019.04.14.23.43.10 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 14 Apr 2019 23:43:10 -0700 (PDT) From: Jun Li To: ffmpeg-devel@ffmpeg.org Date: Sun, 14 Apr 2019 23:43:02 -0700 Message-Id: <20190415064302.8693-1-junli1026@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH v1] lavf/img2enc: add support for option strftime_source X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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) {