2 * Copyright (c) 2007-2010 Stefano Sabatini
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * simple media prober based on the FFmpeg libraries
28 #include "libavformat/avformat.h"
29 #include "libavcodec/avcodec.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/dict.h"
34 #include "libavdevice/avdevice.h"
37 const char program_name[] = "ffprobe";
38 const int program_birth_year = 2007;
40 static int do_show_format = 0;
41 static int do_show_packets = 0;
42 static int do_show_streams = 0;
44 static int show_value_unit = 0;
45 static int use_value_prefix = 0;
46 static int use_byte_value_binary_prefix = 0;
47 static int use_value_sexagesimal_format = 0;
48 static int show_private_data = 1;
50 static char *print_format;
52 static const OptionDef options[];
55 static const char *input_filename;
56 static AVInputFormat *iformat = NULL;
58 static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
59 static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
61 static const char *unit_second_str = "s" ;
62 static const char *unit_hertz_str = "Hz" ;
63 static const char *unit_byte_str = "byte" ;
64 static const char *unit_bit_per_second_str = "bit/s";
66 void av_noreturn exit_program(int ret)
72 union { double d; int i; } val;
76 static char *value_string(char *buf, int buf_size, struct unit_value uv)
81 if (uv.unit == unit_second_str) {
88 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
92 mins = (int)secs / 60;
93 secs = secs - mins * 60;
96 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
97 } else if (use_value_prefix) {
98 const char *prefix_string;
101 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
102 index = (int) (log(vald)/log(2)) / 10;
103 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
104 vald /= pow(2, index*10);
105 prefix_string = binary_unit_prefixes[index];
107 index = (int) (log10(vald)) / 3;
108 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
109 vald /= pow(10, index*3);
110 prefix_string = decimal_unit_prefixes[index];
113 if (show_float || vald != (int)vald) l = snprintf(buf, buf_size, "%.3f", vald);
114 else l = snprintf(buf, buf_size, "%d", (int)vald);
115 snprintf(buf+l, buf_size-l, "%s%s%s", prefix_string || show_value_unit ? " " : "",
116 prefix_string, show_value_unit ? uv.unit : "");
120 if (show_float) l = snprintf(buf, buf_size, "%.3f", vald);
121 else l = snprintf(buf, buf_size, "%d", (int)vald);
122 snprintf(buf+l, buf_size-l, "%s%s", show_value_unit ? " " : "",
123 show_value_unit ? uv.unit : "");
131 typedef struct WriterContext WriterContext;
133 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
135 typedef struct Writer {
136 int priv_size; ///< private size for the writer context
139 int (*init) (WriterContext *wctx, const char *args, void *opaque);
140 void (*uninit)(WriterContext *wctx);
142 void (*print_header)(WriterContext *ctx);
143 void (*print_footer)(WriterContext *ctx);
145 void (*print_chapter_header)(WriterContext *wctx, const char *);
146 void (*print_chapter_footer)(WriterContext *wctx, const char *);
147 void (*print_section_header)(WriterContext *wctx, const char *);
148 void (*print_section_footer)(WriterContext *wctx, const char *);
149 void (*print_integer) (WriterContext *wctx, const char *, long long int);
150 void (*print_string) (WriterContext *wctx, const char *, const char *);
151 void (*show_tags) (WriterContext *wctx, AVDictionary *dict);
152 int flags; ///< a combination or WRITER_FLAG_*
155 struct WriterContext {
156 const AVClass *class; ///< class of the writer
157 const Writer *writer; ///< the Writer of which this is an instance
158 char *name; ///< name of this writer instance
159 void *priv; ///< private data for use by the filter
160 unsigned int nb_item; ///< number of the item printed in the given section, starting at 0
161 unsigned int nb_section; ///< number of the section printed in the given section sequence, starting at 0
162 unsigned int nb_chapter; ///< number of the chapter, starting at 0
165 static const char *writer_get_name(void *p)
167 WriterContext *wctx = p;
168 return wctx->writer->name;
171 static const AVClass writer_class = {
175 LIBAVUTIL_VERSION_INT,
178 static void writer_close(WriterContext **wctx)
180 if (*wctx && (*wctx)->writer->uninit)
181 (*wctx)->writer->uninit(*wctx);
183 av_freep(&((*wctx)->priv));
187 static int writer_open(WriterContext **wctx, const Writer *writer,
188 const char *args, void *opaque)
192 if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
193 ret = AVERROR(ENOMEM);
197 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
198 ret = AVERROR(ENOMEM);
202 (*wctx)->class = &writer_class;
203 (*wctx)->writer = writer;
204 if ((*wctx)->writer->init)
205 ret = (*wctx)->writer->init(*wctx, args, opaque);
216 static inline void writer_print_header(WriterContext *wctx)
218 if (wctx->writer->print_header)
219 wctx->writer->print_header(wctx);
220 wctx->nb_chapter = 0;
223 static inline void writer_print_footer(WriterContext *wctx)
225 if (wctx->writer->print_footer)
226 wctx->writer->print_footer(wctx);
229 static inline void writer_print_chapter_header(WriterContext *wctx,
232 if (wctx->writer->print_chapter_header)
233 wctx->writer->print_chapter_header(wctx, header);
234 wctx->nb_section = 0;
237 static inline void writer_print_chapter_footer(WriterContext *wctx,
240 if (wctx->writer->print_chapter_footer)
241 wctx->writer->print_chapter_footer(wctx, footer);
245 static inline void writer_print_section_header(WriterContext *wctx,
248 if (wctx->writer->print_section_header)
249 wctx->writer->print_section_header(wctx, header);
253 static inline void writer_print_section_footer(WriterContext *wctx,
256 if (wctx->writer->print_section_footer)
257 wctx->writer->print_section_footer(wctx, footer);
261 static inline void writer_print_integer(WriterContext *wctx,
262 const char *key, long long int val)
264 wctx->writer->print_integer(wctx, key, val);
268 static inline void writer_print_string(WriterContext *wctx,
269 const char *key, const char *val, int opt)
271 if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
273 wctx->writer->print_string(wctx, key, val);
277 static void writer_print_time(WriterContext *wctx, const char *key,
278 int64_t ts, const AVRational *time_base)
282 if (ts == AV_NOPTS_VALUE) {
283 writer_print_string(wctx, key, "N/A", 1);
285 double d = ts * av_q2d(*time_base);
286 value_string(buf, sizeof(buf), (struct unit_value){.val.d=d, .unit=unit_second_str});
287 writer_print_string(wctx, key, buf, 0);
291 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts)
293 if (ts == AV_NOPTS_VALUE) {
294 writer_print_string(wctx, key, "N/A", 1);
296 writer_print_integer(wctx, key, ts);
300 static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
302 wctx->writer->show_tags(wctx, dict);
305 #define MAX_REGISTERED_WRITERS_NB 64
307 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
309 static int writer_register(const Writer *writer)
311 static int next_registered_writer_idx = 0;
313 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
314 return AVERROR(ENOMEM);
316 registered_writers[next_registered_writer_idx++] = writer;
320 static const Writer *writer_get_by_name(const char *name)
324 for (i = 0; registered_writers[i]; i++)
325 if (!strcmp(registered_writers[i]->name, name))
326 return registered_writers[i];
338 static char *fast_asprintf(struct print_buf *pbuf, const char *fmt, ...)
344 len = vsnprintf(NULL, 0, fmt, va);
349 if (pbuf->len < len) {
350 char *p = av_realloc(pbuf->s, len + 1);
358 len = vsnprintf(pbuf->s, len + 1, fmt, va);
370 #define ESCAPE_INIT_BUF_SIZE 256
372 #define ESCAPE_CHECK_SIZE(src, size, max_size) \
373 if (size > max_size) { \
375 snprintf(buf, sizeof(buf), "%s", src); \
376 av_log(log_ctx, AV_LOG_WARNING, \
377 "String '%s...' with is too big\n", buf); \
378 return "FFPROBE_TOO_BIG_STRING"; \
381 #define ESCAPE_REALLOC_BUF(dst_size_p, dst_p, src, size) \
382 if (*dst_size_p < size) { \
383 char *q = av_realloc(*dst_p, size); \
386 snprintf(buf, sizeof(buf), "%s", src); \
387 av_log(log_ctx, AV_LOG_WARNING, \
388 "String '%s...' could not be escaped\n", buf); \
389 return "FFPROBE_THIS_STRING_COULD_NOT_BE_ESCAPED"; \
391 *dst_size_p = size; \
399 static void default_print_footer(WriterContext *wctx)
404 static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
406 if (wctx->nb_chapter)
410 /* lame uppercasing routine, assumes the string is lower case ASCII */
411 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
414 for (i = 0; src[i] && i < dst_size-1; i++)
420 static void default_print_section_header(WriterContext *wctx, const char *section)
424 if (wctx->nb_section)
426 printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
429 static void default_print_section_footer(WriterContext *wctx, const char *section)
433 printf("[/%s]", upcase_string(buf, sizeof(buf), section));
436 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
438 printf("%s=%s\n", key, value);
441 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
443 printf("%s=%lld\n", key, value);
446 static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
448 AVDictionaryEntry *tag = NULL;
449 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
451 writer_print_string(wctx, tag->key, tag->value, 0);
455 static const Writer default_writer = {
457 .print_footer = default_print_footer,
458 .print_chapter_header = default_print_chapter_header,
459 .print_section_header = default_print_section_header,
460 .print_section_footer = default_print_section_footer,
461 .print_integer = default_print_int,
462 .print_string = default_print_str,
463 .show_tags = default_show_tags,
464 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
470 * Escape \n, \r, \\ and sep characters contained in s, and print the
473 static const char *c_escape_str(char **dst, size_t *dst_size,
474 const char *src, const char sep, void *log_ctx)
480 /* precompute size */
481 for (p = src; *p; p++, size++) {
482 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-2);
483 if (*p == '\n' || *p == '\r' || *p == '\\')
487 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
490 for (p = src; *p; p++) {
492 case '\n': *q++ = '\\'; *q++ = 'n'; break;
493 case '\r': *q++ = '\\'; *q++ = 'r'; break;
494 case '\\': *q++ = '\\'; *q++ = '\\'; break;
506 * Quote fields containing special characters, check RFC4180.
508 static const char *csv_escape_str(char **dst, size_t *dst_size,
509 const char *src, const char sep, void *log_ctx)
516 /* precompute size */
517 for (p = src; *p; p++, size++) {
518 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-4);
519 if (*p == '"' || *p == sep || *p == '\n' || *p == '\r')
528 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
546 static const char *none_escape_str(char **dst, size_t *dst_size,
547 const char *src, const char sep, void *log_ctx)
552 typedef struct CompactContext {
553 const AVClass *class;
559 char *escape_mode_str;
560 const char * (*escape_str)(char **dst, size_t *dst_size,
561 const char *src, const char sep, void *log_ctx);
564 #define OFFSET(x) offsetof(CompactContext, x)
566 static const AVOption compact_options[]= {
567 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
568 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
569 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
570 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
571 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
572 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
576 static const char *compact_get_name(void *ctx)
581 static const AVClass compact_class = {
587 static av_cold int compact_init(WriterContext *wctx, const char *args, void *opaque)
589 CompactContext *compact = wctx->priv;
592 compact->class = &compact_class;
593 av_opt_set_defaults(compact);
596 (err = (av_set_options_string(compact, args, "=", ":"))) < 0) {
597 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
600 if (strlen(compact->item_sep_str) != 1) {
601 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
602 compact->item_sep_str);
603 return AVERROR(EINVAL);
605 compact->item_sep = compact->item_sep_str[0];
607 compact->buf_size = ESCAPE_INIT_BUF_SIZE;
608 if (!(compact->buf = av_malloc(compact->buf_size)))
609 return AVERROR(ENOMEM);
611 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
612 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
613 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
615 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
616 return AVERROR(EINVAL);
622 static av_cold void compact_uninit(WriterContext *wctx)
624 CompactContext *compact = wctx->priv;
626 av_freep(&compact->item_sep_str);
627 av_freep(&compact->buf);
628 av_freep(&compact->escape_mode_str);
631 static void compact_print_section_header(WriterContext *wctx, const char *section)
633 CompactContext *compact = wctx->priv;
635 printf("%s%c", section, compact->item_sep);
638 static void compact_print_section_footer(WriterContext *wctx, const char *section)
643 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
645 CompactContext *compact = wctx->priv;
647 if (wctx->nb_item) printf("%c", compact->item_sep);
650 printf("%s", compact->escape_str(&compact->buf, &compact->buf_size,
651 value, compact->item_sep, wctx));
654 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
656 CompactContext *compact = wctx->priv;
658 if (wctx->nb_item) printf("%c", compact->item_sep);
661 printf("%lld", value);
664 static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
666 CompactContext *compact = wctx->priv;
667 AVDictionaryEntry *tag = NULL;
669 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
670 if (wctx->nb_item) printf("%c", compact->item_sep);
672 printf("tag:%s=", compact->escape_str(&compact->buf, &compact->buf_size,
673 tag->key, compact->item_sep, wctx));
674 printf("%s", compact->escape_str(&compact->buf, &compact->buf_size,
675 tag->value, compact->item_sep, wctx));
679 static const Writer compact_writer = {
681 .priv_size = sizeof(CompactContext),
682 .init = compact_init,
683 .uninit = compact_uninit,
684 .print_section_header = compact_print_section_header,
685 .print_section_footer = compact_print_section_footer,
686 .print_integer = compact_print_int,
687 .print_string = compact_print_str,
688 .show_tags = compact_show_tags,
689 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
694 static av_cold int csv_init(WriterContext *wctx, const char *args, void *opaque)
696 return compact_init(wctx, "item_sep=,:nokey=1:escape=csv", opaque);
699 static const Writer csv_writer = {
701 .priv_size = sizeof(CompactContext),
703 .uninit = compact_uninit,
704 .print_section_header = compact_print_section_header,
705 .print_section_footer = compact_print_section_footer,
706 .print_integer = compact_print_int,
707 .print_string = compact_print_str,
708 .show_tags = compact_show_tags,
709 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
715 int multiple_entries; ///< tells if the given chapter requires multiple entries
720 static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
722 JSONContext *json = wctx->priv;
724 json->buf_size = ESCAPE_INIT_BUF_SIZE;
725 if (!(json->buf = av_malloc(json->buf_size)))
726 return AVERROR(ENOMEM);
731 static av_cold void json_uninit(WriterContext *wctx)
733 JSONContext *json = wctx->priv;
734 av_freep(&json->buf);
737 static const char *json_escape_str(char **dst, size_t *dst_size, const char *src,
740 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
741 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
746 // compute the length of the escaped string
747 for (p = src; *p; p++) {
748 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-6);
749 if (strchr(json_escape, *p)) size += 2; // simple escape
750 else if ((unsigned char)*p < 32) size += 6; // handle non-printable chars
751 else size += 1; // char copy
753 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
756 for (p = src; *p; p++) {
757 char *s = strchr(json_escape, *p);
760 *q++ = json_subst[s - json_escape];
761 } else if ((unsigned char)*p < 32) {
762 snprintf(q, 7, "\\u00%02x", *p & 0xff);
772 static void json_print_header(WriterContext *wctx)
777 static void json_print_footer(WriterContext *wctx)
782 static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
784 JSONContext *json = wctx->priv;
786 if (wctx->nb_chapter)
788 json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "streams");
789 printf("\n \"%s\":%s", json_escape_str(&json->buf, &json->buf_size, chapter, wctx),
790 json->multiple_entries ? " [" : " ");
793 static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
795 JSONContext *json = wctx->priv;
797 if (json->multiple_entries)
801 static void json_print_section_header(WriterContext *wctx, const char *section)
803 if (wctx->nb_section) printf(",");
807 static void json_print_section_footer(WriterContext *wctx, const char *section)
812 static inline void json_print_item_str(WriterContext *wctx,
813 const char *key, const char *value,
816 JSONContext *json = wctx->priv;
818 printf("%s\"%s\":", indent, json_escape_str(&json->buf, &json->buf_size, key, wctx));
819 printf(" \"%s\"", json_escape_str(&json->buf, &json->buf_size, value, wctx));
824 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
826 if (wctx->nb_item) printf(",\n");
827 json_print_item_str(wctx, key, value, INDENT);
830 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
832 JSONContext *json = wctx->priv;
834 if (wctx->nb_item) printf(",\n");
835 printf(INDENT "\"%s\": %lld",
836 json_escape_str(&json->buf, &json->buf_size, key, wctx), value);
839 static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
841 AVDictionaryEntry *tag = NULL;
845 printf(",\n" INDENT "\"tags\": {\n");
846 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
847 if (is_first) is_first = 0;
849 json_print_item_str(wctx, tag->key, tag->value, INDENT INDENT);
854 static const Writer json_writer = {
856 .priv_size = sizeof(JSONContext),
858 .uninit = json_uninit,
859 .print_header = json_print_header,
860 .print_footer = json_print_footer,
861 .print_chapter_header = json_print_chapter_header,
862 .print_chapter_footer = json_print_chapter_footer,
863 .print_section_header = json_print_section_header,
864 .print_section_footer = json_print_section_footer,
865 .print_integer = json_print_int,
866 .print_string = json_print_str,
867 .show_tags = json_show_tags,
873 const AVClass *class;
875 int multiple_entries; ///< tells if the given chapter requires multiple entries
884 #define OFFSET(x) offsetof(XMLContext, x)
886 static const AVOption xml_options[] = {
887 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
888 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
889 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
890 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
894 static const char *xml_get_name(void *ctx)
899 static const AVClass xml_class = {
905 static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
907 XMLContext *xml = wctx->priv;
910 xml->class = &xml_class;
911 av_opt_set_defaults(xml);
914 (err = (av_set_options_string(xml, args, "=", ":"))) < 0) {
915 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
919 if (xml->xsd_strict) {
920 xml->fully_qualified = 1;
921 #define CHECK_COMPLIANCE(opt, opt_name) \
923 av_log(wctx, AV_LOG_ERROR, \
924 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
925 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
927 CHECK_COMPLIANCE(show_private_data, "private");
928 CHECK_COMPLIANCE(show_value_unit, "unit");
929 CHECK_COMPLIANCE(use_value_prefix, "prefix");
932 xml->buf_size = ESCAPE_INIT_BUF_SIZE;
933 if (!(xml->buf = av_malloc(xml->buf_size)))
934 return AVERROR(ENOMEM);
938 static av_cold void xml_uninit(WriterContext *wctx)
940 XMLContext *xml = wctx->priv;
944 static const char *xml_escape_str(char **dst, size_t *dst_size, const char *src,
951 /* precompute size */
952 for (p = src; *p; p++, size++) {
953 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-10);
955 case '&' : size += strlen("&"); break;
956 case '<' : size += strlen("<"); break;
957 case '>' : size += strlen(">"); break;
958 case '\"': size += strlen("""); break;
959 case '\'': size += strlen("'"); break;
963 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
965 #define COPY_STR(str) { \
966 const char *s = str; \
975 case '&' : COPY_STR("&"); break;
976 case '<' : COPY_STR("<"); break;
977 case '>' : COPY_STR(">"); break;
978 case '\"': COPY_STR("""); break;
979 case '\'': COPY_STR("'"); break;
989 static void xml_print_header(WriterContext *wctx)
991 XMLContext *xml = wctx->priv;
992 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
993 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
994 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
996 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
997 printf("<%sffprobe%s>\n",
998 xml->fully_qualified ? "ffprobe:" : "",
999 xml->fully_qualified ? qual : "");
1001 xml->indent_level++;
1004 static void xml_print_footer(WriterContext *wctx)
1006 XMLContext *xml = wctx->priv;
1008 xml->indent_level--;
1009 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1012 #define XML_INDENT() { int i; for (i = 0; i < xml->indent_level; i++) printf(INDENT); }
1014 static void xml_print_chapter_header(WriterContext *wctx, const char *chapter)
1016 XMLContext *xml = wctx->priv;
1018 if (wctx->nb_chapter)
1020 xml->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "streams");
1022 if (xml->multiple_entries) {
1023 XML_INDENT(); printf("<%s>\n", chapter);
1024 xml->indent_level++;
1028 static void xml_print_chapter_footer(WriterContext *wctx, const char *chapter)
1030 XMLContext *xml = wctx->priv;
1032 if (xml->multiple_entries) {
1033 xml->indent_level--;
1034 XML_INDENT(); printf("</%s>\n", chapter);
1038 static void xml_print_section_header(WriterContext *wctx, const char *section)
1040 XMLContext *xml = wctx->priv;
1042 XML_INDENT(); printf("<%s ", section);
1043 xml->within_tag = 1;
1046 static void xml_print_section_footer(WriterContext *wctx, const char *section)
1048 XMLContext *xml = wctx->priv;
1050 if (xml->within_tag)
1053 XML_INDENT(); printf("</%s>\n", section);
1057 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1059 XMLContext *xml = wctx->priv;
1063 printf("%s=\"%s\"", key, xml_escape_str(&xml->buf, &xml->buf_size, value, wctx));
1066 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1070 printf("%s=\"%lld\"", key, value);
1073 static void xml_show_tags(WriterContext *wctx, AVDictionary *dict)
1075 XMLContext *xml = wctx->priv;
1076 AVDictionaryEntry *tag = NULL;
1079 xml->indent_level++;
1080 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1082 /* close section tag */
1084 xml->within_tag = 0;
1088 printf("<tag key=\"%s\"",
1089 xml_escape_str(&xml->buf, &xml->buf_size, tag->key, wctx));
1090 printf(" value=\"%s\"/>\n",
1091 xml_escape_str(&xml->buf, &xml->buf_size, tag->value, wctx));
1093 xml->indent_level--;
1096 static Writer xml_writer = {
1098 .priv_size = sizeof(XMLContext),
1100 .uninit = xml_uninit,
1101 .print_header = xml_print_header,
1102 .print_footer = xml_print_footer,
1103 .print_chapter_header = xml_print_chapter_header,
1104 .print_chapter_footer = xml_print_chapter_footer,
1105 .print_section_header = xml_print_section_header,
1106 .print_section_footer = xml_print_section_footer,
1107 .print_integer = xml_print_int,
1108 .print_string = xml_print_str,
1109 .show_tags = xml_show_tags,
1112 static void writer_register_all(void)
1114 static int initialized;
1120 writer_register(&default_writer);
1121 writer_register(&compact_writer);
1122 writer_register(&csv_writer);
1123 writer_register(&json_writer);
1124 writer_register(&xml_writer);
1127 #define print_fmt(k, f, ...) do { \
1128 if (fast_asprintf(&pbuf, f, __VA_ARGS__)) \
1129 writer_print_string(w, k, pbuf.s, 0); \
1132 #define print_fmt_opt(k, f, ...) do { \
1133 if (fast_asprintf(&pbuf, f, __VA_ARGS__)) \
1134 writer_print_string(w, k, pbuf.s, 1); \
1137 #define print_int(k, v) writer_print_integer(w, k, v)
1138 #define print_str(k, v) writer_print_string(w, k, v, 0)
1139 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
1140 #define print_time(k, v, tb) writer_print_time(w, k, v, tb)
1141 #define print_ts(k, v) writer_print_ts(w, k, v)
1142 #define print_val(k, v, u) writer_print_string(w, k, \
1143 value_string(val_str, sizeof(val_str), (struct unit_value){.val.i = v, .unit=u}), 0)
1144 #define print_section_header(s) writer_print_section_header(w, s)
1145 #define print_section_footer(s) writer_print_section_footer(w, s)
1146 #define show_tags(metadata) writer_show_tags(w, metadata)
1148 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1151 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1152 struct print_buf pbuf = {.s = NULL};
1155 print_section_header("packet");
1156 s = av_get_media_type_string(st->codec->codec_type);
1157 if (s) print_str ("codec_type", s);
1158 else print_str_opt("codec_type", "unknown");
1159 print_int("stream_index", pkt->stream_index);
1160 print_ts ("pts", pkt->pts);
1161 print_time("pts_time", pkt->pts, &st->time_base);
1162 print_ts ("dts", pkt->dts);
1163 print_time("dts_time", pkt->dts, &st->time_base);
1164 print_ts ("duration", pkt->duration);
1165 print_time("duration_time", pkt->duration, &st->time_base);
1166 print_val("size", pkt->size, unit_byte_str);
1167 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1168 else print_str_opt("pos", "N/A");
1169 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1170 print_section_footer("packet");
1176 static void show_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1181 av_init_packet(&pkt);
1183 while (!av_read_frame(fmt_ctx, &pkt))
1184 show_packet(w, fmt_ctx, &pkt, i++);
1187 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
1189 AVStream *stream = fmt_ctx->streams[stream_idx];
1190 AVCodecContext *dec_ctx;
1194 AVRational display_aspect_ratio;
1195 struct print_buf pbuf = {.s = NULL};
1197 print_section_header("stream");
1199 print_int("index", stream->index);
1201 if ((dec_ctx = stream->codec)) {
1202 if ((dec = dec_ctx->codec)) {
1203 print_str("codec_name", dec->name);
1204 print_str("codec_long_name", dec->long_name);
1206 print_str_opt("codec_name", "unknown");
1207 print_str_opt("codec_long_name", "unknown");
1210 s = av_get_media_type_string(dec_ctx->codec_type);
1211 if (s) print_str ("codec_type", s);
1212 else print_str_opt("codec_type", "unknown");
1213 print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
1215 /* print AVI/FourCC tag */
1216 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
1217 print_str("codec_tag_string", val_str);
1218 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
1220 switch (dec_ctx->codec_type) {
1221 case AVMEDIA_TYPE_VIDEO:
1222 print_int("width", dec_ctx->width);
1223 print_int("height", dec_ctx->height);
1224 print_int("has_b_frames", dec_ctx->has_b_frames);
1225 if (dec_ctx->sample_aspect_ratio.num) {
1226 print_fmt("sample_aspect_ratio", "%d:%d",
1227 dec_ctx->sample_aspect_ratio.num,
1228 dec_ctx->sample_aspect_ratio.den);
1229 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1230 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
1231 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
1233 print_fmt("display_aspect_ratio", "%d:%d",
1234 display_aspect_ratio.num,
1235 display_aspect_ratio.den);
1237 print_str_opt("sample_aspect_ratio", "N/A");
1238 print_str_opt("display_aspect_ratio", "N/A");
1240 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
1241 if (s) print_str ("pix_fmt", s);
1242 else print_str_opt("pix_fmt", "unknown");
1243 print_int("level", dec_ctx->level);
1244 if (dec_ctx->timecode_frame_start >= 0) {
1245 uint32_t tc = dec_ctx->timecode_frame_start;
1246 print_fmt("timecode", "%02d:%02d:%02d%c%02d",
1247 tc>>19 & 0x1f, // hours
1248 tc>>13 & 0x3f, // minutes
1249 tc>>6 & 0x3f, // seconds
1250 tc & 1<<24 ? ';' : ':', // drop
1251 tc & 0x3f); // frames
1253 print_str_opt("timecode", "N/A");
1257 case AVMEDIA_TYPE_AUDIO:
1258 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
1259 if (s) print_str ("sample_fmt", s);
1260 else print_str_opt("sample_fmt", "unknown");
1261 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
1262 print_int("channels", dec_ctx->channels);
1263 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
1267 print_str_opt("codec_type", "unknown");
1269 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
1270 const AVOption *opt = NULL;
1271 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
1273 if (opt->flags) continue;
1274 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
1275 print_str(opt->name, str);
1281 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
1282 else print_str_opt("id", "N/A");
1283 print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
1284 print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
1285 print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
1286 print_time("start_time", stream->start_time, &stream->time_base);
1287 print_time("duration", stream->duration, &stream->time_base);
1288 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
1289 else print_str_opt("nb_frames", "N/A");
1290 show_tags(stream->metadata);
1292 print_section_footer("stream");
1297 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
1300 for (i = 0; i < fmt_ctx->nb_streams; i++)
1301 show_stream(w, fmt_ctx, i);
1304 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
1307 int64_t size = avio_size(fmt_ctx->pb);
1308 struct print_buf pbuf = {.s = NULL};
1310 print_section_header("format");
1311 print_str("filename", fmt_ctx->filename);
1312 print_int("nb_streams", fmt_ctx->nb_streams);
1313 print_str("format_name", fmt_ctx->iformat->name);
1314 print_str("format_long_name", fmt_ctx->iformat->long_name);
1315 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
1316 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
1317 if (size >= 0) print_val ("size", size, unit_byte_str);
1318 else print_str_opt("size", "N/A");
1319 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
1320 else print_str_opt("bit_rate", "N/A");
1321 show_tags(fmt_ctx->metadata);
1322 print_section_footer("format");
1327 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
1330 AVFormatContext *fmt_ctx = NULL;
1331 AVDictionaryEntry *t;
1333 if ((err = avformat_open_input(&fmt_ctx, filename, iformat, &format_opts)) < 0) {
1334 print_error(filename, err);
1337 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1338 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
1339 return AVERROR_OPTION_NOT_FOUND;
1343 /* fill the streams in the format context */
1344 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
1345 print_error(filename, err);
1349 av_dump_format(fmt_ctx, 0, filename, 0);
1351 /* bind a decoder to each input stream */
1352 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1353 AVStream *stream = fmt_ctx->streams[i];
1356 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
1357 fprintf(stderr, "Unsupported codec with id %d for input stream %d\n",
1358 stream->codec->codec_id, stream->index);
1359 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
1360 fprintf(stderr, "Error while opening codec for input stream %d\n",
1365 *fmt_ctx_ptr = fmt_ctx;
1369 #define PRINT_CHAPTER(name) do { \
1370 if (do_show_ ## name) { \
1371 writer_print_chapter_header(wctx, #name); \
1372 show_ ## name (wctx, fmt_ctx); \
1373 writer_print_chapter_footer(wctx, #name); \
1377 static int probe_file(const char *filename)
1379 AVFormatContext *fmt_ctx;
1383 char *w_name = NULL, *w_args = NULL;
1384 WriterContext *wctx;
1386 writer_register_all();
1389 print_format = av_strdup("default");
1390 w_name = av_strtok(print_format, "=", &buf);
1393 w = writer_get_by_name(w_name);
1395 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
1396 ret = AVERROR(EINVAL);
1400 if ((ret = writer_open(&wctx, w, w_args, NULL)) < 0)
1402 if ((ret = open_input_file(&fmt_ctx, filename)))
1405 writer_print_header(wctx);
1406 PRINT_CHAPTER(packets);
1407 PRINT_CHAPTER(streams);
1408 PRINT_CHAPTER(format);
1409 writer_print_footer(wctx);
1411 avformat_close_input(&fmt_ctx);
1412 writer_close(&wctx);
1415 av_freep(&print_format);
1420 static void show_usage(void)
1422 printf("Simple multimedia streams analyzer\n");
1423 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
1427 static int opt_format(const char *opt, const char *arg)
1429 iformat = av_find_input_format(arg);
1431 fprintf(stderr, "Unknown input format: %s\n", arg);
1432 return AVERROR(EINVAL);
1437 static void opt_input_file(void *optctx, const char *arg)
1439 if (input_filename) {
1440 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
1441 arg, input_filename);
1444 if (!strcmp(arg, "-"))
1446 input_filename = arg;
1449 static int opt_help(const char *opt, const char *arg)
1451 av_log_set_callback(log_callback_help);
1453 show_help_options(options, "Main options:\n", 0, 0);
1456 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
1461 static int opt_pretty(const char *opt, const char *arg)
1463 show_value_unit = 1;
1464 use_value_prefix = 1;
1465 use_byte_value_binary_prefix = 1;
1466 use_value_sexagesimal_format = 1;
1470 static const OptionDef options[] = {
1471 #include "cmdutils_common_opts.h"
1472 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
1473 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
1474 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
1475 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
1476 "use binary prefixes for byte units" },
1477 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
1478 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
1479 { "pretty", 0, {(void*)&opt_pretty},
1480 "prettify the format of displayed values, make it more human readable" },
1481 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
1482 "set the output printing format (available formats are: default, compact, csv, json, xml)", "format" },
1483 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
1484 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
1485 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
1486 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
1487 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
1488 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
1489 { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
1493 int main(int argc, char **argv)
1497 parse_loglevel(argc, argv, options);
1499 avformat_network_init();
1502 avdevice_register_all();
1505 show_banner(argc, argv, options);
1506 parse_options(NULL, argc, argv, options, opt_input_file);
1508 if (!input_filename) {
1510 fprintf(stderr, "You have to specify one input file.\n");
1511 fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
1515 ret = probe_file(input_filename);
1517 avformat_network_deinit();