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

diff --git a/avprobe.c b/avprobe.c
index 68423fb..cc58236 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;
 
@@ -208,6 +209,107 @@ static void ini_print_string(const char *key, const char 
*value)
 }
 
 /*
+ * Alternate format, JSON
+ */
+
+static void json_print_header(void)
+{
+    avio_printf(avp_out, "{");
+    indent_level++;
+    in_object = 1;
+}
+static void json_print_footer(void)
+{
+    avio_printf(avp_out, "}\n");
+    indent_level--;
+}
+
+static void json_print_array_header(const char *name)
+{
+    if (print_array_separator)
+        avio_printf(avp_out, ",\n");
+    AVP_INDENT();
+    avio_printf(avp_out, "\"%s\" : ", name);
+    avio_printf(avp_out, "[\n");
+    indent_level++;
+    in_object = 0;
+}
+
+static void json_print_array_footer(const char *name)
+{
+    indent_level--;
+    avio_printf(avp_out, "\n");
+    AVP_INDENT();
+    avio_printf(avp_out, "]");
+    in_object = 1;
+}
+
+static void json_print_object_header(const char *name)
+{
+    if (print_object_separator)
+        avio_printf(avp_out, ",\n");
+    AVP_INDENT();
+    if (in_object)
+        avio_printf(avp_out, "\"%s\" : ", name);
+    avio_printf(avp_out, "{\n");
+    indent_level++;
+    in_object = 1;
+}
+
+static void json_print_object_footer(const char *name)
+{
+    indent_level--;
+    avio_printf(avp_out, "\n");
+    AVP_INDENT();
+    avio_printf(avp_out, "}");
+    in_object = 0;
+}
+
+static void json_print_integer(const char *key, int64_t value)
+{
+    if (print_item_separator)
+        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 (print_item_separator)
+        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.
  */
 
@@ -646,6 +748,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;
@@ -706,6 +822,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" },
@@ -757,6 +874,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

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to