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

Stefano Sabatini stefano.sabatini-lala at poste.it
Sun Aug 28 13:09:44 CEST 2011


On date Sunday 2011-08-28 12:20:14 +0200, Clément Bœsch encoded:
> On Sat, Aug 27, 2011 at 08:36:47PM +0200, Clément Bœsch wrote:
> > 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.
> > 
> 
> And again, introducing multiple_entries flag and
> print_section_{start,end}. Still in need of the string escaping.
> 
> -- 
> Clément B.

> From 20b39f32008575b02b2ce5b22e298e1763fbfa77 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 |   84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
>  1 files changed, 80 insertions(+), 4 deletions(-)
> 
> diff --git a/ffprobe.c b/ffprobe.c
> index 18e73bf..c4c1011 100644
> --- a/ffprobe.c
> +++ b/ffprobe.c
> @@ -130,6 +130,8 @@ struct writer {
>      const char *items_sep;          ///< separator between sets of key/value couples
>      const char *section_sep;        ///< separator between sections (streams, packets, ...)
>      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);
> @@ -139,6 +141,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);
> +}

As already mentioned, you need to escape the string.

> +
> +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("]");
> +}

Please show an output example so it's easier to get what this does.

> +
> +
>  /* Default output */
>  
>  static void default_print_header(const char *section)
> @@ -230,6 +273,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;
> @@ -398,6 +459,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,9 +483,13 @@ static int get_writer(const char *name)
>      return -1;
>  }
>  
> -#define SECTION_PRINT(name, left) do {                        \
> +#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);                     \
>      }                                                         \
> @@ -439,9 +514,9 @@ static int probe_file(const char *filename)
>      if (w->header)
>          printf("%s", w->header);
>  
> -    SECTION_PRINT(packets, do_show_streams || do_show_format);
> -    SECTION_PRINT(streams, do_show_format);
> -    SECTION_PRINT(format,  0);
> +    SECTION_PRINT(packets, 1, do_show_streams || do_show_format);
> +    SECTION_PRINT(streams, 1, do_show_format);
> +    SECTION_PRINT(format,  0, 0);
>  
>      if (w->footer)
>          printf("%s", w->footer);
> @@ -511,6 +586,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" },

Semantics of help message is "what the option does", not "what the
option argument is", so change it accordingly (-> "set the output
format used...").

[...]

Missing docs in ffprobe.texi.
-- 
FFmpeg = Freak & Fancy Muttering Patchable Evangelical God


More information about the ffmpeg-devel mailing list