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
29 #include "libavformat/avformat.h"
30 #include "libavcodec/avcodec.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/bprint.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/dict.h"
36 #include "libavutil/libm.h"
37 #include "libavutil/timecode.h"
38 #include "libavdevice/avdevice.h"
39 #include "libswscale/swscale.h"
40 #include "libswresample/swresample.h"
41 #include "libpostproc/postprocess.h"
44 const char program_name[] = "ffprobe";
45 const int program_birth_year = 2007;
47 static int do_bitexact = 0;
48 static int do_count_frames = 0;
49 static int do_count_packets = 0;
50 static int do_read_frames = 0;
51 static int do_read_packets = 0;
52 static int do_show_error = 0;
53 static int do_show_format = 0;
54 static int do_show_frames = 0;
55 static AVDictionary *fmt_entries_to_show = NULL;
56 static int do_show_packets = 0;
57 static int do_show_streams = 0;
58 static int do_show_data = 0;
59 static int do_show_program_version = 0;
60 static int do_show_library_versions = 0;
62 static int show_value_unit = 0;
63 static int use_value_prefix = 0;
64 static int use_byte_value_binary_prefix = 0;
65 static int use_value_sexagesimal_format = 0;
66 static int show_private_data = 1;
68 static char *print_format;
70 static const OptionDef *options;
73 static const char *input_filename;
74 static AVInputFormat *iformat = NULL;
76 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
77 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
79 static const char unit_second_str[] = "s" ;
80 static const char unit_hertz_str[] = "Hz" ;
81 static const char unit_byte_str[] = "byte" ;
82 static const char unit_bit_per_second_str[] = "bit/s";
83 static uint64_t *nb_streams_packets;
84 static uint64_t *nb_streams_frames;
86 void av_noreturn exit_program(int ret)
88 av_dict_free(&fmt_entries_to_show);
93 union { double d; long long int i; } val;
97 static char *value_string(char *buf, int buf_size, struct unit_value uv)
103 if (uv.unit == unit_second_str) {
107 vald = vali = uv.val.i;
110 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
114 mins = (int)secs / 60;
115 secs = secs - mins * 60;
118 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
120 const char *prefix_string = "";
122 if (use_value_prefix && vald > 1) {
125 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
126 index = (long long int) (log2(vald)) / 10;
127 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
128 vald /= exp2(index * 10);
129 prefix_string = binary_unit_prefixes[index];
131 index = (long long int) (log10(vald)) / 3;
132 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
133 vald /= pow(10, index * 3);
134 prefix_string = decimal_unit_prefixes[index];
138 if (show_float || (use_value_prefix && vald != (long long int)vald))
139 snprintf(buf, buf_size, "%f", vald);
141 snprintf(buf, buf_size, "%lld", vali);
142 av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
143 prefix_string, show_value_unit ? uv.unit : "");
151 typedef struct WriterContext WriterContext;
153 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
154 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
156 typedef struct Writer {
157 const AVClass *priv_class; ///< private class of the writer, if any
158 int priv_size; ///< private size for the writer context
161 int (*init) (WriterContext *wctx, const char *args, void *opaque);
162 void (*uninit)(WriterContext *wctx);
164 void (*print_header)(WriterContext *ctx);
165 void (*print_footer)(WriterContext *ctx);
167 void (*print_chapter_header)(WriterContext *wctx, const char *);
168 void (*print_chapter_footer)(WriterContext *wctx, const char *);
169 void (*print_section_header)(WriterContext *wctx, const char *);
170 void (*print_section_footer)(WriterContext *wctx, const char *);
171 void (*print_integer) (WriterContext *wctx, const char *, long long int);
172 void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
173 void (*print_string) (WriterContext *wctx, const char *, const char *);
174 void (*show_tags) (WriterContext *wctx, AVDictionary *dict);
175 int flags; ///< a combination or WRITER_FLAG_*
178 struct WriterContext {
179 const AVClass *class; ///< class of the writer
180 const Writer *writer; ///< the Writer of which this is an instance
181 char *name; ///< name of this writer instance
182 void *priv; ///< private data for use by the filter
183 unsigned int nb_item; ///< number of the item printed in the given section, starting at 0
184 unsigned int nb_section; ///< number of the section printed in the given section sequence, starting at 0
185 unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
186 unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
187 unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
188 unsigned int nb_chapter; ///< number of the chapter, starting at 0
190 int multiple_sections; ///< tells if the current chapter can contain multiple sections
191 int is_fmt_chapter; ///< tells if the current chapter is "format", required by the print_format_entry option
192 int is_packets_and_frames; ///< tells if the current section is "packets_and_frames"
195 static const char *writer_get_name(void *p)
197 WriterContext *wctx = p;
198 return wctx->writer->name;
201 static const AVClass writer_class = {
205 LIBAVUTIL_VERSION_INT,
208 static void writer_close(WriterContext **wctx)
213 if ((*wctx)->writer->uninit)
214 (*wctx)->writer->uninit(*wctx);
215 if ((*wctx)->writer->priv_class)
216 av_opt_free((*wctx)->priv);
217 av_freep(&((*wctx)->priv));
221 static int writer_open(WriterContext **wctx, const Writer *writer,
222 const char *args, void *opaque)
226 if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
227 ret = AVERROR(ENOMEM);
231 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
232 ret = AVERROR(ENOMEM);
236 (*wctx)->class = &writer_class;
237 (*wctx)->writer = writer;
239 if (writer->priv_class) {
240 void *priv_ctx = (*wctx)->priv;
241 *((const AVClass **)priv_ctx) = writer->priv_class;
242 av_opt_set_defaults(priv_ctx);
245 (ret = av_set_options_string(priv_ctx, args, "=", ":")) < 0)
248 if ((*wctx)->writer->init)
249 ret = (*wctx)->writer->init(*wctx, args, opaque);
260 static inline void writer_print_header(WriterContext *wctx)
262 if (wctx->writer->print_header)
263 wctx->writer->print_header(wctx);
264 wctx->nb_chapter = 0;
267 static inline void writer_print_footer(WriterContext *wctx)
269 if (wctx->writer->print_footer)
270 wctx->writer->print_footer(wctx);
273 static inline void writer_print_chapter_header(WriterContext *wctx,
277 wctx->nb_section_packet = wctx->nb_section_frame =
278 wctx->nb_section_packet_frame = 0;
279 wctx->is_packets_and_frames = !strcmp(chapter, "packets_and_frames");
280 wctx->multiple_sections = !strcmp(chapter, "packets") || !strcmp(chapter, "frames" ) ||
281 wctx->is_packets_and_frames ||
282 !strcmp(chapter, "streams") || !strcmp(chapter, "library_versions");
283 wctx->is_fmt_chapter = !strcmp(chapter, "format");
285 if (wctx->writer->print_chapter_header)
286 wctx->writer->print_chapter_header(wctx, chapter);
289 static inline void writer_print_chapter_footer(WriterContext *wctx,
292 if (wctx->writer->print_chapter_footer)
293 wctx->writer->print_chapter_footer(wctx, chapter);
297 static inline void writer_print_section_header(WriterContext *wctx,
300 if (wctx->is_packets_and_frames)
301 wctx->nb_section_packet_frame = !strcmp(section, "packet") ? wctx->nb_section_packet
302 : wctx->nb_section_frame;
303 if (wctx->writer->print_section_header)
304 wctx->writer->print_section_header(wctx, section);
308 static inline void writer_print_section_footer(WriterContext *wctx,
311 if (wctx->writer->print_section_footer)
312 wctx->writer->print_section_footer(wctx, section);
313 if (wctx->is_packets_and_frames) {
314 if (!strcmp(section, "packet")) wctx->nb_section_packet++;
315 else wctx->nb_section_frame++;
320 static inline void writer_print_integer(WriterContext *wctx,
321 const char *key, long long int val)
323 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
324 wctx->writer->print_integer(wctx, key, val);
329 static inline void writer_print_rational(WriterContext *wctx,
330 const char *key, AVRational q, char sep)
333 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
334 av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
335 wctx->writer->print_string(wctx, key, buf.str);
339 static inline void writer_print_string(WriterContext *wctx,
340 const char *key, const char *val, int opt)
342 if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
344 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
345 wctx->writer->print_string(wctx, key, val);
350 static void writer_print_time(WriterContext *wctx, const char *key,
351 int64_t ts, const AVRational *time_base, int is_duration)
355 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
356 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
357 writer_print_string(wctx, key, "N/A", 1);
359 double d = ts * av_q2d(*time_base);
360 struct unit_value uv;
362 uv.unit = unit_second_str;
363 value_string(buf, sizeof(buf), uv);
364 writer_print_string(wctx, key, buf, 0);
369 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
371 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
372 writer_print_string(wctx, key, "N/A", 1);
374 writer_print_integer(wctx, key, ts);
378 static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
380 wctx->writer->show_tags(wctx, dict);
383 static void writer_print_data(WriterContext *wctx, const char *name,
384 uint8_t *data, int size)
387 int offset = 0, l, i;
389 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
390 av_bprintf(&bp, "\n");
392 av_bprintf(&bp, "%08x: ", offset);
394 for (i = 0; i < l; i++) {
395 av_bprintf(&bp, "%02x", data[i]);
397 av_bprintf(&bp, " ");
399 av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
400 for (i = 0; i < l; i++)
401 av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
402 av_bprintf(&bp, "\n");
407 writer_print_string(wctx, name, bp.str, 0);
408 av_bprint_finalize(&bp, NULL);
411 #define MAX_REGISTERED_WRITERS_NB 64
413 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
415 static int writer_register(const Writer *writer)
417 static int next_registered_writer_idx = 0;
419 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
420 return AVERROR(ENOMEM);
422 registered_writers[next_registered_writer_idx++] = writer;
426 static const Writer *writer_get_by_name(const char *name)
430 for (i = 0; registered_writers[i]; i++)
431 if (!strcmp(registered_writers[i]->name, name))
432 return registered_writers[i];
440 #define DEFINE_WRITER_CLASS(name) \
441 static const char *name##_get_name(void *ctx) \
445 static const AVClass name##_class = { \
453 typedef struct DefaultContext {
454 const AVClass *class;
456 int noprint_wrappers;
459 #define OFFSET(x) offsetof(DefaultContext, x)
461 static const AVOption default_options[] = {
462 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
463 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
464 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
465 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
469 DEFINE_WRITER_CLASS(default);
471 /* lame uppercasing routine, assumes the string is lower case ASCII */
472 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
475 for (i = 0; src[i] && i < dst_size-1; i++)
476 dst[i] = av_toupper(src[i]);
481 static void default_print_section_header(WriterContext *wctx, const char *section)
483 DefaultContext *def = wctx->priv;
486 if (!def->noprint_wrappers)
487 printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
490 static void default_print_section_footer(WriterContext *wctx, const char *section)
492 DefaultContext *def = wctx->priv;
495 if (!def->noprint_wrappers)
496 printf("[/%s]\n", upcase_string(buf, sizeof(buf), section));
499 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
501 DefaultContext *def = wctx->priv;
504 printf("%s\n", value);
507 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
509 DefaultContext *def = wctx->priv;
513 printf("%lld\n", value);
516 static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
518 AVDictionaryEntry *tag = NULL;
519 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
520 if (!fmt_entries_to_show || (tag->key && av_dict_get(fmt_entries_to_show, tag->key, NULL, 0)))
522 writer_print_string(wctx, tag->key, tag->value, 0);
526 static const Writer default_writer = {
528 .priv_size = sizeof(DefaultContext),
529 .print_section_header = default_print_section_header,
530 .print_section_footer = default_print_section_footer,
531 .print_integer = default_print_int,
532 .print_string = default_print_str,
533 .show_tags = default_show_tags,
534 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
535 .priv_class = &default_class,
541 * Apply C-language-like string escaping.
543 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
547 for (p = src; *p; p++) {
549 case '\b': av_bprintf(dst, "%s", "\\b"); break;
550 case '\f': av_bprintf(dst, "%s", "\\f"); break;
551 case '\n': av_bprintf(dst, "%s", "\\n"); break;
552 case '\r': av_bprintf(dst, "%s", "\\r"); break;
553 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
556 av_bprint_chars(dst, '\\', 1);
557 av_bprint_chars(dst, *p, 1);
564 * Quote fields containing special characters, check RFC4180.
566 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
571 /* check if input needs quoting */
572 for (p = src; *p; p++)
573 if (*p == '"' || *p == sep || *p == '\n' || *p == '\r')
577 av_bprint_chars(dst, '\"', 1);
579 for (p = src; *p; p++) {
581 av_bprint_chars(dst, '\"', 1);
582 av_bprint_chars(dst, *p, 1);
585 av_bprint_chars(dst, '\"', 1);
589 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
594 typedef struct CompactContext {
595 const AVClass *class;
600 char *escape_mode_str;
601 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
605 #define OFFSET(x) offsetof(CompactContext, x)
607 static const AVOption compact_options[]= {
608 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
609 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
610 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
611 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
612 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
613 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
614 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
615 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
619 DEFINE_WRITER_CLASS(compact);
621 static av_cold int compact_init(WriterContext *wctx, const char *args, void *opaque)
623 CompactContext *compact = wctx->priv;
625 if (strlen(compact->item_sep_str) != 1) {
626 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
627 compact->item_sep_str);
628 return AVERROR(EINVAL);
630 compact->item_sep = compact->item_sep_str[0];
632 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
633 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
634 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
636 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
637 return AVERROR(EINVAL);
643 static void compact_print_section_header(WriterContext *wctx, const char *section)
645 CompactContext *compact = wctx->priv;
647 if (compact->print_section)
648 printf("%s%c", section, compact->item_sep);
651 static void compact_print_section_footer(WriterContext *wctx, const char *section)
656 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
658 CompactContext *compact = wctx->priv;
661 if (wctx->nb_item) printf("%c", compact->item_sep);
664 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
665 printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
666 av_bprint_finalize(&buf, NULL);
669 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
671 CompactContext *compact = wctx->priv;
673 if (wctx->nb_item) printf("%c", compact->item_sep);
676 printf("%lld", value);
679 static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
681 CompactContext *compact = wctx->priv;
682 AVDictionaryEntry *tag = NULL;
685 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
686 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
687 if (wctx->nb_item) printf("%c", compact->item_sep);
688 if (!compact->nokey) {
689 av_bprint_clear(&buf);
690 printf("tag:%s=", compact->escape_str(&buf, tag->key, compact->item_sep, wctx));
692 av_bprint_clear(&buf);
693 printf("%s", compact->escape_str(&buf, tag->value, compact->item_sep, wctx));
695 av_bprint_finalize(&buf, NULL);
698 static const Writer compact_writer = {
700 .priv_size = sizeof(CompactContext),
701 .init = compact_init,
702 .print_section_header = compact_print_section_header,
703 .print_section_footer = compact_print_section_footer,
704 .print_integer = compact_print_int,
705 .print_string = compact_print_str,
706 .show_tags = compact_show_tags,
707 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
708 .priv_class = &compact_class,
714 #define OFFSET(x) offsetof(CompactContext, x)
716 static const AVOption csv_options[] = {
717 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
718 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
719 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
720 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
721 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
722 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
723 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
724 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
728 DEFINE_WRITER_CLASS(csv);
730 static const Writer csv_writer = {
732 .priv_size = sizeof(CompactContext),
733 .init = compact_init,
734 .print_section_header = compact_print_section_header,
735 .print_section_footer = compact_print_section_footer,
736 .print_integer = compact_print_int,
737 .print_string = compact_print_str,
738 .show_tags = compact_show_tags,
739 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
740 .priv_class = &csv_class,
745 typedef struct FlatContext {
746 const AVClass *class;
747 const char *section, *chapter;
754 #define OFFSET(x) offsetof(FlatContext, x)
756 static const AVOption flat_options[]= {
757 {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
758 {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
759 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
760 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
764 DEFINE_WRITER_CLASS(flat);
766 static av_cold int flat_init(WriterContext *wctx, const char *args, void *opaque)
768 FlatContext *flat = wctx->priv;
770 if (strlen(flat->sep_str) != 1) {
771 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
773 return AVERROR(EINVAL);
775 flat->sep = flat->sep_str[0];
779 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
783 for (p = src; *p; p++) {
784 if (!((*p >= '0' && *p <= '9') ||
785 (*p >= 'a' && *p <= 'z') ||
786 (*p >= 'A' && *p <= 'Z')))
787 av_bprint_chars(dst, '_', 1);
789 av_bprint_chars(dst, *p, 1);
794 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
798 for (p = src; *p; p++) {
800 case '\n': av_bprintf(dst, "%s", "\\n"); break;
801 case '\r': av_bprintf(dst, "%s", "\\r"); break;
802 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
803 case '"': av_bprintf(dst, "%s", "\\\""); break;
804 case '`': av_bprintf(dst, "%s", "\\`"); break;
805 case '$': av_bprintf(dst, "%s", "\\$"); break;
806 default: av_bprint_chars(dst, *p, 1); break;
812 static void flat_print_chapter_header(WriterContext *wctx, const char *chapter)
814 FlatContext *flat = wctx->priv;
815 flat->chapter = chapter;
818 static void flat_print_section_header(WriterContext *wctx, const char *section)
820 FlatContext *flat = wctx->priv;
821 flat->section = section;
824 static void flat_print_section(WriterContext *wctx)
826 FlatContext *flat = wctx->priv;
827 int n = wctx->is_packets_and_frames ? wctx->nb_section_packet_frame
830 if (flat->hierarchical && wctx->multiple_sections)
831 printf("%s%c", flat->chapter, flat->sep);
832 printf("%s%c", flat->section, flat->sep);
833 if (wctx->multiple_sections)
834 printf("%d%c", n, flat->sep);
837 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
839 flat_print_section(wctx);
840 printf("%s=%lld\n", key, value);
843 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
845 FlatContext *flat = wctx->priv;
848 flat_print_section(wctx);
849 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
850 printf("%s=", flat_escape_key_str(&buf, key, flat->sep));
851 av_bprint_clear(&buf);
852 printf("\"%s\"\n", flat_escape_value_str(&buf, value));
853 av_bprint_finalize(&buf, NULL);
856 static void flat_show_tags(WriterContext *wctx, AVDictionary *dict)
858 FlatContext *flat = wctx->priv;
860 AVDictionaryEntry *tag = NULL;
862 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
863 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
864 flat_print_section(wctx);
865 av_bprint_clear(&buf);
866 printf("tags%c%s=", flat->sep, flat_escape_key_str(&buf, tag->key, flat->sep));
867 av_bprint_clear(&buf);
868 printf("\"%s\"\n", flat_escape_value_str(&buf, tag->value));
870 av_bprint_finalize(&buf, NULL);
873 static const Writer flat_writer = {
875 .priv_size = sizeof(FlatContext),
877 .print_chapter_header = flat_print_chapter_header,
878 .print_section_header = flat_print_section_header,
879 .print_integer = flat_print_int,
880 .print_string = flat_print_str,
881 .show_tags = flat_show_tags,
882 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
883 .priv_class = &flat_class,
886 /* INI format output */
889 const AVClass *class;
890 AVBPrint chapter_name, section_name;
895 #define OFFSET(x) offsetof(INIContext, x)
897 static const AVOption ini_options[] = {
898 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
899 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
903 DEFINE_WRITER_CLASS(ini);
905 static av_cold int ini_init(WriterContext *wctx, const char *args, void *opaque)
907 INIContext *ini = wctx->priv;
909 av_bprint_init(&ini->chapter_name, 1, AV_BPRINT_SIZE_UNLIMITED);
910 av_bprint_init(&ini->section_name, 1, AV_BPRINT_SIZE_UNLIMITED);
915 static av_cold void ini_uninit(WriterContext *wctx)
917 INIContext *ini = wctx->priv;
918 av_bprint_finalize(&ini->chapter_name, NULL);
919 av_bprint_finalize(&ini->section_name, NULL);
922 static void ini_print_header(WriterContext *wctx)
924 printf("# ffprobe output\n\n");
927 static char *ini_escape_str(AVBPrint *dst, const char *src)
932 while (c = src[i++]) {
934 case '\b': av_bprintf(dst, "%s", "\\b"); break;
935 case '\f': av_bprintf(dst, "%s", "\\f"); break;
936 case '\n': av_bprintf(dst, "%s", "\\n"); break;
937 case '\r': av_bprintf(dst, "%s", "\\r"); break;
938 case '\t': av_bprintf(dst, "%s", "\\t"); break;
942 case ':' : av_bprint_chars(dst, '\\', 1);
944 if ((unsigned char)c < 32)
945 av_bprintf(dst, "\\x00%02x", c & 0xff);
947 av_bprint_chars(dst, c, 1);
954 static void ini_print_chapter_header(WriterContext *wctx, const char *chapter)
956 INIContext *ini = wctx->priv;
958 av_bprint_clear(&ini->chapter_name);
959 av_bprintf(&ini->chapter_name, "%s", chapter);
961 if (wctx->nb_chapter)
965 static void ini_print_section_header(WriterContext *wctx, const char *section)
967 INIContext *ini = wctx->priv;
968 int n = wctx->is_packets_and_frames ? wctx->nb_section_packet_frame
970 if (wctx->nb_section)
972 av_bprint_clear(&ini->section_name);
974 if (ini->hierarchical && wctx->multiple_sections)
975 av_bprintf(&ini->section_name, "%s.", ini->chapter_name.str);
976 av_bprintf(&ini->section_name, "%s", section);
978 if (wctx->multiple_sections)
979 av_bprintf(&ini->section_name, ".%d", n);
980 printf("[%s]\n", ini->section_name.str);
983 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
987 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
988 printf("%s=", ini_escape_str(&buf, key));
989 av_bprint_clear(&buf);
990 printf("%s\n", ini_escape_str(&buf, value));
991 av_bprint_finalize(&buf, NULL);
994 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
996 printf("%s=%lld\n", key, value);
999 static void ini_show_tags(WriterContext *wctx, AVDictionary *dict)
1001 INIContext *ini = wctx->priv;
1002 AVDictionaryEntry *tag = NULL;
1005 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1007 printf("\n[%s.tags]\n", ini->section_name.str);
1010 writer_print_string(wctx, tag->key, tag->value, 0);
1014 static const Writer ini_writer = {
1016 .priv_size = sizeof(INIContext),
1018 .uninit = ini_uninit,
1019 .print_header = ini_print_header,
1020 .print_chapter_header = ini_print_chapter_header,
1021 .print_section_header = ini_print_section_header,
1022 .print_integer = ini_print_int,
1023 .print_string = ini_print_str,
1024 .show_tags = ini_show_tags,
1025 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1026 .priv_class = &ini_class,
1032 const AVClass *class;
1035 const char *item_sep, *item_start_end;
1039 #define OFFSET(x) offsetof(JSONContext, x)
1041 static const AVOption json_options[]= {
1042 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1043 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1047 DEFINE_WRITER_CLASS(json);
1049 static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
1051 JSONContext *json = wctx->priv;
1053 json->item_sep = json->compact ? ", " : ",\n";
1054 json->item_start_end = json->compact ? " " : "\n";
1059 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1061 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1062 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1065 for (p = src; *p; p++) {
1066 char *s = strchr(json_escape, *p);
1068 av_bprint_chars(dst, '\\', 1);
1069 av_bprint_chars(dst, json_subst[s - json_escape], 1);
1070 } else if ((unsigned char)*p < 32) {
1071 av_bprintf(dst, "\\u00%02x", *p & 0xff);
1073 av_bprint_chars(dst, *p, 1);
1079 static void json_print_header(WriterContext *wctx)
1081 JSONContext *json = wctx->priv;
1083 json->indent_level++;
1086 static void json_print_footer(WriterContext *wctx)
1088 JSONContext *json = wctx->priv;
1089 json->indent_level--;
1093 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
1095 static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
1097 JSONContext *json = wctx->priv;
1100 if (wctx->nb_chapter)
1103 if (wctx->multiple_sections) {
1105 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1106 printf("\"%s\": [\n", json_escape_str(&buf, chapter, wctx));
1107 av_bprint_finalize(&buf, NULL);
1108 json->indent_level++;
1112 static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
1114 JSONContext *json = wctx->priv;
1116 if (wctx->multiple_sections) {
1118 json->indent_level--;
1124 static void json_print_section_header(WriterContext *wctx, const char *section)
1126 JSONContext *json = wctx->priv;
1128 if (wctx->nb_section)
1131 if (!wctx->multiple_sections)
1132 printf("\"%s\": ", section);
1133 printf("{%s", json->item_start_end);
1134 json->indent_level++;
1135 /* this is required so the parser can distinguish between packets and frames */
1136 if (wctx->is_packets_and_frames) {
1139 printf("\"type\": \"%s\"%s", section, json->item_sep);
1143 static void json_print_section_footer(WriterContext *wctx, const char *section)
1145 JSONContext *json = wctx->priv;
1147 printf("%s", json->item_start_end);
1148 json->indent_level--;
1154 static inline void json_print_item_str(WriterContext *wctx,
1155 const char *key, const char *value)
1159 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1160 printf("\"%s\":", json_escape_str(&buf, key, wctx));
1161 av_bprint_clear(&buf);
1162 printf(" \"%s\"", json_escape_str(&buf, value, wctx));
1163 av_bprint_finalize(&buf, NULL);
1166 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1168 JSONContext *json = wctx->priv;
1170 if (wctx->nb_item) printf("%s", json->item_sep);
1173 json_print_item_str(wctx, key, value);
1176 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1178 JSONContext *json = wctx->priv;
1181 if (wctx->nb_item) printf("%s", json->item_sep);
1185 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1186 printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1187 av_bprint_finalize(&buf, NULL);
1190 static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
1192 JSONContext *json = wctx->priv;
1193 AVDictionaryEntry *tag = NULL;
1197 printf("%s", json->item_sep);
1200 printf("\"tags\": {%s", json->item_start_end);
1201 json->indent_level++;
1202 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1203 if (is_first) is_first = 0;
1204 else printf("%s", json->item_sep);
1207 json_print_item_str(wctx, tag->key, tag->value);
1209 json->indent_level--;
1210 printf("%s", json->item_start_end);
1216 static const Writer json_writer = {
1218 .priv_size = sizeof(JSONContext),
1220 .print_header = json_print_header,
1221 .print_footer = json_print_footer,
1222 .print_chapter_header = json_print_chapter_header,
1223 .print_chapter_footer = json_print_chapter_footer,
1224 .print_section_header = json_print_section_header,
1225 .print_section_footer = json_print_section_footer,
1226 .print_integer = json_print_int,
1227 .print_string = json_print_str,
1228 .show_tags = json_show_tags,
1229 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1230 .priv_class = &json_class,
1236 const AVClass *class;
1239 int fully_qualified;
1244 #define OFFSET(x) offsetof(XMLContext, x)
1246 static const AVOption xml_options[] = {
1247 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1248 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1249 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1250 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1254 DEFINE_WRITER_CLASS(xml);
1256 static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
1258 XMLContext *xml = wctx->priv;
1260 if (xml->xsd_strict) {
1261 xml->fully_qualified = 1;
1262 #define CHECK_COMPLIANCE(opt, opt_name) \
1264 av_log(wctx, AV_LOG_ERROR, \
1265 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1266 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1267 return AVERROR(EINVAL); \
1269 CHECK_COMPLIANCE(show_private_data, "private");
1270 CHECK_COMPLIANCE(show_value_unit, "unit");
1271 CHECK_COMPLIANCE(use_value_prefix, "prefix");
1273 if (do_show_frames && do_show_packets) {
1274 av_log(wctx, AV_LOG_ERROR,
1275 "Interleaved frames and packets are not allowed in XSD. "
1276 "Select only one between the -show_frames and the -show_packets options.\n");
1277 return AVERROR(EINVAL);
1284 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1288 for (p = src; *p; p++) {
1290 case '&' : av_bprintf(dst, "%s", "&"); break;
1291 case '<' : av_bprintf(dst, "%s", "<"); break;
1292 case '>' : av_bprintf(dst, "%s", ">"); break;
1293 case '\"': av_bprintf(dst, "%s", """); break;
1294 case '\'': av_bprintf(dst, "%s", "'"); break;
1295 default: av_bprint_chars(dst, *p, 1);
1302 static void xml_print_header(WriterContext *wctx)
1304 XMLContext *xml = wctx->priv;
1305 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1306 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1307 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1309 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1310 printf("<%sffprobe%s>\n",
1311 xml->fully_qualified ? "ffprobe:" : "",
1312 xml->fully_qualified ? qual : "");
1314 xml->indent_level++;
1317 static void xml_print_footer(WriterContext *wctx)
1319 XMLContext *xml = wctx->priv;
1321 xml->indent_level--;
1322 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1325 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1327 static void xml_print_chapter_header(WriterContext *wctx, const char *chapter)
1329 XMLContext *xml = wctx->priv;
1331 if (wctx->nb_chapter)
1333 if (wctx->multiple_sections) {
1334 XML_INDENT(); printf("<%s>\n", chapter);
1335 xml->indent_level++;
1339 static void xml_print_chapter_footer(WriterContext *wctx, const char *chapter)
1341 XMLContext *xml = wctx->priv;
1343 if (wctx->multiple_sections) {
1344 xml->indent_level--;
1345 XML_INDENT(); printf("</%s>\n", chapter);
1349 static void xml_print_section_header(WriterContext *wctx, const char *section)
1351 XMLContext *xml = wctx->priv;
1353 XML_INDENT(); printf("<%s ", section);
1354 xml->within_tag = 1;
1357 static void xml_print_section_footer(WriterContext *wctx, const char *section)
1359 XMLContext *xml = wctx->priv;
1361 if (xml->within_tag)
1364 XML_INDENT(); printf("</%s>\n", section);
1368 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1374 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1375 printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
1376 av_bprint_finalize(&buf, NULL);
1379 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1383 printf("%s=\"%lld\"", key, value);
1386 static void xml_show_tags(WriterContext *wctx, AVDictionary *dict)
1388 XMLContext *xml = wctx->priv;
1389 AVDictionaryEntry *tag = NULL;
1393 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1394 xml->indent_level++;
1395 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1397 /* close section tag */
1399 xml->within_tag = 0;
1404 av_bprint_clear(&buf);
1405 printf("<tag key=\"%s\"", xml_escape_str(&buf, tag->key, wctx));
1406 av_bprint_clear(&buf);
1407 printf(" value=\"%s\"/>\n", xml_escape_str(&buf, tag->value, wctx));
1409 av_bprint_finalize(&buf, NULL);
1410 xml->indent_level--;
1413 static Writer xml_writer = {
1415 .priv_size = sizeof(XMLContext),
1417 .print_header = xml_print_header,
1418 .print_footer = xml_print_footer,
1419 .print_chapter_header = xml_print_chapter_header,
1420 .print_chapter_footer = xml_print_chapter_footer,
1421 .print_section_header = xml_print_section_header,
1422 .print_section_footer = xml_print_section_footer,
1423 .print_integer = xml_print_int,
1424 .print_string = xml_print_str,
1425 .show_tags = xml_show_tags,
1426 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1427 .priv_class = &xml_class,
1430 static void writer_register_all(void)
1432 static int initialized;
1438 writer_register(&default_writer);
1439 writer_register(&compact_writer);
1440 writer_register(&csv_writer);
1441 writer_register(&flat_writer);
1442 writer_register(&ini_writer);
1443 writer_register(&json_writer);
1444 writer_register(&xml_writer);
1447 #define print_fmt(k, f, ...) do { \
1448 av_bprint_clear(&pbuf); \
1449 av_bprintf(&pbuf, f, __VA_ARGS__); \
1450 writer_print_string(w, k, pbuf.str, 0); \
1453 #define print_int(k, v) writer_print_integer(w, k, v)
1454 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1455 #define print_str(k, v) writer_print_string(w, k, v, 0)
1456 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
1457 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1458 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1459 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1460 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1461 #define print_val(k, v, u) do { \
1462 struct unit_value uv; \
1465 writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
1468 #define print_section_header(s) writer_print_section_header(w, s)
1469 #define print_section_footer(s) writer_print_section_footer(w, s)
1470 #define show_tags(metadata) writer_show_tags(w, metadata)
1472 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1475 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1479 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1481 print_section_header("packet");
1482 s = av_get_media_type_string(st->codec->codec_type);
1483 if (s) print_str ("codec_type", s);
1484 else print_str_opt("codec_type", "unknown");
1485 print_int("stream_index", pkt->stream_index);
1486 print_ts ("pts", pkt->pts);
1487 print_time("pts_time", pkt->pts, &st->time_base);
1488 print_ts ("dts", pkt->dts);
1489 print_time("dts_time", pkt->dts, &st->time_base);
1490 print_duration_ts("duration", pkt->duration);
1491 print_duration_time("duration_time", pkt->duration, &st->time_base);
1492 print_duration_ts("convergence_duration", pkt->convergence_duration);
1493 print_duration_time("convergence_duration_time", pkt->convergence_duration, &st->time_base);
1494 print_val("size", pkt->size, unit_byte_str);
1495 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1496 else print_str_opt("pos", "N/A");
1497 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1499 writer_print_data(w, "data", pkt->data, pkt->size);
1500 print_section_footer("packet");
1502 av_bprint_finalize(&pbuf, NULL);
1506 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
1507 AVFormatContext *fmt_ctx)
1512 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1514 print_section_header("frame");
1516 s = av_get_media_type_string(stream->codec->codec_type);
1517 if (s) print_str ("media_type", s);
1518 else print_str_opt("media_type", "unknown");
1519 print_int("key_frame", frame->key_frame);
1520 print_ts ("pkt_pts", frame->pkt_pts);
1521 print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
1522 print_ts ("pkt_dts", frame->pkt_dts);
1523 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
1524 print_duration_ts ("pkt_duration", frame->pkt_duration);
1525 print_duration_time("pkt_duration_time", frame->pkt_duration, &stream->time_base);
1526 if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
1527 else print_str_opt("pkt_pos", "N/A");
1529 switch (stream->codec->codec_type) {
1532 case AVMEDIA_TYPE_VIDEO:
1533 print_int("width", frame->width);
1534 print_int("height", frame->height);
1535 s = av_get_pix_fmt_name(frame->format);
1536 if (s) print_str ("pix_fmt", s);
1537 else print_str_opt("pix_fmt", "unknown");
1538 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
1540 print_q("sample_aspect_ratio", sar, ':');
1542 print_str_opt("sample_aspect_ratio", "N/A");
1544 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
1545 print_int("coded_picture_number", frame->coded_picture_number);
1546 print_int("display_picture_number", frame->display_picture_number);
1547 print_int("interlaced_frame", frame->interlaced_frame);
1548 print_int("top_field_first", frame->top_field_first);
1549 print_int("repeat_pict", frame->repeat_pict);
1550 print_int("reference", frame->reference);
1553 case AVMEDIA_TYPE_AUDIO:
1554 s = av_get_sample_fmt_name(frame->format);
1555 if (s) print_str ("sample_fmt", s);
1556 else print_str_opt("sample_fmt", "unknown");
1557 print_int("nb_samples", frame->nb_samples);
1558 print_int("channels", av_frame_get_channels(frame));
1559 if (av_frame_get_channel_layout(frame)) {
1560 av_bprint_clear(&pbuf);
1561 av_bprint_channel_layout(&pbuf, av_frame_get_channels(frame),
1562 av_frame_get_channel_layout(frame));
1563 print_str ("channel_layout", pbuf.str);
1565 print_str_opt("channel_layout", "unknown");
1568 show_tags(av_frame_get_metadata(frame));
1570 print_section_footer("frame");
1572 av_bprint_finalize(&pbuf, NULL);
1576 static av_always_inline int process_frame(WriterContext *w,
1577 AVFormatContext *fmt_ctx,
1578 AVFrame *frame, AVPacket *pkt)
1580 AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
1581 int ret = 0, got_frame = 0;
1583 avcodec_get_frame_defaults(frame);
1584 if (dec_ctx->codec) {
1585 switch (dec_ctx->codec_type) {
1586 case AVMEDIA_TYPE_VIDEO:
1587 ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, pkt);
1590 case AVMEDIA_TYPE_AUDIO:
1591 ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
1598 ret = FFMIN(ret, pkt->size); /* guard against bogus return values */
1602 nb_streams_frames[pkt->stream_index]++;
1604 show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
1609 static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1615 av_init_packet(&pkt);
1617 while (!av_read_frame(fmt_ctx, &pkt)) {
1618 if (do_read_packets) {
1619 if (do_show_packets)
1620 show_packet(w, fmt_ctx, &pkt, i++);
1621 nb_streams_packets[pkt.stream_index]++;
1623 if (do_read_frames) {
1625 while (pkt1.size && process_frame(w, fmt_ctx, &frame, &pkt1) > 0);
1627 av_free_packet(&pkt);
1629 av_init_packet(&pkt);
1632 //Flush remaining frames that are cached in the decoder
1633 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1634 pkt.stream_index = i;
1636 while (process_frame(w, fmt_ctx, &frame, &pkt) > 0);
1640 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
1642 AVStream *stream = fmt_ctx->streams[stream_idx];
1643 AVCodecContext *dec_ctx;
1647 AVRational sar, dar;
1650 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1652 print_section_header("stream");
1654 print_int("index", stream->index);
1656 if ((dec_ctx = stream->codec)) {
1657 const char *profile = NULL;
1658 dec = dec_ctx->codec;
1660 print_str("codec_name", dec->name);
1662 if (dec->long_name) print_str ("codec_long_name", dec->long_name);
1663 else print_str_opt("codec_long_name", "unknown");
1666 print_str_opt("codec_name", "unknown");
1668 print_str_opt("codec_long_name", "unknown");
1672 if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
1673 print_str("profile", profile);
1675 print_str_opt("profile", "unknown");
1677 s = av_get_media_type_string(dec_ctx->codec_type);
1678 if (s) print_str ("codec_type", s);
1679 else print_str_opt("codec_type", "unknown");
1680 print_q("codec_time_base", dec_ctx->time_base, '/');
1682 /* print AVI/FourCC tag */
1683 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
1684 print_str("codec_tag_string", val_str);
1685 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
1687 /* Print useful disposition */
1688 print_int("default", !!(stream->disposition & AV_DISPOSITION_DEFAULT));
1689 print_int("forced", !!(stream->disposition & AV_DISPOSITION_FORCED));
1691 switch (dec_ctx->codec_type) {
1692 case AVMEDIA_TYPE_VIDEO:
1693 print_int("width", dec_ctx->width);
1694 print_int("height", dec_ctx->height);
1695 print_int("has_b_frames", dec_ctx->has_b_frames);
1696 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
1698 print_q("sample_aspect_ratio", sar, ':');
1699 av_reduce(&dar.num, &dar.den,
1700 dec_ctx->width * sar.num,
1701 dec_ctx->height * sar.den,
1703 print_q("display_aspect_ratio", dar, ':');
1705 print_str_opt("sample_aspect_ratio", "N/A");
1706 print_str_opt("display_aspect_ratio", "N/A");
1708 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
1709 if (s) print_str ("pix_fmt", s);
1710 else print_str_opt("pix_fmt", "unknown");
1711 print_int("level", dec_ctx->level);
1712 if (dec_ctx->timecode_frame_start >= 0) {
1713 char tcbuf[AV_TIMECODE_STR_SIZE];
1714 av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
1715 print_str("timecode", tcbuf);
1717 print_str_opt("timecode", "N/A");
1719 print_int("attached_pic",
1720 !!(stream->disposition & AV_DISPOSITION_ATTACHED_PIC));
1723 case AVMEDIA_TYPE_AUDIO:
1724 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
1725 if (s) print_str ("sample_fmt", s);
1726 else print_str_opt("sample_fmt", "unknown");
1727 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
1728 print_int("channels", dec_ctx->channels);
1729 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
1733 print_str_opt("codec_type", "unknown");
1735 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
1736 const AVOption *opt = NULL;
1737 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
1739 if (opt->flags) continue;
1740 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
1741 print_str(opt->name, str);
1747 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
1748 else print_str_opt("id", "N/A");
1749 print_q("r_frame_rate", stream->r_frame_rate, '/');
1750 print_q("avg_frame_rate", stream->avg_frame_rate, '/');
1751 print_q("time_base", stream->time_base, '/');
1752 print_ts ("start_pts", stream->start_time);
1753 print_time("start_time", stream->start_time, &stream->time_base);
1754 print_ts ("duration_ts", stream->duration);
1755 print_time("duration", stream->duration, &stream->time_base);
1756 if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
1757 else print_str_opt("bit_rate", "N/A");
1758 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
1759 else print_str_opt("nb_frames", "N/A");
1760 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
1761 else print_str_opt("nb_read_frames", "N/A");
1762 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
1763 else print_str_opt("nb_read_packets", "N/A");
1765 writer_print_data(w, "extradata", dec_ctx->extradata,
1766 dec_ctx->extradata_size);
1767 show_tags(stream->metadata);
1769 print_section_footer("stream");
1770 av_bprint_finalize(&pbuf, NULL);
1774 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
1777 for (i = 0; i < fmt_ctx->nb_streams; i++)
1778 show_stream(w, fmt_ctx, i);
1781 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
1784 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
1786 print_section_header("format");
1787 print_str("filename", fmt_ctx->filename);
1788 print_int("nb_streams", fmt_ctx->nb_streams);
1789 print_str("format_name", fmt_ctx->iformat->name);
1791 if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
1792 else print_str_opt("format_long_name", "unknown");
1794 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
1795 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
1796 if (size >= 0) print_val ("size", size, unit_byte_str);
1797 else print_str_opt("size", "N/A");
1798 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
1799 else print_str_opt("bit_rate", "N/A");
1800 show_tags(fmt_ctx->metadata);
1801 print_section_footer("format");
1805 static void show_error(WriterContext *w, int err)
1808 const char *errbuf_ptr = errbuf;
1810 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
1811 errbuf_ptr = strerror(AVUNERROR(err));
1813 writer_print_chapter_header(w, "error");
1814 print_section_header("error");
1815 print_int("code", err);
1816 print_str("string", errbuf_ptr);
1817 print_section_footer("error");
1818 writer_print_chapter_footer(w, "error");
1821 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
1824 AVFormatContext *fmt_ctx = NULL;
1825 AVDictionaryEntry *t;
1827 if ((err = avformat_open_input(&fmt_ctx, filename,
1828 iformat, &format_opts)) < 0) {
1829 print_error(filename, err);
1832 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1833 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
1834 return AVERROR_OPTION_NOT_FOUND;
1838 /* fill the streams in the format context */
1839 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
1840 print_error(filename, err);
1844 av_dump_format(fmt_ctx, 0, filename, 0);
1846 /* bind a decoder to each input stream */
1847 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1848 AVStream *stream = fmt_ctx->streams[i];
1851 if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
1852 av_log(NULL, AV_LOG_ERROR,
1853 "Failed to probe codec for input stream %d\n",
1855 } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
1856 av_log(NULL, AV_LOG_ERROR,
1857 "Unsupported codec with id %d for input stream %d\n",
1858 stream->codec->codec_id, stream->index);
1859 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
1860 av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
1865 *fmt_ctx_ptr = fmt_ctx;
1869 static void close_input_file(AVFormatContext **ctx_ptr)
1872 AVFormatContext *fmt_ctx = *ctx_ptr;
1874 /* close decoder for each stream */
1875 for (i = 0; i < fmt_ctx->nb_streams; i++)
1876 if (fmt_ctx->streams[i]->codec->codec_id != AV_CODEC_ID_NONE)
1877 avcodec_close(fmt_ctx->streams[i]->codec);
1879 avformat_close_input(ctx_ptr);
1882 #define PRINT_CHAPTER(name) do { \
1883 if (do_show_ ## name) { \
1884 writer_print_chapter_header(wctx, #name); \
1885 show_ ## name (wctx, fmt_ctx); \
1886 writer_print_chapter_footer(wctx, #name); \
1890 static int probe_file(WriterContext *wctx, const char *filename)
1892 AVFormatContext *fmt_ctx;
1895 do_read_frames = do_show_frames || do_count_frames;
1896 do_read_packets = do_show_packets || do_count_packets;
1898 ret = open_input_file(&fmt_ctx, filename);
1900 nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
1901 nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
1902 if (do_read_frames || do_read_packets) {
1903 const char *chapter;
1904 if (do_show_frames && do_show_packets &&
1905 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
1906 chapter = "packets_and_frames";
1907 else if (do_show_packets && !do_show_frames)
1908 chapter = "packets";
1909 else // (!do_show_packets && do_show_frames)
1911 if (do_show_frames || do_show_packets)
1912 writer_print_chapter_header(wctx, chapter);
1913 read_packets(wctx, fmt_ctx);
1914 if (do_show_frames || do_show_packets)
1915 writer_print_chapter_footer(wctx, chapter);
1917 PRINT_CHAPTER(streams);
1918 PRINT_CHAPTER(format);
1919 close_input_file(&fmt_ctx);
1920 av_freep(&nb_streams_frames);
1921 av_freep(&nb_streams_packets);
1926 static void show_usage(void)
1928 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
1929 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
1930 av_log(NULL, AV_LOG_INFO, "\n");
1933 static void ffprobe_show_program_version(WriterContext *w)
1936 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1938 writer_print_chapter_header(w, "program_version");
1939 print_section_header("program_version");
1940 print_str("version", FFMPEG_VERSION);
1941 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
1942 program_birth_year, this_year);
1943 print_str("build_date", __DATE__);
1944 print_str("build_time", __TIME__);
1945 print_str("compiler_ident", CC_IDENT);
1946 print_str("configuration", FFMPEG_CONFIGURATION);
1947 print_section_footer("program_version");
1948 writer_print_chapter_footer(w, "program_version");
1950 av_bprint_finalize(&pbuf, NULL);
1953 #define SHOW_LIB_VERSION(libname, LIBNAME) \
1955 if (CONFIG_##LIBNAME) { \
1956 unsigned int version = libname##_version(); \
1957 print_section_header("library_version"); \
1958 print_str("name", "lib" #libname); \
1959 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
1960 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
1961 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
1962 print_int("version", version); \
1963 print_section_footer("library_version"); \
1967 static void ffprobe_show_library_versions(WriterContext *w)
1969 writer_print_chapter_header(w, "library_versions");
1970 SHOW_LIB_VERSION(avutil, AVUTIL);
1971 SHOW_LIB_VERSION(avcodec, AVCODEC);
1972 SHOW_LIB_VERSION(avformat, AVFORMAT);
1973 SHOW_LIB_VERSION(avdevice, AVDEVICE);
1974 SHOW_LIB_VERSION(avfilter, AVFILTER);
1975 SHOW_LIB_VERSION(swscale, SWSCALE);
1976 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
1977 SHOW_LIB_VERSION(postproc, POSTPROC);
1978 writer_print_chapter_footer(w, "library_versions");
1981 static int opt_format(void *optctx, const char *opt, const char *arg)
1983 iformat = av_find_input_format(arg);
1985 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
1986 return AVERROR(EINVAL);
1991 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
1994 av_dict_set(&fmt_entries_to_show, arg, "", 0);
1998 static void opt_input_file(void *optctx, const char *arg)
2000 if (input_filename) {
2001 av_log(NULL, AV_LOG_ERROR,
2002 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
2003 arg, input_filename);
2006 if (!strcmp(arg, "-"))
2008 input_filename = arg;
2011 static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
2013 opt_input_file(optctx, arg);
2017 void show_help_default(const char *opt, const char *arg)
2019 av_log_set_callback(log_callback_help);
2021 show_help_options(options, "Main options:", 0, 0, 0);
2024 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
2027 static int opt_pretty(void *optctx, const char *opt, const char *arg)
2029 show_value_unit = 1;
2030 use_value_prefix = 1;
2031 use_byte_value_binary_prefix = 1;
2032 use_value_sexagesimal_format = 1;
2036 static int opt_show_versions(const char *opt, const char *arg)
2038 do_show_program_version = 1;
2039 do_show_library_versions = 1;
2043 static const OptionDef real_options[] = {
2044 #include "cmdutils_common_opts.h"
2045 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
2046 { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
2047 { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
2048 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
2049 "use binary prefixes for byte units" },
2050 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
2051 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
2052 { "pretty", 0, {.func_arg = opt_pretty},
2053 "prettify the format of displayed values, make it more human readable" },
2054 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
2055 "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
2056 { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
2057 { "show_data", OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
2058 { "show_error", OPT_BOOL, {(void*)&do_show_error} , "show probing error" },
2059 { "show_format", OPT_BOOL, {&do_show_format} , "show format/container info" },
2060 { "show_frames", OPT_BOOL, {(void*)&do_show_frames} , "show frames info" },
2061 { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
2062 "show a particular entry from the format/container info", "entry" },
2063 { "show_packets", OPT_BOOL, {&do_show_packets}, "show packets info" },
2064 { "show_streams", OPT_BOOL, {&do_show_streams}, "show streams info" },
2065 { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
2066 { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
2067 { "show_program_version", OPT_BOOL, {(void*)&do_show_program_version}, "show ffprobe version" },
2068 { "show_library_versions", OPT_BOOL, {(void*)&do_show_library_versions}, "show library versions" },
2069 { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
2070 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
2071 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
2072 { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
2073 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
2074 { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
2078 int main(int argc, char **argv)
2081 WriterContext *wctx;
2083 char *w_name = NULL, *w_args = NULL;
2086 av_log_set_flags(AV_LOG_SKIP_REPEATED);
2087 options = real_options;
2088 parse_loglevel(argc, argv, options);
2090 avformat_network_init();
2093 avdevice_register_all();
2096 show_banner(argc, argv, options);
2097 parse_options(NULL, argc, argv, options, opt_input_file);
2099 if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
2100 av_log(NULL, AV_LOG_ERROR,
2101 "-bitexact and -show_program_version or -show_library_versions "
2102 "options are incompatible\n");
2103 ret = AVERROR(EINVAL);
2107 writer_register_all();
2110 print_format = av_strdup("default");
2111 w_name = av_strtok(print_format, "=", &buf);
2114 w = writer_get_by_name(w_name);
2116 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
2117 ret = AVERROR(EINVAL);
2121 if ((ret = writer_open(&wctx, w, w_args, NULL)) >= 0) {
2122 writer_print_header(wctx);
2124 if (do_show_program_version)
2125 ffprobe_show_program_version(wctx);
2126 if (do_show_library_versions)
2127 ffprobe_show_library_versions(wctx);
2129 if (!input_filename &&
2130 ((do_show_format || do_show_streams || do_show_packets || do_show_error) ||
2131 (!do_show_program_version && !do_show_library_versions))) {
2133 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
2134 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
2135 ret = AVERROR(EINVAL);
2136 } else if (input_filename) {
2137 ret = probe_file(wctx, input_filename);
2138 if (ret < 0 && do_show_error)
2139 show_error(wctx, ret);
2142 writer_print_footer(wctx);
2143 writer_close(&wctx);
2147 av_freep(&print_format);
2150 av_dict_free(&fmt_entries_to_show);
2152 avformat_network_deinit();