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

Clément Bœsch ubitux at gmail.com
Sat Aug 27 20:36:47 CEST 2011


On Sat, Aug 27, 2011 at 06:39:13PM +0200, Alexander Strasser wrote:
> Hello,
> 
>   I like your idea of adding a JSON output writer to ffprobe.
> 
> Clément Bœsch wrote:
> > ---
> >  ffprobe.c |   79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> >  1 files changed, 79 insertions(+), 0 deletions(-)
> > 
> > diff --git a/ffprobe.c b/ffprobe.c
> [...]
> 
>   It would be better to output the strings with the proper escapes
> if necessary. For more information, you could have a look at json.org
> or at the RFC 4627. (I think you knew that already, just repeating it
> here for convenience.) 
> 

Yes, right. This was the reason of the "basic" in commit message. I'll fix
that before pushing it. New patch attached anyway for demonstration
purpose of the first writer patch.

-- 
Clément B.
-------------- next part --------------
From 857275272526ef85a093228a262a49bbe9302c35 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
Date: Sat, 27 Aug 2011 20:19:44 +0200
Subject: [PATCH 2/2] ffprobe: add basic JSON print format.

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

diff --git a/ffprobe.c b/ffprobe.c
index f566ec5..36f37ab 100644
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -128,6 +128,8 @@ struct writer {
     const char *name;
     const char *item_sep, *items_sep, *section_sep;
     const char *header, *footer;
+    void (*print_section_start)(const char *, int);
+    void (*print_section_end)  (const char *, int);
     void (*print_header)(const char *);
     void (*print_footer)(const char *);
     void (*print_fmt_f)(const char *, const char *, va_list ap);
@@ -137,6 +139,47 @@ 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  }");
+}
+
+static void json_print_section_start(const char *section, int multiple_entries)
+{
+    printf("\n  \"%s\":%s", section, multiple_entries ? " [" : " ");
+}
+
+static void json_print_section_end(const char *section, int multiple_entries)
+{
+    if (multiple_entries)
+        printf("]");
+}
+
+
 /* Default output */
 
 static void default_print_header(const char *section)
@@ -228,6 +271,24 @@ static void show_packets(struct writer *w, AVFormatContext *fmt_ctx)
         show_packet(w, fmt_ctx, &pkt, i++);
 }
 
+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;
@@ -396,6 +457,16 @@ static struct writer writers[] = {{
         .section_sep  = "\n",
         .footer       = "\n",
         WRITER_FUNC(default),
+    },{
+        .name         = "json",
+        .header       = "{",
+        .item_sep     = ",\n",
+        .items_sep    = ",",
+        .section_sep  = ",",
+        .footer       = "\n}\n",
+        .print_section_start = json_print_section_start,
+        .print_section_end   = json_print_section_end,
+        WRITER_FUNC(json),
     }
 };
 
@@ -412,7 +483,11 @@ static int get_writer(const char *name)
 
 #define SECTION_PRINT(name, multiple_entries, left) do {      \
     if (do_show_ ## name) {                                   \
+        if (w->print_section_start)                           \
+            w->print_section_start(#name, multiple_entries);  \
         show_ ## name (w, fmt_ctx);                           \
+        if (w->print_section_end)                             \
+            w->print_section_end(#name, multiple_entries);    \
         if (left)                                             \
             printf("%s", w->section_sep);                     \
     }                                                         \
@@ -509,6 +584,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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20110827/22115bf8/attachment.asc>


More information about the ffmpeg-devel mailing list