[libav-devel] [PATCH 2/2] avprobe: provide JSON output

Luca Barbato lu_zero at gentoo.org
Thu May 24 21:35:36 CEST 2012


JSON usage is quite widespread.
---
 avprobe.c |  110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 110 insertions(+), 0 deletions(-)

diff --git a/avprobe.c b/avprobe.c
index e79bdab..8b2533e 100644
--- a/avprobe.c
+++ b/avprobe.c
@@ -35,6 +35,7 @@ const int program_birth_year = 2007;
 static int do_show_format  = 0;
 static AVDictionary *fmt_entries_to_show = NULL;
 static int nb_fmt_entries_to_show;
+static int do_output_json  = 0;
 static int do_show_packets = 0;
 static int do_show_streams = 0;
 
@@ -97,6 +98,7 @@ typedef struct {
 
 static AVIOContext *avp_out = NULL;
 static OutputContext octx;
+#define AVP_INDENT() avio_printf(avp_out, "%*c", octx.level * 2, ' ')
 
 /*
  * Default format, INI
@@ -190,6 +192,96 @@ static void ini_print_string(const char *key, const char *value)
 }
 
 /*
+ * Alternate format, JSON
+ */
+
+static void json_print_header(void)
+{
+    avio_printf(avp_out, "{");
+}
+static void json_print_footer(void)
+{
+    avio_printf(avp_out, "}\n");
+}
+
+static void json_print_array_header(const char *name)
+{
+    if (octx.prefix[octx.level -1].nb_elems)
+        avio_printf(avp_out, ",\n");
+    AVP_INDENT();
+    avio_printf(avp_out, "\"%s\" : ", name);
+    avio_printf(avp_out, "[\n");
+}
+
+static void json_print_array_footer(const char *name)
+{
+    avio_printf(avp_out, "\n");
+    AVP_INDENT();
+    avio_printf(avp_out, "]");
+}
+
+static void json_print_object_header(const char *name)
+{
+    if (octx.prefix[octx.level -1].nb_elems)
+        avio_printf(avp_out, ",\n");
+    AVP_INDENT();
+    if (octx.prefix[octx.level -1].type == OBJECT)
+        avio_printf(avp_out, "\"%s\" : ", name);
+    avio_printf(avp_out, "{\n");
+}
+
+static void json_print_object_footer(const char *name)
+{
+    avio_printf(avp_out, "\n");
+    AVP_INDENT();
+    avio_printf(avp_out, "}");
+}
+
+static void json_print_integer(const char *key, int64_t value)
+{
+    if (octx.prefix[octx.level -1].nb_elems)
+        avio_printf(avp_out, ",\n");
+    AVP_INDENT();
+    avio_printf(avp_out, "\"%s\" : %"PRId64"", key, value);
+}
+
+static void json_escape_print(const char *s)
+{
+    int i = 0;
+    char c = 0;
+
+    while (c = s[i++]) {
+        switch (c) {
+        case '\r': avio_printf(avp_out, "%s", "\\r"); break;
+        case '\n': avio_printf(avp_out, "%s", "\\n"); break;
+        case '\f': avio_printf(avp_out, "%s", "\\f"); break;
+        case '\b': avio_printf(avp_out, "%s", "\\b"); break;
+        case '\t': avio_printf(avp_out, "%s", "\\t"); break;
+        case '\\':
+        case '"' : avio_w8(avp_out, '\\');
+        default:
+            if ((unsigned char)c < 32)
+                avio_printf(avp_out, "\\u00%02x", c & 0xff);
+            else
+                avio_w8(avp_out, c);
+        break;
+        }
+    }
+}
+
+static void json_print_string(const char *key, const char *value)
+{
+    if (octx.prefix[octx.level -1].nb_elems)
+        avio_printf(avp_out, ",\n");
+    AVP_INDENT();
+    avio_w8(avp_out, '\"');
+    json_escape_print(key);
+    avio_printf(avp_out, "\" : \"");
+    json_escape_print(value);
+    avio_w8(avp_out, '\"');
+}
+
+/*
  * Simple Formatter for single entries.
  */
 
@@ -653,6 +745,20 @@ static int opt_format(const char *opt, const char *arg)
     return 0;
 }
 
+static void opt_output_json(void)
+{
+    print_header        = json_print_header;
+    print_footer        = json_print_footer;
+    print_array_header  = json_print_array_header;
+    print_array_footer  = json_print_array_footer;
+    print_object_header = json_print_object_header;
+    print_object_footer = json_print_object_footer;
+
+    print_integer = json_print_integer;
+    print_string  = json_print_string;
+
+}
+
 static int opt_show_format_entry(const char *opt, const char *arg)
 {
     do_show_format = 1;
@@ -713,6 +819,7 @@ static const OptionDef options[] = {
       "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
     { "pretty", 0, {(void*)&opt_pretty},
       "prettify the format of displayed values, make it more human readable" },
+    { "json", OPT_BOOL, {(void*)&do_output_json}, "output a json document instead of an ini one" },
     { "show_format",  OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
     { "show_format_entry", HAS_ARG, {(void*)opt_show_format_entry},
       "show a particular entry from the format/container info", "entry" },
@@ -764,6 +871,9 @@ int main(int argc, char **argv)
     if (!avp_out)
         exit(1);
 
+    if (do_output_json)
+        opt_output_json();
+
     avp_header();
     ret = probe_file(input_filename);
     avp_footer();
-- 
1.7.8.rc1



More information about the libav-devel mailing list