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 nb_fmt_entries_to_show;
55 static int do_show_packets = 0;
56 static int do_show_streams = 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_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_chapter; ///< number of the chapter, starting at 0
183 int is_fmt_chapter; ///< tells if the current chapter is "format", required by the print_format_entry option
186 static const char *writer_get_name(void *p)
188 WriterContext *wctx = p;
189 return wctx->writer->name;
192 static const AVClass writer_class = {
196 LIBAVUTIL_VERSION_INT,
199 static void writer_close(WriterContext **wctx)
204 if ((*wctx)->writer->uninit)
205 (*wctx)->writer->uninit(*wctx);
206 av_freep(&((*wctx)->priv));
210 static int writer_open(WriterContext **wctx, const Writer *writer,
211 const char *args, void *opaque)
215 if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
216 ret = AVERROR(ENOMEM);
220 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
221 ret = AVERROR(ENOMEM);
225 (*wctx)->class = &writer_class;
226 (*wctx)->writer = writer;
227 if ((*wctx)->writer->init)
228 ret = (*wctx)->writer->init(*wctx, args, opaque);
239 static inline void writer_print_header(WriterContext *wctx)
241 if (wctx->writer->print_header)
242 wctx->writer->print_header(wctx);
243 wctx->nb_chapter = 0;
246 static inline void writer_print_footer(WriterContext *wctx)
248 if (wctx->writer->print_footer)
249 wctx->writer->print_footer(wctx);
252 static inline void writer_print_chapter_header(WriterContext *wctx,
255 if (wctx->writer->print_chapter_header)
256 wctx->writer->print_chapter_header(wctx, chapter);
257 wctx->nb_section = 0;
259 wctx->is_fmt_chapter = !strcmp(chapter, "format");
262 static inline void writer_print_chapter_footer(WriterContext *wctx,
265 if (wctx->writer->print_chapter_footer)
266 wctx->writer->print_chapter_footer(wctx, chapter);
270 static inline void writer_print_section_header(WriterContext *wctx,
273 if (wctx->writer->print_section_header)
274 wctx->writer->print_section_header(wctx, section);
278 static inline void writer_print_section_footer(WriterContext *wctx,
281 if (wctx->writer->print_section_footer)
282 wctx->writer->print_section_footer(wctx, section);
286 static inline void writer_print_integer(WriterContext *wctx,
287 const char *key, long long int val)
289 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
290 wctx->writer->print_integer(wctx, key, val);
295 static inline void writer_print_string(WriterContext *wctx,
296 const char *key, const char *val, int opt)
298 if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
300 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
301 wctx->writer->print_string(wctx, key, val);
306 static void writer_print_time(WriterContext *wctx, const char *key,
307 int64_t ts, const AVRational *time_base)
311 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
312 if (ts == AV_NOPTS_VALUE) {
313 writer_print_string(wctx, key, "N/A", 1);
315 double d = ts * av_q2d(*time_base);
316 value_string(buf, sizeof(buf), (struct unit_value){.val.d=d, .unit=unit_second_str});
317 writer_print_string(wctx, key, buf, 0);
322 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts)
324 if (ts == AV_NOPTS_VALUE) {
325 writer_print_string(wctx, key, "N/A", 1);
327 writer_print_integer(wctx, key, ts);
331 static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
333 wctx->writer->show_tags(wctx, dict);
336 #define MAX_REGISTERED_WRITERS_NB 64
338 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
340 static int writer_register(const Writer *writer)
342 static int next_registered_writer_idx = 0;
344 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
345 return AVERROR(ENOMEM);
347 registered_writers[next_registered_writer_idx++] = writer;
351 static const Writer *writer_get_by_name(const char *name)
355 for (i = 0; registered_writers[i]; i++)
356 if (!strcmp(registered_writers[i]->name, name))
357 return registered_writers[i];
367 typedef struct DefaultContext {
368 const AVClass *class;
370 int noprint_wrappers;
373 #define OFFSET(x) offsetof(DefaultContext, x)
375 static const AVOption default_options[] = {
376 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
377 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
378 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
379 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
383 static const char *default_get_name(void *ctx)
388 static const AVClass default_class = {
394 static av_cold int default_init(WriterContext *wctx, const char *args, void *opaque)
396 DefaultContext *def = wctx->priv;
399 def->class = &default_class;
400 av_opt_set_defaults(def);
402 if (nb_fmt_entries_to_show == 1)
406 (err = (av_set_options_string(def, args, "=", ":"))) < 0) {
407 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
414 static void default_print_footer(WriterContext *wctx)
416 DefaultContext *def = wctx->priv;
418 if (!def->noprint_wrappers)
422 static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
424 DefaultContext *def = wctx->priv;
426 if (!def->noprint_wrappers && wctx->nb_chapter)
430 /* lame uppercasing routine, assumes the string is lower case ASCII */
431 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
434 for (i = 0; src[i] && i < dst_size-1; i++)
435 dst[i] = av_toupper(src[i]);
440 static void default_print_section_header(WriterContext *wctx, const char *section)
442 DefaultContext *def = wctx->priv;
445 if (wctx->nb_section)
447 if (!def->noprint_wrappers)
448 printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
451 static void default_print_section_footer(WriterContext *wctx, const char *section)
453 DefaultContext *def = wctx->priv;
456 if (!def->noprint_wrappers)
457 printf("[/%s]", upcase_string(buf, sizeof(buf), section));
460 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
462 DefaultContext *def = wctx->priv;
465 printf("%s\n", value);
468 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
470 DefaultContext *def = wctx->priv;
474 printf("%lld\n", value);
477 static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
479 AVDictionaryEntry *tag = NULL;
480 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
481 if (!fmt_entries_to_show || (tag->key && av_dict_get(fmt_entries_to_show, tag->key, NULL, 0)))
483 writer_print_string(wctx, tag->key, tag->value, 0);
487 static const Writer default_writer = {
489 .priv_size = sizeof(DefaultContext),
490 .init = default_init,
491 .print_footer = default_print_footer,
492 .print_chapter_header = default_print_chapter_header,
493 .print_section_header = default_print_section_header,
494 .print_section_footer = default_print_section_footer,
495 .print_integer = default_print_int,
496 .print_string = default_print_str,
497 .show_tags = default_show_tags,
498 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
504 * Escape \n, \r, \\ and sep characters contained in s, and print the
507 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
511 for (p = src; *p; p++) {
513 case '\n': av_bprintf(dst, "%s", "\\n"); break;
514 case '\r': av_bprintf(dst, "%s", "\\r"); break;
515 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
518 av_bprint_chars(dst, '\\', 1);
519 av_bprint_chars(dst, *p, 1);
526 * Quote fields containing special characters, check RFC4180.
528 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
533 /* check if input needs quoting */
534 for (p = src; *p; p++)
535 if (*p == '"' || *p == sep || *p == '\n' || *p == '\r')
539 av_bprint_chars(dst, '\"', 1);
541 for (p = src; *p; p++) {
543 av_bprint_chars(dst, '\"', 1);
544 av_bprint_chars(dst, *p, 1);
547 av_bprint_chars(dst, '\"', 1);
551 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
556 typedef struct CompactContext {
557 const AVClass *class;
561 char *escape_mode_str;
562 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
566 #define OFFSET(x) offsetof(CompactContext, x)
568 static const AVOption compact_options[]= {
569 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
570 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
571 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
572 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
573 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
574 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
578 static const char *compact_get_name(void *ctx)
583 static const AVClass compact_class = {
589 static av_cold int compact_init(WriterContext *wctx, const char *args, void *opaque)
591 CompactContext *compact = wctx->priv;
594 compact->class = &compact_class;
595 av_opt_set_defaults(compact);
598 (err = (av_set_options_string(compact, args, "=", ":"))) < 0) {
599 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
602 if (strlen(compact->item_sep_str) != 1) {
603 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
604 compact->item_sep_str);
605 return AVERROR(EINVAL);
607 compact->item_sep = compact->item_sep_str[0];
609 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
610 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
611 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
613 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
614 return AVERROR(EINVAL);
620 static av_cold void compact_uninit(WriterContext *wctx)
622 CompactContext *compact = wctx->priv;
624 av_freep(&compact->item_sep_str);
625 av_freep(&compact->escape_mode_str);
628 static void compact_print_section_header(WriterContext *wctx, const char *section)
630 CompactContext *compact = wctx->priv;
632 printf("%s%c", section, compact->item_sep);
635 static void compact_print_section_footer(WriterContext *wctx, const char *section)
640 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
642 CompactContext *compact = wctx->priv;
645 if (wctx->nb_item) printf("%c", compact->item_sep);
648 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
649 printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
650 av_bprint_finalize(&buf, NULL);
653 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
655 CompactContext *compact = wctx->priv;
657 if (wctx->nb_item) printf("%c", compact->item_sep);
660 printf("%lld", value);
663 static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
665 CompactContext *compact = wctx->priv;
666 AVDictionaryEntry *tag = NULL;
669 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
670 if (wctx->nb_item) printf("%c", compact->item_sep);
672 if (!compact->nokey) {
673 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
674 printf("tag:%s=", compact->escape_str(&buf, tag->key, compact->item_sep, wctx));
675 av_bprint_finalize(&buf, NULL);
678 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
679 printf("%s", compact->escape_str(&buf, tag->value, compact->item_sep, wctx));
680 av_bprint_finalize(&buf, NULL);
684 static const Writer compact_writer = {
686 .priv_size = sizeof(CompactContext),
687 .init = compact_init,
688 .uninit = compact_uninit,
689 .print_section_header = compact_print_section_header,
690 .print_section_footer = compact_print_section_footer,
691 .print_integer = compact_print_int,
692 .print_string = compact_print_str,
693 .show_tags = compact_show_tags,
694 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
699 static av_cold int csv_init(WriterContext *wctx, const char *args, void *opaque)
701 return compact_init(wctx, "item_sep=,:nokey=1:escape=csv", opaque);
704 static const Writer csv_writer = {
706 .priv_size = sizeof(CompactContext),
708 .uninit = compact_uninit,
709 .print_section_header = compact_print_section_header,
710 .print_section_footer = compact_print_section_footer,
711 .print_integer = compact_print_int,
712 .print_string = compact_print_str,
713 .show_tags = compact_show_tags,
714 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
720 const AVClass *class;
721 int multiple_entries; ///< tells if the given chapter requires multiple entries
722 int print_packets_and_frames;
725 const char *item_sep, *item_start_end;
729 #define OFFSET(x) offsetof(JSONContext, x)
731 static const AVOption json_options[]= {
732 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
733 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
737 static const char *json_get_name(void *ctx)
742 static const AVClass json_class = {
748 static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
750 JSONContext *json = wctx->priv;
753 json->class = &json_class;
754 av_opt_set_defaults(json);
757 (err = (av_set_options_string(json, args, "=", ":"))) < 0) {
758 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
762 json->item_sep = json->compact ? ", " : ",\n";
763 json->item_start_end = json->compact ? " " : "\n";
768 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
770 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
771 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
774 for (p = src; *p; p++) {
775 char *s = strchr(json_escape, *p);
777 av_bprint_chars(dst, '\\', 1);
778 av_bprint_chars(dst, json_subst[s - json_escape], 1);
779 } else if ((unsigned char)*p < 32) {
780 av_bprintf(dst, "\\u00%02x", *p & 0xff);
782 av_bprint_chars(dst, *p, 1);
788 static void json_print_header(WriterContext *wctx)
790 JSONContext *json = wctx->priv;
792 json->indent_level++;
795 static void json_print_footer(WriterContext *wctx)
797 JSONContext *json = wctx->priv;
798 json->indent_level--;
802 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
804 static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
806 JSONContext *json = wctx->priv;
809 if (wctx->nb_chapter)
812 json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "frames" ) ||
813 !strcmp(chapter, "packets_and_frames") ||
814 !strcmp(chapter, "streams") || !strcmp(chapter, "library_versions");
815 if (json->multiple_entries) {
817 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
818 printf("\"%s\": [\n", json_escape_str(&buf, chapter, wctx));
819 av_bprint_finalize(&buf, NULL);
820 json->print_packets_and_frames = !strcmp(chapter, "packets_and_frames");
821 json->indent_level++;
825 static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
827 JSONContext *json = wctx->priv;
829 if (json->multiple_entries) {
831 json->indent_level--;
837 static void json_print_section_header(WriterContext *wctx, const char *section)
839 JSONContext *json = wctx->priv;
841 if (wctx->nb_section)
844 if (!json->multiple_entries)
845 printf("\"%s\": ", section);
846 printf("{%s", json->item_start_end);
847 json->indent_level++;
848 /* this is required so the parser can distinguish between packets and frames */
849 if (json->print_packets_and_frames) {
852 printf("\"type\": \"%s\"%s", section, json->item_sep);
856 static void json_print_section_footer(WriterContext *wctx, const char *section)
858 JSONContext *json = wctx->priv;
860 printf("%s", json->item_start_end);
861 json->indent_level--;
867 static inline void json_print_item_str(WriterContext *wctx,
868 const char *key, const char *value)
872 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
873 printf("\"%s\":", json_escape_str(&buf, key, wctx));
874 av_bprint_finalize(&buf, NULL);
876 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
877 printf(" \"%s\"", json_escape_str(&buf, value, wctx));
878 av_bprint_finalize(&buf, NULL);
881 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
883 JSONContext *json = wctx->priv;
885 if (wctx->nb_item) printf("%s", json->item_sep);
888 json_print_item_str(wctx, key, value);
891 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
893 JSONContext *json = wctx->priv;
896 if (wctx->nb_item) printf("%s", json->item_sep);
900 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
901 printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
902 av_bprint_finalize(&buf, NULL);
905 static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
907 JSONContext *json = wctx->priv;
908 AVDictionaryEntry *tag = NULL;
912 printf("%s", json->item_sep);
915 printf("\"tags\": {%s", json->item_start_end);
916 json->indent_level++;
917 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
918 if (is_first) is_first = 0;
919 else printf("%s", json->item_sep);
922 json_print_item_str(wctx, tag->key, tag->value);
924 json->indent_level--;
925 printf("%s", json->item_start_end);
931 static const Writer json_writer = {
933 .priv_size = sizeof(JSONContext),
935 .print_header = json_print_header,
936 .print_footer = json_print_footer,
937 .print_chapter_header = json_print_chapter_header,
938 .print_chapter_footer = json_print_chapter_footer,
939 .print_section_header = json_print_section_header,
940 .print_section_footer = json_print_section_footer,
941 .print_integer = json_print_int,
942 .print_string = json_print_str,
943 .show_tags = json_show_tags,
944 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
950 const AVClass *class;
952 int multiple_entries; ///< tells if the given chapter requires multiple entries
959 #define OFFSET(x) offsetof(XMLContext, x)
961 static const AVOption xml_options[] = {
962 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
963 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
964 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
965 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
969 static const char *xml_get_name(void *ctx)
974 static const AVClass xml_class = {
980 static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
982 XMLContext *xml = wctx->priv;
985 xml->class = &xml_class;
986 av_opt_set_defaults(xml);
989 (err = (av_set_options_string(xml, args, "=", ":"))) < 0) {
990 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
994 if (xml->xsd_strict) {
995 xml->fully_qualified = 1;
996 #define CHECK_COMPLIANCE(opt, opt_name) \
998 av_log(wctx, AV_LOG_ERROR, \
999 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1000 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1001 return AVERROR(EINVAL); \
1003 CHECK_COMPLIANCE(show_private_data, "private");
1004 CHECK_COMPLIANCE(show_value_unit, "unit");
1005 CHECK_COMPLIANCE(use_value_prefix, "prefix");
1007 if (do_show_frames && do_show_packets) {
1008 av_log(wctx, AV_LOG_ERROR,
1009 "Interleaved frames and packets are not allowed in XSD. "
1010 "Select only one between the -show_frames and the -show_packets options.\n");
1011 return AVERROR(EINVAL);
1018 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1022 for (p = src; *p; p++) {
1024 case '&' : av_bprintf(dst, "%s", "&"); break;
1025 case '<' : av_bprintf(dst, "%s", "<"); break;
1026 case '>' : av_bprintf(dst, "%s", ">"); break;
1027 case '\"': av_bprintf(dst, "%s", """); break;
1028 case '\'': av_bprintf(dst, "%s", "'"); break;
1029 default: av_bprint_chars(dst, *p, 1);
1036 static void xml_print_header(WriterContext *wctx)
1038 XMLContext *xml = wctx->priv;
1039 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1040 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1041 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1043 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1044 printf("<%sffprobe%s>\n",
1045 xml->fully_qualified ? "ffprobe:" : "",
1046 xml->fully_qualified ? qual : "");
1048 xml->indent_level++;
1051 static void xml_print_footer(WriterContext *wctx)
1053 XMLContext *xml = wctx->priv;
1055 xml->indent_level--;
1056 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1059 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1061 static void xml_print_chapter_header(WriterContext *wctx, const char *chapter)
1063 XMLContext *xml = wctx->priv;
1065 if (wctx->nb_chapter)
1067 xml->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "frames") ||
1068 !strcmp(chapter, "packets_and_frames") ||
1069 !strcmp(chapter, "streams") || !strcmp(chapter, "library_versions");
1071 if (xml->multiple_entries) {
1072 XML_INDENT(); printf("<%s>\n", chapter);
1073 xml->indent_level++;
1077 static void xml_print_chapter_footer(WriterContext *wctx, const char *chapter)
1079 XMLContext *xml = wctx->priv;
1081 if (xml->multiple_entries) {
1082 xml->indent_level--;
1083 XML_INDENT(); printf("</%s>\n", chapter);
1087 static void xml_print_section_header(WriterContext *wctx, const char *section)
1089 XMLContext *xml = wctx->priv;
1091 XML_INDENT(); printf("<%s ", section);
1092 xml->within_tag = 1;
1095 static void xml_print_section_footer(WriterContext *wctx, const char *section)
1097 XMLContext *xml = wctx->priv;
1099 if (xml->within_tag)
1102 XML_INDENT(); printf("</%s>\n", section);
1106 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1112 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1113 printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
1114 av_bprint_finalize(&buf, NULL);
1117 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1121 printf("%s=\"%lld\"", key, value);
1124 static void xml_show_tags(WriterContext *wctx, AVDictionary *dict)
1126 XMLContext *xml = wctx->priv;
1127 AVDictionaryEntry *tag = NULL;
1131 xml->indent_level++;
1132 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1134 /* close section tag */
1136 xml->within_tag = 0;
1141 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1142 printf("<tag key=\"%s\"", xml_escape_str(&buf, tag->key, wctx));
1143 av_bprint_finalize(&buf, NULL);
1145 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1146 printf(" value=\"%s\"/>\n", xml_escape_str(&buf, tag->value, wctx));
1147 av_bprint_finalize(&buf, NULL);
1149 xml->indent_level--;
1152 static Writer xml_writer = {
1154 .priv_size = sizeof(XMLContext),
1156 .print_header = xml_print_header,
1157 .print_footer = xml_print_footer,
1158 .print_chapter_header = xml_print_chapter_header,
1159 .print_chapter_footer = xml_print_chapter_footer,
1160 .print_section_header = xml_print_section_header,
1161 .print_section_footer = xml_print_section_footer,
1162 .print_integer = xml_print_int,
1163 .print_string = xml_print_str,
1164 .show_tags = xml_show_tags,
1165 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1168 static void writer_register_all(void)
1170 static int initialized;
1176 writer_register(&default_writer);
1177 writer_register(&compact_writer);
1178 writer_register(&csv_writer);
1179 writer_register(&json_writer);
1180 writer_register(&xml_writer);
1183 #define print_fmt(k, f, ...) do { \
1184 av_bprint_clear(&pbuf); \
1185 av_bprintf(&pbuf, f, __VA_ARGS__); \
1186 writer_print_string(w, k, pbuf.str, 0); \
1189 #define print_int(k, v) writer_print_integer(w, k, v)
1190 #define print_str(k, v) writer_print_string(w, k, v, 0)
1191 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
1192 #define print_time(k, v, tb) writer_print_time(w, k, v, tb)
1193 #define print_ts(k, v) writer_print_ts(w, k, v)
1194 #define print_val(k, v, u) writer_print_string(w, k, \
1195 value_string(val_str, sizeof(val_str), (struct unit_value){.val.i = v, .unit=u}), 0)
1196 #define print_section_header(s) writer_print_section_header(w, s)
1197 #define print_section_footer(s) writer_print_section_footer(w, s)
1198 #define show_tags(metadata) writer_show_tags(w, metadata)
1200 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1203 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1207 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1209 print_section_header("packet");
1210 s = av_get_media_type_string(st->codec->codec_type);
1211 if (s) print_str ("codec_type", s);
1212 else print_str_opt("codec_type", "unknown");
1213 print_int("stream_index", pkt->stream_index);
1214 print_ts ("pts", pkt->pts);
1215 print_time("pts_time", pkt->pts, &st->time_base);
1216 print_ts ("dts", pkt->dts);
1217 print_time("dts_time", pkt->dts, &st->time_base);
1218 print_ts ("duration", pkt->duration);
1219 print_time("duration_time", pkt->duration, &st->time_base);
1220 print_val("size", pkt->size, unit_byte_str);
1221 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1222 else print_str_opt("pos", "N/A");
1223 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1224 print_section_footer("packet");
1226 av_bprint_finalize(&pbuf, NULL);
1230 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream)
1235 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1237 print_section_header("frame");
1239 s = av_get_media_type_string(stream->codec->codec_type);
1240 if (s) print_str ("media_type", s);
1241 else print_str_opt("media_type", "unknown");
1242 print_int("key_frame", frame->key_frame);
1243 print_ts ("pkt_pts", frame->pkt_pts);
1244 print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
1245 print_ts ("pkt_dts", frame->pkt_dts);
1246 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
1247 if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
1248 else print_str_opt("pkt_pos", "N/A");
1250 switch (stream->codec->codec_type) {
1251 case AVMEDIA_TYPE_VIDEO:
1252 print_int("width", frame->width);
1253 print_int("height", frame->height);
1254 s = av_get_pix_fmt_name(frame->format);
1255 if (s) print_str ("pix_fmt", s);
1256 else print_str_opt("pix_fmt", "unknown");
1257 if (frame->sample_aspect_ratio.num) {
1258 print_fmt("sample_aspect_ratio", "%d:%d",
1259 frame->sample_aspect_ratio.num,
1260 frame->sample_aspect_ratio.den);
1262 print_str_opt("sample_aspect_ratio", "N/A");
1264 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
1265 print_int("coded_picture_number", frame->coded_picture_number);
1266 print_int("display_picture_number", frame->display_picture_number);
1267 print_int("interlaced_frame", frame->interlaced_frame);
1268 print_int("top_field_first", frame->top_field_first);
1269 print_int("repeat_pict", frame->repeat_pict);
1270 print_int("reference", frame->reference);
1273 case AVMEDIA_TYPE_AUDIO:
1274 s = av_get_sample_fmt_name(frame->format);
1275 if (s) print_str ("sample_fmt", s);
1276 else print_str_opt("sample_fmt", "unknown");
1277 print_int("nb_samples", frame->nb_samples);
1281 print_section_footer("frame");
1283 av_bprint_finalize(&pbuf, NULL);
1287 static av_always_inline int get_decoded_frame(AVFormatContext *fmt_ctx,
1288 AVFrame *frame, int *got_frame,
1291 AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
1295 switch (dec_ctx->codec_type) {
1296 case AVMEDIA_TYPE_VIDEO:
1297 ret = avcodec_decode_video2(dec_ctx, frame, got_frame, pkt);
1300 case AVMEDIA_TYPE_AUDIO:
1301 ret = avcodec_decode_audio4(dec_ctx, frame, got_frame, pkt);
1308 static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1312 int i = 0, ret, got_frame;
1314 av_init_packet(&pkt);
1316 while (!av_read_frame(fmt_ctx, &pkt)) {
1317 if (do_read_packets) {
1318 if (do_show_packets)
1319 show_packet(w, fmt_ctx, &pkt, i++);
1320 nb_streams_packets[pkt.stream_index]++;
1322 if (do_read_frames) {
1325 avcodec_get_frame_defaults(&frame);
1326 ret = get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt1);
1327 if (ret < 0 || !got_frame)
1330 show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
1333 nb_streams_frames[pkt.stream_index]++;
1336 av_free_packet(&pkt);
1338 av_init_packet(&pkt);
1341 //Flush remaining frames that are cached in the decoder
1342 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1343 pkt.stream_index = i;
1344 while (get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt) >= 0 && got_frame) {
1345 if (do_read_frames) {
1347 show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
1348 nb_streams_frames[pkt.stream_index]++;
1354 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
1356 AVStream *stream = fmt_ctx->streams[stream_idx];
1357 AVCodecContext *dec_ctx;
1361 AVRational display_aspect_ratio;
1364 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1366 print_section_header("stream");
1368 print_int("index", stream->index);
1370 if ((dec_ctx = stream->codec)) {
1371 if ((dec = dec_ctx->codec)) {
1372 print_str("codec_name", dec->name);
1373 print_str("codec_long_name", dec->long_name);
1375 print_str_opt("codec_name", "unknown");
1376 print_str_opt("codec_long_name", "unknown");
1379 s = av_get_media_type_string(dec_ctx->codec_type);
1380 if (s) print_str ("codec_type", s);
1381 else print_str_opt("codec_type", "unknown");
1382 print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
1384 /* print AVI/FourCC tag */
1385 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
1386 print_str("codec_tag_string", val_str);
1387 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
1389 switch (dec_ctx->codec_type) {
1390 case AVMEDIA_TYPE_VIDEO:
1391 print_int("width", dec_ctx->width);
1392 print_int("height", dec_ctx->height);
1393 print_int("has_b_frames", dec_ctx->has_b_frames);
1394 if (dec_ctx->sample_aspect_ratio.num) {
1395 print_fmt("sample_aspect_ratio", "%d:%d",
1396 dec_ctx->sample_aspect_ratio.num,
1397 dec_ctx->sample_aspect_ratio.den);
1398 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1399 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
1400 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
1402 print_fmt("display_aspect_ratio", "%d:%d",
1403 display_aspect_ratio.num,
1404 display_aspect_ratio.den);
1406 print_str_opt("sample_aspect_ratio", "N/A");
1407 print_str_opt("display_aspect_ratio", "N/A");
1409 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
1410 if (s) print_str ("pix_fmt", s);
1411 else print_str_opt("pix_fmt", "unknown");
1412 print_int("level", dec_ctx->level);
1413 if (dec_ctx->timecode_frame_start >= 0) {
1414 char tcbuf[AV_TIMECODE_STR_SIZE];
1415 av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
1416 print_str("timecode", tcbuf);
1418 print_str_opt("timecode", "N/A");
1422 case AVMEDIA_TYPE_AUDIO:
1423 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
1424 if (s) print_str ("sample_fmt", s);
1425 else print_str_opt("sample_fmt", "unknown");
1426 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
1427 print_int("channels", dec_ctx->channels);
1428 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
1432 print_str_opt("codec_type", "unknown");
1434 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
1435 const AVOption *opt = NULL;
1436 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
1438 if (opt->flags) continue;
1439 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
1440 print_str(opt->name, str);
1446 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
1447 else print_str_opt("id", "N/A");
1448 print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
1449 print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
1450 print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
1451 print_time("start_time", stream->start_time, &stream->time_base);
1452 print_time("duration", stream->duration, &stream->time_base);
1453 if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
1454 else print_str_opt("bit_rate", "N/A");
1455 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
1456 else print_str_opt("nb_frames", "N/A");
1457 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
1458 else print_str_opt("nb_read_frames", "N/A");
1459 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
1460 else print_str_opt("nb_read_packets", "N/A");
1461 show_tags(stream->metadata);
1463 print_section_footer("stream");
1464 av_bprint_finalize(&pbuf, NULL);
1468 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
1471 for (i = 0; i < fmt_ctx->nb_streams; i++)
1472 show_stream(w, fmt_ctx, i);
1475 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
1478 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
1480 print_section_header("format");
1481 print_str("filename", fmt_ctx->filename);
1482 print_int("nb_streams", fmt_ctx->nb_streams);
1483 print_str("format_name", fmt_ctx->iformat->name);
1484 print_str("format_long_name", fmt_ctx->iformat->long_name);
1485 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
1486 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
1487 if (size >= 0) print_val ("size", size, unit_byte_str);
1488 else print_str_opt("size", "N/A");
1489 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
1490 else print_str_opt("bit_rate", "N/A");
1491 show_tags(fmt_ctx->metadata);
1492 print_section_footer("format");
1496 static void show_error(WriterContext *w, int err)
1499 const char *errbuf_ptr = errbuf;
1501 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
1502 errbuf_ptr = strerror(AVUNERROR(err));
1504 writer_print_chapter_header(w, "error");
1505 print_section_header("error");
1506 print_int("code", err);
1507 print_str("string", errbuf_ptr);
1508 print_section_footer("error");
1509 writer_print_chapter_footer(w, "error");
1512 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
1515 AVFormatContext *fmt_ctx = NULL;
1516 AVDictionaryEntry *t;
1518 if ((err = avformat_open_input(&fmt_ctx, filename,
1519 iformat, &format_opts)) < 0) {
1520 print_error(filename, err);
1523 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1524 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
1525 return AVERROR_OPTION_NOT_FOUND;
1529 /* fill the streams in the format context */
1530 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
1531 print_error(filename, err);
1535 av_dump_format(fmt_ctx, 0, filename, 0);
1537 /* bind a decoder to each input stream */
1538 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1539 AVStream *stream = fmt_ctx->streams[i];
1542 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
1543 av_log(NULL, AV_LOG_ERROR,
1544 "Unsupported codec with id %d for input stream %d\n",
1545 stream->codec->codec_id, stream->index);
1546 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
1547 av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
1552 *fmt_ctx_ptr = fmt_ctx;
1556 static void close_input_file(AVFormatContext **ctx_ptr)
1559 AVFormatContext *fmt_ctx = *ctx_ptr;
1561 /* close decoder for each stream */
1562 for (i = 0; i < fmt_ctx->nb_streams; i++)
1563 if (fmt_ctx->streams[i]->codec->codec_id != CODEC_ID_NONE)
1564 avcodec_close(fmt_ctx->streams[i]->codec);
1566 avformat_close_input(ctx_ptr);
1569 #define PRINT_CHAPTER(name) do { \
1570 if (do_show_ ## name) { \
1571 writer_print_chapter_header(wctx, #name); \
1572 show_ ## name (wctx, fmt_ctx); \
1573 writer_print_chapter_footer(wctx, #name); \
1577 static int probe_file(WriterContext *wctx, const char *filename)
1579 AVFormatContext *fmt_ctx;
1582 do_read_frames = do_show_frames || do_count_frames;
1583 do_read_packets = do_show_packets || do_count_packets;
1585 ret = open_input_file(&fmt_ctx, filename);
1587 nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
1588 nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
1589 if (do_read_frames || do_read_packets) {
1590 const char *chapter;
1591 if (do_show_frames && do_show_packets &&
1592 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
1593 chapter = "packets_and_frames";
1594 else if (do_show_packets && !do_show_frames)
1595 chapter = "packets";
1596 else // (!do_show_packets && do_show_frames)
1598 if (do_show_frames || do_show_packets)
1599 writer_print_chapter_header(wctx, chapter);
1600 read_packets(wctx, fmt_ctx);
1601 if (do_show_frames || do_show_packets)
1602 writer_print_chapter_footer(wctx, chapter);
1604 PRINT_CHAPTER(streams);
1605 PRINT_CHAPTER(format);
1606 close_input_file(&fmt_ctx);
1607 av_freep(&nb_streams_frames);
1608 av_freep(&nb_streams_packets);
1613 static void show_usage(void)
1615 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
1616 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
1617 av_log(NULL, AV_LOG_INFO, "\n");
1620 static void ffprobe_show_program_version(WriterContext *w)
1623 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1625 writer_print_chapter_header(w, "program_version");
1626 print_section_header("program_version");
1627 print_str("version", FFMPEG_VERSION);
1628 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
1629 program_birth_year, this_year);
1630 print_str("build_date", __DATE__);
1631 print_str("build_time", __TIME__);
1632 print_str("compiler_type", CC_TYPE);
1633 print_str("compiler_version", CC_VERSION);
1634 print_str("configuration", FFMPEG_CONFIGURATION);
1635 print_section_footer("program_version");
1636 writer_print_chapter_footer(w, "program_version");
1638 av_bprint_finalize(&pbuf, NULL);
1641 #define SHOW_LIB_VERSION(libname, LIBNAME) \
1643 if (CONFIG_##LIBNAME) { \
1644 unsigned int version = libname##_version(); \
1645 print_section_header("library_version"); \
1646 print_str("name", "lib" #libname); \
1647 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
1648 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
1649 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
1650 print_int("version", version); \
1651 print_section_footer("library_version"); \
1655 static void ffprobe_show_library_versions(WriterContext *w)
1657 writer_print_chapter_header(w, "library_versions");
1658 SHOW_LIB_VERSION(avutil, AVUTIL);
1659 SHOW_LIB_VERSION(avcodec, AVCODEC);
1660 SHOW_LIB_VERSION(avformat, AVFORMAT);
1661 SHOW_LIB_VERSION(avdevice, AVDEVICE);
1662 SHOW_LIB_VERSION(avfilter, AVFILTER);
1663 SHOW_LIB_VERSION(swscale, SWSCALE);
1664 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
1665 SHOW_LIB_VERSION(postproc, POSTPROC);
1666 writer_print_chapter_footer(w, "library_versions");
1669 static int opt_format(const char *opt, const char *arg)
1671 iformat = av_find_input_format(arg);
1673 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
1674 return AVERROR(EINVAL);
1679 static int opt_show_format_entry(const char *opt, const char *arg)
1682 nb_fmt_entries_to_show++;
1683 av_dict_set(&fmt_entries_to_show, arg, "", 0);
1687 static void opt_input_file(void *optctx, const char *arg)
1689 if (input_filename) {
1690 av_log(NULL, AV_LOG_ERROR,
1691 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
1692 arg, input_filename);
1695 if (!strcmp(arg, "-"))
1697 input_filename = arg;
1700 static int opt_help(const char *opt, const char *arg)
1702 av_log_set_callback(log_callback_help);
1704 show_help_options(options, "Main options:\n", 0, 0);
1707 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
1712 static int opt_pretty(const char *opt, const char *arg)
1714 show_value_unit = 1;
1715 use_value_prefix = 1;
1716 use_byte_value_binary_prefix = 1;
1717 use_value_sexagesimal_format = 1;
1721 static int opt_show_versions(const char *opt, const char *arg)
1723 do_show_program_version = 1;
1724 do_show_library_versions = 1;
1728 static const OptionDef options[] = {
1729 #include "cmdutils_common_opts.h"
1730 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
1731 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
1732 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
1733 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
1734 "use binary prefixes for byte units" },
1735 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
1736 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
1737 { "pretty", 0, {(void*)&opt_pretty},
1738 "prettify the format of displayed values, make it more human readable" },
1739 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
1740 "set the output printing format (available formats are: default, compact, csv, json, xml)", "format" },
1741 { "show_error", OPT_BOOL, {(void*)&do_show_error} , "show probing error" },
1742 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
1743 { "show_frames", OPT_BOOL, {(void*)&do_show_frames} , "show frames info" },
1744 { "show_format_entry", HAS_ARG, {(void*)opt_show_format_entry},
1745 "show a particular entry from the format/container info", "entry" },
1746 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
1747 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
1748 { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
1749 { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
1750 { "show_program_version", OPT_BOOL, {(void*)&do_show_program_version}, "show ffprobe version" },
1751 { "show_library_versions", OPT_BOOL, {(void*)&do_show_library_versions}, "show library versions" },
1752 { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
1753 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
1754 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
1755 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
1756 { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
1760 int main(int argc, char **argv)
1763 WriterContext *wctx;
1765 char *w_name = NULL, *w_args = NULL;
1768 av_log_set_flags(AV_LOG_SKIP_REPEATED);
1769 parse_loglevel(argc, argv, options);
1771 avformat_network_init();
1774 avdevice_register_all();
1777 show_banner(argc, argv, options);
1778 parse_options(NULL, argc, argv, options, opt_input_file);
1780 writer_register_all();
1783 print_format = av_strdup("default");
1784 w_name = av_strtok(print_format, "=", &buf);
1787 w = writer_get_by_name(w_name);
1789 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
1790 ret = AVERROR(EINVAL);
1794 if ((ret = writer_open(&wctx, w, w_args, NULL)) >= 0) {
1795 writer_print_header(wctx);
1797 if (do_show_program_version)
1798 ffprobe_show_program_version(wctx);
1799 if (do_show_library_versions)
1800 ffprobe_show_library_versions(wctx);
1802 if (!input_filename &&
1803 ((do_show_format || do_show_streams || do_show_packets || do_show_error) ||
1804 (!do_show_program_version && !do_show_library_versions))) {
1806 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
1807 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
1808 ret = AVERROR(EINVAL);
1809 } else if (input_filename) {
1810 ret = probe_file(wctx, input_filename);
1811 if (ret < 0 && do_show_error)
1812 show_error(wctx, ret);
1815 writer_print_footer(wctx);
1816 writer_close(&wctx);
1820 av_freep(&print_format);
1823 av_dict_free(&fmt_entries_to_show);
1825 avformat_network_deinit();