2 * ffprobe : Simple Media Prober based on the FFmpeg libraries
3 * Copyright (c) 2007-2010 Stefano Sabatini
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavformat/avformat.h"
25 #include "libavcodec/avcodec.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/dict.h"
30 #include "libavdevice/avdevice.h"
33 const char program_name[] = "ffprobe";
34 const int program_birth_year = 2007;
36 static int do_show_format = 0;
37 static int do_show_packets = 0;
38 static int do_show_streams = 0;
40 static int show_value_unit = 0;
41 static int use_value_prefix = 0;
42 static int use_byte_value_binary_prefix = 0;
43 static int use_value_sexagesimal_format = 0;
45 static char *print_format;
47 static const OptionDef options[];
50 static const char *input_filename;
51 static AVInputFormat *iformat = NULL;
53 static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
54 static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
56 static const char *unit_second_str = "s" ;
57 static const char *unit_hertz_str = "Hz" ;
58 static const char *unit_byte_str = "byte" ;
59 static const char *unit_bit_per_second_str = "bit/s";
61 void av_noreturn exit_program(int ret)
67 union { double d; int i; } val;
71 static char *value_string(char *buf, int buf_size, struct unit_value uv)
76 if (uv.unit == unit_second_str) {
83 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
87 mins = (int)secs / 60;
88 secs = secs - mins * 60;
91 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
92 } else if (use_value_prefix) {
93 const char *prefix_string;
96 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
97 index = (int) (log(vald)/log(2)) / 10;
98 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
99 vald /= pow(2, index*10);
100 prefix_string = binary_unit_prefixes[index];
102 index = (int) (log10(vald)) / 3;
103 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
104 vald /= pow(10, index*3);
105 prefix_string = decimal_unit_prefixes[index];
108 if (show_float || vald != (int)vald) l = snprintf(buf, buf_size, "%.3f", vald);
109 else l = snprintf(buf, buf_size, "%d", (int)vald);
110 snprintf(buf+l, buf_size-l, "%s%s%s", prefix_string || show_value_unit ? " " : "",
111 prefix_string, show_value_unit ? uv.unit : "");
115 if (show_float) l = snprintf(buf, buf_size, "%.3f", vald);
116 else l = snprintf(buf, buf_size, "%d", (int)vald);
117 snprintf(buf+l, buf_size-l, "%s%s", show_value_unit ? " " : "",
118 show_value_unit ? uv.unit : "");
126 typedef struct WriterContext WriterContext;
128 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
130 typedef struct Writer {
131 int priv_size; ///< private size for the writer context
134 int (*init) (WriterContext *wctx, const char *args, void *opaque);
135 void (*uninit)(WriterContext *wctx);
137 void (*print_header)(WriterContext *ctx);
138 void (*print_footer)(WriterContext *ctx);
140 void (*print_chapter_header)(WriterContext *wctx, const char *);
141 void (*print_chapter_footer)(WriterContext *wctx, const char *);
142 void (*print_section_header)(WriterContext *wctx, const char *);
143 void (*print_section_footer)(WriterContext *wctx, const char *);
144 void (*print_integer) (WriterContext *wctx, const char *, int);
145 void (*print_string) (WriterContext *wctx, const char *, const char *);
146 void (*show_tags) (WriterContext *wctx, AVDictionary *dict);
147 int flags; ///< a combination or WRITER_FLAG_*
150 struct WriterContext {
151 const AVClass *class; ///< class of the writer
152 const Writer *writer; ///< the Writer of which this is an instance
153 char *name; ///< name of this writer instance
154 void *priv; ///< private data for use by the filter
155 unsigned int nb_item; ///< number of the item printed in the given section, starting at 0
156 unsigned int nb_section; ///< number of the section printed in the given section sequence, starting at 0
157 unsigned int nb_chapter; ///< number of the chapter, starting at 0
160 static const char *writer_get_name(void *p)
162 WriterContext *wctx = p;
163 return wctx->writer->name;
166 static const AVClass writer_class = {
170 LIBAVUTIL_VERSION_INT,
173 static void writer_close(WriterContext **wctx)
175 if (*wctx && (*wctx)->writer->uninit)
176 (*wctx)->writer->uninit(*wctx);
178 av_freep(&((*wctx)->priv));
182 static int writer_open(WriterContext **wctx, const Writer *writer,
183 const char *args, void *opaque)
187 if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
188 ret = AVERROR(ENOMEM);
192 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
193 ret = AVERROR(ENOMEM);
197 (*wctx)->class = &writer_class;
198 (*wctx)->writer = writer;
199 if ((*wctx)->writer->init)
200 ret = (*wctx)->writer->init(*wctx, args, opaque);
211 static inline void writer_print_header(WriterContext *wctx)
213 if (wctx->writer->print_header)
214 wctx->writer->print_header(wctx);
215 wctx->nb_chapter = 0;
218 static inline void writer_print_footer(WriterContext *wctx)
220 if (wctx->writer->print_footer)
221 wctx->writer->print_footer(wctx);
224 static inline void writer_print_chapter_header(WriterContext *wctx,
227 if (wctx->writer->print_chapter_header)
228 wctx->writer->print_chapter_header(wctx, header);
229 wctx->nb_section = 0;
232 static inline void writer_print_chapter_footer(WriterContext *wctx,
235 if (wctx->writer->print_chapter_footer)
236 wctx->writer->print_chapter_footer(wctx, footer);
240 static inline void writer_print_section_header(WriterContext *wctx,
243 if (wctx->writer->print_section_header)
244 wctx->writer->print_section_header(wctx, header);
248 static inline void writer_print_section_footer(WriterContext *wctx,
251 if (wctx->writer->print_section_footer)
252 wctx->writer->print_section_footer(wctx, footer);
256 static inline void writer_print_integer(WriterContext *wctx,
257 const char *key, int val)
259 wctx->writer->print_integer(wctx, key, val);
263 static inline void writer_print_string(WriterContext *wctx,
264 const char *key, const char *val, int opt)
266 if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
268 wctx->writer->print_string(wctx, key, val);
272 static void writer_print_time(WriterContext *wctx, const char *key,
273 int64_t ts, const AVRational *time_base)
277 if (ts == AV_NOPTS_VALUE) {
278 writer_print_string(wctx, key, "N/A", 1);
280 double d = ts * av_q2d(*time_base);
281 value_string(buf, sizeof(buf), (struct unit_value){.val.d=d, .unit=unit_second_str});
282 writer_print_string(wctx, key, buf, 0);
286 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts)
290 if (ts == AV_NOPTS_VALUE) {
291 writer_print_string(wctx, key, "N/A", 1);
293 snprintf(buf, sizeof(buf), "%"PRId64, ts);
294 writer_print_string(wctx, key, buf, 0);
298 static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
300 wctx->writer->show_tags(wctx, dict);
303 #define MAX_REGISTERED_WRITERS_NB 64
305 static Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
307 static int writer_register(Writer *writer)
309 static int next_registered_writer_idx = 0;
311 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
312 return AVERROR(ENOMEM);
314 registered_writers[next_registered_writer_idx++] = writer;
318 static Writer *writer_get_by_name(const char *name)
322 for (i = 0; registered_writers[i]; i++)
323 if (!strcmp(registered_writers[i]->name, name))
324 return registered_writers[i];
336 static char *fast_asprintf(struct print_buf *pbuf, const char *fmt, ...)
342 len = vsnprintf(NULL, 0, fmt, va);
347 if (pbuf->len < len) {
348 char *p = av_realloc(pbuf->s, len + 1);
356 len = vsnprintf(pbuf->s, len + 1, fmt, va);
368 #define ESCAPE_INIT_BUF_SIZE 256
370 #define ESCAPE_CHECK_SIZE(src, size, max_size) \
371 if (size > max_size) { \
373 snprintf(buf, sizeof(buf), "%s", src); \
374 av_log(log_ctx, AV_LOG_WARNING, \
375 "String '%s...' with is too big\n", buf); \
376 return "FFPROBE_TOO_BIG_STRING"; \
379 #define ESCAPE_REALLOC_BUF(dst_size_p, dst_p, src, size) \
380 if (*dst_size_p < size) { \
381 char *q = av_realloc(*dst_p, size); \
384 snprintf(buf, sizeof(buf), "%s", src); \
385 av_log(log_ctx, AV_LOG_WARNING, \
386 "String '%s...' could not be escaped\n", buf); \
387 return "FFPROBE_THIS_STRING_COULD_NOT_BE_ESCAPED"; \
389 *dst_size_p = size; \
397 static void default_print_footer(WriterContext *wctx)
402 static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
404 if (wctx->nb_chapter)
408 /* lame uppercasing routine, assumes the string is lower case ASCII */
409 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
412 for (i = 0; src[i] && i < dst_size-1; i++)
418 static void default_print_section_header(WriterContext *wctx, const char *section)
422 if (wctx->nb_section)
424 printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
427 static void default_print_section_footer(WriterContext *wctx, const char *section)
431 printf("[/%s]", upcase_string(buf, sizeof(buf), section));
434 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
436 printf("%s=%s\n", key, value);
439 static void default_print_int(WriterContext *wctx, const char *key, int value)
441 printf("%s=%d\n", key, value);
444 static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
446 AVDictionaryEntry *tag = NULL;
447 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
449 writer_print_string(wctx, tag->key, tag->value, 0);
453 static Writer default_writer = {
455 .print_footer = default_print_footer,
456 .print_chapter_header = default_print_chapter_header,
457 .print_section_header = default_print_section_header,
458 .print_section_footer = default_print_section_footer,
459 .print_integer = default_print_int,
460 .print_string = default_print_str,
461 .show_tags = default_show_tags,
462 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
468 * Escape \n, \r, \\ and sep characters contained in s, and print the
471 static const char *c_escape_str(char **dst, size_t *dst_size,
472 const char *src, const char sep, void *log_ctx)
478 /* precompute size */
479 for (p = src; *p; p++, size++) {
480 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-2);
481 if (*p == '\n' || *p == '\r' || *p == '\\')
485 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
488 for (p = src; *p; p++) {
490 case '\n': *q++ = '\\'; *q++ = 'n'; break;
491 case '\r': *q++ = '\\'; *q++ = 'r'; break;
492 case '\\': *q++ = '\\'; *q++ = '\\'; break;
504 * Quote fields containing special characters, check RFC4180.
506 static const char *csv_escape_str(char **dst, size_t *dst_size,
507 const char *src, const char sep, void *log_ctx)
514 /* precompute size */
515 for (p = src; *p; p++, size++) {
516 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-4);
517 if (*p == '"' || *p == sep || *p == '\n' || *p == '\r')
526 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
544 static const char *none_escape_str(char **dst, size_t *dst_size,
545 const char *src, const char sep, void *log_ctx)
550 typedef struct CompactContext {
551 const AVClass *class;
557 char *escape_mode_str;
558 const char * (*escape_str)(char **dst, size_t *dst_size,
559 const char *src, const char sep, void *log_ctx);
562 #define OFFSET(x) offsetof(CompactContext, x)
564 static const AVOption compact_options[]= {
565 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
566 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
567 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
568 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
569 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
570 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
574 static const char *compact_get_name(void *ctx)
579 static const AVClass compact_class = {
585 static av_cold int compact_init(WriterContext *wctx, const char *args, void *opaque)
587 CompactContext *compact = wctx->priv;
590 compact->class = &compact_class;
591 av_opt_set_defaults(compact);
594 (err = (av_set_options_string(compact, args, "=", ":"))) < 0) {
595 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
598 if (strlen(compact->item_sep_str) != 1) {
599 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
600 compact->item_sep_str);
601 return AVERROR(EINVAL);
603 compact->item_sep = compact->item_sep_str[0];
605 compact->buf_size = ESCAPE_INIT_BUF_SIZE;
606 if (!(compact->buf = av_malloc(compact->buf_size)))
607 return AVERROR(ENOMEM);
609 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
610 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
611 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
613 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
614 return AVERROR(EINVAL);
620 static av_cold void compact_uninit(WriterContext *wctx)
622 CompactContext *compact = wctx->priv;
624 av_freep(&compact->item_sep_str);
625 av_freep(&compact->buf);
626 av_freep(&compact->escape_mode_str);
629 static void compact_print_section_header(WriterContext *wctx, const char *section)
631 CompactContext *compact = wctx->priv;
633 printf("%s%c", section, compact->item_sep);
636 static void compact_print_section_footer(WriterContext *wctx, const char *section)
641 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
643 CompactContext *compact = wctx->priv;
645 if (wctx->nb_item) printf("%c", compact->item_sep);
648 printf("%s", compact->escape_str(&compact->buf, &compact->buf_size,
649 value, compact->item_sep, wctx));
652 static void compact_print_int(WriterContext *wctx, const char *key, int value)
654 CompactContext *compact = wctx->priv;
656 if (wctx->nb_item) printf("%c", compact->item_sep);
662 static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
664 CompactContext *compact = wctx->priv;
665 AVDictionaryEntry *tag = NULL;
667 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
668 if (wctx->nb_item) printf("%c", compact->item_sep);
670 printf("tag:%s=", compact->escape_str(&compact->buf, &compact->buf_size,
671 tag->key, compact->item_sep, wctx));
672 printf("%s", compact->escape_str(&compact->buf, &compact->buf_size,
673 tag->value, compact->item_sep, wctx));
677 static Writer compact_writer = {
679 .priv_size = sizeof(CompactContext),
681 .init = compact_init,
682 .uninit = compact_uninit,
683 .print_section_header = compact_print_section_header,
684 .print_section_footer = compact_print_section_footer,
685 .print_integer = compact_print_int,
686 .print_string = compact_print_str,
687 .show_tags = compact_show_tags,
688 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS
694 int multiple_entries; ///< tells if the given chapter requires multiple entries
699 static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
701 JSONContext *json = wctx->priv;
703 json->buf_size = ESCAPE_INIT_BUF_SIZE;
704 if (!(json->buf = av_malloc(json->buf_size)))
705 return AVERROR(ENOMEM);
710 static av_cold void json_uninit(WriterContext *wctx)
712 JSONContext *json = wctx->priv;
713 av_freep(&json->buf);
716 static const char *json_escape_str(char **dst, size_t *dst_size, const char *src,
719 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
720 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
725 // compute the length of the escaped string
726 for (p = src; *p; p++) {
727 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-6);
728 if (strchr(json_escape, *p)) size += 2; // simple escape
729 else if ((unsigned char)*p < 32) size += 6; // handle non-printable chars
730 else size += 1; // char copy
732 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
735 for (p = src; *p; p++) {
736 char *s = strchr(json_escape, *p);
739 *q++ = json_subst[s - json_escape];
740 } else if ((unsigned char)*p < 32) {
741 snprintf(q, 7, "\\u00%02x", *p & 0xff);
751 static void json_print_header(WriterContext *wctx)
756 static void json_print_footer(WriterContext *wctx)
761 static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
763 JSONContext *json = wctx->priv;
765 if (wctx->nb_chapter)
767 json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "streams");
768 printf("\n \"%s\":%s", json_escape_str(&json->buf, &json->buf_size, chapter, wctx),
769 json->multiple_entries ? " [" : " ");
772 static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
774 JSONContext *json = wctx->priv;
776 if (json->multiple_entries)
780 static void json_print_section_header(WriterContext *wctx, const char *section)
782 if (wctx->nb_section) printf(",");
786 static void json_print_section_footer(WriterContext *wctx, const char *section)
791 static inline void json_print_item_str(WriterContext *wctx,
792 const char *key, const char *value,
795 JSONContext *json = wctx->priv;
797 printf("%s\"%s\":", indent, json_escape_str(&json->buf, &json->buf_size, key, wctx));
798 printf(" \"%s\"", json_escape_str(&json->buf, &json->buf_size, value, wctx));
803 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
805 if (wctx->nb_item) printf(",\n");
806 json_print_item_str(wctx, key, value, INDENT);
809 static void json_print_int(WriterContext *wctx, const char *key, int value)
811 JSONContext *json = wctx->priv;
813 if (wctx->nb_item) printf(",\n");
814 printf(INDENT "\"%s\": %d",
815 json_escape_str(&json->buf, &json->buf_size, key, wctx), value);
818 static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
820 AVDictionaryEntry *tag = NULL;
824 printf(",\n" INDENT "\"tags\": {\n");
825 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
826 if (is_first) is_first = 0;
828 json_print_item_str(wctx, tag->key, tag->value, INDENT INDENT);
833 static Writer json_writer = {
835 .priv_size = sizeof(JSONContext),
838 .uninit = json_uninit,
839 .print_header = json_print_header,
840 .print_footer = json_print_footer,
841 .print_chapter_header = json_print_chapter_header,
842 .print_chapter_footer = json_print_chapter_footer,
843 .print_section_header = json_print_section_header,
844 .print_section_footer = json_print_section_footer,
845 .print_integer = json_print_int,
846 .print_string = json_print_str,
847 .show_tags = json_show_tags,
850 static void writer_register_all(void)
852 static int initialized;
858 writer_register(&default_writer);
859 writer_register(&compact_writer);
860 writer_register(&json_writer);
863 #define print_fmt(k, f, ...) do { \
864 if (fast_asprintf(&pbuf, f, __VA_ARGS__)) \
865 writer_print_string(w, k, pbuf.s, 0); \
868 #define print_fmt_opt(k, f, ...) do { \
869 if (fast_asprintf(&pbuf, f, __VA_ARGS__)) \
870 writer_print_string(w, k, pbuf.s, 1); \
873 #define print_int(k, v) writer_print_integer(w, k, v)
874 #define print_str(k, v) writer_print_string(w, k, v, 0)
875 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
876 #define print_time(k, v, tb) writer_print_time(w, k, v, tb)
877 #define print_ts(k, v) writer_print_ts(w, k, v)
878 #define print_val(k, v, u) writer_print_string(w, k, \
879 value_string(val_str, sizeof(val_str), (struct unit_value){.val.i = v, .unit=u}), 1)
880 #define print_section_header(s) writer_print_section_header(w, s)
881 #define print_section_footer(s) writer_print_section_footer(w, s)
882 #define show_tags(metadata) writer_show_tags(w, metadata)
884 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
887 AVStream *st = fmt_ctx->streams[pkt->stream_index];
888 struct print_buf pbuf = {.s = NULL};
891 print_section_header("packet");
892 s = av_get_media_type_string(st->codec->codec_type);
893 if (s) print_str ("codec_type", s);
894 else print_str_opt("codec_type", "unknown");
895 print_int("stream_index", pkt->stream_index);
896 print_ts ("pts", pkt->pts);
897 print_time("pts_time", pkt->pts, &st->time_base);
898 print_ts ("dts", pkt->dts);
899 print_time("dts_time", pkt->dts, &st->time_base);
900 print_ts ("duration", pkt->duration);
901 print_time("duration_time", pkt->duration, &st->time_base);
902 print_val("size", pkt->size, unit_byte_str);
903 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
904 else print_str_opt("pos", "N/A");
905 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
906 print_section_footer("packet");
912 static void show_packets(WriterContext *w, AVFormatContext *fmt_ctx)
917 av_init_packet(&pkt);
919 while (!av_read_frame(fmt_ctx, &pkt))
920 show_packet(w, fmt_ctx, &pkt, i++);
923 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
925 AVStream *stream = fmt_ctx->streams[stream_idx];
926 AVCodecContext *dec_ctx;
930 AVRational display_aspect_ratio;
931 struct print_buf pbuf = {.s = NULL};
933 print_section_header("stream");
935 print_int("index", stream->index);
937 if ((dec_ctx = stream->codec)) {
938 if ((dec = dec_ctx->codec)) {
939 print_str("codec_name", dec->name);
940 print_str("codec_long_name", dec->long_name);
942 print_str_opt("codec_name", "unknown");
943 print_str_opt("codec_long_name", "unknown");
946 s = av_get_media_type_string(dec_ctx->codec_type);
947 if (s) print_str ("codec_type", s);
948 else print_str_opt("codec_type", "unknown");
949 print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
951 /* print AVI/FourCC tag */
952 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
953 print_str("codec_tag_string", val_str);
954 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
956 switch (dec_ctx->codec_type) {
957 case AVMEDIA_TYPE_VIDEO:
958 print_int("width", dec_ctx->width);
959 print_int("height", dec_ctx->height);
960 print_int("has_b_frames", dec_ctx->has_b_frames);
961 if (dec_ctx->sample_aspect_ratio.num) {
962 print_fmt("sample_aspect_ratio", "%d:%d",
963 dec_ctx->sample_aspect_ratio.num,
964 dec_ctx->sample_aspect_ratio.den);
965 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
966 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
967 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
969 print_fmt("display_aspect_ratio", "%d:%d",
970 display_aspect_ratio.num,
971 display_aspect_ratio.den);
973 print_str_opt("sample_aspect_ratio", "N/A");
974 print_str_opt("display_aspect_ratio", "N/A");
976 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
977 if (s) print_str ("pix_fmt", s);
978 else print_str_opt("pix_fmt", "unknown");
979 print_int("level", dec_ctx->level);
982 case AVMEDIA_TYPE_AUDIO:
983 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
984 if (s) print_str ("sample_fmt", s);
985 else print_str_opt("sample_fmt", "unknown");
986 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
987 print_int("channels", dec_ctx->channels);
988 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
992 print_str_opt("codec_type", "unknown");
994 if (dec_ctx->codec && dec_ctx->codec->priv_class) {
995 const AVOption *opt = NULL;
996 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
998 if (opt->flags) continue;
999 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
1000 print_str(opt->name, str);
1006 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
1007 else print_str_opt("id", "N/A");
1008 print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
1009 print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
1010 print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
1011 print_time("start_time", stream->start_time, &stream->time_base);
1012 print_time("duration", stream->duration, &stream->time_base);
1013 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
1014 else print_str_opt("nb_frames", "N/A");
1015 show_tags(stream->metadata);
1017 print_section_footer("stream");
1022 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
1025 for (i = 0; i < fmt_ctx->nb_streams; i++)
1026 show_stream(w, fmt_ctx, i);
1029 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
1032 int64_t size = avio_size(fmt_ctx->pb);
1033 struct print_buf pbuf = {.s = NULL};
1035 print_section_header("format");
1036 print_str("filename", fmt_ctx->filename);
1037 print_int("nb_streams", fmt_ctx->nb_streams);
1038 print_str("format_name", fmt_ctx->iformat->name);
1039 print_str("format_long_name", fmt_ctx->iformat->long_name);
1040 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
1041 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
1042 if (size >= 0) print_val ("size", size, unit_byte_str);
1043 else print_str_opt("size", "N/A");
1044 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
1045 else print_str_opt("bit_rate", "N/A");
1046 show_tags(fmt_ctx->metadata);
1047 print_section_footer("format");
1052 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
1055 AVFormatContext *fmt_ctx = NULL;
1056 AVDictionaryEntry *t;
1058 if ((err = avformat_open_input(&fmt_ctx, filename, iformat, &format_opts)) < 0) {
1059 print_error(filename, err);
1062 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1063 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
1064 return AVERROR_OPTION_NOT_FOUND;
1068 /* fill the streams in the format context */
1069 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
1070 print_error(filename, err);
1074 av_dump_format(fmt_ctx, 0, filename, 0);
1076 /* bind a decoder to each input stream */
1077 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1078 AVStream *stream = fmt_ctx->streams[i];
1081 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
1082 fprintf(stderr, "Unsupported codec with id %d for input stream %d\n",
1083 stream->codec->codec_id, stream->index);
1084 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
1085 fprintf(stderr, "Error while opening codec for input stream %d\n",
1090 *fmt_ctx_ptr = fmt_ctx;
1094 #define PRINT_CHAPTER(name) do { \
1095 if (do_show_ ## name) { \
1096 writer_print_chapter_header(wctx, #name); \
1097 show_ ## name (wctx, fmt_ctx); \
1098 writer_print_chapter_footer(wctx, #name); \
1102 static int probe_file(const char *filename)
1104 AVFormatContext *fmt_ctx;
1108 char *w_name = NULL, *w_args = NULL;
1109 WriterContext *wctx;
1111 writer_register_all();
1114 print_format = av_strdup("default");
1115 w_name = av_strtok(print_format, "=", &buf);
1118 w = writer_get_by_name(w_name);
1120 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
1121 ret = AVERROR(EINVAL);
1125 if ((ret = writer_open(&wctx, w, w_args, NULL)) < 0)
1127 if ((ret = open_input_file(&fmt_ctx, filename)))
1130 writer_print_header(wctx);
1131 PRINT_CHAPTER(packets);
1132 PRINT_CHAPTER(streams);
1133 PRINT_CHAPTER(format);
1134 writer_print_footer(wctx);
1136 av_close_input_file(fmt_ctx);
1137 writer_close(&wctx);
1140 av_freep(&print_format);
1145 static void show_usage(void)
1147 printf("Simple multimedia streams analyzer\n");
1148 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
1152 static int opt_format(const char *opt, const char *arg)
1154 iformat = av_find_input_format(arg);
1156 fprintf(stderr, "Unknown input format: %s\n", arg);
1157 return AVERROR(EINVAL);
1162 static void opt_input_file(void *optctx, const char *arg)
1164 if (input_filename) {
1165 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
1166 arg, input_filename);
1169 if (!strcmp(arg, "-"))
1171 input_filename = arg;
1174 static int opt_help(const char *opt, const char *arg)
1176 av_log_set_callback(log_callback_help);
1178 show_help_options(options, "Main options:\n", 0, 0);
1181 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
1186 static int opt_pretty(const char *opt, const char *arg)
1188 show_value_unit = 1;
1189 use_value_prefix = 1;
1190 use_byte_value_binary_prefix = 1;
1191 use_value_sexagesimal_format = 1;
1195 static const OptionDef options[] = {
1196 #include "cmdutils_common_opts.h"
1197 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
1198 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
1199 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
1200 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
1201 "use binary prefixes for byte units" },
1202 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
1203 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
1204 { "pretty", 0, {(void*)&opt_pretty},
1205 "prettify the format of displayed values, make it more human readable" },
1206 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format}, "set the output printing format (available formats are: default, compact, json)", "format" },
1207 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
1208 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
1209 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
1210 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
1211 { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
1215 int main(int argc, char **argv)
1219 parse_loglevel(argc, argv, options);
1221 avformat_network_init();
1224 avdevice_register_all();
1228 parse_options(NULL, argc, argv, options, opt_input_file);
1230 if (!input_filename) {
1232 fprintf(stderr, "You have to specify one input file.\n");
1233 fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
1237 ret = probe_file(input_filename);
1239 avformat_network_deinit();