]> git.sesse.net Git - ffmpeg/commitdiff
avformat/img2dec: add option to provide metadata fields related to input path
authorAlexandre Heitor Schmidt <alexandre.schmidt@gmail.com>
Wed, 1 Jan 2020 16:57:02 +0000 (16:57 +0000)
committerMarton Balint <cus@passwd.hu>
Fri, 10 Jan 2020 22:08:18 +0000 (23:08 +0100)
libavformat/img2.h: New field export_path_metadata to
VideoDemuxData to only allow the use of the extra metadata
upon explicit user request, for security reasons.

libavformat/img2dec.c: Modify image2 demuxer to make available
two special metadata entries called lavf.image2dec.source_path
and lavf.image2dec.source_basename, which represents, respectively,
the complete path to the source image for the current frame and
the basename i.e. the file name related to the current frame.
These can then be used by filters like drawtext and others. The
metadata fields will only be available when explicitly enabled
with image2 option -export_path_metadata 1.

doc/demuxers.texi: Documented the new metadata fields available
for image2 and how to use them.

doc/filters.texi: Added an example on how to use the new metadata
fields with drawtext filter, in order to plot the input file path
to each output frame.

Usage example:

ffmpeg -f image2 -export_path_metadata 1 -pattern_type glob
 -framerate 18 -i '/path/to/input/files/*.jpg'
 -filter_complex drawtext="fontsize=40:fontcolor=white:
 fontfile=FreeSans.ttf:borderw=2:bordercolor=black:
 text='%{metadata\:lavf.image2dec.source_basename\:NA}':x=5:y=50"
 output.avi

Fixes #2874.

Signed-off-by: Alexandre Heitor Schmidt <alexandre.schmidt@gmail.com>
Signed-off-by: Marton Balint <cus@passwd.hu>
doc/demuxers.texi
doc/filters.texi
libavformat/img2.h
libavformat/img2dec.c
libavformat/version.h

index 0d13bdd1b309542fce7a3a03d68ef70b4acf7c92..2de14b68d349f6e03e88a185359c65d71e715d7b 100644 (file)
@@ -456,6 +456,17 @@ nanosecond precision.
 @item video_size
 Set the video size of the images to read. If not specified the video
 size is guessed from the first image file in the sequence.
+@item export_path_metadata
+If set to 1, will add two extra fields to the metadata found in input, making them
+also available for other filters (see @var{drawtext} filter for examples). Default
+value is 0. The extra fields are described below:
+@table @option
+@item lavf.image2dec.source_path
+Corresponds to the full path to the input file being read.
+@item lavf.image2dec.source_basename
+Corresponds to the name of the file being read.
+@end table
+
 @end table
 
 @subsection Examples
index 6fb660b05a1c082b8cb0fd445175974ef716eaf4..a2f862e85fe34bcd8c12e02834f38e5781b3634a 100644 (file)
@@ -9932,6 +9932,15 @@ drawtext=fontfile=FreeSans.ttf:text=DOG:fontsize=24:x=10:y=20+24-max_glyph_a,
 drawtext=fontfile=FreeSans.ttf:text=cow:fontsize=24:x=80:y=20+24-max_glyph_a
 @end example
 
+@item
+Plot special @var{lavf.image2dec.source_basename} metadata onto each frame if
+such metadata exists. Otherwise, plot the string "NA". Note that image2 demuxer
+must have option @option{-export_path_metadata 1} for the special metadata fields
+to be available for filters.
+@example
+drawtext="fontsize=20:fontcolor=white:fontfile=FreeSans.ttf:text='%@{metadata\:lavf.image2dec.source_basename\:NA@}':x=10:y=10"
+@end example
+
 @end itemize
 
 For more information about libfreetype, check:
index 0e5b374a6b25adacadfa87a5f54d52b3a3ea63ef..5fd8ff77fc22b933f95037628fca8eca52e9f13c 100644 (file)
@@ -61,6 +61,7 @@ typedef struct VideoDemuxData {
     int start_number_range;
     int frame_size;
     int ts_from_file;
+    int export_path_metadata; /**< enabled when set to 1. */
 } VideoDemuxData;
 
 typedef struct IdStrMap {
index f8b4a655a53cde6b9b8a18da0d576ff4e4997f98..37ee1bb7463ed1ec92089fc93bdf03a7e84606a9 100644 (file)
@@ -374,6 +374,33 @@ int ff_img_read_header(AVFormatContext *s1)
     return 0;
 }
 
+/**
+ * Add this frame's source path and basename to packet's sidedata
+ * as a dictionary, so it can be used by filters like 'drawtext'.
+ */
+static int add_filename_as_pkt_side_data(char *filename, AVPacket *pkt) {
+    uint8_t* metadata;
+    int metadata_len;
+    AVDictionary *d = NULL;
+    char *packed_metadata = NULL;
+
+    av_dict_set(&d, "lavf.image2dec.source_path", filename, 0);
+    av_dict_set(&d, "lavf.image2dec.source_basename", av_basename(filename), 0);
+
+    packed_metadata = av_packet_pack_dictionary(d, &metadata_len);
+    av_dict_free(&d);
+    if (!packed_metadata)
+        return AVERROR(ENOMEM);
+    if (!(metadata = av_packet_new_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, metadata_len))) {
+        av_freep(&packed_metadata);
+        return AVERROR(ENOMEM);
+    }
+    memcpy(metadata, packed_metadata, metadata_len);
+    av_freep(&packed_metadata);
+
+    return 0;
+}
+
 int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
 {
     VideoDemuxData *s = s1->priv_data;
@@ -486,6 +513,17 @@ int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
     if (s->is_pipe)
         pkt->pos = avio_tell(f[0]);
 
+    /*
+     * export_path_metadata must be explicitly enabled via
+     * command line options for path metadata to be exported
+     * as packet side_data.
+     */
+    if (!s->is_pipe && s->export_path_metadata == 1) {
+        res = add_filename_as_pkt_side_data(filename, pkt);
+        if (res < 0)
+            goto fail;
+    }
+
     pkt->size = 0;
     for (i = 0; i < 3; i++) {
         if (f[i]) {
@@ -585,6 +623,7 @@ const AVOption ff_img_options[] = {
     { "none", "none",                   0, AV_OPT_TYPE_CONST,    {.i64 = 0   }, 0, 2,       DEC, "ts_type" },
     { "sec",  "second precision",       0, AV_OPT_TYPE_CONST,    {.i64 = 1   }, 0, 2,       DEC, "ts_type" },
     { "ns",   "nano second precision",  0, AV_OPT_TYPE_CONST,    {.i64 = 2   }, 0, 2,       DEC, "ts_type" },
+    { "export_path_metadata", "enable metadata containing input path information", OFFSET(export_path_metadata), AV_OPT_TYPE_BOOL,   {.i64 = 0   }, 0, 1,       DEC }, \
     COMMON_OPTIONS
 };
 
index 213b66b45f5cedf36ef95f0c04ec63f56efe78c4..43f3811df127af72a11bc64c965d7ad6ed25507f 100644 (file)
@@ -33,7 +33,7 @@
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
 #define LIBAVFORMAT_VERSION_MINOR  35
-#define LIBAVFORMAT_VERSION_MICRO 101
+#define LIBAVFORMAT_VERSION_MICRO 102
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \