[FFmpeg-devel] [PATCH] ffprobe: change formatting logic in the JSON writer

Stefano Sabatini stefasab at gmail.com
Sun Jan 8 02:27:36 CET 2012


Print a "\n" at the end of each section, also print the section name in
the section print function, print the chapter name only in case the
chapter contains multiple entries.

Increase textual output sex appeal - different sections can be
distinguished more easily - and simplify logic of pending changes.
---
 ffprobe.c |   23 ++++++++++++++++-------
 1 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/ffprobe.c b/ffprobe.c
index 3a03e21..5b524b8 100644
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -722,6 +722,7 @@ typedef struct {
     size_t buf_size;
     int print_packets_and_frames;
     int indent_level;
+    int section_is_named;
 } JSONContext;
 
 static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
@@ -800,15 +801,17 @@ static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
 
     if (wctx->nb_chapter)
         printf(",");
+    printf("\n");
     json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "frames" ) ||
                              !strcmp(chapter, "packets_and_frames") ||
                              !strcmp(chapter, "streams");
-    printf("\n"); JSON_INDENT();
-    printf("\"%s\":%s", json_escape_str(&json->buf, &json->buf_size, chapter, wctx),
-           json->multiple_entries ? " [" : " ");
-    json->print_packets_and_frames = !strcmp(chapter, "packets_and_frames");
-    if (json->multiple_entries)
+    json->section_is_named = !json->multiple_entries;
+    if (json->multiple_entries) {
+        JSON_INDENT();
+        printf("\"%s\": [\n", json_escape_str(&json->buf, &json->buf_size, chapter, wctx));
+        json->print_packets_and_frames = !strcmp(chapter, "packets_and_frames");
         json->indent_level++;
+    }
 }
 
 static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
@@ -816,8 +819,10 @@ static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
     JSONContext *json = wctx->priv;
 
     if (json->multiple_entries) {
-        printf("]");
+        printf("\n");
         json->indent_level--;
+        JSON_INDENT();
+        printf("]");
     }
 }
 
@@ -825,7 +830,11 @@ static void json_print_section_header(WriterContext *wctx, const char *section)
 {
     JSONContext *json = wctx->priv;
 
-    if (wctx->nb_section) printf(",");
+    if (wctx->nb_section)
+        printf(",\n");
+    JSON_INDENT();
+    if (json->section_is_named)
+        printf("\"%s\": ", section);
     printf("{\n");
     json->indent_level++;
     /* this is required so the parser can distinguish between packets and frames */
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list