[FFmpeg-devel] [PATCH 1/2] img2: added support for %t output pattern

Roger Pack rogerdpack2 at gmail.com
Tue Oct 25 00:20:14 EEST 2016


On 10/16/16, Michael Niedermayer <michael at niedermayer.cc> wrote:
> On Mon, Oct 10, 2016 at 02:56:24PM -0600, Roger Pack wrote:
>> On 9/22/16, Roger Pack <rogerdpack2 at gmail.com> wrote:
>> > On 1/4/12, Yuval Adam <yuv.adm at gmail.com> wrote:
>> >> From: Yuval Adam <yuv.adm at gmail.com>
>> >>
>> >> The image2 muxer now supports timestamps in output filenames.
>> >> When used in an output patterm '%t' will be replaced with the frames
>> >> timestamp in hours, minutes and seconds (hh:mm:ss).
>> >
>> > A somewhat updated (but not yet cleaned up) revision:
>> >
>> > https://gist.github.com/rdp/e518616f2a702367ae5a922b56e09e04
>> >
>> > see also https://trac.ffmpeg.org/ticket/1452
>>
>> OK attached is the "cleaned up" patch, ready for review/commit.
>>
>> how to test:
>> (apply then) run this:
>>
>> ./ffmpeg -i input -copyts -vsync vfr temp/abc-%d-%t.jpeg
>> and compare filenames with the timestamps from video packets of
>> ffprobe -show_packets.
>>
>> Probably a better way would have been to mix it into
>> av_bprint_strftime however I wasn't sure how to use that within
>> libavformat/utils.c av_get_frame_filename2
>>
>> Adam's initial patch
>> (https://github.com/yuvadm/FFmpeg/commit/0eb002821a2076cb3593c823399aeef9fdd29525)
>> also deprecated av_get_frame_filename
>>
>> but I wasn't sure if we wanted that here or not so didn't include it.
>> Thank you for your consideration.
>> -roger-
>
>>  doc/muxers.texi        |   19 ++++++++++++++++---
>>  libavformat/avformat.h |    3 ++-
>>  libavformat/hlsenc.c   |    6 +++---
>>  libavformat/img2enc.c  |    7 +++++--
>>  libavformat/utils.c    |   36 ++++++++++++++++++++++++++++++++----
>>  5 files changed, 58 insertions(+), 13 deletions(-)
>> 06950fc8ba5a9163ffb838a2bff9933e69255b41
>> 0001-img2-encoder-allow-t-in-filename-based-on-patch-from.patch
>> From 11deddfacc595c43a4f542fffe5e90b142e39c85 Mon Sep 17 00:00:00 2001
>> From: rogerdpack <rogerpack2005 at gmail.com>
>> Date: Mon, 10 Oct 2016 14:50:20 -0600
>> Subject: [PATCH] img2 encoder: allow %t in filename, based on patch from
>> Yuval
>>  Adam
>>
>> Signed-off-by: rogerdpack <rogerpack2005 at gmail.com>
>> ---
>>  doc/muxers.texi        | 19 ++++++++++++++++---
>>  libavformat/avformat.h |  3 ++-
>>  libavformat/hlsenc.c   |  6 +++---
>>  libavformat/img2enc.c  |  7 +++++--
>>  libavformat/utils.c    | 36 ++++++++++++++++++++++++++++++++----
>>  5 files changed, 58 insertions(+), 13 deletions(-)
>>
>> diff --git a/doc/muxers.texi b/doc/muxers.texi
>> index 9ec2e31..6fff966 100644
>> --- a/doc/muxers.texi
>> +++ b/doc/muxers.texi
>> @@ -619,6 +619,12 @@ If the pattern contains "%d" or "%0 at var{N}d", the
>> first filename of
>>  the file list specified will contain the number 1, all the following
>>  numbers will be sequential.
>>
>> +If the pattern contains "%t", the frame's timestamps will be inserted
>> +in the filename like "00.00.00.000" for hours, minutes, seconds,
>> +and milliseconds.
>> +
>> +The "%t" and "%d" patterns may be used simultaneously.
>> +
>>  The pattern may contain a suffix which is used to automatically
>>  determine the format of the image files to write.
>>
>> @@ -635,7 +641,7 @@ The following example shows how to use
>> @command{ffmpeg} for creating a
>>  sequence of files @file{img-001.jpeg}, @file{img-002.jpeg}, ...,
>>  taking one image every second from the input video:
>>  @example
>> -ffmpeg -i in.avi -vsync 1 -r 1 -f image2 'img-%03d.jpeg'
>> +ffmpeg -i in.avi -vsync cfr -r 1 -f image2 'img-%03d.jpeg'
>>  @end example
>>
>>  Note that with @command{ffmpeg}, if the format is not specified with the
>> @@ -643,12 +649,12 @@ Note that with @command{ffmpeg}, if the format is
>> not specified with the
>>  format, the image2 muxer is automatically selected, so the previous
>>  command can be written as:
>>  @example
>> -ffmpeg -i in.avi -vsync 1 -r 1 'img-%03d.jpeg'
>> +ffmpeg -i in.avi -vsync cfr -r 1 'img-%03d.jpeg'
>>  @end example
>>
>>  Note also that the pattern must not necessarily contain "%d" or
>>  "%0 at var{N}d", for example to create a single image file
>> - at file{img.jpeg} from the input video you can employ the command:
>> + at file{img.jpeg} from the start of the input video you can employ the
>> command:
>>  @example
>>  ffmpeg -i in.avi -f image2 -frames:v 1 img.jpeg
>>  @end example
>> @@ -664,6 +670,13 @@ can be used:
>>  ffmpeg -f v4l2 -r 1 -i /dev/video0 -f image2 -strftime 1
>> "%Y-%m-%d_%H-%M-%S.jpg"
>>  @end example
>>
>> +The following example uses the timestamp parameter to generate one
>> +image file per video frame from the input, and name it including its
>> original
>> +timestamp.
>> + at example
>> +ffmpeg -i in.avi -vsync vfr -copyts img-%t.jpg
>> + at end example
>> +
>>  @subsection Options
>>
>>  @table @option
>> diff --git a/libavformat/avformat.h b/libavformat/avformat.h
>> index 057f8c5..4eeb6f4 100644
>> --- a/libavformat/avformat.h
>> +++ b/libavformat/avformat.h
>> @@ -2744,10 +2744,11 @@ void av_dump_format(AVFormatContext *ic,
>>   * @param path numbered sequence string
>>   * @param number frame number
>>   * @param flags AV_FRAME_FILENAME_FLAGS_*
>> + * @param ts frame timestamp in AV_TIME_BASE fractional seconds.
>>   * @return 0 if OK, -1 on format error
>>   */
>>  int av_get_frame_filename2(char *buf, int buf_size,
>> -                          const char *path, int number, int flags);
>> +                          const char *path, int number, int flags,
>> int64_t ts);
>>
>>  int av_get_frame_filename(char *buf, int buf_size,
>>                            const char *path, int number);
>> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
>> index 9ca2df7..02d8d44 100644
>> --- a/libavformat/hlsenc.c
>> +++ b/libavformat/hlsenc.c
>> @@ -654,7 +654,7 @@ static int hls_start(AVFormatContext *s)
>>      } else if (c->max_seg_size > 0) {
>>          if (av_get_frame_filename2(oc->filename, sizeof(oc->filename),
>>              c->basename, c->wrap ? c->sequence % c->wrap : c->sequence,
>> -            AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0) {
>> +            AV_FRAME_FILENAME_FLAGS_MULTIPLE, 0) < 0) {
>>                  av_log(oc, AV_LOG_ERROR, "Invalid segment filename
>> template '%s', you can try to use -use_localtime 1 with it\n",
>> c->basename);
>>                  return AVERROR(EINVAL);
>>          }
>> @@ -685,14 +685,14 @@ static int hls_start(AVFormatContext *s)
>>              }
>>          } else if (av_get_frame_filename2(oc->filename,
>> sizeof(oc->filename),
>>                                    c->basename, c->wrap ? c->sequence %
>> c->wrap : c->sequence,
>> -                                  AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0)
>> {
>> +                                  AV_FRAME_FILENAME_FLAGS_MULTIPLE, 0) <
>> 0) {
>>              av_log(oc, AV_LOG_ERROR, "Invalid segment filename template
>> '%s' you can try to use -use_localtime 1 with it\n", c->basename);
>>              return AVERROR(EINVAL);
>>          }
>>          if( c->vtt_basename) {
>>              if (av_get_frame_filename2(vtt_oc->filename,
>> sizeof(vtt_oc->filename),
>>                                c->vtt_basename, c->wrap ? c->sequence %
>> c->wrap : c->sequence,
>> -                              AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0) {
>> +                              AV_FRAME_FILENAME_FLAGS_MULTIPLE, 0) < 0)
>> {
>>                  av_log(vtt_oc, AV_LOG_ERROR, "Invalid segment filename
>> template '%s'\n", c->vtt_basename);
>>                  return AVERROR(EINVAL);
>>              }
>> diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
>> index 1297b1a..e5db290 100644
>> --- a/libavformat/img2enc.c
>> +++ b/libavformat/img2enc.c
>> @@ -80,10 +80,13 @@ static int write_packet(AVFormatContext *s, AVPacket
>> *pkt)
>>      VideoMuxData *img = s->priv_data;
>>      AVIOContext *pb[4];
>>      char filename[1024];
>> -    AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
>> +    AVStream *stream = s->streams[ pkt->stream_index ];
>> +    AVCodecParameters *par = stream->codecpar;
>
>> +    AVCodecContext *codec = stream->codec;
>
> unused

Oops, removed it now.

>>      const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(par->format);
>>      int i;
>>      int nb_renames = 0;
>> +    int64_t ts = av_rescale_q(pkt->pts, stream->time_base,
>> AV_TIME_BASE_Q);
>>
>>      if (!img->is_pipe) {
>>          if (img->update) {
>> @@ -99,7 +102,7 @@ static int write_packet(AVFormatContext *s, AVPacket
>> *pkt)
>>              }
>>          } else if (av_get_frame_filename2(filename, sizeof(filename),
>> img->path,
>>                                            img->img_number,
>> -
>> AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0 &&
>> +
>> AV_FRAME_FILENAME_FLAGS_MULTIPLE, ts) < 0 &&
>>                     img->img_number > 1) {
>>              av_log(s, AV_LOG_ERROR,
>>                     "Could not get frame filename number %d from pattern
>> '%s' (either set updatefirst or use a pattern like %%03d within the
>> filename pattern)\n",
>> diff --git a/libavformat/utils.c b/libavformat/utils.c
>> index 8a51aea..a8b0e8c 100644
>> --- a/libavformat/utils.c
>> +++ b/libavformat/utils.c
>> @@ -4377,15 +4377,17 @@ uint64_t ff_ntp_time(void)
>>      return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
>>  }
>>
>> -int av_get_frame_filename2(char *buf, int buf_size, const char *path, int
>> number, int flags)
>> +int av_get_frame_filename2(char *buf, int buf_size, const char *path, int
>> number, int flags, int64_t ts)
>>  {
>>      const char *p;
>>      char *q, buf1[20], c;
>> -    int nd, len, percentd_found;
>> +    int nd, len, percentd_found, percentt_found;
>> +    int hours, mins, secs, ms;
>>
>>      q = buf;
>>      p = path;
>>      percentd_found = 0;
>> +    percentt_found = 0;
>>      for (;;) {
>>          c = *p++;
>>          if (c == '\0')
>
>> @@ -4414,6 +4416,32 @@ int av_get_frame_filename2(char *buf, int buf_size,
>> const char *path, int number
>>                  memcpy(q, buf1, len);
>>                  q += len;
>>                  break;
>> +            case 't':
>> +                if (!(flags & AV_FRAME_FILENAME_FLAGS_MULTIPLE) &&
>> percentt_found) {
>> +                    av_log(NULL, AV_LOG_ERROR, "double %%t not
>> allowed");
>> +                    goto fail;
>> +                }
>> +                if (ts == 0) {
>> +                    av_log(NULL, AV_LOG_DEBUG, "%%t but no ts, using 0");
>> // necessary for first frame on some streams
>> +                }
>> +                percentt_found = 1;
>> +                ms = ts % AV_TIME_BASE;
>> +                ts /= AV_TIME_BASE;
>> +                secs = ts % 60;
>> +                ts /= 60;
>> +                mins = ts % 60;
>> +                ts /= 60;
>> +                hours = ts;
>
> does this support negative ts ?
> i think nothig stops negative ts from reaching this

turns out it did not.
Please see the attached patch which does.
Thank you.
-roger-
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-img2-encoder-allow-t-in-filename-based-on-patch-from.patch
Type: application/octet-stream
Size: 9566 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20161024/4c0496a9/attachment.obj>


More information about the ffmpeg-devel mailing list