2 * Copyright (c) 2007-2010 Stefano Sabatini
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * simple media prober based on the FFmpeg libraries
29 #include "libavformat/avformat.h"
30 #include "libavcodec/avcodec.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/bprint.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/dict.h"
36 #include "libavutil/timecode.h"
37 #include "libavdevice/avdevice.h"
38 #include "libswscale/swscale.h"
39 #include "libswresample/swresample.h"
40 #include "libpostproc/postprocess.h"
43 const char program_name[] = "ffprobe";
44 const int program_birth_year = 2007;
46 static int do_count_frames = 0;
47 static int do_count_packets = 0;
48 static int do_read_frames = 0;
49 static int do_read_packets = 0;
50 static int do_show_error = 0;
51 static int do_show_format = 0;
52 static int do_show_frames = 0;
53 static AVDictionary *fmt_entries_to_show = NULL;
54 static int do_show_packets = 0;
55 static int do_show_streams = 0;
56 static int do_show_program_version = 0;
57 static int do_show_library_versions = 0;
59 static int show_value_unit = 0;
60 static int use_value_prefix = 0;
61 static int use_byte_value_binary_prefix = 0;
62 static int use_value_sexagesimal_format = 0;
63 static int show_private_data = 1;
65 static char *print_format;
67 static const OptionDef options[];
70 static const char *input_filename;
71 static AVInputFormat *iformat = NULL;
73 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
74 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
76 static const char unit_second_str[] = "s" ;
77 static const char unit_hertz_str[] = "Hz" ;
78 static const char unit_byte_str[] = "byte" ;
79 static const char unit_bit_per_second_str[] = "bit/s";
80 static uint64_t *nb_streams_packets;
81 static uint64_t *nb_streams_frames;
83 void av_noreturn exit_program(int ret)
85 av_dict_free(&fmt_entries_to_show);
90 union { double d; long long int i; } val;
94 static char *value_string(char *buf, int buf_size, struct unit_value uv)
99 if (uv.unit == unit_second_str) {
106 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
110 mins = (int)secs / 60;
111 secs = secs - mins * 60;
114 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
116 const char *prefix_string = "";
119 if (use_value_prefix && vald > 1) {
122 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
123 index = (long long int) (log(vald)/log(2)) / 10;
124 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
125 vald /= pow(2, index * 10);
126 prefix_string = binary_unit_prefixes[index];
128 index = (long long int) (log10(vald)) / 3;
129 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
130 vald /= pow(10, index * 3);
131 prefix_string = decimal_unit_prefixes[index];
135 if (show_float || (use_value_prefix && vald != (long long int)vald))
136 l = snprintf(buf, buf_size, "%f", vald);
138 l = snprintf(buf, buf_size, "%lld", (long long int)vald);
139 snprintf(buf+l, buf_size-l, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
140 prefix_string, show_value_unit ? uv.unit : "");
148 typedef struct WriterContext WriterContext;
150 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
151 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
153 typedef struct Writer {
154 int priv_size; ///< private size for the writer context
157 int (*init) (WriterContext *wctx, const char *args, void *opaque);
158 void (*uninit)(WriterContext *wctx);
160 void (*print_header)(WriterContext *ctx);
161 void (*print_footer)(WriterContext *ctx);
163 void (*print_chapter_header)(WriterContext *wctx, const char *);
164 void (*print_chapter_footer)(WriterContext *wctx, const char *);
165 void (*print_section_header)(WriterContext *wctx, const char *);
166 void (*print_section_footer)(WriterContext *wctx, const char *);
167 void (*print_integer) (WriterContext *wctx, const char *, long long int);
168 void (*print_string) (WriterContext *wctx, const char *, const char *);
169 void (*show_tags) (WriterContext *wctx, AVDictionary *dict);
170 int flags; ///< a combination or WRITER_FLAG_*
173 struct WriterContext {
174 const AVClass *class; ///< class of the writer
175 const Writer *writer; ///< the Writer of which this is an instance
176 char *name; ///< name of this writer instance
177 void *priv; ///< private data for use by the filter
178 unsigned int nb_item; ///< number of the item printed in the given section, starting at 0
179 unsigned int nb_section; ///< number of the section printed in the given section sequence, starting at 0
180 unsigned int nb_chapter; ///< number of the chapter, starting at 0
182 int is_fmt_chapter; ///< tells if the current chapter is "format", required by the print_format_entry option
185 static const char *writer_get_name(void *p)
187 WriterContext *wctx = p;
188 return wctx->writer->name;
191 static const AVClass writer_class = {
195 LIBAVUTIL_VERSION_INT,
198 static void writer_close(WriterContext **wctx)
203 if ((*wctx)->writer->uninit)
204 (*wctx)->writer->uninit(*wctx);
205 av_freep(&((*wctx)->priv));
209 static int writer_open(WriterContext **wctx, const Writer *writer,
210 const char *args, void *opaque)
214 if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
215 ret = AVERROR(ENOMEM);
219 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
220 ret = AVERROR(ENOMEM);
224 (*wctx)->class = &writer_class;
225 (*wctx)->writer = writer;
226 if ((*wctx)->writer->init)
227 ret = (*wctx)->writer->init(*wctx, args, opaque);
238 static inline void writer_print_header(WriterContext *wctx)
240 if (wctx->writer->print_header)
241 wctx->writer->print_header(wctx);
242 wctx->nb_chapter = 0;
245 static inline void writer_print_footer(WriterContext *wctx)
247 if (wctx->writer->print_footer)
248 wctx->writer->print_footer(wctx);
251 static inline void writer_print_chapter_header(WriterContext *wctx,
254 if (wctx->writer->print_chapter_header)
255 wctx->writer->print_chapter_header(wctx, chapter);
256 wctx->nb_section = 0;
258 wctx->is_fmt_chapter = !strcmp(chapter, "format");
261 static inline void writer_print_chapter_footer(WriterContext *wctx,
264 if (wctx->writer->print_chapter_footer)
265 wctx->writer->print_chapter_footer(wctx, chapter);
269 static inline void writer_print_section_header(WriterContext *wctx,
272 if (wctx->writer->print_section_header)
273 wctx->writer->print_section_header(wctx, section);
277 static inline void writer_print_section_footer(WriterContext *wctx,
280 if (wctx->writer->print_section_footer)
281 wctx->writer->print_section_footer(wctx, section);
285 static inline void writer_print_integer(WriterContext *wctx,
286 const char *key, long long int val)
288 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
289 wctx->writer->print_integer(wctx, key, val);
294 static inline void writer_print_string(WriterContext *wctx,
295 const char *key, const char *val, int opt)
297 if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
299 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
300 wctx->writer->print_string(wctx, key, val);
305 static void writer_print_time(WriterContext *wctx, const char *key,
306 int64_t ts, const AVRational *time_base)
310 if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
311 if (ts == AV_NOPTS_VALUE) {
312 writer_print_string(wctx, key, "N/A", 1);
314 double d = ts * av_q2d(*time_base);
315 value_string(buf, sizeof(buf), (struct unit_value){.val.d=d, .unit=unit_second_str});
316 writer_print_string(wctx, key, buf, 0);
321 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts)
323 if (ts == AV_NOPTS_VALUE) {
324 writer_print_string(wctx, key, "N/A", 1);
326 writer_print_integer(wctx, key, ts);
330 static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
332 wctx->writer->show_tags(wctx, dict);
335 #define MAX_REGISTERED_WRITERS_NB 64
337 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
339 static int writer_register(const Writer *writer)
341 static int next_registered_writer_idx = 0;
343 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
344 return AVERROR(ENOMEM);
346 registered_writers[next_registered_writer_idx++] = writer;
350 static const Writer *writer_get_by_name(const char *name)
354 for (i = 0; registered_writers[i]; i++)
355 if (!strcmp(registered_writers[i]->name, name))
356 return registered_writers[i];
366 typedef struct DefaultContext {
367 const AVClass *class;
369 int noprint_wrappers;
372 #define OFFSET(x) offsetof(DefaultContext, x)
374 static const AVOption default_options[] = {
375 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
376 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
377 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
378 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
382 static const char *default_get_name(void *ctx)
387 static const AVClass default_class = {
393 static av_cold int default_init(WriterContext *wctx, const char *args, void *opaque)
395 DefaultContext *def = wctx->priv;
398 def->class = &default_class;
399 av_opt_set_defaults(def);
402 (err = (av_set_options_string(def, args, "=", ":"))) < 0) {
403 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
410 static void default_print_footer(WriterContext *wctx)
412 DefaultContext *def = wctx->priv;
414 if (!def->noprint_wrappers)
418 static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
420 DefaultContext *def = wctx->priv;
422 if (!def->noprint_wrappers && wctx->nb_chapter)
426 /* lame uppercasing routine, assumes the string is lower case ASCII */
427 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
430 for (i = 0; src[i] && i < dst_size-1; i++)
431 dst[i] = av_toupper(src[i]);
436 static void default_print_section_header(WriterContext *wctx, const char *section)
438 DefaultContext *def = wctx->priv;
441 if (wctx->nb_section)
443 if (!def->noprint_wrappers)
444 printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
447 static void default_print_section_footer(WriterContext *wctx, const char *section)
449 DefaultContext *def = wctx->priv;
452 if (!def->noprint_wrappers)
453 printf("[/%s]", upcase_string(buf, sizeof(buf), section));
456 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
458 DefaultContext *def = wctx->priv;
461 printf("%s\n", value);
464 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
466 DefaultContext *def = wctx->priv;
470 printf("%lld\n", value);
473 static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
475 AVDictionaryEntry *tag = NULL;
476 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
477 if (!fmt_entries_to_show || (tag->key && av_dict_get(fmt_entries_to_show, tag->key, NULL, 0)))
479 writer_print_string(wctx, tag->key, tag->value, 0);
483 static const Writer default_writer = {
485 .priv_size = sizeof(DefaultContext),
486 .init = default_init,
487 .print_footer = default_print_footer,
488 .print_chapter_header = default_print_chapter_header,
489 .print_section_header = default_print_section_header,
490 .print_section_footer = default_print_section_footer,
491 .print_integer = default_print_int,
492 .print_string = default_print_str,
493 .show_tags = default_show_tags,
494 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
500 * Escape \n, \r, \\ and sep characters contained in s, and print the
503 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
507 for (p = src; *p; p++) {
509 case '\n': av_bprintf(dst, "%s", "\\n"); break;
510 case '\r': av_bprintf(dst, "%s", "\\r"); break;
511 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
514 av_bprint_chars(dst, '\\', 1);
515 av_bprint_chars(dst, *p, 1);
522 * Quote fields containing special characters, check RFC4180.
524 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
529 /* check if input needs quoting */
530 for (p = src; *p; p++)
531 if (*p == '"' || *p == sep || *p == '\n' || *p == '\r')
535 av_bprint_chars(dst, '\"', 1);
537 for (p = src; *p; p++) {
539 av_bprint_chars(dst, '\"', 1);
540 av_bprint_chars(dst, *p, 1);
543 av_bprint_chars(dst, '\"', 1);
547 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
552 typedef struct CompactContext {
553 const AVClass *class;
557 char *escape_mode_str;
558 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
562 #define OFFSET(x) offsetof(CompactContext, x)
564 static const AVOption compact_options[]= {
565 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
566 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
567 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
568 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
569 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
570 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
574 static const char *compact_get_name(void *ctx)
579 static const AVClass compact_class = {
585 static av_cold int compact_init(WriterContext *wctx, const char *args, void *opaque)
587 CompactContext *compact = wctx->priv;
590 compact->class = &compact_class;
591 av_opt_set_defaults(compact);
594 (err = (av_set_options_string(compact, args, "=", ":"))) < 0) {
595 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
598 if (strlen(compact->item_sep_str) != 1) {
599 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
600 compact->item_sep_str);
601 return AVERROR(EINVAL);
603 compact->item_sep = compact->item_sep_str[0];
605 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
606 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
607 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
609 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
610 return AVERROR(EINVAL);
616 static av_cold void compact_uninit(WriterContext *wctx)
618 CompactContext *compact = wctx->priv;
620 av_freep(&compact->item_sep_str);
621 av_freep(&compact->escape_mode_str);
624 static void compact_print_section_header(WriterContext *wctx, const char *section)
626 CompactContext *compact = wctx->priv;
628 printf("%s%c", section, compact->item_sep);
631 static void compact_print_section_footer(WriterContext *wctx, const char *section)
636 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
638 CompactContext *compact = wctx->priv;
641 if (wctx->nb_item) printf("%c", compact->item_sep);
644 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
645 printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
646 av_bprint_finalize(&buf, NULL);
649 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
651 CompactContext *compact = wctx->priv;
653 if (wctx->nb_item) printf("%c", compact->item_sep);
656 printf("%lld", value);
659 static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
661 CompactContext *compact = wctx->priv;
662 AVDictionaryEntry *tag = NULL;
665 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
666 if (wctx->nb_item) printf("%c", compact->item_sep);
668 if (!compact->nokey) {
669 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
670 printf("tag:%s=", compact->escape_str(&buf, tag->key, compact->item_sep, wctx));
671 av_bprint_finalize(&buf, NULL);
674 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
675 printf("%s", compact->escape_str(&buf, tag->value, compact->item_sep, wctx));
676 av_bprint_finalize(&buf, NULL);
680 static const Writer compact_writer = {
682 .priv_size = sizeof(CompactContext),
683 .init = compact_init,
684 .uninit = compact_uninit,
685 .print_section_header = compact_print_section_header,
686 .print_section_footer = compact_print_section_footer,
687 .print_integer = compact_print_int,
688 .print_string = compact_print_str,
689 .show_tags = compact_show_tags,
690 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
695 static av_cold int csv_init(WriterContext *wctx, const char *args, void *opaque)
697 return compact_init(wctx, "item_sep=,:nokey=1:escape=csv", opaque);
700 static const Writer csv_writer = {
702 .priv_size = sizeof(CompactContext),
704 .uninit = compact_uninit,
705 .print_section_header = compact_print_section_header,
706 .print_section_footer = compact_print_section_footer,
707 .print_integer = compact_print_int,
708 .print_string = compact_print_str,
709 .show_tags = compact_show_tags,
710 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
716 const AVClass *class;
717 int multiple_entries; ///< tells if the given chapter requires multiple entries
718 int print_packets_and_frames;
721 const char *item_sep, *item_start_end;
725 #define OFFSET(x) offsetof(JSONContext, x)
727 static const AVOption json_options[]= {
728 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
729 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
733 static const char *json_get_name(void *ctx)
738 static const AVClass json_class = {
744 static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
746 JSONContext *json = wctx->priv;
749 json->class = &json_class;
750 av_opt_set_defaults(json);
753 (err = (av_set_options_string(json, args, "=", ":"))) < 0) {
754 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
758 json->item_sep = json->compact ? ", " : ",\n";
759 json->item_start_end = json->compact ? " " : "\n";
764 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
766 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
767 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
770 for (p = src; *p; p++) {
771 char *s = strchr(json_escape, *p);
773 av_bprint_chars(dst, '\\', 1);
774 av_bprint_chars(dst, json_subst[s - json_escape], 1);
775 } else if ((unsigned char)*p < 32) {
776 av_bprintf(dst, "\\u00%02x", *p & 0xff);
778 av_bprint_chars(dst, *p, 1);
784 static void json_print_header(WriterContext *wctx)
786 JSONContext *json = wctx->priv;
788 json->indent_level++;
791 static void json_print_footer(WriterContext *wctx)
793 JSONContext *json = wctx->priv;
794 json->indent_level--;
798 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
800 static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
802 JSONContext *json = wctx->priv;
805 if (wctx->nb_chapter)
808 json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "frames" ) ||
809 !strcmp(chapter, "packets_and_frames") ||
810 !strcmp(chapter, "streams") || !strcmp(chapter, "library_versions");
811 if (json->multiple_entries) {
813 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
814 printf("\"%s\": [\n", json_escape_str(&buf, chapter, wctx));
815 av_bprint_finalize(&buf, NULL);
816 json->print_packets_and_frames = !strcmp(chapter, "packets_and_frames");
817 json->indent_level++;
821 static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
823 JSONContext *json = wctx->priv;
825 if (json->multiple_entries) {
827 json->indent_level--;
833 static void json_print_section_header(WriterContext *wctx, const char *section)
835 JSONContext *json = wctx->priv;
837 if (wctx->nb_section)
840 if (!json->multiple_entries)
841 printf("\"%s\": ", section);
842 printf("{%s", json->item_start_end);
843 json->indent_level++;
844 /* this is required so the parser can distinguish between packets and frames */
845 if (json->print_packets_and_frames) {
848 printf("\"type\": \"%s\"%s", section, json->item_sep);
852 static void json_print_section_footer(WriterContext *wctx, const char *section)
854 JSONContext *json = wctx->priv;
856 printf("%s", json->item_start_end);
857 json->indent_level--;
863 static inline void json_print_item_str(WriterContext *wctx,
864 const char *key, const char *value)
868 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
869 printf("\"%s\":", json_escape_str(&buf, key, wctx));
870 av_bprint_finalize(&buf, NULL);
872 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
873 printf(" \"%s\"", json_escape_str(&buf, value, wctx));
874 av_bprint_finalize(&buf, NULL);
877 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
879 JSONContext *json = wctx->priv;
881 if (wctx->nb_item) printf("%s", json->item_sep);
884 json_print_item_str(wctx, key, value);
887 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
889 JSONContext *json = wctx->priv;
892 if (wctx->nb_item) printf("%s", json->item_sep);
896 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
897 printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
898 av_bprint_finalize(&buf, NULL);
901 static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
903 JSONContext *json = wctx->priv;
904 AVDictionaryEntry *tag = NULL;
908 printf("%s", json->item_sep);
911 printf("\"tags\": {%s", json->item_start_end);
912 json->indent_level++;
913 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
914 if (is_first) is_first = 0;
915 else printf("%s", json->item_sep);
918 json_print_item_str(wctx, tag->key, tag->value);
920 json->indent_level--;
921 printf("%s", json->item_start_end);
927 static const Writer json_writer = {
929 .priv_size = sizeof(JSONContext),
931 .print_header = json_print_header,
932 .print_footer = json_print_footer,
933 .print_chapter_header = json_print_chapter_header,
934 .print_chapter_footer = json_print_chapter_footer,
935 .print_section_header = json_print_section_header,
936 .print_section_footer = json_print_section_footer,
937 .print_integer = json_print_int,
938 .print_string = json_print_str,
939 .show_tags = json_show_tags,
940 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
946 const AVClass *class;
948 int multiple_entries; ///< tells if the given chapter requires multiple entries
955 #define OFFSET(x) offsetof(XMLContext, x)
957 static const AVOption xml_options[] = {
958 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
959 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
960 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
961 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
965 static const char *xml_get_name(void *ctx)
970 static const AVClass xml_class = {
976 static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
978 XMLContext *xml = wctx->priv;
981 xml->class = &xml_class;
982 av_opt_set_defaults(xml);
985 (err = (av_set_options_string(xml, args, "=", ":"))) < 0) {
986 av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
990 if (xml->xsd_strict) {
991 xml->fully_qualified = 1;
992 #define CHECK_COMPLIANCE(opt, opt_name) \
994 av_log(wctx, AV_LOG_ERROR, \
995 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
996 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
997 return AVERROR(EINVAL); \
999 CHECK_COMPLIANCE(show_private_data, "private");
1000 CHECK_COMPLIANCE(show_value_unit, "unit");
1001 CHECK_COMPLIANCE(use_value_prefix, "prefix");
1003 if (do_show_frames && do_show_packets) {
1004 av_log(wctx, AV_LOG_ERROR,
1005 "Interleaved frames and packets are not allowed in XSD. "
1006 "Select only one between the -show_frames and the -show_packets options.\n");
1007 return AVERROR(EINVAL);
1014 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1018 for (p = src; *p; p++) {
1020 case '&' : av_bprintf(dst, "%s", "&"); break;
1021 case '<' : av_bprintf(dst, "%s", "<"); break;
1022 case '>' : av_bprintf(dst, "%s", ">"); break;
1023 case '\"': av_bprintf(dst, "%s", """); break;
1024 case '\'': av_bprintf(dst, "%s", "'"); break;
1025 default: av_bprint_chars(dst, *p, 1);
1032 static void xml_print_header(WriterContext *wctx)
1034 XMLContext *xml = wctx->priv;
1035 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1036 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1037 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1039 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1040 printf("<%sffprobe%s>\n",
1041 xml->fully_qualified ? "ffprobe:" : "",
1042 xml->fully_qualified ? qual : "");
1044 xml->indent_level++;
1047 static void xml_print_footer(WriterContext *wctx)
1049 XMLContext *xml = wctx->priv;
1051 xml->indent_level--;
1052 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1055 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1057 static void xml_print_chapter_header(WriterContext *wctx, const char *chapter)
1059 XMLContext *xml = wctx->priv;
1061 if (wctx->nb_chapter)
1063 xml->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "frames") ||
1064 !strcmp(chapter, "packets_and_frames") ||
1065 !strcmp(chapter, "streams") || !strcmp(chapter, "library_versions");
1067 if (xml->multiple_entries) {
1068 XML_INDENT(); printf("<%s>\n", chapter);
1069 xml->indent_level++;
1073 static void xml_print_chapter_footer(WriterContext *wctx, const char *chapter)
1075 XMLContext *xml = wctx->priv;
1077 if (xml->multiple_entries) {
1078 xml->indent_level--;
1079 XML_INDENT(); printf("</%s>\n", chapter);
1083 static void xml_print_section_header(WriterContext *wctx, const char *section)
1085 XMLContext *xml = wctx->priv;
1087 XML_INDENT(); printf("<%s ", section);
1088 xml->within_tag = 1;
1091 static void xml_print_section_footer(WriterContext *wctx, const char *section)
1093 XMLContext *xml = wctx->priv;
1095 if (xml->within_tag)
1098 XML_INDENT(); printf("</%s>\n", section);
1102 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1108 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1109 printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
1110 av_bprint_finalize(&buf, NULL);
1113 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1117 printf("%s=\"%lld\"", key, value);
1120 static void xml_show_tags(WriterContext *wctx, AVDictionary *dict)
1122 XMLContext *xml = wctx->priv;
1123 AVDictionaryEntry *tag = NULL;
1127 xml->indent_level++;
1128 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
1130 /* close section tag */
1132 xml->within_tag = 0;
1137 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1138 printf("<tag key=\"%s\"", xml_escape_str(&buf, tag->key, wctx));
1139 av_bprint_finalize(&buf, NULL);
1141 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1142 printf(" value=\"%s\"/>\n", xml_escape_str(&buf, tag->value, wctx));
1143 av_bprint_finalize(&buf, NULL);
1145 xml->indent_level--;
1148 static Writer xml_writer = {
1150 .priv_size = sizeof(XMLContext),
1152 .print_header = xml_print_header,
1153 .print_footer = xml_print_footer,
1154 .print_chapter_header = xml_print_chapter_header,
1155 .print_chapter_footer = xml_print_chapter_footer,
1156 .print_section_header = xml_print_section_header,
1157 .print_section_footer = xml_print_section_footer,
1158 .print_integer = xml_print_int,
1159 .print_string = xml_print_str,
1160 .show_tags = xml_show_tags,
1161 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1164 static void writer_register_all(void)
1166 static int initialized;
1172 writer_register(&default_writer);
1173 writer_register(&compact_writer);
1174 writer_register(&csv_writer);
1175 writer_register(&json_writer);
1176 writer_register(&xml_writer);
1179 #define print_fmt(k, f, ...) do { \
1180 av_bprint_clear(&pbuf); \
1181 av_bprintf(&pbuf, f, __VA_ARGS__); \
1182 writer_print_string(w, k, pbuf.str, 0); \
1185 #define print_int(k, v) writer_print_integer(w, k, v)
1186 #define print_str(k, v) writer_print_string(w, k, v, 0)
1187 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
1188 #define print_time(k, v, tb) writer_print_time(w, k, v, tb)
1189 #define print_ts(k, v) writer_print_ts(w, k, v)
1190 #define print_val(k, v, u) writer_print_string(w, k, \
1191 value_string(val_str, sizeof(val_str), (struct unit_value){.val.i = v, .unit=u}), 0)
1192 #define print_section_header(s) writer_print_section_header(w, s)
1193 #define print_section_footer(s) writer_print_section_footer(w, s)
1194 #define show_tags(metadata) writer_show_tags(w, metadata)
1196 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1199 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1203 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1205 print_section_header("packet");
1206 s = av_get_media_type_string(st->codec->codec_type);
1207 if (s) print_str ("codec_type", s);
1208 else print_str_opt("codec_type", "unknown");
1209 print_int("stream_index", pkt->stream_index);
1210 print_ts ("pts", pkt->pts);
1211 print_time("pts_time", pkt->pts, &st->time_base);
1212 print_ts ("dts", pkt->dts);
1213 print_time("dts_time", pkt->dts, &st->time_base);
1214 print_ts ("duration", pkt->duration);
1215 print_time("duration_time", pkt->duration, &st->time_base);
1216 print_val("size", pkt->size, unit_byte_str);
1217 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1218 else print_str_opt("pos", "N/A");
1219 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1220 print_section_footer("packet");
1222 av_bprint_finalize(&pbuf, NULL);
1226 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream)
1231 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1233 print_section_header("frame");
1235 s = av_get_media_type_string(stream->codec->codec_type);
1236 if (s) print_str ("media_type", s);
1237 else print_str_opt("media_type", "unknown");
1238 print_int("key_frame", frame->key_frame);
1239 print_ts ("pkt_pts", frame->pkt_pts);
1240 print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
1241 print_ts ("pkt_dts", frame->pkt_dts);
1242 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
1243 if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
1244 else print_str_opt("pkt_pos", "N/A");
1246 switch (stream->codec->codec_type) {
1247 case AVMEDIA_TYPE_VIDEO:
1248 print_int("width", frame->width);
1249 print_int("height", frame->height);
1250 s = av_get_pix_fmt_name(frame->format);
1251 if (s) print_str ("pix_fmt", s);
1252 else print_str_opt("pix_fmt", "unknown");
1253 if (frame->sample_aspect_ratio.num) {
1254 print_fmt("sample_aspect_ratio", "%d:%d",
1255 frame->sample_aspect_ratio.num,
1256 frame->sample_aspect_ratio.den);
1258 print_str_opt("sample_aspect_ratio", "N/A");
1260 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
1261 print_int("coded_picture_number", frame->coded_picture_number);
1262 print_int("display_picture_number", frame->display_picture_number);
1263 print_int("interlaced_frame", frame->interlaced_frame);
1264 print_int("top_field_first", frame->top_field_first);
1265 print_int("repeat_pict", frame->repeat_pict);
1266 print_int("reference", frame->reference);
1269 case AVMEDIA_TYPE_AUDIO:
1270 s = av_get_sample_fmt_name(frame->format);
1271 if (s) print_str ("sample_fmt", s);
1272 else print_str_opt("sample_fmt", "unknown");
1273 print_int("nb_samples", frame->nb_samples);
1277 print_section_footer("frame");
1279 av_bprint_finalize(&pbuf, NULL);
1283 static av_always_inline int get_decoded_frame(AVFormatContext *fmt_ctx,
1284 AVFrame *frame, int *got_frame,
1287 AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
1291 switch (dec_ctx->codec_type) {
1292 case AVMEDIA_TYPE_VIDEO:
1293 ret = avcodec_decode_video2(dec_ctx, frame, got_frame, pkt);
1296 case AVMEDIA_TYPE_AUDIO:
1297 ret = avcodec_decode_audio4(dec_ctx, frame, got_frame, pkt);
1304 static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1308 int i = 0, ret, got_frame;
1310 av_init_packet(&pkt);
1312 while (!av_read_frame(fmt_ctx, &pkt)) {
1313 if (do_read_packets) {
1314 if (do_show_packets)
1315 show_packet(w, fmt_ctx, &pkt, i++);
1316 nb_streams_packets[pkt.stream_index]++;
1318 if (do_read_frames) {
1321 avcodec_get_frame_defaults(&frame);
1322 ret = get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt1);
1323 if (ret < 0 || !got_frame)
1326 show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
1329 nb_streams_frames[pkt.stream_index]++;
1332 av_free_packet(&pkt);
1334 av_init_packet(&pkt);
1337 //Flush remaining frames that are cached in the decoder
1338 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1339 pkt.stream_index = i;
1340 while (get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt) >= 0 && got_frame) {
1341 if (do_read_frames) {
1343 show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
1344 nb_streams_frames[pkt.stream_index]++;
1350 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
1352 AVStream *stream = fmt_ctx->streams[stream_idx];
1353 AVCodecContext *dec_ctx;
1357 AVRational display_aspect_ratio;
1360 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1362 print_section_header("stream");
1364 print_int("index", stream->index);
1366 if ((dec_ctx = stream->codec)) {
1367 if ((dec = dec_ctx->codec)) {
1368 print_str("codec_name", dec->name);
1369 print_str("codec_long_name", dec->long_name);
1371 print_str_opt("codec_name", "unknown");
1372 print_str_opt("codec_long_name", "unknown");
1375 s = av_get_media_type_string(dec_ctx->codec_type);
1376 if (s) print_str ("codec_type", s);
1377 else print_str_opt("codec_type", "unknown");
1378 print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
1380 /* print AVI/FourCC tag */
1381 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
1382 print_str("codec_tag_string", val_str);
1383 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
1385 switch (dec_ctx->codec_type) {
1386 case AVMEDIA_TYPE_VIDEO:
1387 print_int("width", dec_ctx->width);
1388 print_int("height", dec_ctx->height);
1389 print_int("has_b_frames", dec_ctx->has_b_frames);
1390 if (dec_ctx->sample_aspect_ratio.num) {
1391 print_fmt("sample_aspect_ratio", "%d:%d",
1392 dec_ctx->sample_aspect_ratio.num,
1393 dec_ctx->sample_aspect_ratio.den);
1394 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1395 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
1396 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
1398 print_fmt("display_aspect_ratio", "%d:%d",
1399 display_aspect_ratio.num,
1400 display_aspect_ratio.den);
1402 print_str_opt("sample_aspect_ratio", "N/A");
1403 print_str_opt("display_aspect_ratio", "N/A");
1405 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
1406 if (s) print_str ("pix_fmt", s);
1407 else print_str_opt("pix_fmt", "unknown");
1408 print_int("level", dec_ctx->level);
1409 if (dec_ctx->timecode_frame_start >= 0) {
1410 char tcbuf[AV_TIMECODE_STR_SIZE];
1411 av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
1412 print_str("timecode", tcbuf);
1414 print_str_opt("timecode", "N/A");
1418 case AVMEDIA_TYPE_AUDIO:
1419 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
1420 if (s) print_str ("sample_fmt", s);
1421 else print_str_opt("sample_fmt", "unknown");
1422 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
1423 print_int("channels", dec_ctx->channels);
1424 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
1428 print_str_opt("codec_type", "unknown");
1430 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
1431 const AVOption *opt = NULL;
1432 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
1434 if (opt->flags) continue;
1435 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
1436 print_str(opt->name, str);
1442 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
1443 else print_str_opt("id", "N/A");
1444 print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
1445 print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
1446 print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
1447 print_time("start_time", stream->start_time, &stream->time_base);
1448 print_time("duration", stream->duration, &stream->time_base);
1449 if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
1450 else print_str_opt("bit_rate", "N/A");
1451 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
1452 else print_str_opt("nb_frames", "N/A");
1453 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
1454 else print_str_opt("nb_read_frames", "N/A");
1455 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
1456 else print_str_opt("nb_read_packets", "N/A");
1457 show_tags(stream->metadata);
1459 print_section_footer("stream");
1460 av_bprint_finalize(&pbuf, NULL);
1464 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
1467 for (i = 0; i < fmt_ctx->nb_streams; i++)
1468 show_stream(w, fmt_ctx, i);
1471 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
1474 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
1476 print_section_header("format");
1477 print_str("filename", fmt_ctx->filename);
1478 print_int("nb_streams", fmt_ctx->nb_streams);
1479 print_str("format_name", fmt_ctx->iformat->name);
1480 print_str("format_long_name", fmt_ctx->iformat->long_name);
1481 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
1482 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
1483 if (size >= 0) print_val ("size", size, unit_byte_str);
1484 else print_str_opt("size", "N/A");
1485 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
1486 else print_str_opt("bit_rate", "N/A");
1487 show_tags(fmt_ctx->metadata);
1488 print_section_footer("format");
1492 static void show_error(WriterContext *w, int err)
1495 const char *errbuf_ptr = errbuf;
1497 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
1498 errbuf_ptr = strerror(AVUNERROR(err));
1500 writer_print_chapter_header(w, "error");
1501 print_section_header("error");
1502 print_int("code", err);
1503 print_str("string", errbuf_ptr);
1504 print_section_footer("error");
1505 writer_print_chapter_footer(w, "error");
1508 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
1511 AVFormatContext *fmt_ctx = NULL;
1512 AVDictionaryEntry *t;
1514 if ((err = avformat_open_input(&fmt_ctx, filename,
1515 iformat, &format_opts)) < 0) {
1516 print_error(filename, err);
1519 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1520 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
1521 return AVERROR_OPTION_NOT_FOUND;
1525 /* fill the streams in the format context */
1526 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
1527 print_error(filename, err);
1531 av_dump_format(fmt_ctx, 0, filename, 0);
1533 /* bind a decoder to each input stream */
1534 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1535 AVStream *stream = fmt_ctx->streams[i];
1538 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
1539 av_log(NULL, AV_LOG_ERROR,
1540 "Unsupported codec with id %d for input stream %d\n",
1541 stream->codec->codec_id, stream->index);
1542 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
1543 av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
1548 *fmt_ctx_ptr = fmt_ctx;
1552 static void close_input_file(AVFormatContext **ctx_ptr)
1555 AVFormatContext *fmt_ctx = *ctx_ptr;
1557 /* close decoder for each stream */
1558 for (i = 0; i < fmt_ctx->nb_streams; i++)
1559 if (fmt_ctx->streams[i]->codec->codec_id != CODEC_ID_NONE)
1560 avcodec_close(fmt_ctx->streams[i]->codec);
1562 avformat_close_input(ctx_ptr);
1565 #define PRINT_CHAPTER(name) do { \
1566 if (do_show_ ## name) { \
1567 writer_print_chapter_header(wctx, #name); \
1568 show_ ## name (wctx, fmt_ctx); \
1569 writer_print_chapter_footer(wctx, #name); \
1573 static int probe_file(WriterContext *wctx, const char *filename)
1575 AVFormatContext *fmt_ctx;
1578 do_read_frames = do_show_frames || do_count_frames;
1579 do_read_packets = do_show_packets || do_count_packets;
1581 ret = open_input_file(&fmt_ctx, filename);
1583 nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
1584 nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
1585 if (do_read_frames || do_read_packets) {
1586 const char *chapter;
1587 if (do_show_frames && do_show_packets &&
1588 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
1589 chapter = "packets_and_frames";
1590 else if (do_show_packets && !do_show_frames)
1591 chapter = "packets";
1592 else // (!do_show_packets && do_show_frames)
1594 if (do_show_frames || do_show_packets)
1595 writer_print_chapter_header(wctx, chapter);
1596 read_packets(wctx, fmt_ctx);
1597 if (do_show_frames || do_show_packets)
1598 writer_print_chapter_footer(wctx, chapter);
1600 PRINT_CHAPTER(streams);
1601 PRINT_CHAPTER(format);
1602 close_input_file(&fmt_ctx);
1603 av_freep(&nb_streams_frames);
1604 av_freep(&nb_streams_packets);
1609 static void show_usage(void)
1611 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
1612 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
1613 av_log(NULL, AV_LOG_INFO, "\n");
1616 static void ffprobe_show_program_version(WriterContext *w)
1619 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1621 writer_print_chapter_header(w, "program_version");
1622 print_section_header("program_version");
1623 print_str("version", FFMPEG_VERSION);
1624 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
1625 program_birth_year, this_year);
1626 print_str("build_date", __DATE__);
1627 print_str("build_time", __TIME__);
1628 print_str("compiler_type", CC_TYPE);
1629 print_str("compiler_version", CC_VERSION);
1630 print_str("configuration", FFMPEG_CONFIGURATION);
1631 print_section_footer("program_version");
1632 writer_print_chapter_footer(w, "program_version");
1634 av_bprint_finalize(&pbuf, NULL);
1637 #define SHOW_LIB_VERSION(libname, LIBNAME) \
1639 if (CONFIG_##LIBNAME) { \
1640 unsigned int version = libname##_version(); \
1641 print_section_header("library_version"); \
1642 print_str("name", "lib" #libname); \
1643 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
1644 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
1645 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
1646 print_int("version", version); \
1647 print_section_footer("library_version"); \
1651 static void ffprobe_show_library_versions(WriterContext *w)
1653 writer_print_chapter_header(w, "library_versions");
1654 SHOW_LIB_VERSION(avutil, AVUTIL);
1655 SHOW_LIB_VERSION(avcodec, AVCODEC);
1656 SHOW_LIB_VERSION(avformat, AVFORMAT);
1657 SHOW_LIB_VERSION(avdevice, AVDEVICE);
1658 SHOW_LIB_VERSION(avfilter, AVFILTER);
1659 SHOW_LIB_VERSION(swscale, SWSCALE);
1660 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
1661 SHOW_LIB_VERSION(postproc, POSTPROC);
1662 writer_print_chapter_footer(w, "library_versions");
1665 static int opt_format(const char *opt, const char *arg)
1667 iformat = av_find_input_format(arg);
1669 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
1670 return AVERROR(EINVAL);
1675 static int opt_show_format_entry(const char *opt, const char *arg)
1678 av_dict_set(&fmt_entries_to_show, arg, "", 0);
1682 static void opt_input_file(void *optctx, const char *arg)
1684 if (input_filename) {
1685 av_log(NULL, AV_LOG_ERROR,
1686 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
1687 arg, input_filename);
1690 if (!strcmp(arg, "-"))
1692 input_filename = arg;
1695 static int opt_help(const char *opt, const char *arg)
1697 av_log_set_callback(log_callback_help);
1699 show_help_options(options, "Main options:\n", 0, 0);
1702 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
1707 static int opt_pretty(const char *opt, const char *arg)
1709 show_value_unit = 1;
1710 use_value_prefix = 1;
1711 use_byte_value_binary_prefix = 1;
1712 use_value_sexagesimal_format = 1;
1716 static int opt_show_versions(const char *opt, const char *arg)
1718 do_show_program_version = 1;
1719 do_show_library_versions = 1;
1723 static const OptionDef options[] = {
1724 #include "cmdutils_common_opts.h"
1725 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
1726 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
1727 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
1728 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
1729 "use binary prefixes for byte units" },
1730 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
1731 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
1732 { "pretty", 0, {(void*)&opt_pretty},
1733 "prettify the format of displayed values, make it more human readable" },
1734 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
1735 "set the output printing format (available formats are: default, compact, csv, json, xml)", "format" },
1736 { "show_error", OPT_BOOL, {(void*)&do_show_error} , "show probing error" },
1737 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
1738 { "show_frames", OPT_BOOL, {(void*)&do_show_frames} , "show frames info" },
1739 { "show_format_entry", HAS_ARG, {(void*)opt_show_format_entry},
1740 "show a particular entry from the format/container info", "entry" },
1741 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
1742 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
1743 { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
1744 { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
1745 { "show_program_version", OPT_BOOL, {(void*)&do_show_program_version}, "show ffprobe version" },
1746 { "show_library_versions", OPT_BOOL, {(void*)&do_show_library_versions}, "show library versions" },
1747 { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
1748 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
1749 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
1750 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
1751 { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
1755 int main(int argc, char **argv)
1758 WriterContext *wctx;
1760 char *w_name = NULL, *w_args = NULL;
1763 av_log_set_flags(AV_LOG_SKIP_REPEATED);
1764 parse_loglevel(argc, argv, options);
1766 avformat_network_init();
1769 avdevice_register_all();
1772 show_banner(argc, argv, options);
1773 parse_options(NULL, argc, argv, options, opt_input_file);
1775 writer_register_all();
1778 print_format = av_strdup("default");
1779 w_name = av_strtok(print_format, "=", &buf);
1782 w = writer_get_by_name(w_name);
1784 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
1785 ret = AVERROR(EINVAL);
1789 if ((ret = writer_open(&wctx, w, w_args, NULL)) >= 0) {
1790 writer_print_header(wctx);
1792 if (do_show_program_version)
1793 ffprobe_show_program_version(wctx);
1794 if (do_show_library_versions)
1795 ffprobe_show_library_versions(wctx);
1797 if (!input_filename &&
1798 ((do_show_format || do_show_streams || do_show_packets || do_show_error) ||
1799 (!do_show_program_version && !do_show_library_versions))) {
1801 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
1802 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
1803 ret = AVERROR(EINVAL);
1804 } else if (input_filename) {
1805 ret = probe_file(wctx, input_filename);
1806 if (ret < 0 && do_show_error)
1807 show_error(wctx, ret);
1810 writer_print_footer(wctx);
1811 writer_close(&wctx);
1815 av_freep(&print_format);
1818 av_dict_free(&fmt_entries_to_show);
1820 avformat_network_deinit();