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/timecode.h"
37 #include "libavdevice/avdevice.h"
38 #include "libswscale/swscale.h"
39 #include "libswresample/swresample.h"
40 #include "libpostproc/postprocess.h"
43 const char program_name[] = "ffprobe";
44 const int program_birth_year = 2007;
46 static int do_count_frames = 0;
47 static int do_count_packets = 0;
48 static int do_read_frames = 0;
49 static int do_read_packets = 0;
50 static int do_show_error = 0;
51 static int do_show_format = 0;
52 static int do_show_frames = 0;
53 static AVDictionary *fmt_entries_to_show = NULL;
54 static int do_show_packets = 0;
55 static int do_show_streams = 0;
56 static int do_show_program_version = 0;
57 static int do_show_library_versions = 0;
59 static int show_value_unit = 0;
60 static int use_value_prefix = 0;
61 static int use_byte_value_binary_prefix = 0;
62 static int use_value_sexagesimal_format = 0;
63 static int show_private_data = 1;
65 static char *print_format;
67 static const OptionDef options[];
70 static const char *input_filename;
71 static AVInputFormat *iformat = NULL;
73 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
74 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
76 static const char unit_second_str[] = "s" ;
77 static const char unit_hertz_str[] = "Hz" ;
78 static const char unit_byte_str[] = "byte" ;
79 static const char unit_bit_per_second_str[] = "bit/s";
80 static uint64_t *nb_streams_packets;
81 static uint64_t *nb_streams_frames;
83 void av_noreturn exit_program(int ret)
85 av_dict_free(&fmt_entries_to_show);
90 union { double d; long long int i; } val;
94 static char *value_string(char *buf, int buf_size, struct unit_value uv)
99 if (uv.unit == unit_second_str) {
106 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
110 mins = (int)secs / 60;
111 secs = secs - mins * 60;
114 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
116 const char *prefix_string = "";
119 if (use_value_prefix && vald > 1) {
122 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
123 index = (long long int) (log(vald)/log(2)) / 10;
124 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
125 vald /= pow(2, index * 10);
126 prefix_string = binary_unit_prefixes[index];
128 index = (long long int) (log10(vald)) / 3;
129 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
130 vald /= pow(10, index * 3);
131 prefix_string = decimal_unit_prefixes[index];
135 if (show_float || (use_value_prefix && vald != (long long int)vald))
136 l = snprintf(buf, buf_size, "%f", vald);
138 l = snprintf(buf, buf_size, "%lld", (long long int)vald);
139 snprintf(buf+l, buf_size-l, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
140 prefix_string, show_value_unit ? uv.unit : "");
148 typedef struct WriterContext WriterContext;
150 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
151 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
153 typedef struct Writer {
154 int priv_size; ///< private size for the writer context
157 int (*init) (WriterContext *wctx, const char *args, void *opaque);
158 void (*uninit)(WriterContext *wctx);
160 void (*print_header)(WriterContext *ctx);
161 void (*print_footer)(WriterContext *ctx);
163 void (*print_chapter_header)(WriterContext *wctx, const char *);
164 void (*print_chapter_footer)(WriterContext *wctx, const char *);
165 void (*print_section_header)(WriterContext *wctx, const char *);
166 void (*print_section_footer)(WriterContext *wctx, const char *);
167 void (*print_integer) (WriterContext *wctx, const char *, long long int);
168 void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
169 void (*print_string) (WriterContext *wctx, const char *, const char *);
170 void (*show_tags) (WriterContext *wctx, AVDictionary *dict);
171 int flags; ///< a combination or WRITER_FLAG_*
174 struct WriterContext {
175 const AVClass *class; ///< class of the writer
176 const Writer *writer; ///< the Writer of which this is an instance
177 char *name; ///< name of this writer instance
178 void *priv; ///< private data for use by the filter
179 unsigned int nb_item; ///< number of the item printed in the given section, starting at 0
180 unsigned int nb_section; ///< number of the section printed in the given section sequence, starting at 0
181 unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
182 unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
183 unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
184 unsigned int nb_chapter; ///< number of the chapter, starting at 0
186 int multiple_sections; ///< tells if the current chapter can contain multiple sections
187 int is_fmt_chapter; ///< tells if the current chapter is "format", required by the print_format_entry option
188 int is_packets_and_frames; ///< tells if the current section is "packets_and_frames"
191 static const char *writer_get_name(void *p)
193 WriterContext *wctx = p;
194 return wctx->writer->name;
197 static const AVClass writer_class = {
201 LIBAVUTIL_VERSION_INT,
204 static void writer_close(WriterContext **wctx)
209 if ((*wctx)->writer->uninit)
210 (*wctx)->writer->uninit(*wctx);
211 av_freep(&((*wctx)->priv));
215 static int writer_open(WriterContext **wctx, const Writer *writer,
216 const char *args, void *opaque)
220 if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
221 ret = AVERROR(ENOMEM);
225 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
226 ret = AVERROR(ENOMEM);
230 (*wctx)->class = &writer_class;
231 (*wctx)->writer = writer;
232 if ((*wctx)->writer->init)
233 ret = (*wctx)->writer->init(*wctx, args, opaque);
244 static inline void writer_print_header(WriterContext *wctx)
246 if (wctx->writer->print_header)
247 wctx->writer->print_header(wctx);
248 wctx->nb_chapter = 0;
251 static inline void writer_print_footer(WriterContext *wctx)
253 if (wctx->writer->print_footer)
254 wctx->writer->print_footer(wctx);
257 static inline void writer_print_chapter_header(WriterContext *wctx,
261 wctx->nb_section_packet = wctx->nb_section_frame =
262 wctx->nb_section_packet_frame = 0;
263 wctx->is_packets_and_frames = !strcmp(chapter, "packets_and_frames");
264 wctx->multiple_sections = !strcmp(chapter, "packets") || !strcmp(chapter, "frames" ) ||
265 wctx->is_packets_and_frames ||
266 !strcmp(chapter, "streams") || !strcmp(chapter, "library_versions");
267 wctx->is_fmt_chapter = !strcmp(chapter, "format");
269 if (wctx->writer->print_chapter_header)
270 wctx->writer->print_chapter_header(wctx, chapter);
273 static inline void writer_print_chapter_footer(WriterContext *wctx,
276 if (wctx->writer->print_chapter_footer)
277 wctx->writer->print_chapter_footer(wctx, chapter);
281 static inline void writer_print_section_header(WriterContext *wctx,
284 if (wctx->is_packets_and_frames)
285 wctx->nb_section_packet_frame = !strcmp(section, "packet") ? wctx->nb_section_packet
286 : wctx->nb_section_frame;
287 if (wctx->writer->print_section_header)
288 wctx->writer->print_section_header(wctx, section);
292 static inline void writer_print_section_footer(WriterContext *wctx,
295 if (wctx->writer->print_section_footer)
296 wctx->writer->print_section_footer(wctx, section);
297 if (wctx->is_packets_and_frames) {
298 if (!strcmp(section, "packet")) wctx->nb_section_packet++;
299 else wctx->nb_section_frame++;
304 static inline void writer_print_integer(WriterContext *wctx,
305 const char *key, long long int val)
307 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
308 wctx->writer->print_integer(wctx, key, val);
313 static inline void writer_print_rational(WriterContext *wctx,
314 const char *key, AVRational q, char sep)
317 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
318 av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
319 wctx->writer->print_string(wctx, key, buf.str);
323 static inline void writer_print_string(WriterContext *wctx,
324 const char *key, const char *val, int opt)
326 if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
328 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
329 wctx->writer->print_string(wctx, key, val);
334 static void writer_print_time(WriterContext *wctx, const char *key,
335 int64_t ts, const AVRational *time_base, int is_duration)
339 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
340 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
341 writer_print_string(wctx, key, "N/A", 1);
343 double d = ts * av_q2d(*time_base);
344 value_string(buf, sizeof(buf), (struct unit_value){.val.d=d, .unit=unit_second_str});
345 writer_print_string(wctx, key, buf, 0);
350 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
352 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
353 writer_print_string(wctx, key, "N/A", 1);
355 writer_print_integer(wctx, key, ts);
359 static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
361 wctx->writer->show_tags(wctx, dict);
364 #define MAX_REGISTERED_WRITERS_NB 64
366 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
368 static int writer_register(const Writer *writer)
370 static int next_registered_writer_idx = 0;
372 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
373 return AVERROR(ENOMEM);
375 registered_writers[next_registered_writer_idx++] = writer;
379 static const Writer *writer_get_by_name(const char *name)
383 for (i = 0; registered_writers[i]; i++)
384 if (!strcmp(registered_writers[i]->name, name))
385 return registered_writers[i];
395 typedef struct DefaultContext {
396 const AVClass *class;
398 int noprint_wrappers;
401 #define OFFSET(x) offsetof(DefaultContext, x)
403 static const AVOption default_options[] = {
404 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
405 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
406 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
407 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
411 static const char *default_get_name(void *ctx)
416 static const AVClass default_class = {
422 static av_cold int default_init(WriterContext *wctx, const char *args, void *opaque)
424 DefaultContext *def = wctx->priv;
427 def->class = &default_class;
428 av_opt_set_defaults(def);
431 (err = (av_set_options_string(def, args, "=", ":"))) < 0) {
432 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
439 static void default_print_footer(WriterContext *wctx)
441 DefaultContext *def = wctx->priv;
443 if (!def->noprint_wrappers)
447 static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
449 DefaultContext *def = wctx->priv;
451 if (!def->noprint_wrappers && wctx->nb_chapter)
455 /* lame uppercasing routine, assumes the string is lower case ASCII */
456 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
459 for (i = 0; src[i] && i < dst_size-1; i++)
460 dst[i] = av_toupper(src[i]);
465 static void default_print_section_header(WriterContext *wctx, const char *section)
467 DefaultContext *def = wctx->priv;
470 if (wctx->nb_section)
472 if (!def->noprint_wrappers)
473 printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
476 static void default_print_section_footer(WriterContext *wctx, const char *section)
478 DefaultContext *def = wctx->priv;
481 if (!def->noprint_wrappers)
482 printf("[/%s]", upcase_string(buf, sizeof(buf), section));
485 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
487 DefaultContext *def = wctx->priv;
490 printf("%s\n", value);
493 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
495 DefaultContext *def = wctx->priv;
499 printf("%lld\n", value);
502 static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
504 AVDictionaryEntry *tag = NULL;
505 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
506 if (!fmt_entries_to_show || (tag->key && av_dict_get(fmt_entries_to_show, tag->key, NULL, 0)))
508 writer_print_string(wctx, tag->key, tag->value, 0);
512 static const Writer default_writer = {
514 .priv_size = sizeof(DefaultContext),
515 .init = default_init,
516 .print_footer = default_print_footer,
517 .print_chapter_header = default_print_chapter_header,
518 .print_section_header = default_print_section_header,
519 .print_section_footer = default_print_section_footer,
520 .print_integer = default_print_int,
521 .print_string = default_print_str,
522 .show_tags = default_show_tags,
523 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
529 * Apply C-language-like string escaping.
531 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
535 for (p = src; *p; p++) {
537 case '\b': av_bprintf(dst, "%s", "\\b"); break;
538 case '\f': av_bprintf(dst, "%s", "\\f"); break;
539 case '\n': av_bprintf(dst, "%s", "\\n"); break;
540 case '\r': av_bprintf(dst, "%s", "\\r"); break;
541 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
544 av_bprint_chars(dst, '\\', 1);
545 av_bprint_chars(dst, *p, 1);
552 * Quote fields containing special characters, check RFC4180.
554 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
559 /* check if input needs quoting */
560 for (p = src; *p; p++)
561 if (*p == '"' || *p == sep || *p == '\n' || *p == '\r')
565 av_bprint_chars(dst, '\"', 1);
567 for (p = src; *p; p++) {
569 av_bprint_chars(dst, '\"', 1);
570 av_bprint_chars(dst, *p, 1);
573 av_bprint_chars(dst, '\"', 1);
577 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
582 typedef struct CompactContext {
583 const AVClass *class;
587 char *escape_mode_str;
588 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
592 #define OFFSET(x) offsetof(CompactContext, x)
594 static const AVOption compact_options[]= {
595 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
596 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
597 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
598 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
599 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
600 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
604 static const char *compact_get_name(void *ctx)
609 static const AVClass compact_class = {
615 static av_cold int compact_init(WriterContext *wctx, const char *args, void *opaque)
617 CompactContext *compact = wctx->priv;
620 compact->class = &compact_class;
621 av_opt_set_defaults(compact);
624 (err = (av_set_options_string(compact, args, "=", ":"))) < 0) {
625 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
628 if (strlen(compact->item_sep_str) != 1) {
629 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
630 compact->item_sep_str);
631 return AVERROR(EINVAL);
633 compact->item_sep = compact->item_sep_str[0];
635 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
636 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
637 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
639 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
640 return AVERROR(EINVAL);
646 static av_cold void compact_uninit(WriterContext *wctx)
648 CompactContext *compact = wctx->priv;
650 av_freep(&compact->item_sep_str);
651 av_freep(&compact->escape_mode_str);
654 static void compact_print_section_header(WriterContext *wctx, const char *section)
656 CompactContext *compact = wctx->priv;
658 printf("%s%c", section, compact->item_sep);
661 static void compact_print_section_footer(WriterContext *wctx, const char *section)
666 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
668 CompactContext *compact = wctx->priv;
671 if (wctx->nb_item) printf("%c", compact->item_sep);
674 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
675 printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
676 av_bprint_finalize(&buf, NULL);
679 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
681 CompactContext *compact = wctx->priv;
683 if (wctx->nb_item) printf("%c", compact->item_sep);
686 printf("%lld", value);
689 static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
691 CompactContext *compact = wctx->priv;
692 AVDictionaryEntry *tag = NULL;
695 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
696 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
697 if (wctx->nb_item) printf("%c", compact->item_sep);
698 if (!compact->nokey) {
699 av_bprint_clear(&buf);
700 printf("tag:%s=", compact->escape_str(&buf, tag->key, compact->item_sep, wctx));
702 av_bprint_clear(&buf);
703 printf("%s", compact->escape_str(&buf, tag->value, compact->item_sep, wctx));
705 av_bprint_finalize(&buf, NULL);
708 static const Writer compact_writer = {
710 .priv_size = sizeof(CompactContext),
711 .init = compact_init,
712 .uninit = compact_uninit,
713 .print_section_header = compact_print_section_header,
714 .print_section_footer = compact_print_section_footer,
715 .print_integer = compact_print_int,
716 .print_string = compact_print_str,
717 .show_tags = compact_show_tags,
718 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
723 static av_cold int csv_init(WriterContext *wctx, const char *args, void *opaque)
725 return compact_init(wctx, "item_sep=,:nokey=1:escape=csv", opaque);
728 static const Writer csv_writer = {
730 .priv_size = sizeof(CompactContext),
732 .uninit = compact_uninit,
733 .print_section_header = compact_print_section_header,
734 .print_section_footer = compact_print_section_footer,
735 .print_integer = compact_print_int,
736 .print_string = compact_print_str,
737 .show_tags = compact_show_tags,
738 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
743 typedef struct FlatContext {
744 const AVClass *class;
745 const char *section, *chapter;
752 #define OFFSET(x) offsetof(FlatContext, x)
754 static const AVOption flat_options[]= {
755 {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
756 {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
757 {"hierachical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
758 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
762 static const char *flat_get_name(void *ctx)
767 static const AVClass flat_class = {
773 static av_cold int flat_init(WriterContext *wctx, const char *args, void *opaque)
775 FlatContext *flat = wctx->priv;
778 flat->class = &flat_class;
779 av_opt_set_defaults(flat);
782 (err = (av_set_options_string(flat, args, "=", ":"))) < 0) {
783 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
786 if (strlen(flat->sep_str) != 1) {
787 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
789 return AVERROR(EINVAL);
791 flat->sep = flat->sep_str[0];
795 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
799 for (p = src; *p; p++) {
800 if (!((*p >= '0' && *p <= '9') ||
801 (*p >= 'a' && *p <= 'z') ||
802 (*p >= 'A' && *p <= 'Z')))
803 av_bprint_chars(dst, '_', 1);
805 av_bprint_chars(dst, *p, 1);
810 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
814 for (p = src; *p; p++) {
816 case '\n': av_bprintf(dst, "%s", "\\n"); break;
817 case '\r': av_bprintf(dst, "%s", "\\r"); break;
818 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
819 case '"': av_bprintf(dst, "%s", "\\\""); break;
820 case '`': av_bprintf(dst, "%s", "\\`"); break;
821 case '$': av_bprintf(dst, "%s", "\\$"); break;
822 default: av_bprint_chars(dst, *p, 1); break;
828 static void flat_print_chapter_header(WriterContext *wctx, const char *chapter)
830 FlatContext *flat = wctx->priv;
831 flat->chapter = chapter;
834 static void flat_print_section_header(WriterContext *wctx, const char *section)
836 FlatContext *flat = wctx->priv;
837 flat->section = section;
840 static void flat_print_section(WriterContext *wctx)
842 FlatContext *flat = wctx->priv;
843 int n = wctx->is_packets_and_frames ? wctx->nb_section_packet_frame
846 if (flat->hierarchical && wctx->multiple_sections)
847 printf("%s%c", flat->chapter, flat->sep);
848 printf("%s%c", flat->section, flat->sep);
849 if (wctx->multiple_sections)
850 printf("%d%c", n, flat->sep);
853 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
855 flat_print_section(wctx);
856 printf("%s=%lld\n", key, value);
859 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
861 FlatContext *flat = wctx->priv;
864 flat_print_section(wctx);
865 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
866 printf("%s=", flat_escape_key_str(&buf, key, flat->sep));
867 av_bprint_clear(&buf);
868 printf("\"%s\"\n", flat_escape_value_str(&buf, value));
869 av_bprint_finalize(&buf, NULL);
872 static void flat_show_tags(WriterContext *wctx, AVDictionary *dict)
874 FlatContext *flat = wctx->priv;
876 AVDictionaryEntry *tag = NULL;
878 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
879 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
880 flat_print_section(wctx);
881 av_bprint_clear(&buf);
882 printf("tags%c%s=", flat->sep, flat_escape_key_str(&buf, tag->key, flat->sep));
883 av_bprint_clear(&buf);
884 printf("\"%s\"\n", flat_escape_value_str(&buf, tag->value));
886 av_bprint_finalize(&buf, NULL);
889 static const Writer flat_writer = {
891 .priv_size = sizeof(FlatContext),
893 .print_chapter_header = flat_print_chapter_header,
894 .print_section_header = flat_print_section_header,
895 .print_integer = flat_print_int,
896 .print_string = flat_print_str,
897 .show_tags = flat_show_tags,
898 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
901 /* INI format output */
904 const AVClass *class;
905 AVBPrint chapter_name, section_name;
910 #define OFFSET(x) offsetof(INIContext, x)
912 static const AVOption ini_options[] = {
913 {"hierachical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
914 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
918 static const char *ini_get_name(void *ctx)
923 static const AVClass ini_class = {
929 static av_cold int ini_init(WriterContext *wctx, const char *args, void *opaque)
931 INIContext *ini = wctx->priv;
934 av_bprint_init(&ini->chapter_name, 1, AV_BPRINT_SIZE_UNLIMITED);
935 av_bprint_init(&ini->section_name, 1, AV_BPRINT_SIZE_UNLIMITED);
937 ini->class = &ini_class;
938 av_opt_set_defaults(ini);
940 if (args && (err = av_set_options_string(ini, args, "=", ":")) < 0) {
941 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
948 static av_cold void ini_uninit(WriterContext *wctx)
950 INIContext *ini = wctx->priv;
951 av_bprint_finalize(&ini->chapter_name, NULL);
952 av_bprint_finalize(&ini->section_name, NULL);
955 static void ini_print_header(WriterContext *wctx)
957 printf("# ffprobe output\n\n");
960 static char *ini_escape_str(AVBPrint *dst, const char *src)
965 while (c = src[i++]) {
967 case '\b': av_bprintf(dst, "%s", "\\b"); break;
968 case '\f': av_bprintf(dst, "%s", "\\f"); break;
969 case '\n': av_bprintf(dst, "%s", "\\n"); break;
970 case '\r': av_bprintf(dst, "%s", "\\r"); break;
971 case '\t': av_bprintf(dst, "%s", "\\t"); break;
975 case ':' : av_bprint_chars(dst, '\\', 1);
977 if ((unsigned char)c < 32)
978 av_bprintf(dst, "\\x00%02x", c & 0xff);
980 av_bprint_chars(dst, c, 1);
987 static void ini_print_chapter_header(WriterContext *wctx, const char *chapter)
989 INIContext *ini = wctx->priv;
991 av_bprint_clear(&ini->chapter_name);
992 av_bprintf(&ini->chapter_name, "%s", chapter);
994 if (wctx->nb_chapter)
998 static void ini_print_section_header(WriterContext *wctx, const char *section)
1000 INIContext *ini = wctx->priv;
1001 int n = wctx->is_packets_and_frames ? wctx->nb_section_packet_frame
1003 if (wctx->nb_section)
1005 av_bprint_clear(&ini->section_name);
1007 if (ini->hierarchical && wctx->multiple_sections)
1008 av_bprintf(&ini->section_name, "%s.", ini->chapter_name.str);
1009 av_bprintf(&ini->section_name, "%s", section);
1011 if (wctx->multiple_sections)
1012 av_bprintf(&ini->section_name, ".%d", n);
1013 printf("[%s]\n", ini->section_name.str);
1016 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1020 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1021 printf("%s=", ini_escape_str(&buf, key));
1022 av_bprint_clear(&buf);
1023 printf("%s\n", ini_escape_str(&buf, value));
1024 av_bprint_finalize(&buf, NULL);
1027 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1029 printf("%s=%lld\n", key, value);
1032 static void ini_show_tags(WriterContext *wctx, AVDictionary *dict)
1034 INIContext *ini = wctx->priv;
1035 AVDictionaryEntry *tag = NULL;
1038 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1040 printf("\n[%s.tags]\n", ini->section_name.str);
1043 writer_print_string(wctx, tag->key, tag->value, 0);
1047 static const Writer ini_writer = {
1049 .priv_size = sizeof(INIContext),
1051 .uninit = ini_uninit,
1052 .print_header = ini_print_header,
1053 .print_chapter_header = ini_print_chapter_header,
1054 .print_section_header = ini_print_section_header,
1055 .print_integer = ini_print_int,
1056 .print_string = ini_print_str,
1057 .show_tags = ini_show_tags,
1058 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1064 const AVClass *class;
1067 const char *item_sep, *item_start_end;
1071 #define OFFSET(x) offsetof(JSONContext, x)
1073 static const AVOption json_options[]= {
1074 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
1075 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
1079 static const char *json_get_name(void *ctx)
1084 static const AVClass json_class = {
1090 static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
1092 JSONContext *json = wctx->priv;
1095 json->class = &json_class;
1096 av_opt_set_defaults(json);
1099 (err = (av_set_options_string(json, args, "=", ":"))) < 0) {
1100 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
1104 json->item_sep = json->compact ? ", " : ",\n";
1105 json->item_start_end = json->compact ? " " : "\n";
1110 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1112 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1113 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1116 for (p = src; *p; p++) {
1117 char *s = strchr(json_escape, *p);
1119 av_bprint_chars(dst, '\\', 1);
1120 av_bprint_chars(dst, json_subst[s - json_escape], 1);
1121 } else if ((unsigned char)*p < 32) {
1122 av_bprintf(dst, "\\u00%02x", *p & 0xff);
1124 av_bprint_chars(dst, *p, 1);
1130 static void json_print_header(WriterContext *wctx)
1132 JSONContext *json = wctx->priv;
1134 json->indent_level++;
1137 static void json_print_footer(WriterContext *wctx)
1139 JSONContext *json = wctx->priv;
1140 json->indent_level--;
1144 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
1146 static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
1148 JSONContext *json = wctx->priv;
1151 if (wctx->nb_chapter)
1154 if (wctx->multiple_sections) {
1156 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1157 printf("\"%s\": [\n", json_escape_str(&buf, chapter, wctx));
1158 av_bprint_finalize(&buf, NULL);
1159 json->indent_level++;
1163 static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
1165 JSONContext *json = wctx->priv;
1167 if (wctx->multiple_sections) {
1169 json->indent_level--;
1175 static void json_print_section_header(WriterContext *wctx, const char *section)
1177 JSONContext *json = wctx->priv;
1179 if (wctx->nb_section)
1182 if (!wctx->multiple_sections)
1183 printf("\"%s\": ", section);
1184 printf("{%s", json->item_start_end);
1185 json->indent_level++;
1186 /* this is required so the parser can distinguish between packets and frames */
1187 if (wctx->is_packets_and_frames) {
1190 printf("\"type\": \"%s\"%s", section, json->item_sep);
1194 static void json_print_section_footer(WriterContext *wctx, const char *section)
1196 JSONContext *json = wctx->priv;
1198 printf("%s", json->item_start_end);
1199 json->indent_level--;
1205 static inline void json_print_item_str(WriterContext *wctx,
1206 const char *key, const char *value)
1210 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1211 printf("\"%s\":", json_escape_str(&buf, key, wctx));
1212 av_bprint_clear(&buf);
1213 printf(" \"%s\"", json_escape_str(&buf, value, wctx));
1214 av_bprint_finalize(&buf, NULL);
1217 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1219 JSONContext *json = wctx->priv;
1221 if (wctx->nb_item) printf("%s", json->item_sep);
1224 json_print_item_str(wctx, key, value);
1227 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1229 JSONContext *json = wctx->priv;
1232 if (wctx->nb_item) printf("%s", json->item_sep);
1236 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1237 printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1238 av_bprint_finalize(&buf, NULL);
1241 static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
1243 JSONContext *json = wctx->priv;
1244 AVDictionaryEntry *tag = NULL;
1248 printf("%s", json->item_sep);
1251 printf("\"tags\": {%s", json->item_start_end);
1252 json->indent_level++;
1253 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1254 if (is_first) is_first = 0;
1255 else printf("%s", json->item_sep);
1258 json_print_item_str(wctx, tag->key, tag->value);
1260 json->indent_level--;
1261 printf("%s", json->item_start_end);
1267 static const Writer json_writer = {
1269 .priv_size = sizeof(JSONContext),
1271 .print_header = json_print_header,
1272 .print_footer = json_print_footer,
1273 .print_chapter_header = json_print_chapter_header,
1274 .print_chapter_footer = json_print_chapter_footer,
1275 .print_section_header = json_print_section_header,
1276 .print_section_footer = json_print_section_footer,
1277 .print_integer = json_print_int,
1278 .print_string = json_print_str,
1279 .show_tags = json_show_tags,
1280 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1286 const AVClass *class;
1289 int fully_qualified;
1294 #define OFFSET(x) offsetof(XMLContext, x)
1296 static const AVOption xml_options[] = {
1297 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
1298 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
1299 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
1300 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
1304 static const char *xml_get_name(void *ctx)
1309 static const AVClass xml_class = {
1315 static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
1317 XMLContext *xml = wctx->priv;
1320 xml->class = &xml_class;
1321 av_opt_set_defaults(xml);
1324 (err = (av_set_options_string(xml, args, "=", ":"))) < 0) {
1325 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
1329 if (xml->xsd_strict) {
1330 xml->fully_qualified = 1;
1331 #define CHECK_COMPLIANCE(opt, opt_name) \
1333 av_log(wctx, AV_LOG_ERROR, \
1334 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1335 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1336 return AVERROR(EINVAL); \
1338 CHECK_COMPLIANCE(show_private_data, "private");
1339 CHECK_COMPLIANCE(show_value_unit, "unit");
1340 CHECK_COMPLIANCE(use_value_prefix, "prefix");
1342 if (do_show_frames && do_show_packets) {
1343 av_log(wctx, AV_LOG_ERROR,
1344 "Interleaved frames and packets are not allowed in XSD. "
1345 "Select only one between the -show_frames and the -show_packets options.\n");
1346 return AVERROR(EINVAL);
1353 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1357 for (p = src; *p; p++) {
1359 case '&' : av_bprintf(dst, "%s", "&"); break;
1360 case '<' : av_bprintf(dst, "%s", "<"); break;
1361 case '>' : av_bprintf(dst, "%s", ">"); break;
1362 case '\"': av_bprintf(dst, "%s", """); break;
1363 case '\'': av_bprintf(dst, "%s", "'"); break;
1364 default: av_bprint_chars(dst, *p, 1);
1371 static void xml_print_header(WriterContext *wctx)
1373 XMLContext *xml = wctx->priv;
1374 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1375 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1376 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1378 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1379 printf("<%sffprobe%s>\n",
1380 xml->fully_qualified ? "ffprobe:" : "",
1381 xml->fully_qualified ? qual : "");
1383 xml->indent_level++;
1386 static void xml_print_footer(WriterContext *wctx)
1388 XMLContext *xml = wctx->priv;
1390 xml->indent_level--;
1391 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1394 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1396 static void xml_print_chapter_header(WriterContext *wctx, const char *chapter)
1398 XMLContext *xml = wctx->priv;
1400 if (wctx->nb_chapter)
1402 if (wctx->multiple_sections) {
1403 XML_INDENT(); printf("<%s>\n", chapter);
1404 xml->indent_level++;
1408 static void xml_print_chapter_footer(WriterContext *wctx, const char *chapter)
1410 XMLContext *xml = wctx->priv;
1412 if (wctx->multiple_sections) {
1413 xml->indent_level--;
1414 XML_INDENT(); printf("</%s>\n", chapter);
1418 static void xml_print_section_header(WriterContext *wctx, const char *section)
1420 XMLContext *xml = wctx->priv;
1422 XML_INDENT(); printf("<%s ", section);
1423 xml->within_tag = 1;
1426 static void xml_print_section_footer(WriterContext *wctx, const char *section)
1428 XMLContext *xml = wctx->priv;
1430 if (xml->within_tag)
1433 XML_INDENT(); printf("</%s>\n", section);
1437 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1443 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1444 printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
1445 av_bprint_finalize(&buf, NULL);
1448 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1452 printf("%s=\"%lld\"", key, value);
1455 static void xml_show_tags(WriterContext *wctx, AVDictionary *dict)
1457 XMLContext *xml = wctx->priv;
1458 AVDictionaryEntry *tag = NULL;
1462 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1463 xml->indent_level++;
1464 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1466 /* close section tag */
1468 xml->within_tag = 0;
1473 av_bprint_clear(&buf);
1474 printf("<tag key=\"%s\"", xml_escape_str(&buf, tag->key, wctx));
1475 av_bprint_clear(&buf);
1476 printf(" value=\"%s\"/>\n", xml_escape_str(&buf, tag->value, wctx));
1478 av_bprint_finalize(&buf, NULL);
1479 xml->indent_level--;
1482 static Writer xml_writer = {
1484 .priv_size = sizeof(XMLContext),
1486 .print_header = xml_print_header,
1487 .print_footer = xml_print_footer,
1488 .print_chapter_header = xml_print_chapter_header,
1489 .print_chapter_footer = xml_print_chapter_footer,
1490 .print_section_header = xml_print_section_header,
1491 .print_section_footer = xml_print_section_footer,
1492 .print_integer = xml_print_int,
1493 .print_string = xml_print_str,
1494 .show_tags = xml_show_tags,
1495 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1498 static void writer_register_all(void)
1500 static int initialized;
1506 writer_register(&default_writer);
1507 writer_register(&compact_writer);
1508 writer_register(&csv_writer);
1509 writer_register(&flat_writer);
1510 writer_register(&ini_writer);
1511 writer_register(&json_writer);
1512 writer_register(&xml_writer);
1515 #define print_fmt(k, f, ...) do { \
1516 av_bprint_clear(&pbuf); \
1517 av_bprintf(&pbuf, f, __VA_ARGS__); \
1518 writer_print_string(w, k, pbuf.str, 0); \
1521 #define print_int(k, v) writer_print_integer(w, k, v)
1522 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1523 #define print_str(k, v) writer_print_string(w, k, v, 0)
1524 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
1525 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1526 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1527 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1528 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1529 #define print_val(k, v, u) writer_print_string(w, k, \
1530 value_string(val_str, sizeof(val_str), (struct unit_value){.val.i = v, .unit=u}), 0)
1531 #define print_section_header(s) writer_print_section_header(w, s)
1532 #define print_section_footer(s) writer_print_section_footer(w, s)
1533 #define show_tags(metadata) writer_show_tags(w, metadata)
1535 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1538 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1542 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1544 print_section_header("packet");
1545 s = av_get_media_type_string(st->codec->codec_type);
1546 if (s) print_str ("codec_type", s);
1547 else print_str_opt("codec_type", "unknown");
1548 print_int("stream_index", pkt->stream_index);
1549 print_ts ("pts", pkt->pts);
1550 print_time("pts_time", pkt->pts, &st->time_base);
1551 print_ts ("dts", pkt->dts);
1552 print_time("dts_time", pkt->dts, &st->time_base);
1553 print_duration_ts("duration", pkt->duration);
1554 print_duration_time("duration_time", pkt->duration, &st->time_base);
1555 print_val("size", pkt->size, unit_byte_str);
1556 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1557 else print_str_opt("pos", "N/A");
1558 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1559 print_section_footer("packet");
1561 av_bprint_finalize(&pbuf, NULL);
1565 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream)
1570 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1572 print_section_header("frame");
1574 s = av_get_media_type_string(stream->codec->codec_type);
1575 if (s) print_str ("media_type", s);
1576 else print_str_opt("media_type", "unknown");
1577 print_int("key_frame", frame->key_frame);
1578 print_ts ("pkt_pts", frame->pkt_pts);
1579 print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
1580 print_ts ("pkt_dts", frame->pkt_dts);
1581 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
1582 print_duration_ts ("pkt_duration", frame->pkt_duration);
1583 print_duration_time("pkt_duration_time", frame->pkt_duration, &stream->time_base);
1584 if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
1585 else print_str_opt("pkt_pos", "N/A");
1587 switch (stream->codec->codec_type) {
1588 case AVMEDIA_TYPE_VIDEO:
1589 print_int("width", frame->width);
1590 print_int("height", frame->height);
1591 s = av_get_pix_fmt_name(frame->format);
1592 if (s) print_str ("pix_fmt", s);
1593 else print_str_opt("pix_fmt", "unknown");
1594 if (frame->sample_aspect_ratio.num) {
1595 print_q("sample_aspect_ratio", frame->sample_aspect_ratio, ':');
1597 print_str_opt("sample_aspect_ratio", "N/A");
1599 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
1600 print_int("coded_picture_number", frame->coded_picture_number);
1601 print_int("display_picture_number", frame->display_picture_number);
1602 print_int("interlaced_frame", frame->interlaced_frame);
1603 print_int("top_field_first", frame->top_field_first);
1604 print_int("repeat_pict", frame->repeat_pict);
1605 print_int("reference", frame->reference);
1608 case AVMEDIA_TYPE_AUDIO:
1609 s = av_get_sample_fmt_name(frame->format);
1610 if (s) print_str ("sample_fmt", s);
1611 else print_str_opt("sample_fmt", "unknown");
1612 print_int("nb_samples", frame->nb_samples);
1616 print_section_footer("frame");
1618 av_bprint_finalize(&pbuf, NULL);
1622 static av_always_inline int get_decoded_frame(AVFormatContext *fmt_ctx,
1623 AVFrame *frame, int *got_frame,
1626 AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
1630 switch (dec_ctx->codec_type) {
1631 case AVMEDIA_TYPE_VIDEO:
1632 ret = avcodec_decode_video2(dec_ctx, frame, got_frame, pkt);
1635 case AVMEDIA_TYPE_AUDIO:
1636 ret = avcodec_decode_audio4(dec_ctx, frame, got_frame, pkt);
1643 static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1647 int i = 0, ret, got_frame;
1649 av_init_packet(&pkt);
1651 while (!av_read_frame(fmt_ctx, &pkt)) {
1652 if (do_read_packets) {
1653 if (do_show_packets)
1654 show_packet(w, fmt_ctx, &pkt, i++);
1655 nb_streams_packets[pkt.stream_index]++;
1657 if (do_read_frames) {
1660 avcodec_get_frame_defaults(&frame);
1661 ret = get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt1);
1662 if (ret < 0 || !got_frame)
1665 show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
1668 nb_streams_frames[pkt.stream_index]++;
1671 av_free_packet(&pkt);
1673 av_init_packet(&pkt);
1676 //Flush remaining frames that are cached in the decoder
1677 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1678 pkt.stream_index = i;
1679 while (get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt) >= 0 && got_frame) {
1680 if (do_read_frames) {
1682 show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
1683 nb_streams_frames[pkt.stream_index]++;
1689 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
1691 AVStream *stream = fmt_ctx->streams[stream_idx];
1692 AVCodecContext *dec_ctx;
1696 AVRational display_aspect_ratio;
1699 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1701 print_section_header("stream");
1703 print_int("index", stream->index);
1705 if ((dec_ctx = stream->codec)) {
1706 const char *profile = NULL;
1707 if ((dec = dec_ctx->codec)) {
1708 print_str("codec_name", dec->name);
1709 print_str("codec_long_name", dec->long_name);
1711 print_str_opt("codec_name", "unknown");
1712 print_str_opt("codec_long_name", "unknown");
1715 if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
1716 print_str("profile", profile);
1718 print_str_opt("profile", "unknown");
1720 s = av_get_media_type_string(dec_ctx->codec_type);
1721 if (s) print_str ("codec_type", s);
1722 else print_str_opt("codec_type", "unknown");
1723 print_q("codec_time_base", dec_ctx->time_base, '/');
1725 /* print AVI/FourCC tag */
1726 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
1727 print_str("codec_tag_string", val_str);
1728 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
1730 switch (dec_ctx->codec_type) {
1731 case AVMEDIA_TYPE_VIDEO:
1732 print_int("width", dec_ctx->width);
1733 print_int("height", dec_ctx->height);
1734 print_int("has_b_frames", dec_ctx->has_b_frames);
1735 if (dec_ctx->sample_aspect_ratio.num) {
1736 print_q("sample_aspect_ratio", dec_ctx->sample_aspect_ratio, ':');
1737 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1738 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
1739 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
1741 print_q("display_aspect_ratio", display_aspect_ratio, ':');
1743 print_str_opt("sample_aspect_ratio", "N/A");
1744 print_str_opt("display_aspect_ratio", "N/A");
1746 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
1747 if (s) print_str ("pix_fmt", s);
1748 else print_str_opt("pix_fmt", "unknown");
1749 print_int("level", dec_ctx->level);
1750 if (dec_ctx->timecode_frame_start >= 0) {
1751 char tcbuf[AV_TIMECODE_STR_SIZE];
1752 av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
1753 print_str("timecode", tcbuf);
1755 print_str_opt("timecode", "N/A");
1759 case AVMEDIA_TYPE_AUDIO:
1760 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
1761 if (s) print_str ("sample_fmt", s);
1762 else print_str_opt("sample_fmt", "unknown");
1763 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
1764 print_int("channels", dec_ctx->channels);
1765 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
1769 print_str_opt("codec_type", "unknown");
1771 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
1772 const AVOption *opt = NULL;
1773 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
1775 if (opt->flags) continue;
1776 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
1777 print_str(opt->name, str);
1783 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
1784 else print_str_opt("id", "N/A");
1785 print_q("r_frame_rate", stream->r_frame_rate, '/');
1786 print_q("avg_frame_rate", stream->avg_frame_rate, '/');
1787 print_q("time_base", stream->time_base, '/');
1788 print_time("start_time", stream->start_time, &stream->time_base);
1789 print_time("duration", stream->duration, &stream->time_base);
1790 if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
1791 else print_str_opt("bit_rate", "N/A");
1792 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
1793 else print_str_opt("nb_frames", "N/A");
1794 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
1795 else print_str_opt("nb_read_frames", "N/A");
1796 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
1797 else print_str_opt("nb_read_packets", "N/A");
1798 show_tags(stream->metadata);
1800 print_section_footer("stream");
1801 av_bprint_finalize(&pbuf, NULL);
1805 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
1808 for (i = 0; i < fmt_ctx->nb_streams; i++)
1809 show_stream(w, fmt_ctx, i);
1812 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
1815 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
1817 print_section_header("format");
1818 print_str("filename", fmt_ctx->filename);
1819 print_int("nb_streams", fmt_ctx->nb_streams);
1820 print_str("format_name", fmt_ctx->iformat->name);
1821 print_str("format_long_name", fmt_ctx->iformat->long_name);
1822 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
1823 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
1824 if (size >= 0) print_val ("size", size, unit_byte_str);
1825 else print_str_opt("size", "N/A");
1826 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
1827 else print_str_opt("bit_rate", "N/A");
1828 show_tags(fmt_ctx->metadata);
1829 print_section_footer("format");
1833 static void show_error(WriterContext *w, int err)
1836 const char *errbuf_ptr = errbuf;
1838 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
1839 errbuf_ptr = strerror(AVUNERROR(err));
1841 writer_print_chapter_header(w, "error");
1842 print_section_header("error");
1843 print_int("code", err);
1844 print_str("string", errbuf_ptr);
1845 print_section_footer("error");
1846 writer_print_chapter_footer(w, "error");
1849 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
1852 AVFormatContext *fmt_ctx = NULL;
1853 AVDictionaryEntry *t;
1855 if ((err = avformat_open_input(&fmt_ctx, filename,
1856 iformat, &format_opts)) < 0) {
1857 print_error(filename, err);
1860 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1861 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
1862 return AVERROR_OPTION_NOT_FOUND;
1866 /* fill the streams in the format context */
1867 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
1868 print_error(filename, err);
1872 av_dump_format(fmt_ctx, 0, filename, 0);
1874 /* bind a decoder to each input stream */
1875 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1876 AVStream *stream = fmt_ctx->streams[i];
1879 if (stream->codec->codec_id == CODEC_ID_PROBE) {
1880 av_log(NULL, AV_LOG_ERROR,
1881 "Failed to probe codec for input stream %d\n",
1883 } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
1884 av_log(NULL, AV_LOG_ERROR,
1885 "Unsupported codec with id %d for input stream %d\n",
1886 stream->codec->codec_id, stream->index);
1887 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
1888 av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
1893 *fmt_ctx_ptr = fmt_ctx;
1897 static void close_input_file(AVFormatContext **ctx_ptr)
1900 AVFormatContext *fmt_ctx = *ctx_ptr;
1902 /* close decoder for each stream */
1903 for (i = 0; i < fmt_ctx->nb_streams; i++)
1904 if (fmt_ctx->streams[i]->codec->codec_id != CODEC_ID_NONE)
1905 avcodec_close(fmt_ctx->streams[i]->codec);
1907 avformat_close_input(ctx_ptr);
1910 #define PRINT_CHAPTER(name) do { \
1911 if (do_show_ ## name) { \
1912 writer_print_chapter_header(wctx, #name); \
1913 show_ ## name (wctx, fmt_ctx); \
1914 writer_print_chapter_footer(wctx, #name); \
1918 static int probe_file(WriterContext *wctx, const char *filename)
1920 AVFormatContext *fmt_ctx;
1923 do_read_frames = do_show_frames || do_count_frames;
1924 do_read_packets = do_show_packets || do_count_packets;
1926 ret = open_input_file(&fmt_ctx, filename);
1928 nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
1929 nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
1930 if (do_read_frames || do_read_packets) {
1931 const char *chapter;
1932 if (do_show_frames && do_show_packets &&
1933 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
1934 chapter = "packets_and_frames";
1935 else if (do_show_packets && !do_show_frames)
1936 chapter = "packets";
1937 else // (!do_show_packets && do_show_frames)
1939 if (do_show_frames || do_show_packets)
1940 writer_print_chapter_header(wctx, chapter);
1941 read_packets(wctx, fmt_ctx);
1942 if (do_show_frames || do_show_packets)
1943 writer_print_chapter_footer(wctx, chapter);
1945 PRINT_CHAPTER(streams);
1946 PRINT_CHAPTER(format);
1947 close_input_file(&fmt_ctx);
1948 av_freep(&nb_streams_frames);
1949 av_freep(&nb_streams_packets);
1954 static void show_usage(void)
1956 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
1957 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
1958 av_log(NULL, AV_LOG_INFO, "\n");
1961 static void ffprobe_show_program_version(WriterContext *w)
1964 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1966 writer_print_chapter_header(w, "program_version");
1967 print_section_header("program_version");
1968 print_str("version", FFMPEG_VERSION);
1969 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
1970 program_birth_year, this_year);
1971 print_str("build_date", __DATE__);
1972 print_str("build_time", __TIME__);
1973 print_str("compiler_type", CC_TYPE);
1974 print_str("compiler_version", CC_VERSION);
1975 print_str("configuration", FFMPEG_CONFIGURATION);
1976 print_section_footer("program_version");
1977 writer_print_chapter_footer(w, "program_version");
1979 av_bprint_finalize(&pbuf, NULL);
1982 #define SHOW_LIB_VERSION(libname, LIBNAME) \
1984 if (CONFIG_##LIBNAME) { \
1985 unsigned int version = libname##_version(); \
1986 print_section_header("library_version"); \
1987 print_str("name", "lib" #libname); \
1988 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
1989 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
1990 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
1991 print_int("version", version); \
1992 print_section_footer("library_version"); \
1996 static void ffprobe_show_library_versions(WriterContext *w)
1998 writer_print_chapter_header(w, "library_versions");
1999 SHOW_LIB_VERSION(avutil, AVUTIL);
2000 SHOW_LIB_VERSION(avcodec, AVCODEC);
2001 SHOW_LIB_VERSION(avformat, AVFORMAT);
2002 SHOW_LIB_VERSION(avdevice, AVDEVICE);
2003 SHOW_LIB_VERSION(avfilter, AVFILTER);
2004 SHOW_LIB_VERSION(swscale, SWSCALE);
2005 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
2006 SHOW_LIB_VERSION(postproc, POSTPROC);
2007 writer_print_chapter_footer(w, "library_versions");
2010 static int opt_format(const char *opt, const char *arg)
2012 iformat = av_find_input_format(arg);
2014 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
2015 return AVERROR(EINVAL);
2020 static int opt_show_format_entry(const char *opt, const char *arg)
2023 av_dict_set(&fmt_entries_to_show, arg, "", 0);
2027 static void opt_input_file(void *optctx, const char *arg)
2029 if (input_filename) {
2030 av_log(NULL, AV_LOG_ERROR,
2031 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
2032 arg, input_filename);
2035 if (!strcmp(arg, "-"))
2037 input_filename = arg;
2040 static int opt_help(const char *opt, const char *arg)
2042 av_log_set_callback(log_callback_help);
2044 show_help_options(options, "Main options:\n", 0, 0);
2047 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
2052 static int opt_pretty(const char *opt, const char *arg)
2054 show_value_unit = 1;
2055 use_value_prefix = 1;
2056 use_byte_value_binary_prefix = 1;
2057 use_value_sexagesimal_format = 1;
2061 static int opt_show_versions(const char *opt, const char *arg)
2063 do_show_program_version = 1;
2064 do_show_library_versions = 1;
2068 static const OptionDef options[] = {
2069 #include "cmdutils_common_opts.h"
2070 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
2071 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
2072 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
2073 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
2074 "use binary prefixes for byte units" },
2075 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
2076 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
2077 { "pretty", 0, {(void*)&opt_pretty},
2078 "prettify the format of displayed values, make it more human readable" },
2079 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
2080 "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
2081 { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
2082 { "show_error", OPT_BOOL, {(void*)&do_show_error} , "show probing error" },
2083 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
2084 { "show_frames", OPT_BOOL, {(void*)&do_show_frames} , "show frames info" },
2085 { "show_format_entry", HAS_ARG, {(void*)opt_show_format_entry},
2086 "show a particular entry from the format/container info", "entry" },
2087 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
2088 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
2089 { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
2090 { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
2091 { "show_program_version", OPT_BOOL, {(void*)&do_show_program_version}, "show ffprobe version" },
2092 { "show_library_versions", OPT_BOOL, {(void*)&do_show_library_versions}, "show library versions" },
2093 { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
2094 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
2095 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
2096 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
2097 { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
2101 int main(int argc, char **argv)
2104 WriterContext *wctx;
2106 char *w_name = NULL, *w_args = NULL;
2109 av_log_set_flags(AV_LOG_SKIP_REPEATED);
2110 parse_loglevel(argc, argv, options);
2112 avformat_network_init();
2115 avdevice_register_all();
2118 show_banner(argc, argv, options);
2119 parse_options(NULL, argc, argv, options, opt_input_file);
2121 writer_register_all();
2124 print_format = av_strdup("default");
2125 w_name = av_strtok(print_format, "=", &buf);
2128 w = writer_get_by_name(w_name);
2130 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
2131 ret = AVERROR(EINVAL);
2135 if ((ret = writer_open(&wctx, w, w_args, NULL)) >= 0) {
2136 writer_print_header(wctx);
2138 if (do_show_program_version)
2139 ffprobe_show_program_version(wctx);
2140 if (do_show_library_versions)
2141 ffprobe_show_library_versions(wctx);
2143 if (!input_filename &&
2144 ((do_show_format || do_show_streams || do_show_packets || do_show_error) ||
2145 (!do_show_program_version && !do_show_library_versions))) {
2147 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
2148 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
2149 ret = AVERROR(EINVAL);
2150 } else if (input_filename) {
2151 ret = probe_file(wctx, input_filename);
2152 if (ret < 0 && do_show_error)
2153 show_error(wctx, ret);
2156 writer_print_footer(wctx);
2157 writer_close(&wctx);
2161 av_freep(&print_format);
2164 av_dict_free(&fmt_entries_to_show);
2166 avformat_network_deinit();