[FFmpeg-devel] [PATCH 2/2] ffprobe: add basic JSON print format.

Clément Bœsch ubitux at gmail.com
Fri Aug 26 14:36:03 CEST 2011


---
 ffprobe.c |   79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 79 insertions(+), 0 deletions(-)

diff --git a/ffprobe.c b/ffprobe.c
index 3f1799e..a30a128 100644
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -137,6 +137,36 @@ struct writer {
 };
 
 
+/* JSON output */
+
+static void json_print_header(const char *section)
+{
+    printf("{\n");
+}
+
+static void json_print_fmt(const char *key, const char *fmt, va_list ap)
+{
+    printf("    \"%s\": \"", key);
+    vprintf(fmt, ap);
+    printf("\"");
+}
+
+static void json_print_int(const char *key, int value)
+{
+    printf("    \"%s\": %d", key, value);
+}
+
+static void json_print_str(const char *key, const char *value)
+{
+    printf("    \"%s\": %s", key, value);
+}
+
+static void json_print_footer(const char *section)
+{
+    printf("\n  }");
+}
+
+
 /* Default output */
 
 static void default_print_header(const char *section)
@@ -226,6 +256,24 @@ static void show_packets(struct writer *w, AVFormatContext *fmt_ctx)
     }
 }
 
+static void json_show_tags(struct writer *w, AVDictionary *dict)
+{
+    AVDictionaryEntry *tag = NULL;
+    int first = 1;
+    if (!dict)
+        return;
+    printf(",\n    \"tags\": {\n");
+    while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
+        if (first) {
+            print_str0(tag->key, tag->value);
+            first = 0;
+        } else {
+            print_str(tag->key, tag->value);
+        }
+    }
+    printf("\n    }");
+}
+
 static void default_show_tags(struct writer *w, AVDictionary *dict)
 {
     AVDictionaryEntry *tag = NULL;
@@ -391,6 +439,35 @@ static void default_probe_file(struct writer *w, AVFormatContext *fmt_ctx)
         show_format(w, fmt_ctx);
 }
 
+static void json_probe_file(struct writer *w, AVFormatContext *fmt_ctx)
+{
+    int i;
+
+    printf("{");
+
+    if (do_show_packets) {
+        printf("\n  \"packets\": [");
+        show_packets(w, fmt_ctx);
+        printf((do_show_streams || do_show_format) ? "]," : "]");
+    }
+
+    if (do_show_streams) {
+        printf("\n  \"streams\": [");
+        for (i = 0; i < fmt_ctx->nb_streams; i++) {
+            if (i)
+                printf(",");
+            show_stream(w, fmt_ctx, i);
+        }
+        printf(do_show_format ? "]," : "]");
+    }
+
+    if (do_show_format) {
+        printf("\n  \"format\": ");
+        show_format(w, fmt_ctx);
+    }
+
+    printf("\n}\n");
+}
 
 #define WRITER_FUNC(func)                  \
     .probe_file   = func ## _probe_file,   \
@@ -403,6 +480,7 @@ static void default_probe_file(struct writer *w, AVFormatContext *fmt_ctx)
 
 static struct writer writers[] = {
     {.name = "default", .item_sep = "\n",  WRITER_FUNC(default)},
+    {.name = "json",    .item_sep = ",\n", WRITER_FUNC(json)},
 };
 
 static int get_writer(const char *name)
@@ -499,6 +577,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" },
+    { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format}, "Output format used. Available formats: default, json" },
     { "show_format",  OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
     { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
     { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
-- 
1.7.6.1



More information about the ffmpeg-devel mailing list