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_data = 0;
57 static int do_show_program_version = 0;
58 static int do_show_library_versions = 0;
60 static int show_value_unit = 0;
61 static int use_value_prefix = 0;
62 static int use_byte_value_binary_prefix = 0;
63 static int use_value_sexagesimal_format = 0;
64 static int show_private_data = 1;
66 static char *print_format;
68 static const OptionDef options[];
71 static const char *input_filename;
72 static AVInputFormat *iformat = NULL;
74 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
75 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
77 static const char unit_second_str[] = "s" ;
78 static const char unit_hertz_str[] = "Hz" ;
79 static const char unit_byte_str[] = "byte" ;
80 static const char unit_bit_per_second_str[] = "bit/s";
81 static uint64_t *nb_streams_packets;
82 static uint64_t *nb_streams_frames;
84 void av_noreturn exit_program(int ret)
86 av_dict_free(&fmt_entries_to_show);
91 union { double d; long long int i; } val;
95 static char *value_string(char *buf, int buf_size, struct unit_value uv)
100 if (uv.unit == unit_second_str) {
107 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
111 mins = (int)secs / 60;
112 secs = secs - mins * 60;
115 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
117 const char *prefix_string = "";
120 if (use_value_prefix && vald > 1) {
123 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
124 index = (long long int) (log(vald)/log(2)) / 10;
125 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
126 vald /= pow(2, index * 10);
127 prefix_string = binary_unit_prefixes[index];
129 index = (long long int) (log10(vald)) / 3;
130 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
131 vald /= pow(10, index * 3);
132 prefix_string = decimal_unit_prefixes[index];
136 if (show_float || (use_value_prefix && vald != (long long int)vald))
137 l = snprintf(buf, buf_size, "%f", vald);
139 l = snprintf(buf, buf_size, "%lld", (long long int)vald);
140 snprintf(buf+l, buf_size-l, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
141 prefix_string, show_value_unit ? uv.unit : "");
149 typedef struct WriterContext WriterContext;
151 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
152 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
154 typedef struct Writer {
155 int priv_size; ///< private size for the writer context
158 int (*init) (WriterContext *wctx, const char *args, void *opaque);
159 void (*uninit)(WriterContext *wctx);
161 void (*print_header)(WriterContext *ctx);
162 void (*print_footer)(WriterContext *ctx);
164 void (*print_chapter_header)(WriterContext *wctx, const char *);
165 void (*print_chapter_footer)(WriterContext *wctx, const char *);
166 void (*print_section_header)(WriterContext *wctx, const char *);
167 void (*print_section_footer)(WriterContext *wctx, const char *);
168 void (*print_integer) (WriterContext *wctx, const char *, long long int);
169 void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
170 void (*print_string) (WriterContext *wctx, const char *, const char *);
171 void (*show_tags) (WriterContext *wctx, AVDictionary *dict);
172 int flags; ///< a combination or WRITER_FLAG_*
175 struct WriterContext {
176 const AVClass *class; ///< class of the writer
177 const Writer *writer; ///< the Writer of which this is an instance
178 char *name; ///< name of this writer instance
179 void *priv; ///< private data for use by the filter
180 unsigned int nb_item; ///< number of the item printed in the given section, starting at 0
181 unsigned int nb_section; ///< number of the section printed in the given section sequence, starting at 0
182 unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
183 unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
184 unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
185 unsigned int nb_chapter; ///< number of the chapter, starting at 0
187 int multiple_sections; ///< tells if the current chapter can contain multiple sections
188 int is_fmt_chapter; ///< tells if the current chapter is "format", required by the print_format_entry option
189 int is_packets_and_frames; ///< tells if the current section is "packets_and_frames"
192 static const char *writer_get_name(void *p)
194 WriterContext *wctx = p;
195 return wctx->writer->name;
198 static const AVClass writer_class = {
202 LIBAVUTIL_VERSION_INT,
205 static void writer_close(WriterContext **wctx)
210 if ((*wctx)->writer->uninit)
211 (*wctx)->writer->uninit(*wctx);
212 av_freep(&((*wctx)->priv));
216 static int writer_open(WriterContext **wctx, const Writer *writer,
217 const char *args, void *opaque)
221 if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
222 ret = AVERROR(ENOMEM);
226 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
227 ret = AVERROR(ENOMEM);
231 (*wctx)->class = &writer_class;
232 (*wctx)->writer = writer;
233 if ((*wctx)->writer->init)
234 ret = (*wctx)->writer->init(*wctx, args, opaque);
245 static inline void writer_print_header(WriterContext *wctx)
247 if (wctx->writer->print_header)
248 wctx->writer->print_header(wctx);
249 wctx->nb_chapter = 0;
252 static inline void writer_print_footer(WriterContext *wctx)
254 if (wctx->writer->print_footer)
255 wctx->writer->print_footer(wctx);
258 static inline void writer_print_chapter_header(WriterContext *wctx,
262 wctx->nb_section_packet = wctx->nb_section_frame =
263 wctx->nb_section_packet_frame = 0;
264 wctx->is_packets_and_frames = !strcmp(chapter, "packets_and_frames");
265 wctx->multiple_sections = !strcmp(chapter, "packets") || !strcmp(chapter, "frames" ) ||
266 wctx->is_packets_and_frames ||
267 !strcmp(chapter, "streams") || !strcmp(chapter, "library_versions");
268 wctx->is_fmt_chapter = !strcmp(chapter, "format");
270 if (wctx->writer->print_chapter_header)
271 wctx->writer->print_chapter_header(wctx, chapter);
274 static inline void writer_print_chapter_footer(WriterContext *wctx,
277 if (wctx->writer->print_chapter_footer)
278 wctx->writer->print_chapter_footer(wctx, chapter);
282 static inline void writer_print_section_header(WriterContext *wctx,
285 if (wctx->is_packets_and_frames)
286 wctx->nb_section_packet_frame = !strcmp(section, "packet") ? wctx->nb_section_packet
287 : wctx->nb_section_frame;
288 if (wctx->writer->print_section_header)
289 wctx->writer->print_section_header(wctx, section);
293 static inline void writer_print_section_footer(WriterContext *wctx,
296 if (wctx->writer->print_section_footer)
297 wctx->writer->print_section_footer(wctx, section);
298 if (wctx->is_packets_and_frames) {
299 if (!strcmp(section, "packet")) wctx->nb_section_packet++;
300 else wctx->nb_section_frame++;
305 static inline void writer_print_integer(WriterContext *wctx,
306 const char *key, long long int val)
308 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
309 wctx->writer->print_integer(wctx, key, val);
314 static inline void writer_print_rational(WriterContext *wctx,
315 const char *key, AVRational q, char sep)
318 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
319 av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
320 wctx->writer->print_string(wctx, key, buf.str);
324 static inline void writer_print_string(WriterContext *wctx,
325 const char *key, const char *val, int opt)
327 if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
329 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
330 wctx->writer->print_string(wctx, key, val);
335 static void writer_print_time(WriterContext *wctx, const char *key,
336 int64_t ts, const AVRational *time_base, int is_duration)
340 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
341 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
342 writer_print_string(wctx, key, "N/A", 1);
344 double d = ts * av_q2d(*time_base);
345 value_string(buf, sizeof(buf), (struct unit_value){.val.d=d, .unit=unit_second_str});
346 writer_print_string(wctx, key, buf, 0);
351 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
353 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
354 writer_print_string(wctx, key, "N/A", 1);
356 writer_print_integer(wctx, key, ts);
360 static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
362 wctx->writer->show_tags(wctx, dict);
365 static void writer_print_data(WriterContext *wctx, const char *name,
366 uint8_t *data, int size)
369 int offset = 0, l, i;
371 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
372 av_bprintf(&bp, "\n");
374 av_bprintf(&bp, "%08x: ", offset);
376 for (i = 0; i < l; i++) {
377 av_bprintf(&bp, "%02x", data[i]);
379 av_bprintf(&bp, " ");
381 av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
382 for (i = 0; i < l; i++)
383 av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
384 av_bprintf(&bp, "\n");
389 writer_print_string(wctx, name, bp.str, 0);
390 av_bprint_finalize(&bp, NULL);
393 #define MAX_REGISTERED_WRITERS_NB 64
395 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
397 static int writer_register(const Writer *writer)
399 static int next_registered_writer_idx = 0;
401 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
402 return AVERROR(ENOMEM);
404 registered_writers[next_registered_writer_idx++] = writer;
408 static const Writer *writer_get_by_name(const char *name)
412 for (i = 0; registered_writers[i]; i++)
413 if (!strcmp(registered_writers[i]->name, name))
414 return registered_writers[i];
424 typedef struct DefaultContext {
425 const AVClass *class;
427 int noprint_wrappers;
430 #define OFFSET(x) offsetof(DefaultContext, x)
432 static const AVOption default_options[] = {
433 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
434 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
435 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
436 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
440 static const char *default_get_name(void *ctx)
445 static const AVClass default_class = {
451 static av_cold int default_init(WriterContext *wctx, const char *args, void *opaque)
453 DefaultContext *def = wctx->priv;
456 def->class = &default_class;
457 av_opt_set_defaults(def);
460 (err = (av_set_options_string(def, args, "=", ":"))) < 0) {
461 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
468 static void default_print_footer(WriterContext *wctx)
470 DefaultContext *def = wctx->priv;
472 if (!def->noprint_wrappers)
476 static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
478 DefaultContext *def = wctx->priv;
480 if (!def->noprint_wrappers && wctx->nb_chapter)
484 /* lame uppercasing routine, assumes the string is lower case ASCII */
485 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
488 for (i = 0; src[i] && i < dst_size-1; i++)
489 dst[i] = av_toupper(src[i]);
494 static void default_print_section_header(WriterContext *wctx, const char *section)
496 DefaultContext *def = wctx->priv;
499 if (wctx->nb_section)
501 if (!def->noprint_wrappers)
502 printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
505 static void default_print_section_footer(WriterContext *wctx, const char *section)
507 DefaultContext *def = wctx->priv;
510 if (!def->noprint_wrappers)
511 printf("[/%s]", upcase_string(buf, sizeof(buf), section));
514 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
516 DefaultContext *def = wctx->priv;
519 printf("%s\n", value);
522 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
524 DefaultContext *def = wctx->priv;
528 printf("%lld\n", value);
531 static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
533 AVDictionaryEntry *tag = NULL;
534 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
535 if (!fmt_entries_to_show || (tag->key && av_dict_get(fmt_entries_to_show, tag->key, NULL, 0)))
537 writer_print_string(wctx, tag->key, tag->value, 0);
541 static const Writer default_writer = {
543 .priv_size = sizeof(DefaultContext),
544 .init = default_init,
545 .print_footer = default_print_footer,
546 .print_chapter_header = default_print_chapter_header,
547 .print_section_header = default_print_section_header,
548 .print_section_footer = default_print_section_footer,
549 .print_integer = default_print_int,
550 .print_string = default_print_str,
551 .show_tags = default_show_tags,
552 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
558 * Apply C-language-like string escaping.
560 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
564 for (p = src; *p; p++) {
566 case '\b': av_bprintf(dst, "%s", "\\b"); break;
567 case '\f': av_bprintf(dst, "%s", "\\f"); break;
568 case '\n': av_bprintf(dst, "%s", "\\n"); break;
569 case '\r': av_bprintf(dst, "%s", "\\r"); break;
570 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
573 av_bprint_chars(dst, '\\', 1);
574 av_bprint_chars(dst, *p, 1);
581 * Quote fields containing special characters, check RFC4180.
583 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
588 /* check if input needs quoting */
589 for (p = src; *p; p++)
590 if (*p == '"' || *p == sep || *p == '\n' || *p == '\r')
594 av_bprint_chars(dst, '\"', 1);
596 for (p = src; *p; p++) {
598 av_bprint_chars(dst, '\"', 1);
599 av_bprint_chars(dst, *p, 1);
602 av_bprint_chars(dst, '\"', 1);
606 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
611 typedef struct CompactContext {
612 const AVClass *class;
616 char *escape_mode_str;
617 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
621 #define OFFSET(x) offsetof(CompactContext, x)
623 static const AVOption compact_options[]= {
624 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
625 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
626 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
627 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
628 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
629 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
633 static const char *compact_get_name(void *ctx)
638 static const AVClass compact_class = {
644 static av_cold int compact_init(WriterContext *wctx, const char *args, void *opaque)
646 CompactContext *compact = wctx->priv;
649 compact->class = &compact_class;
650 av_opt_set_defaults(compact);
653 (err = (av_set_options_string(compact, args, "=", ":"))) < 0) {
654 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
657 if (strlen(compact->item_sep_str) != 1) {
658 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
659 compact->item_sep_str);
660 return AVERROR(EINVAL);
662 compact->item_sep = compact->item_sep_str[0];
664 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
665 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
666 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
668 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
669 return AVERROR(EINVAL);
675 static av_cold void compact_uninit(WriterContext *wctx)
677 CompactContext *compact = wctx->priv;
679 av_freep(&compact->item_sep_str);
680 av_freep(&compact->escape_mode_str);
683 static void compact_print_section_header(WriterContext *wctx, const char *section)
685 CompactContext *compact = wctx->priv;
687 printf("%s%c", section, compact->item_sep);
690 static void compact_print_section_footer(WriterContext *wctx, const char *section)
695 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
697 CompactContext *compact = wctx->priv;
700 if (wctx->nb_item) printf("%c", compact->item_sep);
703 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
704 printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
705 av_bprint_finalize(&buf, NULL);
708 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
710 CompactContext *compact = wctx->priv;
712 if (wctx->nb_item) printf("%c", compact->item_sep);
715 printf("%lld", value);
718 static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
720 CompactContext *compact = wctx->priv;
721 AVDictionaryEntry *tag = NULL;
724 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
725 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
726 if (wctx->nb_item) printf("%c", compact->item_sep);
727 if (!compact->nokey) {
728 av_bprint_clear(&buf);
729 printf("tag:%s=", compact->escape_str(&buf, tag->key, compact->item_sep, wctx));
731 av_bprint_clear(&buf);
732 printf("%s", compact->escape_str(&buf, tag->value, compact->item_sep, wctx));
734 av_bprint_finalize(&buf, NULL);
737 static const Writer compact_writer = {
739 .priv_size = sizeof(CompactContext),
740 .init = compact_init,
741 .uninit = compact_uninit,
742 .print_section_header = compact_print_section_header,
743 .print_section_footer = compact_print_section_footer,
744 .print_integer = compact_print_int,
745 .print_string = compact_print_str,
746 .show_tags = compact_show_tags,
747 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
752 static av_cold int csv_init(WriterContext *wctx, const char *args, void *opaque)
754 return compact_init(wctx, "item_sep=,:nokey=1:escape=csv", opaque);
757 static const Writer csv_writer = {
759 .priv_size = sizeof(CompactContext),
761 .uninit = compact_uninit,
762 .print_section_header = compact_print_section_header,
763 .print_section_footer = compact_print_section_footer,
764 .print_integer = compact_print_int,
765 .print_string = compact_print_str,
766 .show_tags = compact_show_tags,
767 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
772 typedef struct FlatContext {
773 const AVClass *class;
774 const char *section, *chapter;
781 #define OFFSET(x) offsetof(FlatContext, x)
783 static const AVOption flat_options[]= {
784 {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
785 {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
786 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
787 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
791 static const char *flat_get_name(void *ctx)
796 static const AVClass flat_class = {
802 static av_cold int flat_init(WriterContext *wctx, const char *args, void *opaque)
804 FlatContext *flat = wctx->priv;
807 flat->class = &flat_class;
808 av_opt_set_defaults(flat);
811 (err = (av_set_options_string(flat, args, "=", ":"))) < 0) {
812 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
815 if (strlen(flat->sep_str) != 1) {
816 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
818 return AVERROR(EINVAL);
820 flat->sep = flat->sep_str[0];
824 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
828 for (p = src; *p; p++) {
829 if (!((*p >= '0' && *p <= '9') ||
830 (*p >= 'a' && *p <= 'z') ||
831 (*p >= 'A' && *p <= 'Z')))
832 av_bprint_chars(dst, '_', 1);
834 av_bprint_chars(dst, *p, 1);
839 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
843 for (p = src; *p; p++) {
845 case '\n': av_bprintf(dst, "%s", "\\n"); break;
846 case '\r': av_bprintf(dst, "%s", "\\r"); break;
847 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
848 case '"': av_bprintf(dst, "%s", "\\\""); break;
849 case '`': av_bprintf(dst, "%s", "\\`"); break;
850 case '$': av_bprintf(dst, "%s", "\\$"); break;
851 default: av_bprint_chars(dst, *p, 1); break;
857 static void flat_print_chapter_header(WriterContext *wctx, const char *chapter)
859 FlatContext *flat = wctx->priv;
860 flat->chapter = chapter;
863 static void flat_print_section_header(WriterContext *wctx, const char *section)
865 FlatContext *flat = wctx->priv;
866 flat->section = section;
869 static void flat_print_section(WriterContext *wctx)
871 FlatContext *flat = wctx->priv;
872 int n = wctx->is_packets_and_frames ? wctx->nb_section_packet_frame
875 if (flat->hierarchical && wctx->multiple_sections)
876 printf("%s%c", flat->chapter, flat->sep);
877 printf("%s%c", flat->section, flat->sep);
878 if (wctx->multiple_sections)
879 printf("%d%c", n, flat->sep);
882 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
884 flat_print_section(wctx);
885 printf("%s=%lld\n", key, value);
888 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
890 FlatContext *flat = wctx->priv;
893 flat_print_section(wctx);
894 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
895 printf("%s=", flat_escape_key_str(&buf, key, flat->sep));
896 av_bprint_clear(&buf);
897 printf("\"%s\"\n", flat_escape_value_str(&buf, value));
898 av_bprint_finalize(&buf, NULL);
901 static void flat_show_tags(WriterContext *wctx, AVDictionary *dict)
903 FlatContext *flat = wctx->priv;
905 AVDictionaryEntry *tag = NULL;
907 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
908 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
909 flat_print_section(wctx);
910 av_bprint_clear(&buf);
911 printf("tags%c%s=", flat->sep, flat_escape_key_str(&buf, tag->key, flat->sep));
912 av_bprint_clear(&buf);
913 printf("\"%s\"\n", flat_escape_value_str(&buf, tag->value));
915 av_bprint_finalize(&buf, NULL);
918 static const Writer flat_writer = {
920 .priv_size = sizeof(FlatContext),
922 .print_chapter_header = flat_print_chapter_header,
923 .print_section_header = flat_print_section_header,
924 .print_integer = flat_print_int,
925 .print_string = flat_print_str,
926 .show_tags = flat_show_tags,
927 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
930 /* INI format output */
933 const AVClass *class;
934 AVBPrint chapter_name, section_name;
939 #define OFFSET(x) offsetof(INIContext, x)
941 static const AVOption ini_options[] = {
942 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
943 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
947 static const char *ini_get_name(void *ctx)
952 static const AVClass ini_class = {
958 static av_cold int ini_init(WriterContext *wctx, const char *args, void *opaque)
960 INIContext *ini = wctx->priv;
963 av_bprint_init(&ini->chapter_name, 1, AV_BPRINT_SIZE_UNLIMITED);
964 av_bprint_init(&ini->section_name, 1, AV_BPRINT_SIZE_UNLIMITED);
966 ini->class = &ini_class;
967 av_opt_set_defaults(ini);
969 if (args && (err = av_set_options_string(ini, args, "=", ":")) < 0) {
970 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
977 static av_cold void ini_uninit(WriterContext *wctx)
979 INIContext *ini = wctx->priv;
980 av_bprint_finalize(&ini->chapter_name, NULL);
981 av_bprint_finalize(&ini->section_name, NULL);
984 static void ini_print_header(WriterContext *wctx)
986 printf("# ffprobe output\n\n");
989 static char *ini_escape_str(AVBPrint *dst, const char *src)
994 while (c = src[i++]) {
996 case '\b': av_bprintf(dst, "%s", "\\b"); break;
997 case '\f': av_bprintf(dst, "%s", "\\f"); break;
998 case '\n': av_bprintf(dst, "%s", "\\n"); break;
999 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1000 case '\t': av_bprintf(dst, "%s", "\\t"); break;
1004 case ':' : av_bprint_chars(dst, '\\', 1);
1006 if ((unsigned char)c < 32)
1007 av_bprintf(dst, "\\x00%02x", c & 0xff);
1009 av_bprint_chars(dst, c, 1);
1016 static void ini_print_chapter_header(WriterContext *wctx, const char *chapter)
1018 INIContext *ini = wctx->priv;
1020 av_bprint_clear(&ini->chapter_name);
1021 av_bprintf(&ini->chapter_name, "%s", chapter);
1023 if (wctx->nb_chapter)
1027 static void ini_print_section_header(WriterContext *wctx, const char *section)
1029 INIContext *ini = wctx->priv;
1030 int n = wctx->is_packets_and_frames ? wctx->nb_section_packet_frame
1032 if (wctx->nb_section)
1034 av_bprint_clear(&ini->section_name);
1036 if (ini->hierarchical && wctx->multiple_sections)
1037 av_bprintf(&ini->section_name, "%s.", ini->chapter_name.str);
1038 av_bprintf(&ini->section_name, "%s", section);
1040 if (wctx->multiple_sections)
1041 av_bprintf(&ini->section_name, ".%d", n);
1042 printf("[%s]\n", ini->section_name.str);
1045 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1049 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1050 printf("%s=", ini_escape_str(&buf, key));
1051 av_bprint_clear(&buf);
1052 printf("%s\n", ini_escape_str(&buf, value));
1053 av_bprint_finalize(&buf, NULL);
1056 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1058 printf("%s=%lld\n", key, value);
1061 static void ini_show_tags(WriterContext *wctx, AVDictionary *dict)
1063 INIContext *ini = wctx->priv;
1064 AVDictionaryEntry *tag = NULL;
1067 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1069 printf("\n[%s.tags]\n", ini->section_name.str);
1072 writer_print_string(wctx, tag->key, tag->value, 0);
1076 static const Writer ini_writer = {
1078 .priv_size = sizeof(INIContext),
1080 .uninit = ini_uninit,
1081 .print_header = ini_print_header,
1082 .print_chapter_header = ini_print_chapter_header,
1083 .print_section_header = ini_print_section_header,
1084 .print_integer = ini_print_int,
1085 .print_string = ini_print_str,
1086 .show_tags = ini_show_tags,
1087 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1093 const AVClass *class;
1096 const char *item_sep, *item_start_end;
1100 #define OFFSET(x) offsetof(JSONContext, x)
1102 static const AVOption json_options[]= {
1103 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
1104 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
1108 static const char *json_get_name(void *ctx)
1113 static const AVClass json_class = {
1119 static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
1121 JSONContext *json = wctx->priv;
1124 json->class = &json_class;
1125 av_opt_set_defaults(json);
1128 (err = (av_set_options_string(json, args, "=", ":"))) < 0) {
1129 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
1133 json->item_sep = json->compact ? ", " : ",\n";
1134 json->item_start_end = json->compact ? " " : "\n";
1139 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1141 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1142 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1145 for (p = src; *p; p++) {
1146 char *s = strchr(json_escape, *p);
1148 av_bprint_chars(dst, '\\', 1);
1149 av_bprint_chars(dst, json_subst[s - json_escape], 1);
1150 } else if ((unsigned char)*p < 32) {
1151 av_bprintf(dst, "\\u00%02x", *p & 0xff);
1153 av_bprint_chars(dst, *p, 1);
1159 static void json_print_header(WriterContext *wctx)
1161 JSONContext *json = wctx->priv;
1163 json->indent_level++;
1166 static void json_print_footer(WriterContext *wctx)
1168 JSONContext *json = wctx->priv;
1169 json->indent_level--;
1173 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
1175 static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
1177 JSONContext *json = wctx->priv;
1180 if (wctx->nb_chapter)
1183 if (wctx->multiple_sections) {
1185 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1186 printf("\"%s\": [\n", json_escape_str(&buf, chapter, wctx));
1187 av_bprint_finalize(&buf, NULL);
1188 json->indent_level++;
1192 static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
1194 JSONContext *json = wctx->priv;
1196 if (wctx->multiple_sections) {
1198 json->indent_level--;
1204 static void json_print_section_header(WriterContext *wctx, const char *section)
1206 JSONContext *json = wctx->priv;
1208 if (wctx->nb_section)
1211 if (!wctx->multiple_sections)
1212 printf("\"%s\": ", section);
1213 printf("{%s", json->item_start_end);
1214 json->indent_level++;
1215 /* this is required so the parser can distinguish between packets and frames */
1216 if (wctx->is_packets_and_frames) {
1219 printf("\"type\": \"%s\"%s", section, json->item_sep);
1223 static void json_print_section_footer(WriterContext *wctx, const char *section)
1225 JSONContext *json = wctx->priv;
1227 printf("%s", json->item_start_end);
1228 json->indent_level--;
1234 static inline void json_print_item_str(WriterContext *wctx,
1235 const char *key, const char *value)
1239 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1240 printf("\"%s\":", json_escape_str(&buf, key, wctx));
1241 av_bprint_clear(&buf);
1242 printf(" \"%s\"", json_escape_str(&buf, value, wctx));
1243 av_bprint_finalize(&buf, NULL);
1246 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1248 JSONContext *json = wctx->priv;
1250 if (wctx->nb_item) printf("%s", json->item_sep);
1253 json_print_item_str(wctx, key, value);
1256 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1258 JSONContext *json = wctx->priv;
1261 if (wctx->nb_item) printf("%s", json->item_sep);
1265 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1266 printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1267 av_bprint_finalize(&buf, NULL);
1270 static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
1272 JSONContext *json = wctx->priv;
1273 AVDictionaryEntry *tag = NULL;
1277 printf("%s", json->item_sep);
1280 printf("\"tags\": {%s", json->item_start_end);
1281 json->indent_level++;
1282 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1283 if (is_first) is_first = 0;
1284 else printf("%s", json->item_sep);
1287 json_print_item_str(wctx, tag->key, tag->value);
1289 json->indent_level--;
1290 printf("%s", json->item_start_end);
1296 static const Writer json_writer = {
1298 .priv_size = sizeof(JSONContext),
1300 .print_header = json_print_header,
1301 .print_footer = json_print_footer,
1302 .print_chapter_header = json_print_chapter_header,
1303 .print_chapter_footer = json_print_chapter_footer,
1304 .print_section_header = json_print_section_header,
1305 .print_section_footer = json_print_section_footer,
1306 .print_integer = json_print_int,
1307 .print_string = json_print_str,
1308 .show_tags = json_show_tags,
1309 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1315 const AVClass *class;
1318 int fully_qualified;
1323 #define OFFSET(x) offsetof(XMLContext, x)
1325 static const AVOption xml_options[] = {
1326 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
1327 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
1328 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
1329 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
1333 static const char *xml_get_name(void *ctx)
1338 static const AVClass xml_class = {
1344 static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
1346 XMLContext *xml = wctx->priv;
1349 xml->class = &xml_class;
1350 av_opt_set_defaults(xml);
1353 (err = (av_set_options_string(xml, args, "=", ":"))) < 0) {
1354 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
1358 if (xml->xsd_strict) {
1359 xml->fully_qualified = 1;
1360 #define CHECK_COMPLIANCE(opt, opt_name) \
1362 av_log(wctx, AV_LOG_ERROR, \
1363 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1364 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1365 return AVERROR(EINVAL); \
1367 CHECK_COMPLIANCE(show_private_data, "private");
1368 CHECK_COMPLIANCE(show_value_unit, "unit");
1369 CHECK_COMPLIANCE(use_value_prefix, "prefix");
1371 if (do_show_frames && do_show_packets) {
1372 av_log(wctx, AV_LOG_ERROR,
1373 "Interleaved frames and packets are not allowed in XSD. "
1374 "Select only one between the -show_frames and the -show_packets options.\n");
1375 return AVERROR(EINVAL);
1382 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1386 for (p = src; *p; p++) {
1388 case '&' : av_bprintf(dst, "%s", "&"); break;
1389 case '<' : av_bprintf(dst, "%s", "<"); break;
1390 case '>' : av_bprintf(dst, "%s", ">"); break;
1391 case '\"': av_bprintf(dst, "%s", """); break;
1392 case '\'': av_bprintf(dst, "%s", "'"); break;
1393 default: av_bprint_chars(dst, *p, 1);
1400 static void xml_print_header(WriterContext *wctx)
1402 XMLContext *xml = wctx->priv;
1403 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1404 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1405 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1407 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1408 printf("<%sffprobe%s>\n",
1409 xml->fully_qualified ? "ffprobe:" : "",
1410 xml->fully_qualified ? qual : "");
1412 xml->indent_level++;
1415 static void xml_print_footer(WriterContext *wctx)
1417 XMLContext *xml = wctx->priv;
1419 xml->indent_level--;
1420 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1423 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1425 static void xml_print_chapter_header(WriterContext *wctx, const char *chapter)
1427 XMLContext *xml = wctx->priv;
1429 if (wctx->nb_chapter)
1431 if (wctx->multiple_sections) {
1432 XML_INDENT(); printf("<%s>\n", chapter);
1433 xml->indent_level++;
1437 static void xml_print_chapter_footer(WriterContext *wctx, const char *chapter)
1439 XMLContext *xml = wctx->priv;
1441 if (wctx->multiple_sections) {
1442 xml->indent_level--;
1443 XML_INDENT(); printf("</%s>\n", chapter);
1447 static void xml_print_section_header(WriterContext *wctx, const char *section)
1449 XMLContext *xml = wctx->priv;
1451 XML_INDENT(); printf("<%s ", section);
1452 xml->within_tag = 1;
1455 static void xml_print_section_footer(WriterContext *wctx, const char *section)
1457 XMLContext *xml = wctx->priv;
1459 if (xml->within_tag)
1462 XML_INDENT(); printf("</%s>\n", section);
1466 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1472 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1473 printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
1474 av_bprint_finalize(&buf, NULL);
1477 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1481 printf("%s=\"%lld\"", key, value);
1484 static void xml_show_tags(WriterContext *wctx, AVDictionary *dict)
1486 XMLContext *xml = wctx->priv;
1487 AVDictionaryEntry *tag = NULL;
1491 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1492 xml->indent_level++;
1493 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1495 /* close section tag */
1497 xml->within_tag = 0;
1502 av_bprint_clear(&buf);
1503 printf("<tag key=\"%s\"", xml_escape_str(&buf, tag->key, wctx));
1504 av_bprint_clear(&buf);
1505 printf(" value=\"%s\"/>\n", xml_escape_str(&buf, tag->value, wctx));
1507 av_bprint_finalize(&buf, NULL);
1508 xml->indent_level--;
1511 static Writer xml_writer = {
1513 .priv_size = sizeof(XMLContext),
1515 .print_header = xml_print_header,
1516 .print_footer = xml_print_footer,
1517 .print_chapter_header = xml_print_chapter_header,
1518 .print_chapter_footer = xml_print_chapter_footer,
1519 .print_section_header = xml_print_section_header,
1520 .print_section_footer = xml_print_section_footer,
1521 .print_integer = xml_print_int,
1522 .print_string = xml_print_str,
1523 .show_tags = xml_show_tags,
1524 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1527 static void writer_register_all(void)
1529 static int initialized;
1535 writer_register(&default_writer);
1536 writer_register(&compact_writer);
1537 writer_register(&csv_writer);
1538 writer_register(&flat_writer);
1539 writer_register(&ini_writer);
1540 writer_register(&json_writer);
1541 writer_register(&xml_writer);
1544 #define print_fmt(k, f, ...) do { \
1545 av_bprint_clear(&pbuf); \
1546 av_bprintf(&pbuf, f, __VA_ARGS__); \
1547 writer_print_string(w, k, pbuf.str, 0); \
1550 #define print_int(k, v) writer_print_integer(w, k, v)
1551 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1552 #define print_str(k, v) writer_print_string(w, k, v, 0)
1553 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
1554 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1555 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1556 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1557 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1558 #define print_val(k, v, u) writer_print_string(w, k, \
1559 value_string(val_str, sizeof(val_str), (struct unit_value){.val.i = v, .unit=u}), 0)
1560 #define print_section_header(s) writer_print_section_header(w, s)
1561 #define print_section_footer(s) writer_print_section_footer(w, s)
1562 #define show_tags(metadata) writer_show_tags(w, metadata)
1564 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1567 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1571 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1573 print_section_header("packet");
1574 s = av_get_media_type_string(st->codec->codec_type);
1575 if (s) print_str ("codec_type", s);
1576 else print_str_opt("codec_type", "unknown");
1577 print_int("stream_index", pkt->stream_index);
1578 print_ts ("pts", pkt->pts);
1579 print_time("pts_time", pkt->pts, &st->time_base);
1580 print_ts ("dts", pkt->dts);
1581 print_time("dts_time", pkt->dts, &st->time_base);
1582 print_duration_ts("duration", pkt->duration);
1583 print_duration_time("duration_time", pkt->duration, &st->time_base);
1584 print_duration_ts("convergence_duration", pkt->convergence_duration);
1585 print_duration_time("convergence_duration_time", pkt->convergence_duration, &st->time_base);
1586 print_val("size", pkt->size, unit_byte_str);
1587 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1588 else print_str_opt("pos", "N/A");
1589 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1591 writer_print_data(w, "data", pkt->data, pkt->size);
1592 print_section_footer("packet");
1594 av_bprint_finalize(&pbuf, NULL);
1598 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream)
1603 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1605 print_section_header("frame");
1607 s = av_get_media_type_string(stream->codec->codec_type);
1608 if (s) print_str ("media_type", s);
1609 else print_str_opt("media_type", "unknown");
1610 print_int("key_frame", frame->key_frame);
1611 print_ts ("pkt_pts", frame->pkt_pts);
1612 print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
1613 print_ts ("pkt_dts", frame->pkt_dts);
1614 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
1615 print_duration_ts ("pkt_duration", frame->pkt_duration);
1616 print_duration_time("pkt_duration_time", frame->pkt_duration, &stream->time_base);
1617 if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
1618 else print_str_opt("pkt_pos", "N/A");
1620 switch (stream->codec->codec_type) {
1621 case AVMEDIA_TYPE_VIDEO:
1622 print_int("width", frame->width);
1623 print_int("height", frame->height);
1624 s = av_get_pix_fmt_name(frame->format);
1625 if (s) print_str ("pix_fmt", s);
1626 else print_str_opt("pix_fmt", "unknown");
1627 if (frame->sample_aspect_ratio.num) {
1628 print_q("sample_aspect_ratio", frame->sample_aspect_ratio, ':');
1630 print_str_opt("sample_aspect_ratio", "N/A");
1632 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
1633 print_int("coded_picture_number", frame->coded_picture_number);
1634 print_int("display_picture_number", frame->display_picture_number);
1635 print_int("interlaced_frame", frame->interlaced_frame);
1636 print_int("top_field_first", frame->top_field_first);
1637 print_int("repeat_pict", frame->repeat_pict);
1638 print_int("reference", frame->reference);
1641 case AVMEDIA_TYPE_AUDIO:
1642 s = av_get_sample_fmt_name(frame->format);
1643 if (s) print_str ("sample_fmt", s);
1644 else print_str_opt("sample_fmt", "unknown");
1645 print_int("nb_samples", frame->nb_samples);
1648 show_tags(av_frame_get_metadata(frame));
1650 print_section_footer("frame");
1652 av_bprint_finalize(&pbuf, NULL);
1656 static av_always_inline int get_decoded_frame(AVFormatContext *fmt_ctx,
1657 AVFrame *frame, int *got_frame,
1660 AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
1664 switch (dec_ctx->codec_type) {
1665 case AVMEDIA_TYPE_VIDEO:
1666 ret = avcodec_decode_video2(dec_ctx, frame, got_frame, pkt);
1669 case AVMEDIA_TYPE_AUDIO:
1670 ret = avcodec_decode_audio4(dec_ctx, frame, got_frame, pkt);
1677 static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1681 int i = 0, ret, got_frame;
1683 av_init_packet(&pkt);
1685 while (!av_read_frame(fmt_ctx, &pkt)) {
1686 if (do_read_packets) {
1687 if (do_show_packets)
1688 show_packet(w, fmt_ctx, &pkt, i++);
1689 nb_streams_packets[pkt.stream_index]++;
1691 if (do_read_frames) {
1694 avcodec_get_frame_defaults(&frame);
1695 ret = get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt1);
1696 if (ret < 0 || !got_frame)
1699 show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
1702 nb_streams_frames[pkt.stream_index]++;
1705 av_free_packet(&pkt);
1707 av_init_packet(&pkt);
1710 //Flush remaining frames that are cached in the decoder
1711 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1712 pkt.stream_index = i;
1713 while (get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt) >= 0 && got_frame) {
1714 if (do_read_frames) {
1716 show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
1717 nb_streams_frames[pkt.stream_index]++;
1723 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
1725 AVStream *stream = fmt_ctx->streams[stream_idx];
1726 AVCodecContext *dec_ctx;
1730 AVRational display_aspect_ratio;
1733 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1735 print_section_header("stream");
1737 print_int("index", stream->index);
1739 if ((dec_ctx = stream->codec)) {
1740 const char *profile = NULL;
1741 if ((dec = dec_ctx->codec)) {
1742 print_str("codec_name", dec->name);
1743 print_str("codec_long_name", dec->long_name);
1745 print_str_opt("codec_name", "unknown");
1746 print_str_opt("codec_long_name", "unknown");
1749 if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
1750 print_str("profile", profile);
1752 print_str_opt("profile", "unknown");
1754 s = av_get_media_type_string(dec_ctx->codec_type);
1755 if (s) print_str ("codec_type", s);
1756 else print_str_opt("codec_type", "unknown");
1757 print_q("codec_time_base", dec_ctx->time_base, '/');
1759 /* print AVI/FourCC tag */
1760 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
1761 print_str("codec_tag_string", val_str);
1762 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
1764 switch (dec_ctx->codec_type) {
1765 case AVMEDIA_TYPE_VIDEO:
1766 print_int("width", dec_ctx->width);
1767 print_int("height", dec_ctx->height);
1768 print_int("has_b_frames", dec_ctx->has_b_frames);
1769 if (dec_ctx->sample_aspect_ratio.num) {
1770 print_q("sample_aspect_ratio", dec_ctx->sample_aspect_ratio, ':');
1771 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1772 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
1773 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
1775 print_q("display_aspect_ratio", display_aspect_ratio, ':');
1777 print_str_opt("sample_aspect_ratio", "N/A");
1778 print_str_opt("display_aspect_ratio", "N/A");
1780 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
1781 if (s) print_str ("pix_fmt", s);
1782 else print_str_opt("pix_fmt", "unknown");
1783 print_int("level", dec_ctx->level);
1784 if (dec_ctx->timecode_frame_start >= 0) {
1785 char tcbuf[AV_TIMECODE_STR_SIZE];
1786 av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
1787 print_str("timecode", tcbuf);
1789 print_str_opt("timecode", "N/A");
1793 case AVMEDIA_TYPE_AUDIO:
1794 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
1795 if (s) print_str ("sample_fmt", s);
1796 else print_str_opt("sample_fmt", "unknown");
1797 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
1798 print_int("channels", dec_ctx->channels);
1799 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
1803 print_str_opt("codec_type", "unknown");
1805 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
1806 const AVOption *opt = NULL;
1807 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
1809 if (opt->flags) continue;
1810 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
1811 print_str(opt->name, str);
1817 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
1818 else print_str_opt("id", "N/A");
1819 print_q("r_frame_rate", stream->r_frame_rate, '/');
1820 print_q("avg_frame_rate", stream->avg_frame_rate, '/');
1821 print_q("time_base", stream->time_base, '/');
1822 print_time("start_time", stream->start_time, &stream->time_base);
1823 print_time("duration", stream->duration, &stream->time_base);
1824 if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
1825 else print_str_opt("bit_rate", "N/A");
1826 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
1827 else print_str_opt("nb_frames", "N/A");
1828 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
1829 else print_str_opt("nb_read_frames", "N/A");
1830 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
1831 else print_str_opt("nb_read_packets", "N/A");
1832 show_tags(stream->metadata);
1834 writer_print_data(w, "extradata", dec_ctx->extradata,
1835 dec_ctx->extradata_size);
1837 print_section_footer("stream");
1838 av_bprint_finalize(&pbuf, NULL);
1842 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
1845 for (i = 0; i < fmt_ctx->nb_streams; i++)
1846 show_stream(w, fmt_ctx, i);
1849 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
1852 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
1854 print_section_header("format");
1855 print_str("filename", fmt_ctx->filename);
1856 print_int("nb_streams", fmt_ctx->nb_streams);
1857 print_str("format_name", fmt_ctx->iformat->name);
1858 print_str("format_long_name", fmt_ctx->iformat->long_name);
1859 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
1860 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
1861 if (size >= 0) print_val ("size", size, unit_byte_str);
1862 else print_str_opt("size", "N/A");
1863 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
1864 else print_str_opt("bit_rate", "N/A");
1865 show_tags(fmt_ctx->metadata);
1866 print_section_footer("format");
1870 static void show_error(WriterContext *w, int err)
1873 const char *errbuf_ptr = errbuf;
1875 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
1876 errbuf_ptr = strerror(AVUNERROR(err));
1878 writer_print_chapter_header(w, "error");
1879 print_section_header("error");
1880 print_int("code", err);
1881 print_str("string", errbuf_ptr);
1882 print_section_footer("error");
1883 writer_print_chapter_footer(w, "error");
1886 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
1889 AVFormatContext *fmt_ctx = NULL;
1890 AVDictionaryEntry *t;
1892 if ((err = avformat_open_input(&fmt_ctx, filename,
1893 iformat, &format_opts)) < 0) {
1894 print_error(filename, err);
1897 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1898 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
1899 return AVERROR_OPTION_NOT_FOUND;
1903 /* fill the streams in the format context */
1904 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
1905 print_error(filename, err);
1909 av_dump_format(fmt_ctx, 0, filename, 0);
1911 /* bind a decoder to each input stream */
1912 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1913 AVStream *stream = fmt_ctx->streams[i];
1916 if (stream->codec->codec_id == CODEC_ID_PROBE) {
1917 av_log(NULL, AV_LOG_ERROR,
1918 "Failed to probe codec for input stream %d\n",
1920 } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
1921 av_log(NULL, AV_LOG_ERROR,
1922 "Unsupported codec with id %d for input stream %d\n",
1923 stream->codec->codec_id, stream->index);
1924 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
1925 av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
1930 *fmt_ctx_ptr = fmt_ctx;
1934 static void close_input_file(AVFormatContext **ctx_ptr)
1937 AVFormatContext *fmt_ctx = *ctx_ptr;
1939 /* close decoder for each stream */
1940 for (i = 0; i < fmt_ctx->nb_streams; i++)
1941 if (fmt_ctx->streams[i]->codec->codec_id != CODEC_ID_NONE)
1942 avcodec_close(fmt_ctx->streams[i]->codec);
1944 avformat_close_input(ctx_ptr);
1947 #define PRINT_CHAPTER(name) do { \
1948 if (do_show_ ## name) { \
1949 writer_print_chapter_header(wctx, #name); \
1950 show_ ## name (wctx, fmt_ctx); \
1951 writer_print_chapter_footer(wctx, #name); \
1955 static int probe_file(WriterContext *wctx, const char *filename)
1957 AVFormatContext *fmt_ctx;
1960 do_read_frames = do_show_frames || do_count_frames;
1961 do_read_packets = do_show_packets || do_count_packets;
1963 ret = open_input_file(&fmt_ctx, filename);
1965 nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
1966 nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
1967 if (do_read_frames || do_read_packets) {
1968 const char *chapter;
1969 if (do_show_frames && do_show_packets &&
1970 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
1971 chapter = "packets_and_frames";
1972 else if (do_show_packets && !do_show_frames)
1973 chapter = "packets";
1974 else // (!do_show_packets && do_show_frames)
1976 if (do_show_frames || do_show_packets)
1977 writer_print_chapter_header(wctx, chapter);
1978 read_packets(wctx, fmt_ctx);
1979 if (do_show_frames || do_show_packets)
1980 writer_print_chapter_footer(wctx, chapter);
1982 PRINT_CHAPTER(streams);
1983 PRINT_CHAPTER(format);
1984 close_input_file(&fmt_ctx);
1985 av_freep(&nb_streams_frames);
1986 av_freep(&nb_streams_packets);
1991 static void show_usage(void)
1993 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
1994 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
1995 av_log(NULL, AV_LOG_INFO, "\n");
1998 static void ffprobe_show_program_version(WriterContext *w)
2001 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2003 writer_print_chapter_header(w, "program_version");
2004 print_section_header("program_version");
2005 print_str("version", FFMPEG_VERSION);
2006 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
2007 program_birth_year, this_year);
2008 print_str("build_date", __DATE__);
2009 print_str("build_time", __TIME__);
2010 print_str("compiler_type", CC_TYPE);
2011 print_str("compiler_version", CC_VERSION);
2012 print_str("configuration", FFMPEG_CONFIGURATION);
2013 print_section_footer("program_version");
2014 writer_print_chapter_footer(w, "program_version");
2016 av_bprint_finalize(&pbuf, NULL);
2019 #define SHOW_LIB_VERSION(libname, LIBNAME) \
2021 if (CONFIG_##LIBNAME) { \
2022 unsigned int version = libname##_version(); \
2023 print_section_header("library_version"); \
2024 print_str("name", "lib" #libname); \
2025 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
2026 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
2027 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
2028 print_int("version", version); \
2029 print_section_footer("library_version"); \
2033 static void ffprobe_show_library_versions(WriterContext *w)
2035 writer_print_chapter_header(w, "library_versions");
2036 SHOW_LIB_VERSION(avutil, AVUTIL);
2037 SHOW_LIB_VERSION(avcodec, AVCODEC);
2038 SHOW_LIB_VERSION(avformat, AVFORMAT);
2039 SHOW_LIB_VERSION(avdevice, AVDEVICE);
2040 SHOW_LIB_VERSION(avfilter, AVFILTER);
2041 SHOW_LIB_VERSION(swscale, SWSCALE);
2042 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
2043 SHOW_LIB_VERSION(postproc, POSTPROC);
2044 writer_print_chapter_footer(w, "library_versions");
2047 static int opt_format(const char *opt, const char *arg)
2049 iformat = av_find_input_format(arg);
2051 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
2052 return AVERROR(EINVAL);
2057 static int opt_show_format_entry(const char *opt, const char *arg)
2060 av_dict_set(&fmt_entries_to_show, arg, "", 0);
2064 static void opt_input_file(void *optctx, const char *arg)
2066 if (input_filename) {
2067 av_log(NULL, AV_LOG_ERROR,
2068 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
2069 arg, input_filename);
2072 if (!strcmp(arg, "-"))
2074 input_filename = arg;
2077 static int opt_help(const char *opt, const char *arg)
2079 av_log_set_callback(log_callback_help);
2081 show_help_options(options, "Main options:\n", 0, 0);
2084 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
2089 static int opt_pretty(const char *opt, const char *arg)
2091 show_value_unit = 1;
2092 use_value_prefix = 1;
2093 use_byte_value_binary_prefix = 1;
2094 use_value_sexagesimal_format = 1;
2098 static int opt_show_versions(const char *opt, const char *arg)
2100 do_show_program_version = 1;
2101 do_show_library_versions = 1;
2105 static const OptionDef options[] = {
2106 #include "cmdutils_common_opts.h"
2107 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
2108 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
2109 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
2110 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
2111 "use binary prefixes for byte units" },
2112 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
2113 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
2114 { "pretty", 0, {(void*)&opt_pretty},
2115 "prettify the format of displayed values, make it more human readable" },
2116 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
2117 "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
2118 { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
2119 { "show_data", OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
2120 { "show_error", OPT_BOOL, {(void*)&do_show_error} , "show probing error" },
2121 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
2122 { "show_frames", OPT_BOOL, {(void*)&do_show_frames} , "show frames info" },
2123 { "show_format_entry", HAS_ARG, {(void*)opt_show_format_entry},
2124 "show a particular entry from the format/container info", "entry" },
2125 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
2126 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
2127 { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
2128 { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
2129 { "show_program_version", OPT_BOOL, {(void*)&do_show_program_version}, "show ffprobe version" },
2130 { "show_library_versions", OPT_BOOL, {(void*)&do_show_library_versions}, "show library versions" },
2131 { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
2132 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
2133 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
2134 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
2135 { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
2139 int main(int argc, char **argv)
2142 WriterContext *wctx;
2144 char *w_name = NULL, *w_args = NULL;
2147 av_log_set_flags(AV_LOG_SKIP_REPEATED);
2148 parse_loglevel(argc, argv, options);
2150 avformat_network_init();
2153 avdevice_register_all();
2156 show_banner(argc, argv, options);
2157 parse_options(NULL, argc, argv, options, opt_input_file);
2159 writer_register_all();
2162 print_format = av_strdup("default");
2163 w_name = av_strtok(print_format, "=", &buf);
2166 w = writer_get_by_name(w_name);
2168 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
2169 ret = AVERROR(EINVAL);
2173 if ((ret = writer_open(&wctx, w, w_args, NULL)) >= 0) {
2174 writer_print_header(wctx);
2176 if (do_show_program_version)
2177 ffprobe_show_program_version(wctx);
2178 if (do_show_library_versions)
2179 ffprobe_show_library_versions(wctx);
2181 if (!input_filename &&
2182 ((do_show_format || do_show_streams || do_show_packets || do_show_error) ||
2183 (!do_show_program_version && !do_show_library_versions))) {
2185 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
2186 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
2187 ret = AVERROR(EINVAL);
2188 } else if (input_filename) {
2189 ret = probe_file(wctx, input_filename);
2190 if (ret < 0 && do_show_error)
2191 show_error(wctx, ret);
2194 writer_print_footer(wctx);
2195 writer_close(&wctx);
2199 av_freep(&print_format);
2202 av_dict_free(&fmt_entries_to_show);
2204 avformat_network_deinit();