[FFmpeg-devel] [PATCH 2/2] ffprobe: add basic JSON print format.
Clément Bœsch
ubitux at gmail.com
Thu Sep 1 22:20:09 CEST 2011
On Sun, Aug 28, 2011 at 01:09:44PM +0200, Stefano Sabatini wrote:
[...]
> > +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.
>
Done in the fmt callback (str callback no longer exists).
> > +
> > +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.
>
Sure. This is to handle:
"packets": [
{"index": ... } // packet0
{"index": ... } // packet1
{"index": ... } // packet2
],
"streams": [
...
],
"format": { ... }
"packets" and "streams" need [], while format doesn't. The section
start/end allow that sort of formatting.
[...]
> > 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...").
>
Fixed.
New patch attached.
--
Clément B.
-------------- next part --------------
From 1a956e0b4dcf0016b19e0132c79adf1a6ac9fb76 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] ffprobe: add basic JSON print format.
---
ffprobe.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 129 insertions(+), 4 deletions(-)
diff --git a/ffprobe.c b/ffprobe.c
index 2748224..ba66bdd 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 *, ...);
@@ -138,6 +140,96 @@ struct writer {
};
+/* JSON output */
+
+static void json_print_header(const char *section)
+{
+ printf("{\n");
+}
+
+#define JSON_ESCAPE "\"\\\b\f\n\r\t"
+#define JSON_SUBST "\"\\bfnr"
+
+static char *json_escape_str(const char *s)
+{
+ char *ret, *p;
+ int i, len = 0;
+
+ for (i = 0; s[i]; i++) {
+ if (strchr(JSON_ESCAPE, s[i])) len += 2;
+ else if ((unsigned char)s[i] < 32) len += 6;
+ else len += 1;
+ }
+
+ p = ret = av_malloc(len + 1);
+ for (i = 0; s[i]; i++) {
+ char *q = strchr(JSON_ESCAPE, s[i]);
+ if (q) {
+ *p++ = '\\';
+ *p++ = JSON_SUBST[q - JSON_ESCAPE];
+ } else if ((unsigned char)s[i] < 32) {
+ snprintf(p, 7, "\\u00%02x", s[i] & 0xff);
+ p += 6;
+ } else {
+ *p++ = s[i];
+ }
+ }
+ *p = 0;
+ return ret;
+}
+
+static void json_print_fmt(const char *key, const char *fmt, ...)
+{
+ int len;
+ char *raw_s = NULL, *escaped_s = NULL;
+ va_list ap;
+
+ va_start(ap, fmt);
+ len = vsnprintf(NULL, 0, fmt, ap);
+ va_end(ap);
+ if (len < 0)
+ goto end;
+
+ raw_s = av_malloc(len + 1);
+ if (!raw_s)
+ goto end;
+
+ va_start(ap, fmt);
+ len = vsnprintf(raw_s, len + 1, fmt, ap);
+ va_end(ap);
+ if (len < 0)
+ goto end;
+
+ escaped_s = json_escape_str(raw_s);
+
+end:
+ printf(" \"%s\": \"%s\"", key, escaped_s ? escaped_s : "");
+ av_free(raw_s);
+ av_free(escaped_s);
+}
+
+static void json_print_int(const char *key, int value)
+{
+ printf(" \"%s\": %d", 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)
@@ -219,6 +311,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;
@@ -386,6 +496,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),
}
};
@@ -400,9 +520,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); \
} \
@@ -427,9 +551,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);
@@ -499,6 +623,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}, "Set the 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/20110901/5cdf7d73/attachment.asc>
More information about the ffmpeg-devel
mailing list