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_error = 0;
41 static int do_show_format = 0;
42 static int do_show_packets = 0;
43 static int do_show_streams = 0;
45 static int show_value_unit = 0;
46 static int use_value_prefix = 0;
47 static int use_byte_value_binary_prefix = 0;
48 static int use_value_sexagesimal_format = 0;
49 static int show_private_data = 1;
51 static char *print_format;
53 static const OptionDef options[];
56 static const char *input_filename;
57 static AVInputFormat *iformat = NULL;
59 static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
60 static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
62 static const char *unit_second_str = "s" ;
63 static const char *unit_hertz_str = "Hz" ;
64 static const char *unit_byte_str = "byte" ;
65 static const char *unit_bit_per_second_str = "bit/s";
67 void av_noreturn exit_program(int ret)
73 union { double d; int i; } val;
77 static char *value_string(char *buf, int buf_size, struct unit_value uv)
82 if (uv.unit == unit_second_str) {
89 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
93 mins = (int)secs / 60;
94 secs = secs - mins * 60;
97 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
98 } else if (use_value_prefix) {
99 const char *prefix_string;
102 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
103 index = (int) (log(vald)/log(2)) / 10;
104 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
105 vald /= pow(2, index*10);
106 prefix_string = binary_unit_prefixes[index];
108 index = (int) (log10(vald)) / 3;
109 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
110 vald /= pow(10, index*3);
111 prefix_string = decimal_unit_prefixes[index];
114 if (show_float || vald != (int)vald) l = snprintf(buf, buf_size, "%.3f", vald);
115 else l = snprintf(buf, buf_size, "%d", (int)vald);
116 snprintf(buf+l, buf_size-l, "%s%s%s", prefix_string || show_value_unit ? " " : "",
117 prefix_string, show_value_unit ? uv.unit : "");
121 if (show_float) l = snprintf(buf, buf_size, "%.3f", vald);
122 else l = snprintf(buf, buf_size, "%d", (int)vald);
123 snprintf(buf+l, buf_size-l, "%s%s", show_value_unit ? " " : "",
124 show_value_unit ? uv.unit : "");
132 typedef struct WriterContext WriterContext;
134 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
136 typedef struct Writer {
137 int priv_size; ///< private size for the writer context
140 int (*init) (WriterContext *wctx, const char *args, void *opaque);
141 void (*uninit)(WriterContext *wctx);
143 void (*print_header)(WriterContext *ctx);
144 void (*print_footer)(WriterContext *ctx);
146 void (*print_chapter_header)(WriterContext *wctx, const char *);
147 void (*print_chapter_footer)(WriterContext *wctx, const char *);
148 void (*print_section_header)(WriterContext *wctx, const char *);
149 void (*print_section_footer)(WriterContext *wctx, const char *);
150 void (*print_integer) (WriterContext *wctx, const char *, long long int);
151 void (*print_string) (WriterContext *wctx, const char *, const char *);
152 void (*show_tags) (WriterContext *wctx, AVDictionary *dict);
153 int flags; ///< a combination or WRITER_FLAG_*
156 struct WriterContext {
157 const AVClass *class; ///< class of the writer
158 const Writer *writer; ///< the Writer of which this is an instance
159 char *name; ///< name of this writer instance
160 void *priv; ///< private data for use by the filter
161 unsigned int nb_item; ///< number of the item printed in the given section, starting at 0
162 unsigned int nb_section; ///< number of the section printed in the given section sequence, starting at 0
163 unsigned int nb_chapter; ///< number of the chapter, starting at 0
166 static const char *writer_get_name(void *p)
168 WriterContext *wctx = p;
169 return wctx->writer->name;
172 static const AVClass writer_class = {
176 LIBAVUTIL_VERSION_INT,
179 static void writer_close(WriterContext **wctx)
184 if ((*wctx)->writer->uninit)
185 (*wctx)->writer->uninit(*wctx);
186 av_freep(&((*wctx)->priv));
190 static int writer_open(WriterContext **wctx, const Writer *writer,
191 const char *args, void *opaque)
195 if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
196 ret = AVERROR(ENOMEM);
200 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
201 ret = AVERROR(ENOMEM);
205 (*wctx)->class = &writer_class;
206 (*wctx)->writer = writer;
207 if ((*wctx)->writer->init)
208 ret = (*wctx)->writer->init(*wctx, args, opaque);
219 static inline void writer_print_header(WriterContext *wctx)
221 if (wctx->writer->print_header)
222 wctx->writer->print_header(wctx);
223 wctx->nb_chapter = 0;
226 static inline void writer_print_footer(WriterContext *wctx)
228 if (wctx->writer->print_footer)
229 wctx->writer->print_footer(wctx);
232 static inline void writer_print_chapter_header(WriterContext *wctx,
235 if (wctx->writer->print_chapter_header)
236 wctx->writer->print_chapter_header(wctx, header);
237 wctx->nb_section = 0;
240 static inline void writer_print_chapter_footer(WriterContext *wctx,
243 if (wctx->writer->print_chapter_footer)
244 wctx->writer->print_chapter_footer(wctx, footer);
248 static inline void writer_print_section_header(WriterContext *wctx,
251 if (wctx->writer->print_section_header)
252 wctx->writer->print_section_header(wctx, header);
256 static inline void writer_print_section_footer(WriterContext *wctx,
259 if (wctx->writer->print_section_footer)
260 wctx->writer->print_section_footer(wctx, footer);
264 static inline void writer_print_integer(WriterContext *wctx,
265 const char *key, long long int val)
267 wctx->writer->print_integer(wctx, key, val);
271 static inline void writer_print_string(WriterContext *wctx,
272 const char *key, const char *val, int opt)
274 if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
276 wctx->writer->print_string(wctx, key, val);
280 static void writer_print_time(WriterContext *wctx, const char *key,
281 int64_t ts, const AVRational *time_base)
285 if (ts == AV_NOPTS_VALUE) {
286 writer_print_string(wctx, key, "N/A", 1);
288 double d = ts * av_q2d(*time_base);
289 value_string(buf, sizeof(buf), (struct unit_value){.val.d=d, .unit=unit_second_str});
290 writer_print_string(wctx, key, buf, 0);
294 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts)
296 if (ts == AV_NOPTS_VALUE) {
297 writer_print_string(wctx, key, "N/A", 1);
299 writer_print_integer(wctx, key, ts);
303 static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
305 wctx->writer->show_tags(wctx, dict);
308 #define MAX_REGISTERED_WRITERS_NB 64
310 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
312 static int writer_register(const Writer *writer)
314 static int next_registered_writer_idx = 0;
316 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
317 return AVERROR(ENOMEM);
319 registered_writers[next_registered_writer_idx++] = writer;
323 static const Writer *writer_get_by_name(const char *name)
327 for (i = 0; registered_writers[i]; i++)
328 if (!strcmp(registered_writers[i]->name, name))
329 return registered_writers[i];
341 static char *fast_asprintf(struct print_buf *pbuf, const char *fmt, ...)
347 len = vsnprintf(NULL, 0, fmt, va);
352 if (pbuf->len < len) {
353 char *p = av_realloc(pbuf->s, len + 1);
361 len = vsnprintf(pbuf->s, len + 1, fmt, va);
373 #define ESCAPE_INIT_BUF_SIZE 256
375 #define ESCAPE_CHECK_SIZE(src, size, max_size) \
376 if (size > max_size) { \
378 snprintf(buf, sizeof(buf), "%s", src); \
379 av_log(log_ctx, AV_LOG_WARNING, \
380 "String '%s...' with is too big\n", buf); \
381 return "FFPROBE_TOO_BIG_STRING"; \
384 #define ESCAPE_REALLOC_BUF(dst_size_p, dst_p, src, size) \
385 if (*dst_size_p < size) { \
386 char *q = av_realloc(*dst_p, size); \
389 snprintf(buf, sizeof(buf), "%s", src); \
390 av_log(log_ctx, AV_LOG_WARNING, \
391 "String '%s...' could not be escaped\n", buf); \
392 return "FFPROBE_THIS_STRING_COULD_NOT_BE_ESCAPED"; \
394 *dst_size_p = size; \
402 static void default_print_footer(WriterContext *wctx)
407 static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
409 if (wctx->nb_chapter)
413 /* lame uppercasing routine, assumes the string is lower case ASCII */
414 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
417 for (i = 0; src[i] && i < dst_size-1; i++)
423 static void default_print_section_header(WriterContext *wctx, const char *section)
427 if (wctx->nb_section)
429 printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
432 static void default_print_section_footer(WriterContext *wctx, const char *section)
436 printf("[/%s]", upcase_string(buf, sizeof(buf), section));
439 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
441 printf("%s=%s\n", key, value);
444 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
446 printf("%s=%lld\n", key, value);
449 static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
451 AVDictionaryEntry *tag = NULL;
452 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
454 writer_print_string(wctx, tag->key, tag->value, 0);
458 static const Writer default_writer = {
460 .print_footer = default_print_footer,
461 .print_chapter_header = default_print_chapter_header,
462 .print_section_header = default_print_section_header,
463 .print_section_footer = default_print_section_footer,
464 .print_integer = default_print_int,
465 .print_string = default_print_str,
466 .show_tags = default_show_tags,
467 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
473 * Escape \n, \r, \\ and sep characters contained in s, and print the
476 static const char *c_escape_str(char **dst, size_t *dst_size,
477 const char *src, const char sep, void *log_ctx)
483 /* precompute size */
484 for (p = src; *p; p++, size++) {
485 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-2);
486 if (*p == '\n' || *p == '\r' || *p == '\\')
490 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
493 for (p = src; *p; p++) {
495 case '\n': *q++ = '\\'; *q++ = 'n'; break;
496 case '\r': *q++ = '\\'; *q++ = 'r'; break;
497 case '\\': *q++ = '\\'; *q++ = '\\'; break;
509 * Quote fields containing special characters, check RFC4180.
511 static const char *csv_escape_str(char **dst, size_t *dst_size,
512 const char *src, const char sep, void *log_ctx)
519 /* precompute size */
520 for (p = src; *p; p++, size++) {
521 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-4);
522 if (*p == '"' || *p == sep || *p == '\n' || *p == '\r')
531 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
549 static const char *none_escape_str(char **dst, size_t *dst_size,
550 const char *src, const char sep, void *log_ctx)
555 typedef struct CompactContext {
556 const AVClass *class;
562 char *escape_mode_str;
563 const char * (*escape_str)(char **dst, size_t *dst_size,
564 const char *src, const char sep, void *log_ctx);
567 #define OFFSET(x) offsetof(CompactContext, x)
569 static const AVOption compact_options[]= {
570 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
571 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
572 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
573 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
574 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
575 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
579 static const char *compact_get_name(void *ctx)
584 static const AVClass compact_class = {
590 static av_cold int compact_init(WriterContext *wctx, const char *args, void *opaque)
592 CompactContext *compact = wctx->priv;
595 compact->class = &compact_class;
596 av_opt_set_defaults(compact);
599 (err = (av_set_options_string(compact, args, "=", ":"))) < 0) {
600 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
603 if (strlen(compact->item_sep_str) != 1) {
604 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
605 compact->item_sep_str);
606 return AVERROR(EINVAL);
608 compact->item_sep = compact->item_sep_str[0];
610 compact->buf_size = ESCAPE_INIT_BUF_SIZE;
611 if (!(compact->buf = av_malloc(compact->buf_size)))
612 return AVERROR(ENOMEM);
614 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
615 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
616 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
618 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
619 return AVERROR(EINVAL);
625 static av_cold void compact_uninit(WriterContext *wctx)
627 CompactContext *compact = wctx->priv;
629 av_freep(&compact->item_sep_str);
630 av_freep(&compact->buf);
631 av_freep(&compact->escape_mode_str);
634 static void compact_print_section_header(WriterContext *wctx, const char *section)
636 CompactContext *compact = wctx->priv;
638 printf("%s%c", section, compact->item_sep);
641 static void compact_print_section_footer(WriterContext *wctx, const char *section)
646 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
648 CompactContext *compact = wctx->priv;
650 if (wctx->nb_item) printf("%c", compact->item_sep);
653 printf("%s", compact->escape_str(&compact->buf, &compact->buf_size,
654 value, compact->item_sep, wctx));
657 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
659 CompactContext *compact = wctx->priv;
661 if (wctx->nb_item) printf("%c", compact->item_sep);
664 printf("%lld", value);
667 static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
669 CompactContext *compact = wctx->priv;
670 AVDictionaryEntry *tag = NULL;
672 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
673 if (wctx->nb_item) printf("%c", compact->item_sep);
675 printf("tag:%s=", compact->escape_str(&compact->buf, &compact->buf_size,
676 tag->key, compact->item_sep, wctx));
677 printf("%s", compact->escape_str(&compact->buf, &compact->buf_size,
678 tag->value, compact->item_sep, wctx));
682 static const Writer compact_writer = {
684 .priv_size = sizeof(CompactContext),
685 .init = compact_init,
686 .uninit = compact_uninit,
687 .print_section_header = compact_print_section_header,
688 .print_section_footer = compact_print_section_footer,
689 .print_integer = compact_print_int,
690 .print_string = compact_print_str,
691 .show_tags = compact_show_tags,
692 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
697 static av_cold int csv_init(WriterContext *wctx, const char *args, void *opaque)
699 return compact_init(wctx, "item_sep=,:nokey=1:escape=csv", opaque);
702 static const Writer csv_writer = {
704 .priv_size = sizeof(CompactContext),
706 .uninit = compact_uninit,
707 .print_section_header = compact_print_section_header,
708 .print_section_footer = compact_print_section_footer,
709 .print_integer = compact_print_int,
710 .print_string = compact_print_str,
711 .show_tags = compact_show_tags,
712 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
718 int multiple_entries; ///< tells if the given chapter requires multiple entries
723 static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
725 JSONContext *json = wctx->priv;
727 json->buf_size = ESCAPE_INIT_BUF_SIZE;
728 if (!(json->buf = av_malloc(json->buf_size)))
729 return AVERROR(ENOMEM);
734 static av_cold void json_uninit(WriterContext *wctx)
736 JSONContext *json = wctx->priv;
737 av_freep(&json->buf);
740 static const char *json_escape_str(char **dst, size_t *dst_size, const char *src,
743 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
744 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
749 // compute the length of the escaped string
750 for (p = src; *p; p++) {
751 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-6);
752 if (strchr(json_escape, *p)) size += 2; // simple escape
753 else if ((unsigned char)*p < 32) size += 6; // handle non-printable chars
754 else size += 1; // char copy
756 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
759 for (p = src; *p; p++) {
760 char *s = strchr(json_escape, *p);
763 *q++ = json_subst[s - json_escape];
764 } else if ((unsigned char)*p < 32) {
765 snprintf(q, 7, "\\u00%02x", *p & 0xff);
775 static void json_print_header(WriterContext *wctx)
780 static void json_print_footer(WriterContext *wctx)
785 static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
787 JSONContext *json = wctx->priv;
789 if (wctx->nb_chapter)
791 json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "streams");
792 printf("\n \"%s\":%s", json_escape_str(&json->buf, &json->buf_size, chapter, wctx),
793 json->multiple_entries ? " [" : " ");
796 static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
798 JSONContext *json = wctx->priv;
800 if (json->multiple_entries)
804 static void json_print_section_header(WriterContext *wctx, const char *section)
806 if (wctx->nb_section) printf(",");
810 static void json_print_section_footer(WriterContext *wctx, const char *section)
815 static inline void json_print_item_str(WriterContext *wctx,
816 const char *key, const char *value,
819 JSONContext *json = wctx->priv;
821 printf("%s\"%s\":", indent, json_escape_str(&json->buf, &json->buf_size, key, wctx));
822 printf(" \"%s\"", json_escape_str(&json->buf, &json->buf_size, value, wctx));
827 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
829 if (wctx->nb_item) printf(",\n");
830 json_print_item_str(wctx, key, value, INDENT);
833 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
835 JSONContext *json = wctx->priv;
837 if (wctx->nb_item) printf(",\n");
838 printf(INDENT "\"%s\": %lld",
839 json_escape_str(&json->buf, &json->buf_size, key, wctx), value);
842 static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
844 AVDictionaryEntry *tag = NULL;
848 printf(",\n" INDENT "\"tags\": {\n");
849 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
850 if (is_first) is_first = 0;
852 json_print_item_str(wctx, tag->key, tag->value, INDENT INDENT);
857 static const Writer json_writer = {
859 .priv_size = sizeof(JSONContext),
861 .uninit = json_uninit,
862 .print_header = json_print_header,
863 .print_footer = json_print_footer,
864 .print_chapter_header = json_print_chapter_header,
865 .print_chapter_footer = json_print_chapter_footer,
866 .print_section_header = json_print_section_header,
867 .print_section_footer = json_print_section_footer,
868 .print_integer = json_print_int,
869 .print_string = json_print_str,
870 .show_tags = json_show_tags,
876 const AVClass *class;
878 int multiple_entries; ///< tells if the given chapter requires multiple entries
887 #define OFFSET(x) offsetof(XMLContext, x)
889 static const AVOption xml_options[] = {
890 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
891 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
892 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
893 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
897 static const char *xml_get_name(void *ctx)
902 static const AVClass xml_class = {
908 static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
910 XMLContext *xml = wctx->priv;
913 xml->class = &xml_class;
914 av_opt_set_defaults(xml);
917 (err = (av_set_options_string(xml, args, "=", ":"))) < 0) {
918 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
922 if (xml->xsd_strict) {
923 xml->fully_qualified = 1;
924 #define CHECK_COMPLIANCE(opt, opt_name) \
926 av_log(wctx, AV_LOG_ERROR, \
927 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
928 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
930 CHECK_COMPLIANCE(show_private_data, "private");
931 CHECK_COMPLIANCE(show_value_unit, "unit");
932 CHECK_COMPLIANCE(use_value_prefix, "prefix");
935 xml->buf_size = ESCAPE_INIT_BUF_SIZE;
936 if (!(xml->buf = av_malloc(xml->buf_size)))
937 return AVERROR(ENOMEM);
941 static av_cold void xml_uninit(WriterContext *wctx)
943 XMLContext *xml = wctx->priv;
947 static const char *xml_escape_str(char **dst, size_t *dst_size, const char *src,
954 /* precompute size */
955 for (p = src; *p; p++, size++) {
956 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-10);
958 case '&' : size += strlen("&"); break;
959 case '<' : size += strlen("<"); break;
960 case '>' : size += strlen(">"); break;
961 case '\"': size += strlen("""); break;
962 case '\'': size += strlen("'"); break;
966 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
968 #define COPY_STR(str) { \
969 const char *s = str; \
978 case '&' : COPY_STR("&"); break;
979 case '<' : COPY_STR("<"); break;
980 case '>' : COPY_STR(">"); break;
981 case '\"': COPY_STR("""); break;
982 case '\'': COPY_STR("'"); break;
992 static void xml_print_header(WriterContext *wctx)
994 XMLContext *xml = wctx->priv;
995 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
996 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
997 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
999 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1000 printf("<%sffprobe%s>\n",
1001 xml->fully_qualified ? "ffprobe:" : "",
1002 xml->fully_qualified ? qual : "");
1004 xml->indent_level++;
1007 static void xml_print_footer(WriterContext *wctx)
1009 XMLContext *xml = wctx->priv;
1011 xml->indent_level--;
1012 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1015 #define XML_INDENT() { int i; for (i = 0; i < xml->indent_level; i++) printf(INDENT); }
1017 static void xml_print_chapter_header(WriterContext *wctx, const char *chapter)
1019 XMLContext *xml = wctx->priv;
1021 if (wctx->nb_chapter)
1023 xml->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "streams");
1025 if (xml->multiple_entries) {
1026 XML_INDENT(); printf("<%s>\n", chapter);
1027 xml->indent_level++;
1031 static void xml_print_chapter_footer(WriterContext *wctx, const char *chapter)
1033 XMLContext *xml = wctx->priv;
1035 if (xml->multiple_entries) {
1036 xml->indent_level--;
1037 XML_INDENT(); printf("</%s>\n", chapter);
1041 static void xml_print_section_header(WriterContext *wctx, const char *section)
1043 XMLContext *xml = wctx->priv;
1045 XML_INDENT(); printf("<%s ", section);
1046 xml->within_tag = 1;
1049 static void xml_print_section_footer(WriterContext *wctx, const char *section)
1051 XMLContext *xml = wctx->priv;
1053 if (xml->within_tag)
1056 XML_INDENT(); printf("</%s>\n", section);
1060 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1062 XMLContext *xml = wctx->priv;
1066 printf("%s=\"%s\"", key, xml_escape_str(&xml->buf, &xml->buf_size, value, wctx));
1069 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1073 printf("%s=\"%lld\"", key, value);
1076 static void xml_show_tags(WriterContext *wctx, AVDictionary *dict)
1078 XMLContext *xml = wctx->priv;
1079 AVDictionaryEntry *tag = NULL;
1082 xml->indent_level++;
1083 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1085 /* close section tag */
1087 xml->within_tag = 0;
1091 printf("<tag key=\"%s\"",
1092 xml_escape_str(&xml->buf, &xml->buf_size, tag->key, wctx));
1093 printf(" value=\"%s\"/>\n",
1094 xml_escape_str(&xml->buf, &xml->buf_size, tag->value, wctx));
1096 xml->indent_level--;
1099 static Writer xml_writer = {
1101 .priv_size = sizeof(XMLContext),
1103 .uninit = xml_uninit,
1104 .print_header = xml_print_header,
1105 .print_footer = xml_print_footer,
1106 .print_chapter_header = xml_print_chapter_header,
1107 .print_chapter_footer = xml_print_chapter_footer,
1108 .print_section_header = xml_print_section_header,
1109 .print_section_footer = xml_print_section_footer,
1110 .print_integer = xml_print_int,
1111 .print_string = xml_print_str,
1112 .show_tags = xml_show_tags,
1115 static void writer_register_all(void)
1117 static int initialized;
1123 writer_register(&default_writer);
1124 writer_register(&compact_writer);
1125 writer_register(&csv_writer);
1126 writer_register(&json_writer);
1127 writer_register(&xml_writer);
1130 #define print_fmt(k, f, ...) do { \
1131 if (fast_asprintf(&pbuf, f, __VA_ARGS__)) \
1132 writer_print_string(w, k, pbuf.s, 0); \
1135 #define print_fmt_opt(k, f, ...) do { \
1136 if (fast_asprintf(&pbuf, f, __VA_ARGS__)) \
1137 writer_print_string(w, k, pbuf.s, 1); \
1140 #define print_int(k, v) writer_print_integer(w, k, v)
1141 #define print_str(k, v) writer_print_string(w, k, v, 0)
1142 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
1143 #define print_time(k, v, tb) writer_print_time(w, k, v, tb)
1144 #define print_ts(k, v) writer_print_ts(w, k, v)
1145 #define print_val(k, v, u) writer_print_string(w, k, \
1146 value_string(val_str, sizeof(val_str), (struct unit_value){.val.i = v, .unit=u}), 0)
1147 #define print_section_header(s) writer_print_section_header(w, s)
1148 #define print_section_footer(s) writer_print_section_footer(w, s)
1149 #define show_tags(metadata) writer_show_tags(w, metadata)
1151 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1154 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1155 struct print_buf pbuf = {.s = NULL};
1158 print_section_header("packet");
1159 s = av_get_media_type_string(st->codec->codec_type);
1160 if (s) print_str ("codec_type", s);
1161 else print_str_opt("codec_type", "unknown");
1162 print_int("stream_index", pkt->stream_index);
1163 print_ts ("pts", pkt->pts);
1164 print_time("pts_time", pkt->pts, &st->time_base);
1165 print_ts ("dts", pkt->dts);
1166 print_time("dts_time", pkt->dts, &st->time_base);
1167 print_ts ("duration", pkt->duration);
1168 print_time("duration_time", pkt->duration, &st->time_base);
1169 print_val("size", pkt->size, unit_byte_str);
1170 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1171 else print_str_opt("pos", "N/A");
1172 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1173 print_section_footer("packet");
1179 static void show_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1184 av_init_packet(&pkt);
1186 while (!av_read_frame(fmt_ctx, &pkt))
1187 show_packet(w, fmt_ctx, &pkt, i++);
1190 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
1192 AVStream *stream = fmt_ctx->streams[stream_idx];
1193 AVCodecContext *dec_ctx;
1197 AVRational display_aspect_ratio;
1198 struct print_buf pbuf = {.s = NULL};
1200 print_section_header("stream");
1202 print_int("index", stream->index);
1204 if ((dec_ctx = stream->codec)) {
1205 if ((dec = dec_ctx->codec)) {
1206 print_str("codec_name", dec->name);
1207 print_str("codec_long_name", dec->long_name);
1209 print_str_opt("codec_name", "unknown");
1210 print_str_opt("codec_long_name", "unknown");
1213 s = av_get_media_type_string(dec_ctx->codec_type);
1214 if (s) print_str ("codec_type", s);
1215 else print_str_opt("codec_type", "unknown");
1216 print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
1218 /* print AVI/FourCC tag */
1219 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
1220 print_str("codec_tag_string", val_str);
1221 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
1223 switch (dec_ctx->codec_type) {
1224 case AVMEDIA_TYPE_VIDEO:
1225 print_int("width", dec_ctx->width);
1226 print_int("height", dec_ctx->height);
1227 print_int("has_b_frames", dec_ctx->has_b_frames);
1228 if (dec_ctx->sample_aspect_ratio.num) {
1229 print_fmt("sample_aspect_ratio", "%d:%d",
1230 dec_ctx->sample_aspect_ratio.num,
1231 dec_ctx->sample_aspect_ratio.den);
1232 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1233 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
1234 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
1236 print_fmt("display_aspect_ratio", "%d:%d",
1237 display_aspect_ratio.num,
1238 display_aspect_ratio.den);
1240 print_str_opt("sample_aspect_ratio", "N/A");
1241 print_str_opt("display_aspect_ratio", "N/A");
1243 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
1244 if (s) print_str ("pix_fmt", s);
1245 else print_str_opt("pix_fmt", "unknown");
1246 print_int("level", dec_ctx->level);
1247 if (dec_ctx->timecode_frame_start >= 0) {
1248 uint32_t tc = dec_ctx->timecode_frame_start;
1249 print_fmt("timecode", "%02d:%02d:%02d%c%02d",
1250 tc>>19 & 0x1f, // hours
1251 tc>>13 & 0x3f, // minutes
1252 tc>>6 & 0x3f, // seconds
1253 tc & 1<<24 ? ';' : ':', // drop
1254 tc & 0x3f); // frames
1256 print_str_opt("timecode", "N/A");
1260 case AVMEDIA_TYPE_AUDIO:
1261 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
1262 if (s) print_str ("sample_fmt", s);
1263 else print_str_opt("sample_fmt", "unknown");
1264 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
1265 print_int("channels", dec_ctx->channels);
1266 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
1270 print_str_opt("codec_type", "unknown");
1272 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
1273 const AVOption *opt = NULL;
1274 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
1276 if (opt->flags) continue;
1277 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
1278 print_str(opt->name, str);
1284 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
1285 else print_str_opt("id", "N/A");
1286 print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
1287 print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
1288 print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
1289 print_time("start_time", stream->start_time, &stream->time_base);
1290 print_time("duration", stream->duration, &stream->time_base);
1291 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
1292 else print_str_opt("nb_frames", "N/A");
1293 show_tags(stream->metadata);
1295 print_section_footer("stream");
1300 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
1303 for (i = 0; i < fmt_ctx->nb_streams; i++)
1304 show_stream(w, fmt_ctx, i);
1307 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
1310 int64_t size = avio_size(fmt_ctx->pb);
1312 print_section_header("format");
1313 print_str("filename", fmt_ctx->filename);
1314 print_int("nb_streams", fmt_ctx->nb_streams);
1315 print_str("format_name", fmt_ctx->iformat->name);
1316 print_str("format_long_name", fmt_ctx->iformat->long_name);
1317 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
1318 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
1319 if (size >= 0) print_val ("size", size, unit_byte_str);
1320 else print_str_opt("size", "N/A");
1321 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
1322 else print_str_opt("bit_rate", "N/A");
1323 show_tags(fmt_ctx->metadata);
1324 print_section_footer("format");
1328 static void show_error(WriterContext *w, int err)
1331 const char *errbuf_ptr = errbuf;
1333 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
1334 errbuf_ptr = strerror(AVUNERROR(err));
1336 writer_print_chapter_header(w, "error");
1337 print_section_header("error");
1338 print_int("code", err);
1339 print_str("string", errbuf_ptr);
1340 print_section_footer("error");
1341 writer_print_chapter_footer(w, "error");
1344 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
1347 AVFormatContext *fmt_ctx = NULL;
1348 AVDictionaryEntry *t;
1350 if ((err = avformat_open_input(&fmt_ctx, filename, iformat, &format_opts)) < 0) {
1351 print_error(filename, err);
1354 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1355 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
1356 return AVERROR_OPTION_NOT_FOUND;
1360 /* fill the streams in the format context */
1361 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
1362 print_error(filename, err);
1366 av_dump_format(fmt_ctx, 0, filename, 0);
1368 /* bind a decoder to each input stream */
1369 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1370 AVStream *stream = fmt_ctx->streams[i];
1373 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
1374 av_log(NULL, AV_LOG_ERROR, "Unsupported codec with id %d for input stream %d\n",
1375 stream->codec->codec_id, stream->index);
1376 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
1377 av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
1382 *fmt_ctx_ptr = fmt_ctx;
1386 #define PRINT_CHAPTER(name) do { \
1387 if (do_show_ ## name) { \
1388 writer_print_chapter_header(wctx, #name); \
1389 show_ ## name (wctx, fmt_ctx); \
1390 writer_print_chapter_footer(wctx, #name); \
1394 static int probe_file(const char *filename)
1396 AVFormatContext *fmt_ctx;
1400 char *w_name = NULL, *w_args = NULL;
1401 WriterContext *wctx;
1403 writer_register_all();
1406 print_format = av_strdup("default");
1407 w_name = av_strtok(print_format, "=", &buf);
1410 w = writer_get_by_name(w_name);
1412 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
1413 ret = AVERROR(EINVAL);
1417 if ((ret = writer_open(&wctx, w, w_args, NULL)) < 0)
1420 writer_print_header(wctx);
1421 ret = open_input_file(&fmt_ctx, filename);
1423 PRINT_CHAPTER(packets);
1424 PRINT_CHAPTER(streams);
1425 PRINT_CHAPTER(format);
1426 avformat_close_input(&fmt_ctx);
1427 } else if (do_show_error) {
1428 show_error(wctx, ret);
1430 writer_print_footer(wctx);
1431 writer_close(&wctx);
1434 av_freep(&print_format);
1439 static void show_usage(void)
1441 printf("Simple multimedia streams analyzer\n");
1442 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
1446 static int opt_format(const char *opt, const char *arg)
1448 iformat = av_find_input_format(arg);
1450 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
1451 return AVERROR(EINVAL);
1456 static void opt_input_file(void *optctx, const char *arg)
1458 if (input_filename) {
1459 av_log(NULL, AV_LOG_ERROR, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
1460 arg, input_filename);
1463 if (!strcmp(arg, "-"))
1465 input_filename = arg;
1468 static int opt_help(const char *opt, const char *arg)
1470 av_log_set_callback(log_callback_help);
1472 show_help_options(options, "Main options:\n", 0, 0);
1475 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
1480 static int opt_pretty(const char *opt, const char *arg)
1482 show_value_unit = 1;
1483 use_value_prefix = 1;
1484 use_byte_value_binary_prefix = 1;
1485 use_value_sexagesimal_format = 1;
1489 static const OptionDef options[] = {
1490 #include "cmdutils_common_opts.h"
1491 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
1492 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
1493 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
1494 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
1495 "use binary prefixes for byte units" },
1496 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
1497 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
1498 { "pretty", 0, {(void*)&opt_pretty},
1499 "prettify the format of displayed values, make it more human readable" },
1500 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
1501 "set the output printing format (available formats are: default, compact, csv, json, xml)", "format" },
1502 { "show_error", OPT_BOOL, {(void*)&do_show_error} , "show probing error" },
1503 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
1504 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
1505 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
1506 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
1507 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
1508 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
1509 { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
1513 int main(int argc, char **argv)
1517 parse_loglevel(argc, argv, options);
1519 avformat_network_init();
1522 avdevice_register_all();
1525 show_banner(argc, argv, options);
1526 parse_options(NULL, argc, argv, options, opt_input_file);
1528 if (!input_filename) {
1530 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
1531 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
1535 ret = probe_file(input_filename);
1537 avformat_network_deinit();