[FFmpeg-devel] [PATCH 3/6] ffprobe: add basic JSON output.

Clément Bœsch ubitux at gmail.com
Wed Aug 17 20:32:04 CEST 2011


---
 ffprobe.c |   23 ++++++++++++++++++-----
 1 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/ffprobe.c b/ffprobe.c
index 5eebd45..6b05a74 100644
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -36,6 +36,7 @@ static int do_show_format  = 0;
 static int do_show_packets = 0;
 static int do_show_streams = 0;
 
+static int json_output;
 static int show_value_unit              = 0;
 static int use_value_prefix             = 0;
 static int use_byte_value_binary_prefix = 0;
@@ -129,16 +130,17 @@ static const char *media_type_string(enum AVMediaType media_type)
 
 static void print_info_header(const char *section)
 {
-    printf("[%s]\n", section);
+    if (json_output) printf("{\n");
+    else             printf("[%s]\n", section);
 }
 
 static void print_info_item_fmt(const char *key, const char *fmt, ...)
 {
     va_list ap;
     va_start(ap, fmt);
-    printf("%s=", key);
+    printf(json_output ? "  \"%s\": \"" : "%s=", key);
     vprintf(fmt, ap);
-    printf("\n");
+    printf(json_output ? "\"\n" : "\n");
     va_end(ap);
 }
 
@@ -149,12 +151,13 @@ static void print_info_item_str(const char *key, const char *value)
 
 static void print_info_item_int(const char *key, int value)
 {
-    printf("%s=%d\n", key, value);
+    printf(json_output ? "  \"%s\": %d\n" : "%s=%d\n", key, value);
 }
 
 static void print_info_footer(const char *section)
 {
-    printf("[/%s]\n", section);
+    if (json_output) printf("}\n");
+    else             printf("[/%s]\n", section);
 }
 
 static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
@@ -191,10 +194,19 @@ static void show_packets(AVFormatContext *fmt_ctx)
 static void show_avdict(AVDictionary *dict)
 {
     AVDictionaryEntry *tag = NULL;
+    if (json_output) {
+        printf("  \"tags\": {\n");
+        while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
+            printf("    ");
+            print_info_item_str(tag->key, tag->value);
+        }
+        printf("  }\n");
+    } else {
     while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
         printf("TAG:");
         print_info_item_str(tag->key, tag->value);
     }
+    }
 }
 
 static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
@@ -418,6 +430,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*)&json_output}, "output data in 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



More information about the ffmpeg-devel mailing list