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
31 #include "libavformat/avformat.h"
32 #include "libavcodec/avcodec.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/bprint.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/dict.h"
39 #include "libavutil/libm.h"
40 #include "libavutil/parseutils.h"
41 #include "libavutil/timecode.h"
42 #include "libavutil/timestamp.h"
43 #include "libavdevice/avdevice.h"
44 #include "libswscale/swscale.h"
45 #include "libswresample/swresample.h"
46 #include "libpostproc/postprocess.h"
49 const char program_name[] = "ffprobe";
50 const int program_birth_year = 2007;
52 static int do_bitexact = 0;
53 static int do_count_frames = 0;
54 static int do_count_packets = 0;
55 static int do_read_frames = 0;
56 static int do_read_packets = 0;
57 static int do_show_chapters = 0;
58 static int do_show_error = 0;
59 static int do_show_format = 0;
60 static int do_show_frames = 0;
61 static int do_show_packets = 0;
62 static int do_show_programs = 0;
63 static int do_show_streams = 0;
64 static int do_show_stream_disposition = 0;
65 static int do_show_data = 0;
66 static int do_show_program_version = 0;
67 static int do_show_library_versions = 0;
69 static int show_value_unit = 0;
70 static int use_value_prefix = 0;
71 static int use_byte_value_binary_prefix = 0;
72 static int use_value_sexagesimal_format = 0;
73 static int show_private_data = 1;
75 static char *print_format;
76 static char *stream_specifier;
79 int id; ///< identifier
80 int64_t start, end; ///< start, end in second/AV_TIME_BASE units
81 int has_start, has_end;
82 int start_is_offset, end_is_offset;
86 static ReadInterval *read_intervals;
87 static int read_intervals_nb = 0;
89 /* section structure definition */
91 #define SECTION_MAX_NB_CHILDREN 10
94 int id; ///< unique id identifying a section
97 #define SECTION_FLAG_IS_WRAPPER 1 ///< the section only contains other sections, but has no data at its own level
98 #define SECTION_FLAG_IS_ARRAY 2 ///< the section contains an array of elements of the same type
99 #define SECTION_FLAG_HAS_VARIABLE_FIELDS 4 ///< the section may contain a variable number of fields with variable keys.
100 /// For these sections the element_name field is mandatory.
102 int children_ids[SECTION_MAX_NB_CHILDREN+1]; ///< list of children section IDS, terminated by -1
103 const char *element_name; ///< name of the contained element, if provided
104 const char *unique_name; ///< unique section name, in case the name is ambiguous
105 AVDictionary *entries_to_show;
106 int show_all_entries;
110 SECTION_ID_NONE = -1,
112 SECTION_ID_CHAPTER_TAGS,
116 SECTION_ID_FORMAT_TAGS,
119 SECTION_ID_FRAME_TAGS,
120 SECTION_ID_LIBRARY_VERSION,
121 SECTION_ID_LIBRARY_VERSIONS,
124 SECTION_ID_PACKETS_AND_FRAMES,
125 SECTION_ID_PROGRAM_STREAM_DISPOSITION,
126 SECTION_ID_PROGRAM_STREAM_TAGS,
128 SECTION_ID_PROGRAM_STREAMS,
129 SECTION_ID_PROGRAM_STREAM,
130 SECTION_ID_PROGRAM_TAGS,
131 SECTION_ID_PROGRAM_VERSION,
135 SECTION_ID_STREAM_DISPOSITION,
137 SECTION_ID_STREAM_TAGS,
140 static struct section sections[] = {
141 [SECTION_ID_CHAPTERS] = { SECTION_ID_CHAPTERS, "chapters", SECTION_FLAG_IS_ARRAY, { SECTION_ID_CHAPTER, -1 } },
142 [SECTION_ID_CHAPTER] = { SECTION_ID_CHAPTER, "chapter", 0, { SECTION_ID_CHAPTER_TAGS, -1 } },
143 [SECTION_ID_CHAPTER_TAGS] = { SECTION_ID_CHAPTER_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "chapter_tags" },
144 [SECTION_ID_ERROR] = { SECTION_ID_ERROR, "error", 0, { -1 } },
145 [SECTION_ID_FORMAT] = { SECTION_ID_FORMAT, "format", 0, { SECTION_ID_FORMAT_TAGS, -1 } },
146 [SECTION_ID_FORMAT_TAGS] = { SECTION_ID_FORMAT_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "format_tags" },
147 [SECTION_ID_FRAMES] = { SECTION_ID_FRAMES, "frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_FRAME, -1 } },
148 [SECTION_ID_FRAME] = { SECTION_ID_FRAME, "frame", 0, { SECTION_ID_FRAME_TAGS, -1 } },
149 [SECTION_ID_FRAME_TAGS] = { SECTION_ID_FRAME_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "frame_tags" },
150 [SECTION_ID_LIBRARY_VERSIONS] = { SECTION_ID_LIBRARY_VERSIONS, "library_versions", SECTION_FLAG_IS_ARRAY, { SECTION_ID_LIBRARY_VERSION, -1 } },
151 [SECTION_ID_LIBRARY_VERSION] = { SECTION_ID_LIBRARY_VERSION, "library_version", 0, { -1 } },
152 [SECTION_ID_PACKETS] = { SECTION_ID_PACKETS, "packets", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
153 [SECTION_ID_PACKETS_AND_FRAMES] = { SECTION_ID_PACKETS_AND_FRAMES, "packets_and_frames", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PACKET, -1} },
154 [SECTION_ID_PACKET] = { SECTION_ID_PACKET, "packet", 0, { -1 } },
155 [SECTION_ID_PROGRAM_STREAM_DISPOSITION] = { SECTION_ID_PROGRAM_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "program_stream_disposition" },
156 [SECTION_ID_PROGRAM_STREAM_TAGS] = { SECTION_ID_PROGRAM_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_stream_tags" },
157 [SECTION_ID_PROGRAM] = { SECTION_ID_PROGRAM, "program", 0, { SECTION_ID_PROGRAM_TAGS, SECTION_ID_PROGRAM_STREAMS, -1 } },
158 [SECTION_ID_PROGRAM_STREAMS] = { SECTION_ID_PROGRAM_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM_STREAM, -1 }, .unique_name = "program_streams" },
159 [SECTION_ID_PROGRAM_STREAM] = { SECTION_ID_PROGRAM_STREAM, "stream", 0, { SECTION_ID_PROGRAM_STREAM_DISPOSITION, SECTION_ID_PROGRAM_STREAM_TAGS, -1 }, .unique_name = "program_stream" },
160 [SECTION_ID_PROGRAM_TAGS] = { SECTION_ID_PROGRAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "program_tags" },
161 [SECTION_ID_PROGRAM_VERSION] = { SECTION_ID_PROGRAM_VERSION, "program_version", 0, { -1 } },
162 [SECTION_ID_PROGRAMS] = { SECTION_ID_PROGRAMS, "programs", SECTION_FLAG_IS_ARRAY, { SECTION_ID_PROGRAM, -1 } },
163 [SECTION_ID_ROOT] = { SECTION_ID_ROOT, "root", SECTION_FLAG_IS_WRAPPER,
164 { SECTION_ID_CHAPTERS, SECTION_ID_FORMAT, SECTION_ID_FRAMES, SECTION_ID_PROGRAMS, SECTION_ID_STREAMS,
165 SECTION_ID_PACKETS, SECTION_ID_ERROR, SECTION_ID_PROGRAM_VERSION, SECTION_ID_LIBRARY_VERSIONS, -1} },
166 [SECTION_ID_STREAMS] = { SECTION_ID_STREAMS, "streams", SECTION_FLAG_IS_ARRAY, { SECTION_ID_STREAM, -1 } },
167 [SECTION_ID_STREAM] = { SECTION_ID_STREAM, "stream", 0, { SECTION_ID_STREAM_DISPOSITION, SECTION_ID_STREAM_TAGS, -1 } },
168 [SECTION_ID_STREAM_DISPOSITION] = { SECTION_ID_STREAM_DISPOSITION, "disposition", 0, { -1 }, .unique_name = "stream_disposition" },
169 [SECTION_ID_STREAM_TAGS] = { SECTION_ID_STREAM_TAGS, "tags", SECTION_FLAG_HAS_VARIABLE_FIELDS, { -1 }, .element_name = "tag", .unique_name = "stream_tags" },
172 static const OptionDef *options;
174 /* FFprobe context */
175 static const char *input_filename;
176 static AVInputFormat *iformat = NULL;
178 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
179 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
181 static const char unit_second_str[] = "s" ;
182 static const char unit_hertz_str[] = "Hz" ;
183 static const char unit_byte_str[] = "byte" ;
184 static const char unit_bit_per_second_str[] = "bit/s";
186 static uint64_t *nb_streams_packets;
187 static uint64_t *nb_streams_frames;
188 static int *selected_streams;
190 static void ffprobe_cleanup(int ret)
193 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
194 av_dict_free(&(sections[i].entries_to_show));
198 union { double d; long long int i; } val;
202 static char *value_string(char *buf, int buf_size, struct unit_value uv)
208 if (uv.unit == unit_second_str) {
212 vald = vali = uv.val.i;
215 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
219 mins = (int)secs / 60;
220 secs = secs - mins * 60;
223 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
225 const char *prefix_string = "";
227 if (use_value_prefix && vald > 1) {
230 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
231 index = (long long int) (log2(vald)) / 10;
232 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
233 vald /= exp2(index * 10);
234 prefix_string = binary_unit_prefixes[index];
236 index = (long long int) (log10(vald)) / 3;
237 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
238 vald /= pow(10, index * 3);
239 prefix_string = decimal_unit_prefixes[index];
243 if (show_float || (use_value_prefix && vald != (long long int)vald))
244 snprintf(buf, buf_size, "%f", vald);
246 snprintf(buf, buf_size, "%lld", vali);
247 av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
248 prefix_string, show_value_unit ? uv.unit : "");
256 typedef struct WriterContext WriterContext;
258 #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
259 #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
261 typedef struct Writer {
262 const AVClass *priv_class; ///< private class of the writer, if any
263 int priv_size; ///< private size for the writer context
266 int (*init) (WriterContext *wctx);
267 void (*uninit)(WriterContext *wctx);
269 void (*print_section_header)(WriterContext *wctx);
270 void (*print_section_footer)(WriterContext *wctx);
271 void (*print_integer) (WriterContext *wctx, const char *, long long int);
272 void (*print_rational) (WriterContext *wctx, AVRational *q, char *sep);
273 void (*print_string) (WriterContext *wctx, const char *, const char *);
274 int flags; ///< a combination or WRITER_FLAG_*
277 #define SECTION_MAX_NB_LEVELS 10
279 struct WriterContext {
280 const AVClass *class; ///< class of the writer
281 const Writer *writer; ///< the Writer of which this is an instance
282 char *name; ///< name of this writer instance
283 void *priv; ///< private data for use by the filter
285 const struct section *sections; ///< array containing all sections
286 int nb_sections; ///< number of sections
288 int level; ///< current level, starting from 0
290 /** number of the item printed in the given section, starting from 0 */
291 unsigned int nb_item[SECTION_MAX_NB_LEVELS];
293 /** section per each level */
294 const struct section *section[SECTION_MAX_NB_LEVELS];
295 AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
296 /// used by various writers
298 unsigned int nb_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
299 unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
300 unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
303 static const char *writer_get_name(void *p)
305 WriterContext *wctx = p;
306 return wctx->writer->name;
309 static const AVClass writer_class = {
313 LIBAVUTIL_VERSION_INT,
316 static void writer_close(WriterContext **wctx)
323 if ((*wctx)->writer->uninit)
324 (*wctx)->writer->uninit(*wctx);
325 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
326 av_bprint_finalize(&(*wctx)->section_pbuf[i], NULL);
327 if ((*wctx)->writer->priv_class)
328 av_opt_free((*wctx)->priv);
329 av_freep(&((*wctx)->priv));
333 static int writer_open(WriterContext **wctx, const Writer *writer, const char *args,
334 const struct section *sections, int nb_sections)
338 if (!(*wctx = av_mallocz(sizeof(WriterContext)))) {
339 ret = AVERROR(ENOMEM);
343 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
344 ret = AVERROR(ENOMEM);
348 (*wctx)->class = &writer_class;
349 (*wctx)->writer = writer;
351 (*wctx)->sections = sections;
352 (*wctx)->nb_sections = nb_sections;
354 if (writer->priv_class) {
355 void *priv_ctx = (*wctx)->priv;
356 *((const AVClass **)priv_ctx) = writer->priv_class;
357 av_opt_set_defaults(priv_ctx);
360 (ret = av_set_options_string(priv_ctx, args, "=", ":")) < 0)
364 for (i = 0; i < SECTION_MAX_NB_LEVELS; i++)
365 av_bprint_init(&(*wctx)->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
367 if ((*wctx)->writer->init)
368 ret = (*wctx)->writer->init(*wctx);
379 static inline void writer_print_section_header(WriterContext *wctx,
382 int parent_section_id;
384 av_assert0(wctx->level < SECTION_MAX_NB_LEVELS);
385 parent_section_id = wctx->level ?
386 (wctx->section[wctx->level-1])->id : SECTION_ID_NONE;
388 wctx->nb_item[wctx->level] = 0;
389 wctx->section[wctx->level] = &wctx->sections[section_id];
391 if (section_id == SECTION_ID_PACKETS_AND_FRAMES) {
392 wctx->nb_section_packet = wctx->nb_section_frame =
393 wctx->nb_section_packet_frame = 0;
394 } else if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
395 wctx->nb_section_packet_frame = section_id == SECTION_ID_PACKET ?
396 wctx->nb_section_packet : wctx->nb_section_frame;
399 if (wctx->writer->print_section_header)
400 wctx->writer->print_section_header(wctx);
403 static inline void writer_print_section_footer(WriterContext *wctx)
405 int section_id = wctx->section[wctx->level]->id;
406 int parent_section_id = wctx->level ?
407 wctx->section[wctx->level-1]->id : SECTION_ID_NONE;
409 if (parent_section_id != SECTION_ID_NONE)
410 wctx->nb_item[wctx->level-1]++;
411 if (parent_section_id == SECTION_ID_PACKETS_AND_FRAMES) {
412 if (section_id == SECTION_ID_PACKET) wctx->nb_section_packet++;
413 else wctx->nb_section_frame++;
415 if (wctx->writer->print_section_footer)
416 wctx->writer->print_section_footer(wctx);
420 static inline void writer_print_integer(WriterContext *wctx,
421 const char *key, long long int val)
423 const struct section *section = wctx->section[wctx->level];
425 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
426 wctx->writer->print_integer(wctx, key, val);
427 wctx->nb_item[wctx->level]++;
431 static inline void writer_print_string(WriterContext *wctx,
432 const char *key, const char *val, int opt)
434 const struct section *section = wctx->section[wctx->level];
436 if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
439 if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
440 wctx->writer->print_string(wctx, key, val);
441 wctx->nb_item[wctx->level]++;
445 static inline void writer_print_rational(WriterContext *wctx,
446 const char *key, AVRational q, char sep)
449 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
450 av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
451 writer_print_string(wctx, key, buf.str, 0);
454 static void writer_print_time(WriterContext *wctx, const char *key,
455 int64_t ts, const AVRational *time_base, int is_duration)
459 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
460 writer_print_string(wctx, key, "N/A", 1);
462 double d = ts * av_q2d(*time_base);
463 struct unit_value uv;
465 uv.unit = unit_second_str;
466 value_string(buf, sizeof(buf), uv);
467 writer_print_string(wctx, key, buf, 0);
471 static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts, int is_duration)
473 if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
474 writer_print_string(wctx, key, "N/A", 1);
476 writer_print_integer(wctx, key, ts);
480 static void writer_print_data(WriterContext *wctx, const char *name,
481 uint8_t *data, int size)
484 int offset = 0, l, i;
486 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
487 av_bprintf(&bp, "\n");
489 av_bprintf(&bp, "%08x: ", offset);
491 for (i = 0; i < l; i++) {
492 av_bprintf(&bp, "%02x", data[i]);
494 av_bprintf(&bp, " ");
496 av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
497 for (i = 0; i < l; i++)
498 av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
499 av_bprintf(&bp, "\n");
504 writer_print_string(wctx, name, bp.str, 0);
505 av_bprint_finalize(&bp, NULL);
508 #define MAX_REGISTERED_WRITERS_NB 64
510 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
512 static int writer_register(const Writer *writer)
514 static int next_registered_writer_idx = 0;
516 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
517 return AVERROR(ENOMEM);
519 registered_writers[next_registered_writer_idx++] = writer;
523 static const Writer *writer_get_by_name(const char *name)
527 for (i = 0; registered_writers[i]; i++)
528 if (!strcmp(registered_writers[i]->name, name))
529 return registered_writers[i];
537 #define DEFINE_WRITER_CLASS(name) \
538 static const char *name##_get_name(void *ctx) \
542 static const AVClass name##_class = { \
550 typedef struct DefaultContext {
551 const AVClass *class;
553 int noprint_wrappers;
554 int nested_section[SECTION_MAX_NB_LEVELS];
557 #define OFFSET(x) offsetof(DefaultContext, x)
559 static const AVOption default_options[] = {
560 { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
561 { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
562 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
563 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
567 DEFINE_WRITER_CLASS(default);
569 /* lame uppercasing routine, assumes the string is lower case ASCII */
570 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
573 for (i = 0; src[i] && i < dst_size-1; i++)
574 dst[i] = av_toupper(src[i]);
579 static void default_print_section_header(WriterContext *wctx)
581 DefaultContext *def = wctx->priv;
583 const struct section *section = wctx->section[wctx->level];
584 const struct section *parent_section = wctx->level ?
585 wctx->section[wctx->level-1] : NULL;
587 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
588 if (parent_section &&
589 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
590 def->nested_section[wctx->level] = 1;
591 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
592 wctx->section_pbuf[wctx->level-1].str,
593 upcase_string(buf, sizeof(buf),
594 av_x_if_null(section->element_name, section->name)));
597 if (def->noprint_wrappers || def->nested_section[wctx->level])
600 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
601 printf("[%s]\n", upcase_string(buf, sizeof(buf), section->name));
604 static void default_print_section_footer(WriterContext *wctx)
606 DefaultContext *def = wctx->priv;
607 const struct section *section = wctx->section[wctx->level];
610 if (def->noprint_wrappers || def->nested_section[wctx->level])
613 if (!(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
614 printf("[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
617 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
619 DefaultContext *def = wctx->priv;
622 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
623 printf("%s\n", value);
626 static void default_print_int(WriterContext *wctx, const char *key, long long int value)
628 DefaultContext *def = wctx->priv;
631 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
632 printf("%lld\n", value);
635 static const Writer default_writer = {
637 .priv_size = sizeof(DefaultContext),
638 .print_section_header = default_print_section_header,
639 .print_section_footer = default_print_section_footer,
640 .print_integer = default_print_int,
641 .print_string = default_print_str,
642 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
643 .priv_class = &default_class,
649 * Apply C-language-like string escaping.
651 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
655 for (p = src; *p; p++) {
657 case '\b': av_bprintf(dst, "%s", "\\b"); break;
658 case '\f': av_bprintf(dst, "%s", "\\f"); break;
659 case '\n': av_bprintf(dst, "%s", "\\n"); break;
660 case '\r': av_bprintf(dst, "%s", "\\r"); break;
661 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
664 av_bprint_chars(dst, '\\', 1);
665 av_bprint_chars(dst, *p, 1);
672 * Quote fields containing special characters, check RFC4180.
674 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
676 char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
677 int needs_quoting = !!src[strcspn(src, meta_chars)];
680 av_bprint_chars(dst, '"', 1);
682 for (; *src; src++) {
684 av_bprint_chars(dst, '"', 1);
685 av_bprint_chars(dst, *src, 1);
688 av_bprint_chars(dst, '"', 1);
692 static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
697 typedef struct CompactContext {
698 const AVClass *class;
703 char *escape_mode_str;
704 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
705 int nested_section[SECTION_MAX_NB_LEVELS];
706 int has_nested_elems[SECTION_MAX_NB_LEVELS];
707 int terminate_line[SECTION_MAX_NB_LEVELS];
711 #define OFFSET(x) offsetof(CompactContext, x)
713 static const AVOption compact_options[]= {
714 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
715 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
716 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
717 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
718 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
719 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
720 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
721 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
725 DEFINE_WRITER_CLASS(compact);
727 static av_cold int compact_init(WriterContext *wctx)
729 CompactContext *compact = wctx->priv;
731 if (strlen(compact->item_sep_str) != 1) {
732 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
733 compact->item_sep_str);
734 return AVERROR(EINVAL);
736 compact->item_sep = compact->item_sep_str[0];
738 if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
739 else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
740 else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
742 av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
743 return AVERROR(EINVAL);
749 static void compact_print_section_header(WriterContext *wctx)
751 CompactContext *compact = wctx->priv;
752 const struct section *section = wctx->section[wctx->level];
753 const struct section *parent_section = wctx->level ?
754 wctx->section[wctx->level-1] : NULL;
755 compact->terminate_line[wctx->level] = 1;
756 compact->has_nested_elems[wctx->level] = 0;
758 av_bprint_clear(&wctx->section_pbuf[wctx->level]);
759 if (!(section->flags & SECTION_FLAG_IS_ARRAY) && parent_section &&
760 !(parent_section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY))) {
761 compact->nested_section[wctx->level] = 1;
762 compact->has_nested_elems[wctx->level-1] = 1;
763 av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
764 wctx->section_pbuf[wctx->level-1].str,
765 (char *)av_x_if_null(section->element_name, section->name));
766 wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level-1];
768 if (parent_section && compact->has_nested_elems[wctx->level-1] &&
769 (section->flags & SECTION_FLAG_IS_ARRAY)) {
770 compact->terminate_line[wctx->level-1] = 0;
773 if (compact->print_section &&
774 !(section->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
775 printf("%s%c", section->name, compact->item_sep);
779 static void compact_print_section_footer(WriterContext *wctx)
781 CompactContext *compact = wctx->priv;
783 if (!compact->nested_section[wctx->level] &&
784 compact->terminate_line[wctx->level] &&
785 !(wctx->section[wctx->level]->flags & (SECTION_FLAG_IS_WRAPPER|SECTION_FLAG_IS_ARRAY)))
789 static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
791 CompactContext *compact = wctx->priv;
794 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
796 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
797 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
798 printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
799 av_bprint_finalize(&buf, NULL);
802 static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
804 CompactContext *compact = wctx->priv;
806 if (wctx->nb_item[wctx->level]) printf("%c", compact->item_sep);
808 printf("%s%s=", wctx->section_pbuf[wctx->level].str, key);
809 printf("%lld", value);
812 static const Writer compact_writer = {
814 .priv_size = sizeof(CompactContext),
815 .init = compact_init,
816 .print_section_header = compact_print_section_header,
817 .print_section_footer = compact_print_section_footer,
818 .print_integer = compact_print_int,
819 .print_string = compact_print_str,
820 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
821 .priv_class = &compact_class,
827 #define OFFSET(x) offsetof(CompactContext, x)
829 static const AVOption csv_options[] = {
830 {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
831 {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str=","}, CHAR_MIN, CHAR_MAX },
832 {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
833 {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
834 {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
835 {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="csv"}, CHAR_MIN, CHAR_MAX },
836 {"print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
837 {"p", "print section name", OFFSET(print_section), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
841 DEFINE_WRITER_CLASS(csv);
843 static const Writer csv_writer = {
845 .priv_size = sizeof(CompactContext),
846 .init = compact_init,
847 .print_section_header = compact_print_section_header,
848 .print_section_footer = compact_print_section_footer,
849 .print_integer = compact_print_int,
850 .print_string = compact_print_str,
851 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
852 .priv_class = &csv_class,
857 typedef struct FlatContext {
858 const AVClass *class;
865 #define OFFSET(x) offsetof(FlatContext, x)
867 static const AVOption flat_options[]= {
868 {"sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
869 {"s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, {.str="."}, CHAR_MIN, CHAR_MAX },
870 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
871 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
875 DEFINE_WRITER_CLASS(flat);
877 static av_cold int flat_init(WriterContext *wctx)
879 FlatContext *flat = wctx->priv;
881 if (strlen(flat->sep_str) != 1) {
882 av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
884 return AVERROR(EINVAL);
886 flat->sep = flat->sep_str[0];
891 static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
895 for (p = src; *p; p++) {
896 if (!((*p >= '0' && *p <= '9') ||
897 (*p >= 'a' && *p <= 'z') ||
898 (*p >= 'A' && *p <= 'Z')))
899 av_bprint_chars(dst, '_', 1);
901 av_bprint_chars(dst, *p, 1);
906 static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
910 for (p = src; *p; p++) {
912 case '\n': av_bprintf(dst, "%s", "\\n"); break;
913 case '\r': av_bprintf(dst, "%s", "\\r"); break;
914 case '\\': av_bprintf(dst, "%s", "\\\\"); break;
915 case '"': av_bprintf(dst, "%s", "\\\""); break;
916 case '`': av_bprintf(dst, "%s", "\\`"); break;
917 case '$': av_bprintf(dst, "%s", "\\$"); break;
918 default: av_bprint_chars(dst, *p, 1); break;
924 static void flat_print_section_header(WriterContext *wctx)
926 FlatContext *flat = wctx->priv;
927 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
928 const struct section *section = wctx->section[wctx->level];
929 const struct section *parent_section = wctx->level ?
930 wctx->section[wctx->level-1] : NULL;
932 /* build section header */
933 av_bprint_clear(buf);
936 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
938 if (flat->hierarchical ||
939 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
940 av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
942 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
943 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
944 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
945 av_bprintf(buf, "%d%s", n, flat->sep_str);
950 static void flat_print_int(WriterContext *wctx, const char *key, long long int value)
952 printf("%s%s=%lld\n", wctx->section_pbuf[wctx->level].str, key, value);
955 static void flat_print_str(WriterContext *wctx, const char *key, const char *value)
957 FlatContext *flat = wctx->priv;
960 printf("%s", wctx->section_pbuf[wctx->level].str);
961 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
962 printf("%s=", flat_escape_key_str(&buf, key, flat->sep));
963 av_bprint_clear(&buf);
964 printf("\"%s\"\n", flat_escape_value_str(&buf, value));
965 av_bprint_finalize(&buf, NULL);
968 static const Writer flat_writer = {
970 .priv_size = sizeof(FlatContext),
972 .print_section_header = flat_print_section_header,
973 .print_integer = flat_print_int,
974 .print_string = flat_print_str,
975 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
976 .priv_class = &flat_class,
979 /* INI format output */
982 const AVClass *class;
987 #define OFFSET(x) offsetof(INIContext, x)
989 static const AVOption ini_options[] = {
990 {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
991 {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.i64=1}, 0, 1 },
995 DEFINE_WRITER_CLASS(ini);
997 static char *ini_escape_str(AVBPrint *dst, const char *src)
1002 while (c = src[i++]) {
1004 case '\b': av_bprintf(dst, "%s", "\\b"); break;
1005 case '\f': av_bprintf(dst, "%s", "\\f"); break;
1006 case '\n': av_bprintf(dst, "%s", "\\n"); break;
1007 case '\r': av_bprintf(dst, "%s", "\\r"); break;
1008 case '\t': av_bprintf(dst, "%s", "\\t"); break;
1012 case ':' : av_bprint_chars(dst, '\\', 1);
1014 if ((unsigned char)c < 32)
1015 av_bprintf(dst, "\\x00%02x", c & 0xff);
1017 av_bprint_chars(dst, c, 1);
1024 static void ini_print_section_header(WriterContext *wctx)
1026 INIContext *ini = wctx->priv;
1027 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
1028 const struct section *section = wctx->section[wctx->level];
1029 const struct section *parent_section = wctx->level ?
1030 wctx->section[wctx->level-1] : NULL;
1032 av_bprint_clear(buf);
1033 if (!parent_section) {
1034 printf("# ffprobe output\n\n");
1038 if (wctx->nb_item[wctx->level-1])
1041 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level-1].str);
1042 if (ini->hierarchical ||
1043 !(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER))) {
1044 av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
1046 if (parent_section->flags & SECTION_FLAG_IS_ARRAY) {
1047 int n = parent_section->id == SECTION_ID_PACKETS_AND_FRAMES ?
1048 wctx->nb_section_packet_frame : wctx->nb_item[wctx->level-1];
1049 av_bprintf(buf, ".%d", n);
1053 if (!(section->flags & (SECTION_FLAG_IS_ARRAY|SECTION_FLAG_IS_WRAPPER)))
1054 printf("[%s]\n", buf->str);
1057 static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
1061 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1062 printf("%s=", ini_escape_str(&buf, key));
1063 av_bprint_clear(&buf);
1064 printf("%s\n", ini_escape_str(&buf, value));
1065 av_bprint_finalize(&buf, NULL);
1068 static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
1070 printf("%s=%lld\n", key, value);
1073 static const Writer ini_writer = {
1075 .priv_size = sizeof(INIContext),
1076 .print_section_header = ini_print_section_header,
1077 .print_integer = ini_print_int,
1078 .print_string = ini_print_str,
1079 .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1080 .priv_class = &ini_class,
1086 const AVClass *class;
1089 const char *item_sep, *item_start_end;
1093 #define OFFSET(x) offsetof(JSONContext, x)
1095 static const AVOption json_options[]= {
1096 { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1097 { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1101 DEFINE_WRITER_CLASS(json);
1103 static av_cold int json_init(WriterContext *wctx)
1105 JSONContext *json = wctx->priv;
1107 json->item_sep = json->compact ? ", " : ",\n";
1108 json->item_start_end = json->compact ? " " : "\n";
1113 static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1115 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
1116 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
1119 for (p = src; *p; p++) {
1120 char *s = strchr(json_escape, *p);
1122 av_bprint_chars(dst, '\\', 1);
1123 av_bprint_chars(dst, json_subst[s - json_escape], 1);
1124 } else if ((unsigned char)*p < 32) {
1125 av_bprintf(dst, "\\u00%02x", *p & 0xff);
1127 av_bprint_chars(dst, *p, 1);
1133 #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
1135 static void json_print_section_header(WriterContext *wctx)
1137 JSONContext *json = wctx->priv;
1139 const struct section *section = wctx->section[wctx->level];
1140 const struct section *parent_section = wctx->level ?
1141 wctx->section[wctx->level-1] : NULL;
1143 if (wctx->level && wctx->nb_item[wctx->level-1])
1146 if (section->flags & SECTION_FLAG_IS_WRAPPER) {
1148 json->indent_level++;
1150 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1151 json_escape_str(&buf, section->name, wctx);
1154 json->indent_level++;
1155 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1156 printf("\"%s\": [\n", buf.str);
1157 } else if (parent_section && !(parent_section->flags & SECTION_FLAG_IS_ARRAY)) {
1158 printf("\"%s\": {%s", buf.str, json->item_start_end);
1160 printf("{%s", json->item_start_end);
1162 /* this is required so the parser can distinguish between packets and frames */
1163 if (parent_section && parent_section->id == SECTION_ID_PACKETS_AND_FRAMES) {
1166 printf("\"type\": \"%s\"%s", section->name, json->item_sep);
1169 av_bprint_finalize(&buf, NULL);
1173 static void json_print_section_footer(WriterContext *wctx)
1175 JSONContext *json = wctx->priv;
1176 const struct section *section = wctx->section[wctx->level];
1178 if (wctx->level == 0) {
1179 json->indent_level--;
1181 } else if (section->flags & SECTION_FLAG_IS_ARRAY) {
1183 json->indent_level--;
1187 printf("%s", json->item_start_end);
1188 json->indent_level--;
1195 static inline void json_print_item_str(WriterContext *wctx,
1196 const char *key, const char *value)
1200 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1201 printf("\"%s\":", json_escape_str(&buf, key, wctx));
1202 av_bprint_clear(&buf);
1203 printf(" \"%s\"", json_escape_str(&buf, value, wctx));
1204 av_bprint_finalize(&buf, NULL);
1207 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
1209 JSONContext *json = wctx->priv;
1211 if (wctx->nb_item[wctx->level])
1212 printf("%s", json->item_sep);
1215 json_print_item_str(wctx, key, value);
1218 static void json_print_int(WriterContext *wctx, const char *key, long long int value)
1220 JSONContext *json = wctx->priv;
1223 if (wctx->nb_item[wctx->level])
1224 printf("%s", json->item_sep);
1228 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1229 printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
1230 av_bprint_finalize(&buf, NULL);
1233 static const Writer json_writer = {
1235 .priv_size = sizeof(JSONContext),
1237 .print_section_header = json_print_section_header,
1238 .print_section_footer = json_print_section_footer,
1239 .print_integer = json_print_int,
1240 .print_string = json_print_str,
1241 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1242 .priv_class = &json_class,
1248 const AVClass *class;
1251 int fully_qualified;
1256 #define OFFSET(x) offsetof(XMLContext, x)
1258 static const AVOption xml_options[] = {
1259 {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1260 {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1261 {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1262 {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.i64=0}, 0, 1 },
1266 DEFINE_WRITER_CLASS(xml);
1268 static av_cold int xml_init(WriterContext *wctx)
1270 XMLContext *xml = wctx->priv;
1272 if (xml->xsd_strict) {
1273 xml->fully_qualified = 1;
1274 #define CHECK_COMPLIANCE(opt, opt_name) \
1276 av_log(wctx, AV_LOG_ERROR, \
1277 "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
1278 "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
1279 return AVERROR(EINVAL); \
1281 CHECK_COMPLIANCE(show_private_data, "private");
1282 CHECK_COMPLIANCE(show_value_unit, "unit");
1283 CHECK_COMPLIANCE(use_value_prefix, "prefix");
1285 if (do_show_frames && do_show_packets) {
1286 av_log(wctx, AV_LOG_ERROR,
1287 "Interleaved frames and packets are not allowed in XSD. "
1288 "Select only one between the -show_frames and the -show_packets options.\n");
1289 return AVERROR(EINVAL);
1296 static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
1300 for (p = src; *p; p++) {
1302 case '&' : av_bprintf(dst, "%s", "&"); break;
1303 case '<' : av_bprintf(dst, "%s", "<"); break;
1304 case '>' : av_bprintf(dst, "%s", ">"); break;
1305 case '"' : av_bprintf(dst, "%s", """); break;
1306 case '\'': av_bprintf(dst, "%s", "'"); break;
1307 default: av_bprint_chars(dst, *p, 1);
1314 #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
1316 static void xml_print_section_header(WriterContext *wctx)
1318 XMLContext *xml = wctx->priv;
1319 const struct section *section = wctx->section[wctx->level];
1320 const struct section *parent_section = wctx->level ?
1321 wctx->section[wctx->level-1] : NULL;
1323 if (wctx->level == 0) {
1324 const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
1325 "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
1326 "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
1328 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1329 printf("<%sffprobe%s>\n",
1330 xml->fully_qualified ? "ffprobe:" : "",
1331 xml->fully_qualified ? qual : "");
1335 if (xml->within_tag) {
1336 xml->within_tag = 0;
1339 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1340 xml->indent_level++;
1342 if (parent_section && (parent_section->flags & SECTION_FLAG_IS_WRAPPER) &&
1343 wctx->level && wctx->nb_item[wctx->level-1])
1345 xml->indent_level++;
1347 if (section->flags & SECTION_FLAG_IS_ARRAY) {
1348 XML_INDENT(); printf("<%s>\n", section->name);
1350 XML_INDENT(); printf("<%s ", section->name);
1351 xml->within_tag = 1;
1356 static void xml_print_section_footer(WriterContext *wctx)
1358 XMLContext *xml = wctx->priv;
1359 const struct section *section = wctx->section[wctx->level];
1361 if (wctx->level == 0) {
1362 printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
1363 } else if (xml->within_tag) {
1364 xml->within_tag = 0;
1366 xml->indent_level--;
1367 } else if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1368 xml->indent_level--;
1370 XML_INDENT(); printf("</%s>\n", section->name);
1371 xml->indent_level--;
1375 static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
1378 XMLContext *xml = wctx->priv;
1379 const struct section *section = wctx->section[wctx->level];
1381 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
1383 if (section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS) {
1385 printf("<%s key=\"%s\"",
1386 section->element_name, xml_escape_str(&buf, key, wctx));
1387 av_bprint_clear(&buf);
1388 printf(" value=\"%s\"/>\n", xml_escape_str(&buf, value, wctx));
1390 if (wctx->nb_item[wctx->level])
1392 printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
1395 av_bprint_finalize(&buf, NULL);
1398 static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
1400 if (wctx->nb_item[wctx->level])
1402 printf("%s=\"%lld\"", key, value);
1405 static Writer xml_writer = {
1407 .priv_size = sizeof(XMLContext),
1409 .print_section_header = xml_print_section_header,
1410 .print_section_footer = xml_print_section_footer,
1411 .print_integer = xml_print_int,
1412 .print_string = xml_print_str,
1413 .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
1414 .priv_class = &xml_class,
1417 static void writer_register_all(void)
1419 static int initialized;
1425 writer_register(&default_writer);
1426 writer_register(&compact_writer);
1427 writer_register(&csv_writer);
1428 writer_register(&flat_writer);
1429 writer_register(&ini_writer);
1430 writer_register(&json_writer);
1431 writer_register(&xml_writer);
1434 #define print_fmt(k, f, ...) do { \
1435 av_bprint_clear(&pbuf); \
1436 av_bprintf(&pbuf, f, __VA_ARGS__); \
1437 writer_print_string(w, k, pbuf.str, 0); \
1440 #define print_int(k, v) writer_print_integer(w, k, v)
1441 #define print_q(k, v, s) writer_print_rational(w, k, v, s)
1442 #define print_str(k, v) writer_print_string(w, k, v, 0)
1443 #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
1444 #define print_time(k, v, tb) writer_print_time(w, k, v, tb, 0)
1445 #define print_ts(k, v) writer_print_ts(w, k, v, 0)
1446 #define print_duration_time(k, v, tb) writer_print_time(w, k, v, tb, 1)
1447 #define print_duration_ts(k, v) writer_print_ts(w, k, v, 1)
1448 #define print_val(k, v, u) do { \
1449 struct unit_value uv; \
1452 writer_print_string(w, k, value_string(val_str, sizeof(val_str), uv), 0); \
1455 #define print_section_header(s) writer_print_section_header(w, s)
1456 #define print_section_footer(s) writer_print_section_footer(w, s)
1458 static inline void show_tags(WriterContext *wctx, AVDictionary *tags, int section_id)
1460 AVDictionaryEntry *tag = NULL;
1464 writer_print_section_header(wctx, section_id);
1465 while ((tag = av_dict_get(tags, "", tag, AV_DICT_IGNORE_SUFFIX)))
1466 writer_print_string(wctx, tag->key, tag->value, 0);
1467 writer_print_section_footer(wctx);
1470 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
1473 AVStream *st = fmt_ctx->streams[pkt->stream_index];
1477 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1479 writer_print_section_header(w, SECTION_ID_PACKET);
1481 s = av_get_media_type_string(st->codec->codec_type);
1482 if (s) print_str ("codec_type", s);
1483 else print_str_opt("codec_type", "unknown");
1484 print_int("stream_index", pkt->stream_index);
1485 print_ts ("pts", pkt->pts);
1486 print_time("pts_time", pkt->pts, &st->time_base);
1487 print_ts ("dts", pkt->dts);
1488 print_time("dts_time", pkt->dts, &st->time_base);
1489 print_duration_ts("duration", pkt->duration);
1490 print_duration_time("duration_time", pkt->duration, &st->time_base);
1491 print_duration_ts("convergence_duration", pkt->convergence_duration);
1492 print_duration_time("convergence_duration_time", pkt->convergence_duration, &st->time_base);
1493 print_val("size", pkt->size, unit_byte_str);
1494 if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
1495 else print_str_opt("pos", "N/A");
1496 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
1498 writer_print_data(w, "data", pkt->data, pkt->size);
1499 writer_print_section_footer(w);
1501 av_bprint_finalize(&pbuf, NULL);
1505 static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
1506 AVFormatContext *fmt_ctx)
1511 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1513 writer_print_section_header(w, SECTION_ID_FRAME);
1515 s = av_get_media_type_string(stream->codec->codec_type);
1516 if (s) print_str ("media_type", s);
1517 else print_str_opt("media_type", "unknown");
1518 print_int("key_frame", frame->key_frame);
1519 print_ts ("pkt_pts", frame->pkt_pts);
1520 print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
1521 print_ts ("pkt_dts", frame->pkt_dts);
1522 print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
1523 print_duration_ts ("pkt_duration", av_frame_get_pkt_duration(frame));
1524 print_duration_time("pkt_duration_time", av_frame_get_pkt_duration(frame), &stream->time_base);
1525 if (av_frame_get_pkt_pos (frame) != -1) print_fmt ("pkt_pos", "%"PRId64, av_frame_get_pkt_pos(frame));
1526 else print_str_opt("pkt_pos", "N/A");
1527 if (av_frame_get_pkt_size(frame) != -1) print_fmt ("pkt_size", "%d", av_frame_get_pkt_size(frame));
1528 else print_str_opt("pkt_size", "N/A");
1530 switch (stream->codec->codec_type) {
1533 case AVMEDIA_TYPE_VIDEO:
1534 print_int("width", frame->width);
1535 print_int("height", frame->height);
1536 s = av_get_pix_fmt_name(frame->format);
1537 if (s) print_str ("pix_fmt", s);
1538 else print_str_opt("pix_fmt", "unknown");
1539 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
1541 print_q("sample_aspect_ratio", sar, ':');
1543 print_str_opt("sample_aspect_ratio", "N/A");
1545 print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
1546 print_int("coded_picture_number", frame->coded_picture_number);
1547 print_int("display_picture_number", frame->display_picture_number);
1548 print_int("interlaced_frame", frame->interlaced_frame);
1549 print_int("top_field_first", frame->top_field_first);
1550 print_int("repeat_pict", frame->repeat_pict);
1553 case AVMEDIA_TYPE_AUDIO:
1554 s = av_get_sample_fmt_name(frame->format);
1555 if (s) print_str ("sample_fmt", s);
1556 else print_str_opt("sample_fmt", "unknown");
1557 print_int("nb_samples", frame->nb_samples);
1558 print_int("channels", av_frame_get_channels(frame));
1559 if (av_frame_get_channel_layout(frame)) {
1560 av_bprint_clear(&pbuf);
1561 av_bprint_channel_layout(&pbuf, av_frame_get_channels(frame),
1562 av_frame_get_channel_layout(frame));
1563 print_str ("channel_layout", pbuf.str);
1565 print_str_opt("channel_layout", "unknown");
1568 show_tags(w, av_frame_get_metadata(frame), SECTION_ID_FRAME_TAGS);
1570 writer_print_section_footer(w);
1572 av_bprint_finalize(&pbuf, NULL);
1576 static av_always_inline int process_frame(WriterContext *w,
1577 AVFormatContext *fmt_ctx,
1578 AVFrame *frame, AVPacket *pkt)
1580 AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
1581 int ret = 0, got_frame = 0;
1583 if (dec_ctx->codec) {
1584 switch (dec_ctx->codec_type) {
1585 case AVMEDIA_TYPE_VIDEO:
1586 ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, pkt);
1589 case AVMEDIA_TYPE_AUDIO:
1590 ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
1597 ret = FFMIN(ret, pkt->size); /* guard against bogus return values */
1601 nb_streams_frames[pkt->stream_index]++;
1603 show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
1608 static void log_read_interval(const ReadInterval *interval, void *log_ctx, int log_level)
1610 av_log(log_ctx, log_level, "id:%d", interval->id);
1612 if (interval->has_start) {
1613 av_log(log_ctx, log_level, " start:%s%s", interval->start_is_offset ? "+" : "",
1614 av_ts2timestr(interval->start, &AV_TIME_BASE_Q));
1616 av_log(log_ctx, log_level, " start:N/A");
1619 if (interval->has_end) {
1620 av_log(log_ctx, log_level, " end:%s", interval->end_is_offset ? "+" : "");
1621 if (interval->duration_frames)
1622 av_log(log_ctx, log_level, "#%"PRId64, interval->end);
1624 av_log(log_ctx, log_level, "%s", av_ts2timestr(interval->end, &AV_TIME_BASE_Q));
1626 av_log(log_ctx, log_level, " end:N/A");
1629 av_log(log_ctx, log_level, "\n");
1632 static int read_interval_packets(WriterContext *w, AVFormatContext *fmt_ctx,
1633 const ReadInterval *interval, int64_t *cur_ts)
1636 AVFrame *frame = NULL;
1637 int ret = 0, i = 0, frame_count = 0;
1638 int64_t start = -INT64_MAX, end = interval->end;
1639 int has_start = 0, has_end = interval->has_end && !interval->end_is_offset;
1641 av_init_packet(&pkt);
1643 av_log(NULL, AV_LOG_VERBOSE, "Processing read interval ");
1644 log_read_interval(interval, NULL, AV_LOG_VERBOSE);
1646 if (interval->has_start) {
1648 if (interval->start_is_offset) {
1649 if (*cur_ts == AV_NOPTS_VALUE) {
1650 av_log(NULL, AV_LOG_ERROR,
1651 "Could not seek to relative position since current "
1652 "timestamp is not defined\n");
1653 ret = AVERROR(EINVAL);
1656 target = *cur_ts + interval->start;
1658 target = interval->start;
1661 av_log(NULL, AV_LOG_VERBOSE, "Seeking to read interval start point %s\n",
1662 av_ts2timestr(target, &AV_TIME_BASE_Q));
1663 if ((ret = avformat_seek_file(fmt_ctx, -1, -INT64_MAX, target, INT64_MAX, 0)) < 0) {
1664 av_log(NULL, AV_LOG_ERROR, "Could not seek to position %"PRId64": %s\n",
1665 interval->start, av_err2str(ret));
1670 frame = av_frame_alloc();
1672 ret = AVERROR(ENOMEM);
1675 while (!av_read_frame(fmt_ctx, &pkt)) {
1676 if (selected_streams[pkt.stream_index]) {
1677 AVRational tb = fmt_ctx->streams[pkt.stream_index]->time_base;
1679 if (pkt.pts != AV_NOPTS_VALUE)
1680 *cur_ts = av_rescale_q(pkt.pts, tb, AV_TIME_BASE_Q);
1682 if (!has_start && *cur_ts != AV_NOPTS_VALUE) {
1687 if (has_start && !has_end && interval->end_is_offset) {
1688 end = start + interval->end;
1692 if (interval->end_is_offset && interval->duration_frames) {
1693 if (frame_count >= interval->end)
1695 } else if (has_end && *cur_ts != AV_NOPTS_VALUE && *cur_ts >= end) {
1700 if (do_read_packets) {
1701 if (do_show_packets)
1702 show_packet(w, fmt_ctx, &pkt, i++);
1703 nb_streams_packets[pkt.stream_index]++;
1705 if (do_read_frames) {
1707 while (pkt1.size && process_frame(w, fmt_ctx, frame, &pkt1) > 0);
1710 av_free_packet(&pkt);
1712 av_init_packet(&pkt);
1715 //Flush remaining frames that are cached in the decoder
1716 for (i = 0; i < fmt_ctx->nb_streams; i++) {
1717 pkt.stream_index = i;
1719 while (process_frame(w, fmt_ctx, frame, &pkt) > 0);
1723 av_frame_free(&frame);
1725 av_log(NULL, AV_LOG_ERROR, "Could not read packets in interval ");
1726 log_read_interval(interval, NULL, AV_LOG_ERROR);
1731 static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1734 int64_t cur_ts = fmt_ctx->start_time;
1736 if (read_intervals_nb == 0) {
1737 ReadInterval interval = (ReadInterval) { .has_start = 0, .has_end = 0 };
1738 ret = read_interval_packets(w, fmt_ctx, &interval, &cur_ts);
1740 for (i = 0; i < read_intervals_nb; i++) {
1741 ret = read_interval_packets(w, fmt_ctx, &read_intervals[i], &cur_ts);
1748 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx, int in_program)
1750 AVStream *stream = fmt_ctx->streams[stream_idx];
1751 AVCodecContext *dec_ctx;
1755 AVRational sar, dar;
1758 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
1760 writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM : SECTION_ID_STREAM);
1762 print_int("index", stream->index);
1764 if ((dec_ctx = stream->codec)) {
1765 const char *profile = NULL;
1766 dec = dec_ctx->codec;
1768 print_str("codec_name", dec->name);
1770 if (dec->long_name) print_str ("codec_long_name", dec->long_name);
1771 else print_str_opt("codec_long_name", "unknown");
1774 print_str_opt("codec_name", "unknown");
1776 print_str_opt("codec_long_name", "unknown");
1780 if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
1781 print_str("profile", profile);
1783 print_str_opt("profile", "unknown");
1785 s = av_get_media_type_string(dec_ctx->codec_type);
1786 if (s) print_str ("codec_type", s);
1787 else print_str_opt("codec_type", "unknown");
1788 print_q("codec_time_base", dec_ctx->time_base, '/');
1790 /* print AVI/FourCC tag */
1791 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
1792 print_str("codec_tag_string", val_str);
1793 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
1795 switch (dec_ctx->codec_type) {
1796 case AVMEDIA_TYPE_VIDEO:
1797 print_int("width", dec_ctx->width);
1798 print_int("height", dec_ctx->height);
1799 print_int("has_b_frames", dec_ctx->has_b_frames);
1800 sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
1802 print_q("sample_aspect_ratio", sar, ':');
1803 av_reduce(&dar.num, &dar.den,
1804 dec_ctx->width * sar.num,
1805 dec_ctx->height * sar.den,
1807 print_q("display_aspect_ratio", dar, ':');
1809 print_str_opt("sample_aspect_ratio", "N/A");
1810 print_str_opt("display_aspect_ratio", "N/A");
1812 s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
1813 if (s) print_str ("pix_fmt", s);
1814 else print_str_opt("pix_fmt", "unknown");
1815 print_int("level", dec_ctx->level);
1816 if (dec_ctx->timecode_frame_start >= 0) {
1817 char tcbuf[AV_TIMECODE_STR_SIZE];
1818 av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
1819 print_str("timecode", tcbuf);
1821 print_str_opt("timecode", "N/A");
1825 case AVMEDIA_TYPE_AUDIO:
1826 s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
1827 if (s) print_str ("sample_fmt", s);
1828 else print_str_opt("sample_fmt", "unknown");
1829 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
1830 print_int("channels", dec_ctx->channels);
1832 if (dec_ctx->channel_layout) {
1833 av_bprint_clear(&pbuf);
1834 av_bprint_channel_layout(&pbuf, dec_ctx->channels, dec_ctx->channel_layout);
1835 print_str ("channel_layout", pbuf.str);
1837 print_str_opt("channel_layout", "unknown");
1840 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
1843 case AVMEDIA_TYPE_SUBTITLE:
1845 print_int("width", dec_ctx->width);
1847 print_str_opt("width", "N/A");
1848 if (dec_ctx->height)
1849 print_int("height", dec_ctx->height);
1851 print_str_opt("height", "N/A");
1855 print_str_opt("codec_type", "unknown");
1857 if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
1858 const AVOption *opt = NULL;
1859 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
1861 if (opt->flags) continue;
1862 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
1863 print_str(opt->name, str);
1869 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
1870 else print_str_opt("id", "N/A");
1871 print_q("r_frame_rate", stream->r_frame_rate, '/');
1872 print_q("avg_frame_rate", stream->avg_frame_rate, '/');
1873 print_q("time_base", stream->time_base, '/');
1874 print_ts ("start_pts", stream->start_time);
1875 print_time("start_time", stream->start_time, &stream->time_base);
1876 print_ts ("duration_ts", stream->duration);
1877 print_time("duration", stream->duration, &stream->time_base);
1878 if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
1879 else print_str_opt("bit_rate", "N/A");
1880 if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
1881 else print_str_opt("nb_frames", "N/A");
1882 if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
1883 else print_str_opt("nb_read_frames", "N/A");
1884 if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
1885 else print_str_opt("nb_read_packets", "N/A");
1887 writer_print_data(w, "extradata", dec_ctx->extradata,
1888 dec_ctx->extradata_size);
1890 /* Print disposition information */
1891 #define PRINT_DISPOSITION(flagname, name) do { \
1892 print_int(name, !!(stream->disposition & AV_DISPOSITION_##flagname)); \
1895 if (do_show_stream_disposition) {
1896 writer_print_section_header(w, in_program ? SECTION_ID_PROGRAM_STREAM_DISPOSITION : SECTION_ID_STREAM_DISPOSITION);
1897 PRINT_DISPOSITION(DEFAULT, "default");
1898 PRINT_DISPOSITION(DUB, "dub");
1899 PRINT_DISPOSITION(ORIGINAL, "original");
1900 PRINT_DISPOSITION(COMMENT, "comment");
1901 PRINT_DISPOSITION(LYRICS, "lyrics");
1902 PRINT_DISPOSITION(KARAOKE, "karaoke");
1903 PRINT_DISPOSITION(FORCED, "forced");
1904 PRINT_DISPOSITION(HEARING_IMPAIRED, "hearing_impaired");
1905 PRINT_DISPOSITION(VISUAL_IMPAIRED, "visual_impaired");
1906 PRINT_DISPOSITION(CLEAN_EFFECTS, "clean_effects");
1907 PRINT_DISPOSITION(ATTACHED_PIC, "attached_pic");
1908 writer_print_section_footer(w);
1911 show_tags(w, stream->metadata, in_program ? SECTION_ID_PROGRAM_STREAM_TAGS : SECTION_ID_STREAM_TAGS);
1913 writer_print_section_footer(w);
1914 av_bprint_finalize(&pbuf, NULL);
1918 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
1921 writer_print_section_header(w, SECTION_ID_STREAMS);
1922 for (i = 0; i < fmt_ctx->nb_streams; i++)
1923 if (selected_streams[i])
1924 show_stream(w, fmt_ctx, i, 0);
1925 writer_print_section_footer(w);
1928 static void show_program(WriterContext *w, AVFormatContext *fmt_ctx, AVProgram *program)
1932 writer_print_section_header(w, SECTION_ID_PROGRAM);
1933 print_int("program_id", program->id);
1934 print_int("program_num", program->program_num);
1935 print_int("nb_streams", program->nb_stream_indexes);
1936 print_int("pmt_pid", program->pmt_pid);
1937 print_int("pcr_pid", program->pcr_pid);
1938 print_ts("start_pts", program->start_time);
1939 print_time("start_time", program->start_time, &AV_TIME_BASE_Q);
1940 print_ts("end_pts", program->end_time);
1941 print_time("end_time", program->end_time, &AV_TIME_BASE_Q);
1942 show_tags(w, program->metadata, SECTION_ID_PROGRAM_TAGS);
1944 writer_print_section_header(w, SECTION_ID_PROGRAM_STREAMS);
1945 for (i = 0; i < program->nb_stream_indexes; i++) {
1946 if (selected_streams[program->stream_index[i]])
1947 show_stream(w, fmt_ctx, program->stream_index[i], 1);
1949 writer_print_section_footer(w);
1951 writer_print_section_footer(w);
1954 static void show_programs(WriterContext *w, AVFormatContext *fmt_ctx)
1958 writer_print_section_header(w, SECTION_ID_PROGRAMS);
1959 for (i = 0; i < fmt_ctx->nb_programs; i++) {
1960 AVProgram *program = fmt_ctx->programs[i];
1963 show_program(w, fmt_ctx, program);
1965 writer_print_section_footer(w);
1968 static void show_chapters(WriterContext *w, AVFormatContext *fmt_ctx)
1972 writer_print_section_header(w, SECTION_ID_CHAPTERS);
1973 for (i = 0; i < fmt_ctx->nb_chapters; i++) {
1974 AVChapter *chapter = fmt_ctx->chapters[i];
1976 writer_print_section_header(w, SECTION_ID_CHAPTER);
1977 print_int("id", chapter->id);
1978 print_q ("time_base", chapter->time_base, '/');
1979 print_int("start", chapter->start);
1980 print_time("start_time", chapter->start, &chapter->time_base);
1981 print_int("end", chapter->end);
1982 print_time("end_time", chapter->end, &chapter->time_base);
1983 show_tags(w, chapter->metadata, SECTION_ID_CHAPTER_TAGS);
1984 writer_print_section_footer(w);
1986 writer_print_section_footer(w);
1989 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
1992 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
1994 writer_print_section_header(w, SECTION_ID_FORMAT);
1995 print_str("filename", fmt_ctx->filename);
1996 print_int("nb_streams", fmt_ctx->nb_streams);
1997 print_int("nb_programs", fmt_ctx->nb_programs);
1998 print_str("format_name", fmt_ctx->iformat->name);
2000 if (fmt_ctx->iformat->long_name) print_str ("format_long_name", fmt_ctx->iformat->long_name);
2001 else print_str_opt("format_long_name", "unknown");
2003 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
2004 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
2005 if (size >= 0) print_val ("size", size, unit_byte_str);
2006 else print_str_opt("size", "N/A");
2007 if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
2008 else print_str_opt("bit_rate", "N/A");
2009 print_int("probe_score", av_format_get_probe_score(fmt_ctx));
2010 show_tags(w, fmt_ctx->metadata, SECTION_ID_FORMAT_TAGS);
2012 writer_print_section_footer(w);
2016 static void show_error(WriterContext *w, int err)
2019 const char *errbuf_ptr = errbuf;
2021 if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
2022 errbuf_ptr = strerror(AVUNERROR(err));
2024 writer_print_section_header(w, SECTION_ID_ERROR);
2025 print_int("code", err);
2026 print_str("string", errbuf_ptr);
2027 writer_print_section_footer(w);
2030 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
2032 int err, i, orig_nb_streams;
2033 AVFormatContext *fmt_ctx = NULL;
2034 AVDictionaryEntry *t;
2035 AVDictionary **opts;
2037 if ((err = avformat_open_input(&fmt_ctx, filename,
2038 iformat, &format_opts)) < 0) {
2039 print_error(filename, err);
2042 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2043 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2044 return AVERROR_OPTION_NOT_FOUND;
2047 /* fill the streams in the format context */
2048 opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
2049 orig_nb_streams = fmt_ctx->nb_streams;
2051 if ((err = avformat_find_stream_info(fmt_ctx, opts)) < 0) {
2052 print_error(filename, err);
2055 for (i = 0; i < orig_nb_streams; i++)
2056 av_dict_free(&opts[i]);
2059 av_dump_format(fmt_ctx, 0, filename, 0);
2061 /* bind a decoder to each input stream */
2062 for (i = 0; i < fmt_ctx->nb_streams; i++) {
2063 AVStream *stream = fmt_ctx->streams[i];
2066 if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
2067 av_log(NULL, AV_LOG_WARNING,
2068 "Failed to probe codec for input stream %d\n",
2070 } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
2071 av_log(NULL, AV_LOG_WARNING,
2072 "Unsupported codec with id %d for input stream %d\n",
2073 stream->codec->codec_id, stream->index);
2075 AVDictionary *opts = filter_codec_opts(codec_opts, stream->codec->codec_id,
2076 fmt_ctx, stream, codec);
2077 if (avcodec_open2(stream->codec, codec, &opts) < 0) {
2078 av_log(NULL, AV_LOG_WARNING, "Could not open codec for input stream %d\n",
2081 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2082 av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
2083 t->key, stream->index);
2084 return AVERROR_OPTION_NOT_FOUND;
2089 *fmt_ctx_ptr = fmt_ctx;
2093 static void close_input_file(AVFormatContext **ctx_ptr)
2096 AVFormatContext *fmt_ctx = *ctx_ptr;
2098 /* close decoder for each stream */
2099 for (i = 0; i < fmt_ctx->nb_streams; i++)
2100 if (fmt_ctx->streams[i]->codec->codec_id != AV_CODEC_ID_NONE)
2101 avcodec_close(fmt_ctx->streams[i]->codec);
2103 avformat_close_input(ctx_ptr);
2106 static int probe_file(WriterContext *wctx, const char *filename)
2108 AVFormatContext *fmt_ctx;
2112 do_read_frames = do_show_frames || do_count_frames;
2113 do_read_packets = do_show_packets || do_count_packets;
2115 ret = open_input_file(&fmt_ctx, filename);
2119 nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
2120 nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
2121 selected_streams = av_calloc(fmt_ctx->nb_streams, sizeof(*selected_streams));
2123 for (i = 0; i < fmt_ctx->nb_streams; i++) {
2124 if (stream_specifier) {
2125 ret = avformat_match_stream_specifier(fmt_ctx,
2126 fmt_ctx->streams[i],
2131 selected_streams[i] = ret;
2134 selected_streams[i] = 1;
2138 if (do_read_frames || do_read_packets) {
2139 if (do_show_frames && do_show_packets &&
2140 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
2141 section_id = SECTION_ID_PACKETS_AND_FRAMES;
2142 else if (do_show_packets && !do_show_frames)
2143 section_id = SECTION_ID_PACKETS;
2144 else // (!do_show_packets && do_show_frames)
2145 section_id = SECTION_ID_FRAMES;
2146 if (do_show_frames || do_show_packets)
2147 writer_print_section_header(wctx, section_id);
2148 read_packets(wctx, fmt_ctx);
2149 if (do_show_frames || do_show_packets)
2150 writer_print_section_footer(wctx);
2152 if (do_show_programs)
2153 show_programs(wctx, fmt_ctx);
2154 if (do_show_streams)
2155 show_streams(wctx, fmt_ctx);
2156 if (do_show_chapters)
2157 show_chapters(wctx, fmt_ctx);
2159 show_format(wctx, fmt_ctx);
2162 close_input_file(&fmt_ctx);
2163 av_freep(&nb_streams_frames);
2164 av_freep(&nb_streams_packets);
2165 av_freep(&selected_streams);
2170 static void show_usage(void)
2172 av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
2173 av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
2174 av_log(NULL, AV_LOG_INFO, "\n");
2177 static void ffprobe_show_program_version(WriterContext *w)
2180 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
2182 writer_print_section_header(w, SECTION_ID_PROGRAM_VERSION);
2183 print_str("version", FFMPEG_VERSION);
2184 print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
2185 program_birth_year, this_year);
2186 print_str("build_date", __DATE__);
2187 print_str("build_time", __TIME__);
2188 print_str("compiler_ident", CC_IDENT);
2189 print_str("configuration", FFMPEG_CONFIGURATION);
2190 writer_print_section_footer(w);
2192 av_bprint_finalize(&pbuf, NULL);
2195 #define SHOW_LIB_VERSION(libname, LIBNAME) \
2197 if (CONFIG_##LIBNAME) { \
2198 unsigned int version = libname##_version(); \
2199 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSION); \
2200 print_str("name", "lib" #libname); \
2201 print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
2202 print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
2203 print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
2204 print_int("version", version); \
2205 print_str("ident", LIB##LIBNAME##_IDENT); \
2206 writer_print_section_footer(w); \
2210 static void ffprobe_show_library_versions(WriterContext *w)
2212 writer_print_section_header(w, SECTION_ID_LIBRARY_VERSIONS);
2213 SHOW_LIB_VERSION(avutil, AVUTIL);
2214 SHOW_LIB_VERSION(avcodec, AVCODEC);
2215 SHOW_LIB_VERSION(avformat, AVFORMAT);
2216 SHOW_LIB_VERSION(avdevice, AVDEVICE);
2217 SHOW_LIB_VERSION(avfilter, AVFILTER);
2218 SHOW_LIB_VERSION(swscale, SWSCALE);
2219 SHOW_LIB_VERSION(swresample, SWRESAMPLE);
2220 SHOW_LIB_VERSION(postproc, POSTPROC);
2221 writer_print_section_footer(w);
2224 static int opt_format(void *optctx, const char *opt, const char *arg)
2226 iformat = av_find_input_format(arg);
2228 av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
2229 return AVERROR(EINVAL);
2234 static inline void mark_section_show_entries(SectionID section_id,
2235 int show_all_entries, AVDictionary *entries)
2237 struct section *section = §ions[section_id];
2239 section->show_all_entries = show_all_entries;
2240 if (show_all_entries) {
2242 for (id = section->children_ids; *id != -1; id++)
2243 mark_section_show_entries(*id, show_all_entries, entries);
2245 av_dict_copy(§ion->entries_to_show, entries, 0);
2249 static int match_section(const char *section_name,
2250 int show_all_entries, AVDictionary *entries)
2254 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++) {
2255 const struct section *section = §ions[i];
2256 if (!strcmp(section_name, section->name) ||
2257 (section->unique_name && !strcmp(section_name, section->unique_name))) {
2258 av_log(NULL, AV_LOG_DEBUG,
2259 "'%s' matches section with unique name '%s'\n", section_name,
2260 (char *)av_x_if_null(section->unique_name, section->name));
2262 mark_section_show_entries(section->id, show_all_entries, entries);
2268 static int opt_show_entries(void *optctx, const char *opt, const char *arg)
2270 const char *p = arg;
2274 AVDictionary *entries = NULL;
2275 char *section_name = av_get_token(&p, "=:");
2276 int show_all_entries = 0;
2278 if (!section_name) {
2279 av_log(NULL, AV_LOG_ERROR,
2280 "Missing section name for option '%s'\n", opt);
2281 return AVERROR(EINVAL);
2286 while (*p && *p != ':') {
2287 char *entry = av_get_token(&p, ",:");
2290 av_log(NULL, AV_LOG_VERBOSE,
2291 "Adding '%s' to the entries to show in section '%s'\n",
2292 entry, section_name);
2293 av_dict_set(&entries, entry, "", AV_DICT_DONT_STRDUP_KEY);
2298 show_all_entries = 1;
2301 ret = match_section(section_name, show_all_entries, entries);
2303 av_log(NULL, AV_LOG_ERROR, "No match for section '%s'\n", section_name);
2304 ret = AVERROR(EINVAL);
2306 av_dict_free(&entries);
2307 av_free(section_name);
2318 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
2320 char *buf = av_asprintf("format=%s", arg);
2323 av_log(NULL, AV_LOG_WARNING,
2324 "Option '%s' is deprecated, use '-show_entries format=%s' instead\n",
2326 ret = opt_show_entries(optctx, opt, buf);
2331 static void opt_input_file(void *optctx, const char *arg)
2333 if (input_filename) {
2334 av_log(NULL, AV_LOG_ERROR,
2335 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
2336 arg, input_filename);
2339 if (!strcmp(arg, "-"))
2341 input_filename = arg;
2344 static int opt_input_file_i(void *optctx, const char *opt, const char *arg)
2346 opt_input_file(optctx, arg);
2350 void show_help_default(const char *opt, const char *arg)
2352 av_log_set_callback(log_callback_help);
2354 show_help_options(options, "Main options:", 0, 0, 0);
2357 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
2361 * Parse interval specification, according to the format:
2362 * INTERVAL ::= [START|+START_OFFSET][%[END|+END_OFFSET]]
2363 * INTERVALS ::= INTERVAL[,INTERVALS]
2365 static int parse_read_interval(const char *interval_spec,
2366 ReadInterval *interval)
2369 char *next, *p, *spec = av_strdup(interval_spec);
2371 return AVERROR(ENOMEM);
2374 av_log(NULL, AV_LOG_ERROR, "Invalid empty interval specification\n");
2375 ret = AVERROR(EINVAL);
2380 next = strchr(spec, '%');
2384 /* parse first part */
2386 interval->has_start = 1;
2389 interval->start_is_offset = 1;
2392 interval->start_is_offset = 0;
2395 ret = av_parse_time(&interval->start, p, 1);
2397 av_log(NULL, AV_LOG_ERROR, "Invalid interval start specification '%s'\n", p);
2401 interval->has_start = 0;
2404 /* parse second part */
2408 interval->has_end = 1;
2411 interval->end_is_offset = 1;
2414 interval->end_is_offset = 0;
2417 if (interval->end_is_offset && *p == '#') {
2420 interval->duration_frames = 1;
2422 lli = strtoll(p, &tail, 10);
2423 if (*tail || lli < 0) {
2424 av_log(NULL, AV_LOG_ERROR,
2425 "Invalid or negative value '%s' for duration number of frames\n", p);
2428 interval->end = lli;
2430 ret = av_parse_time(&us, p, 1);
2432 av_log(NULL, AV_LOG_ERROR, "Invalid interval end/duration specification '%s'\n", p);
2438 interval->has_end = 0;
2446 static int parse_read_intervals(const char *intervals_spec)
2449 char *p, *spec = av_strdup(intervals_spec);
2451 return AVERROR(ENOMEM);
2453 /* preparse specification, get number of intervals */
2454 for (n = 0, p = spec; *p; p++)
2459 read_intervals = av_malloc(n * sizeof(*read_intervals));
2460 if (!read_intervals) {
2461 ret = AVERROR(ENOMEM);
2464 read_intervals_nb = n;
2466 /* parse intervals */
2468 for (i = 0; p; i++) {
2471 av_assert0(i < read_intervals_nb);
2472 next = strchr(p, ',');
2476 read_intervals[i].id = i;
2477 ret = parse_read_interval(p, &read_intervals[i]);
2479 av_log(NULL, AV_LOG_ERROR, "Error parsing read interval #%d '%s'\n",
2483 av_log(NULL, AV_LOG_VERBOSE, "Parsed log interval ");
2484 log_read_interval(&read_intervals[i], NULL, AV_LOG_VERBOSE);
2487 av_assert0(i == read_intervals_nb);
2494 static int opt_read_intervals(void *optctx, const char *opt, const char *arg)
2496 return parse_read_intervals(arg);
2499 static int opt_pretty(void *optctx, const char *opt, const char *arg)
2501 show_value_unit = 1;
2502 use_value_prefix = 1;
2503 use_byte_value_binary_prefix = 1;
2504 use_value_sexagesimal_format = 1;
2508 static void print_section(SectionID id, int level)
2510 const SectionID *pid;
2511 const struct section *section = §ions[id];
2513 section->flags & SECTION_FLAG_IS_WRAPPER ? 'W' : '.',
2514 section->flags & SECTION_FLAG_IS_ARRAY ? 'A' : '.',
2515 section->flags & SECTION_FLAG_HAS_VARIABLE_FIELDS ? 'V' : '.');
2516 printf("%*c %s", level * 4, ' ', section->name);
2517 if (section->unique_name)
2518 printf("/%s", section->unique_name);
2521 for (pid = section->children_ids; *pid != -1; pid++)
2522 print_section(*pid, level+1);
2525 static int opt_sections(void *optctx, const char *opt, const char *arg)
2527 printf("Sections:\n"
2528 "W.. = Section is a wrapper (contains other sections, no local entries)\n"
2529 ".A. = Section contains an array of elements of the same type\n"
2530 "..V = Section may contain a variable number of fields with variable keys\n"
2531 "FLAGS NAME/UNIQUE_NAME\n"
2533 print_section(SECTION_ID_ROOT, 0);
2537 static int opt_show_versions(const char *opt, const char *arg)
2539 mark_section_show_entries(SECTION_ID_PROGRAM_VERSION, 1, NULL);
2540 mark_section_show_entries(SECTION_ID_LIBRARY_VERSION, 1, NULL);
2544 #define DEFINE_OPT_SHOW_SECTION(section, target_section_id) \
2545 static int opt_show_##section(const char *opt, const char *arg) \
2547 mark_section_show_entries(SECTION_ID_##target_section_id, 1, NULL); \
2551 DEFINE_OPT_SHOW_SECTION(chapters, CHAPTERS);
2552 DEFINE_OPT_SHOW_SECTION(error, ERROR);
2553 DEFINE_OPT_SHOW_SECTION(format, FORMAT);
2554 DEFINE_OPT_SHOW_SECTION(frames, FRAMES);
2555 DEFINE_OPT_SHOW_SECTION(library_versions, LIBRARY_VERSIONS);
2556 DEFINE_OPT_SHOW_SECTION(packets, PACKETS);
2557 DEFINE_OPT_SHOW_SECTION(program_version, PROGRAM_VERSION);
2558 DEFINE_OPT_SHOW_SECTION(streams, STREAMS);
2559 DEFINE_OPT_SHOW_SECTION(programs, PROGRAMS);
2561 static const OptionDef real_options[] = {
2562 #include "cmdutils_common_opts.h"
2563 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
2564 { "unit", OPT_BOOL, {&show_value_unit}, "show unit of the displayed values" },
2565 { "prefix", OPT_BOOL, {&use_value_prefix}, "use SI prefixes for the displayed values" },
2566 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
2567 "use binary prefixes for byte units" },
2568 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
2569 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
2570 { "pretty", 0, {.func_arg = opt_pretty},
2571 "prettify the format of displayed values, make it more human readable" },
2572 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
2573 "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
2574 { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
2575 { "select_streams", OPT_STRING | HAS_ARG, {(void*)&stream_specifier}, "select the specified streams", "stream_specifier" },
2576 { "sections", OPT_EXIT, {.func_arg = opt_sections}, "print sections structure and section information, and exit" },
2577 { "show_data", OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
2578 { "show_error", 0, {(void*)&opt_show_error}, "show probing error" },
2579 { "show_format", 0, {(void*)&opt_show_format}, "show format/container info" },
2580 { "show_frames", 0, {(void*)&opt_show_frames}, "show frames info" },
2581 { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
2582 "show a particular entry from the format/container info", "entry" },
2583 { "show_entries", HAS_ARG, {.func_arg = opt_show_entries},
2584 "show a set of specified entries", "entry_list" },
2585 { "show_packets", 0, {(void*)&opt_show_packets}, "show packets info" },
2586 { "show_programs", 0, {(void*)&opt_show_programs}, "show programs info" },
2587 { "show_streams", 0, {(void*)&opt_show_streams}, "show streams info" },
2588 { "show_chapters", 0, {(void*)&opt_show_chapters}, "show chapters info" },
2589 { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
2590 { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
2591 { "show_program_version", 0, {(void*)&opt_show_program_version}, "show ffprobe version" },
2592 { "show_library_versions", 0, {(void*)&opt_show_library_versions}, "show library versions" },
2593 { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
2594 { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
2595 { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
2596 { "bitexact", OPT_BOOL, {&do_bitexact}, "force bitexact output" },
2597 { "read_intervals", HAS_ARG, {.func_arg = opt_read_intervals}, "set read intervals", "read_intervals" },
2598 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default}, "generic catch all option", "" },
2599 { "i", HAS_ARG, {.func_arg = opt_input_file_i}, "read specified file", "input_file"},
2603 static inline int check_section_show_entries(int section_id)
2606 struct section *section = §ions[section_id];
2607 if (sections[section_id].show_all_entries || sections[section_id].entries_to_show)
2609 for (id = section->children_ids; *id != -1; id++)
2610 if (check_section_show_entries(*id))
2615 #define SET_DO_SHOW(id, varname) do { \
2616 if (check_section_show_entries(SECTION_ID_##id)) \
2617 do_show_##varname = 1; \
2620 int main(int argc, char **argv)
2623 WriterContext *wctx;
2625 char *w_name = NULL, *w_args = NULL;
2628 av_log_set_flags(AV_LOG_SKIP_REPEATED);
2629 register_exit(ffprobe_cleanup);
2631 options = real_options;
2632 parse_loglevel(argc, argv, options);
2634 avformat_network_init();
2637 avdevice_register_all();
2640 show_banner(argc, argv, options);
2641 parse_options(NULL, argc, argv, options, opt_input_file);
2643 /* mark things to show, based on -show_entries */
2644 SET_DO_SHOW(CHAPTERS, chapters);
2645 SET_DO_SHOW(ERROR, error);
2646 SET_DO_SHOW(FORMAT, format);
2647 SET_DO_SHOW(FRAMES, frames);
2648 SET_DO_SHOW(LIBRARY_VERSIONS, library_versions);
2649 SET_DO_SHOW(PACKETS, packets);
2650 SET_DO_SHOW(PROGRAM_VERSION, program_version);
2651 SET_DO_SHOW(PROGRAMS, programs);
2652 SET_DO_SHOW(STREAMS, streams);
2653 SET_DO_SHOW(STREAM_DISPOSITION, stream_disposition);
2654 SET_DO_SHOW(PROGRAM_STREAM_DISPOSITION, stream_disposition);
2656 if (do_bitexact && (do_show_program_version || do_show_library_versions)) {
2657 av_log(NULL, AV_LOG_ERROR,
2658 "-bitexact and -show_program_version or -show_library_versions "
2659 "options are incompatible\n");
2660 ret = AVERROR(EINVAL);
2664 writer_register_all();
2667 print_format = av_strdup("default");
2668 if (!print_format) {
2669 ret = AVERROR(ENOMEM);
2672 w_name = av_strtok(print_format, "=", &buf);
2675 w = writer_get_by_name(w_name);
2677 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
2678 ret = AVERROR(EINVAL);
2682 if ((ret = writer_open(&wctx, w, w_args,
2683 sections, FF_ARRAY_ELEMS(sections))) >= 0) {
2684 writer_print_section_header(wctx, SECTION_ID_ROOT);
2686 if (do_show_program_version)
2687 ffprobe_show_program_version(wctx);
2688 if (do_show_library_versions)
2689 ffprobe_show_library_versions(wctx);
2691 if (!input_filename &&
2692 ((do_show_format || do_show_programs || do_show_streams || do_show_chapters || do_show_packets || do_show_error) ||
2693 (!do_show_program_version && !do_show_library_versions))) {
2695 av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
2696 av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
2697 ret = AVERROR(EINVAL);
2698 } else if (input_filename) {
2699 ret = probe_file(wctx, input_filename);
2700 if (ret < 0 && do_show_error)
2701 show_error(wctx, ret);
2704 writer_print_section_footer(wctx);
2705 writer_close(&wctx);
2709 av_freep(&print_format);
2710 av_freep(&read_intervals);
2713 for (i = 0; i < FF_ARRAY_ELEMS(sections); i++)
2714 av_dict_free(&(sections[i].entries_to_show));
2716 avformat_network_deinit();