2 * avprobe : Simple Media Prober based on the Libav libraries
3 * Copyright (c) 2007-2010 Stefano Sabatini
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavformat/avformat.h"
25 #include "libavcodec/avcodec.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/display.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/dict.h"
31 #include "libavutil/libm.h"
32 #include "libavdevice/avdevice.h"
35 const char program_name[] = "avprobe";
36 const int program_birth_year = 2007;
38 static int do_show_format = 0;
39 static AVDictionary *fmt_entries_to_show = NULL;
40 static int nb_fmt_entries_to_show;
41 static int do_show_packets = 0;
42 static int do_show_streams = 0;
44 static int show_value_unit = 0;
45 static int use_value_prefix = 0;
46 static int use_byte_value_binary_prefix = 0;
47 static int use_value_sexagesimal_format = 0;
50 static const OptionDef *options;
53 static const char *input_filename;
54 static AVInputFormat *iformat = NULL;
56 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
57 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
59 static const char unit_second_str[] = "s" ;
60 static const char unit_hertz_str[] = "Hz" ;
61 static const char unit_byte_str[] = "byte" ;
62 static const char unit_bit_per_second_str[] = "bit/s";
64 static void avprobe_cleanup(int ret)
66 av_dict_free(&fmt_entries_to_show);
70 * The output is structured in array and objects that might contain items
71 * Array could require the objects within to not be named.
72 * Object could require the items within to be named.
74 * For flat representation the name of each section is saved on prefix so it
75 * can be rendered in order to represent nested structures (e.g. array of
76 * objects for the packets list).
78 * Within an array each element can need an unique identifier or an index.
80 * Nesting level is accounted separately.
88 typedef struct PrintElement {
90 PrintElementType type;
95 typedef struct PrintContext {
98 void (*print_header)(void);
99 void (*print_footer)(void);
101 void (*print_array_header) (const char *name, int plain_values);
102 void (*print_array_footer) (const char *name, int plain_values);
103 void (*print_object_header)(const char *name);
104 void (*print_object_footer)(const char *name);
106 void (*print_integer) (const char *key, int64_t value);
107 void (*print_string) (const char *key, const char *value);
110 static AVIOContext *probe_out = NULL;
111 static PrintContext octx;
112 #define AVP_INDENT() avio_printf(probe_out, "%*c", octx.level * 2, ' ')
115 * Default format, INI
117 * - all key and values are utf8
118 * - '.' is the subgroup separator
119 * - newlines and the following characters are escaped
120 * - '\' is the escape character
121 * - '#' is the comment
122 * - '=' is the key/value separators
123 * - ':' is not used but usually parsed as key/value separator
126 static void ini_print_header(void)
128 avio_printf(probe_out, "# avprobe output\n\n");
130 static void ini_print_footer(void)
132 avio_w8(probe_out, '\n');
135 static void ini_escape_print(const char *s)
142 case '\r': avio_printf(probe_out, "%s", "\\r"); break;
143 case '\n': avio_printf(probe_out, "%s", "\\n"); break;
144 case '\f': avio_printf(probe_out, "%s", "\\f"); break;
145 case '\b': avio_printf(probe_out, "%s", "\\b"); break;
146 case '\t': avio_printf(probe_out, "%s", "\\t"); break;
150 case ':' : avio_w8(probe_out, '\\');
152 if ((unsigned char)c < 32)
153 avio_printf(probe_out, "\\x00%02x", c & 0xff);
155 avio_w8(probe_out, c);
161 static void ini_print_array_header(const char *name, int plain_values)
164 /* Add a new line if we create a new full group */
165 if (octx.prefix[octx.level -1].nb_elems)
166 avio_printf(probe_out, "\n");
168 ini_escape_print(name);
169 avio_w8(probe_out, '=');
173 static void ini_print_array_footer(const char *name, int plain_values)
176 avio_printf(probe_out, "\n");
179 static void ini_print_object_header(const char *name)
182 PrintElement *el = octx.prefix + octx.level -1;
185 avio_printf(probe_out, "\n");
187 avio_printf(probe_out, "[");
189 for (i = 1; i < octx.level; i++) {
190 el = octx.prefix + i;
191 avio_printf(probe_out, "%s.", el->name);
193 avio_printf(probe_out, "%"PRId64".", el->index);
196 avio_printf(probe_out, "%s", name);
197 if (el->type == ARRAY)
198 avio_printf(probe_out, ".%"PRId64"", el->nb_elems);
199 avio_printf(probe_out, "]\n");
202 static void ini_print_integer(const char *key, int64_t value)
205 ini_escape_print(key);
206 avio_printf(probe_out, "=%"PRId64"\n", value);
208 if (octx.prefix[octx.level -1].nb_elems)
209 avio_printf(probe_out, ",");
210 avio_printf(probe_out, "%"PRId64, value);
215 static void ini_print_string(const char *key, const char *value)
217 ini_escape_print(key);
218 avio_printf(probe_out, "=");
219 ini_escape_print(value);
220 avio_w8(probe_out, '\n');
224 * Alternate format, JSON
227 static void json_print_header(void)
229 avio_printf(probe_out, "{");
231 static void json_print_footer(void)
233 avio_printf(probe_out, "}\n");
236 static void json_print_array_header(const char *name, int plain_values)
238 if (octx.prefix[octx.level -1].nb_elems)
239 avio_printf(probe_out, ",\n");
241 avio_printf(probe_out, "\"%s\" : ", name);
242 avio_printf(probe_out, "[\n");
245 static void json_print_array_footer(const char *name, int plain_values)
247 avio_printf(probe_out, "\n");
249 avio_printf(probe_out, "]");
252 static void json_print_object_header(const char *name)
254 if (octx.prefix[octx.level -1].nb_elems)
255 avio_printf(probe_out, ",\n");
257 if (octx.prefix[octx.level -1].type == OBJECT)
258 avio_printf(probe_out, "\"%s\" : ", name);
259 avio_printf(probe_out, "{\n");
262 static void json_print_object_footer(const char *name)
264 avio_printf(probe_out, "\n");
266 avio_printf(probe_out, "}");
269 static void json_print_integer(const char *key, int64_t value)
272 if (octx.prefix[octx.level -1].nb_elems)
273 avio_printf(probe_out, ",\n");
275 avio_printf(probe_out, "\"%s\" : ", key);
277 if (octx.prefix[octx.level -1].nb_elems)
278 avio_printf(probe_out, ", ");
282 avio_printf(probe_out, "%"PRId64, value);
285 static void json_escape_print(const char *s)
292 case '\r': avio_printf(probe_out, "%s", "\\r"); break;
293 case '\n': avio_printf(probe_out, "%s", "\\n"); break;
294 case '\f': avio_printf(probe_out, "%s", "\\f"); break;
295 case '\b': avio_printf(probe_out, "%s", "\\b"); break;
296 case '\t': avio_printf(probe_out, "%s", "\\t"); break;
298 case '"' : avio_w8(probe_out, '\\');
300 if ((unsigned char)c < 32)
301 avio_printf(probe_out, "\\u00%02x", c & 0xff);
303 avio_w8(probe_out, c);
309 static void json_print_string(const char *key, const char *value)
311 if (octx.prefix[octx.level -1].nb_elems)
312 avio_printf(probe_out, ",\n");
314 avio_w8(probe_out, '\"');
315 json_escape_print(key);
316 avio_printf(probe_out, "\" : \"");
317 json_escape_print(value);
318 avio_w8(probe_out, '\"');
322 * old-style pseudo-INI
324 static void old_print_object_header(const char *name)
328 if (!strcmp(name, "tags"))
331 str = p = av_strdup(name);
339 avio_printf(probe_out, "[%s]\n", str);
343 static void old_print_object_footer(const char *name)
347 if (!strcmp(name, "tags"))
350 str = p = av_strdup(name);
358 avio_printf(probe_out, "[/%s]\n", str);
362 static void old_print_string(const char *key, const char *value)
364 if (!strcmp(octx.prefix[octx.level - 1].name, "tags"))
365 avio_printf(probe_out, "TAG:");
366 ini_print_string(key, value);
370 * Simple Formatter for single entries.
373 static void show_format_entry_integer(const char *key, int64_t value)
375 if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
376 if (nb_fmt_entries_to_show > 1)
377 avio_printf(probe_out, "%s=", key);
378 avio_printf(probe_out, "%"PRId64"\n", value);
382 static void show_format_entry_string(const char *key, const char *value)
384 if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
385 if (nb_fmt_entries_to_show > 1)
386 avio_printf(probe_out, "%s=", key);
387 avio_printf(probe_out, "%s\n", value);
391 static void probe_group_enter(const char *name, int type)
396 av_realloc(octx.prefix, sizeof(PrintElement) * (octx.level + 1));
398 if (!octx.prefix || !name) {
399 fprintf(stderr, "Out of memory\n");
404 PrintElement *parent = octx.prefix + octx.level -1;
405 if (parent->type == ARRAY)
406 count = parent->nb_elems;
410 octx.prefix[octx.level++] = (PrintElement){name, type, count, 0};
413 static void probe_group_leave(void)
418 static void probe_header(void)
420 if (octx.print_header)
422 probe_group_enter("root", OBJECT);
425 static void probe_footer(void)
427 if (octx.print_footer)
433 static void probe_array_header(const char *name, int plain_values)
435 if (octx.print_array_header)
436 octx.print_array_header(name, plain_values);
438 probe_group_enter(name, ARRAY);
441 static void probe_array_footer(const char *name, int plain_values)
444 if (octx.print_array_footer)
445 octx.print_array_footer(name, plain_values);
448 static void probe_object_header(const char *name)
450 if (octx.print_object_header)
451 octx.print_object_header(name);
453 probe_group_enter(name, OBJECT);
456 static void probe_object_footer(const char *name)
459 if (octx.print_object_footer)
460 octx.print_object_footer(name);
463 static void probe_int(const char *key, int64_t value)
465 octx.print_integer(key, value);
466 octx.prefix[octx.level -1].nb_elems++;
469 static void probe_str(const char *key, const char *value)
471 octx.print_string(key, value);
472 octx.prefix[octx.level -1].nb_elems++;
475 static void probe_dict(AVDictionary *dict, const char *name)
477 AVDictionaryEntry *entry = NULL;
480 probe_object_header(name);
481 while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
482 probe_str(entry->key, entry->value);
484 probe_object_footer(name);
487 static char *value_string(char *buf, int buf_size, double val, const char *unit)
489 if (unit == unit_second_str && use_value_sexagesimal_format) {
493 mins = (int)secs / 60;
494 secs = secs - mins * 60;
497 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
498 } else if (use_value_prefix) {
499 const char *prefix_string;
502 if (unit == unit_byte_str && use_byte_value_binary_prefix) {
503 index = (int) log2(val) / 10;
504 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
505 val /= pow(2, index * 10);
506 prefix_string = binary_unit_prefixes[index];
508 index = (int) (log10(val)) / 3;
509 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
510 val /= pow(10, index * 3);
511 prefix_string = decimal_unit_prefixes[index];
513 snprintf(buf, buf_size, "%.*f%s%s",
516 show_value_unit ? unit : "");
518 snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
524 static char *time_value_string(char *buf, int buf_size, int64_t val,
525 const AVRational *time_base)
527 if (val == AV_NOPTS_VALUE) {
528 snprintf(buf, buf_size, "N/A");
530 value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
536 static char *ts_value_string(char *buf, int buf_size, int64_t ts)
538 if (ts == AV_NOPTS_VALUE) {
539 snprintf(buf, buf_size, "N/A");
541 snprintf(buf, buf_size, "%"PRId64, ts);
547 static char *rational_string(char *buf, int buf_size, const char *sep,
548 const AVRational *rat)
550 snprintf(buf, buf_size, "%d%s%d", rat->num, sep, rat->den);
554 static char *tag_string(char *buf, int buf_size, int tag)
556 snprintf(buf, buf_size, "0x%04x", tag);
560 static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
563 AVStream *st = fmt_ctx->streams[pkt->stream_index];
565 probe_object_header("packet");
566 probe_str("codec_type", media_type_string(st->codec->codec_type));
567 probe_int("stream_index", pkt->stream_index);
568 probe_str("pts", ts_value_string(val_str, sizeof(val_str), pkt->pts));
569 probe_str("pts_time", time_value_string(val_str, sizeof(val_str),
570 pkt->pts, &st->time_base));
571 probe_str("dts", ts_value_string(val_str, sizeof(val_str), pkt->dts));
572 probe_str("dts_time", time_value_string(val_str, sizeof(val_str),
573 pkt->dts, &st->time_base));
574 probe_str("duration", ts_value_string(val_str, sizeof(val_str),
576 probe_str("duration_time", time_value_string(val_str, sizeof(val_str),
579 probe_str("size", value_string(val_str, sizeof(val_str),
580 pkt->size, unit_byte_str));
581 probe_int("pos", pkt->pos);
582 probe_str("flags", pkt->flags & AV_PKT_FLAG_KEY ? "K" : "_");
583 probe_object_footer("packet");
586 static void show_packets(AVFormatContext *fmt_ctx)
590 av_init_packet(&pkt);
591 probe_array_header("packets", 0);
592 while (!av_read_frame(fmt_ctx, &pkt))
593 show_packet(fmt_ctx, &pkt);
594 probe_array_footer("packets", 0);
597 static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
599 AVStream *stream = fmt_ctx->streams[stream_idx];
600 AVCodecContext *dec_ctx;
604 AVRational display_aspect_ratio, *sar = NULL;
605 const AVPixFmtDescriptor *desc;
607 probe_object_header("stream");
609 probe_int("index", stream->index);
611 if ((dec_ctx = stream->codec)) {
612 if ((dec = dec_ctx->codec)) {
613 probe_str("codec_name", dec->name);
614 probe_str("codec_long_name", dec->long_name);
616 probe_str("codec_name", "unknown");
619 probe_str("codec_type", media_type_string(dec_ctx->codec_type));
620 probe_str("codec_time_base",
621 rational_string(val_str, sizeof(val_str),
622 "/", &dec_ctx->time_base));
624 /* print AVI/FourCC tag */
625 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
626 probe_str("codec_tag_string", val_str);
627 probe_str("codec_tag", tag_string(val_str, sizeof(val_str),
628 dec_ctx->codec_tag));
630 /* print profile, if there is one */
631 if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
632 probe_str("profile", profile);
634 switch (dec_ctx->codec_type) {
635 case AVMEDIA_TYPE_VIDEO:
636 probe_int("width", dec_ctx->width);
637 probe_int("height", dec_ctx->height);
638 probe_int("coded_width", dec_ctx->coded_width);
639 probe_int("coded_height", dec_ctx->coded_height);
640 probe_int("has_b_frames", dec_ctx->has_b_frames);
641 if (dec_ctx->sample_aspect_ratio.num)
642 sar = &dec_ctx->sample_aspect_ratio;
643 else if (stream->sample_aspect_ratio.num)
644 sar = &stream->sample_aspect_ratio;
647 probe_str("sample_aspect_ratio",
648 rational_string(val_str, sizeof(val_str), ":", sar));
649 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
650 dec_ctx->width * sar->num, dec_ctx->height * sar->den,
652 probe_str("display_aspect_ratio",
653 rational_string(val_str, sizeof(val_str), ":",
654 &display_aspect_ratio));
656 desc = av_pix_fmt_desc_get(dec_ctx->pix_fmt);
657 probe_str("pix_fmt", desc ? desc->name : "unknown");
658 probe_int("level", dec_ctx->level);
660 probe_str("color_range", av_color_range_name(dec_ctx->color_range));
661 probe_str("color_space", av_color_space_name(dec_ctx->colorspace));
662 probe_str("color_trc", av_color_transfer_name(dec_ctx->color_trc));
663 probe_str("color_pri", av_color_primaries_name(dec_ctx->color_primaries));
664 probe_str("chroma_loc", av_chroma_location_name(dec_ctx->chroma_sample_location));
667 case AVMEDIA_TYPE_AUDIO:
668 probe_str("sample_rate",
669 value_string(val_str, sizeof(val_str),
670 dec_ctx->sample_rate,
672 probe_int("channels", dec_ctx->channels);
673 probe_int("bits_per_sample",
674 av_get_bits_per_sample(dec_ctx->codec_id));
678 probe_str("codec_type", "unknown");
681 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
682 probe_int("id", stream->id);
683 probe_str("avg_frame_rate",
684 rational_string(val_str, sizeof(val_str), "/",
685 &stream->avg_frame_rate));
686 if (dec_ctx->bit_rate)
687 probe_str("bit_rate",
688 value_string(val_str, sizeof(val_str),
689 dec_ctx->bit_rate, unit_bit_per_second_str));
690 probe_str("time_base",
691 rational_string(val_str, sizeof(val_str), "/",
692 &stream->time_base));
693 probe_str("start_time",
694 time_value_string(val_str, sizeof(val_str),
695 stream->start_time, &stream->time_base));
696 probe_str("duration",
697 time_value_string(val_str, sizeof(val_str),
698 stream->duration, &stream->time_base));
699 if (stream->nb_frames)
700 probe_int("nb_frames", stream->nb_frames);
702 probe_dict(stream->metadata, "tags");
704 if (stream->nb_side_data) {
706 probe_object_header("sidedata");
707 for (i = 0; i < stream->nb_side_data; i++) {
708 const AVPacketSideData* sd = &stream->side_data[i];
710 case AV_PKT_DATA_DISPLAYMATRIX:
711 probe_object_header("displaymatrix");
712 probe_array_header("matrix", 1);
713 for (j = 0; j < 9; j++)
714 probe_int(NULL, ((int32_t *)sd->data)[j]);
715 probe_array_footer("matrix", 1);
716 probe_int("rotation",
717 av_display_rotation_get((int32_t *)sd->data));
718 probe_object_footer("displaymatrix");
722 probe_object_footer("sidedata");
725 probe_object_footer("stream");
728 static void show_format(AVFormatContext *fmt_ctx)
731 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
733 probe_object_header("format");
734 probe_str("filename", fmt_ctx->filename);
735 probe_int("nb_streams", fmt_ctx->nb_streams);
736 probe_str("format_name", fmt_ctx->iformat->name);
737 probe_str("format_long_name", fmt_ctx->iformat->long_name);
738 probe_str("start_time",
739 time_value_string(val_str, sizeof(val_str),
740 fmt_ctx->start_time, &AV_TIME_BASE_Q));
741 probe_str("duration",
742 time_value_string(val_str, sizeof(val_str),
743 fmt_ctx->duration, &AV_TIME_BASE_Q));
745 size >= 0 ? value_string(val_str, sizeof(val_str),
748 probe_str("bit_rate",
749 value_string(val_str, sizeof(val_str),
750 fmt_ctx->bit_rate, unit_bit_per_second_str));
752 probe_dict(fmt_ctx->metadata, "tags");
754 probe_object_footer("format");
757 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
760 AVFormatContext *fmt_ctx = NULL;
761 AVDictionaryEntry *t;
763 if ((err = avformat_open_input(&fmt_ctx, filename,
764 iformat, &format_opts)) < 0) {
765 print_error(filename, err);
768 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
769 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
770 return AVERROR_OPTION_NOT_FOUND;
774 /* fill the streams in the format context */
775 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
776 print_error(filename, err);
780 av_dump_format(fmt_ctx, 0, filename, 0);
782 /* bind a decoder to each input stream */
783 for (i = 0; i < fmt_ctx->nb_streams; i++) {
784 AVStream *stream = fmt_ctx->streams[i];
787 if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
788 fprintf(stderr, "Failed to probe codec for input stream %d\n",
790 } else if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
792 "Unsupported codec with id %d for input stream %d\n",
793 stream->codec->codec_id, stream->index);
794 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
795 fprintf(stderr, "Error while opening codec for input stream %d\n",
800 *fmt_ctx_ptr = fmt_ctx;
804 static void close_input_file(AVFormatContext **ctx_ptr)
807 AVFormatContext *fmt_ctx = *ctx_ptr;
809 /* close decoder for each stream */
810 for (i = 0; i < fmt_ctx->nb_streams; i++) {
811 AVStream *stream = fmt_ctx->streams[i];
813 avcodec_close(stream->codec);
815 avformat_close_input(ctx_ptr);
818 static int probe_file(const char *filename)
820 AVFormatContext *fmt_ctx;
823 if ((ret = open_input_file(&fmt_ctx, filename)))
827 show_format(fmt_ctx);
829 if (do_show_streams) {
830 probe_array_header("streams", 0);
831 for (i = 0; i < fmt_ctx->nb_streams; i++)
832 show_stream(fmt_ctx, i);
833 probe_array_footer("streams", 0);
837 show_packets(fmt_ctx);
839 close_input_file(&fmt_ctx);
843 static void show_usage(void)
845 printf("Simple multimedia streams analyzer\n");
846 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
850 static int opt_format(void *optctx, const char *opt, const char *arg)
852 iformat = av_find_input_format(arg);
854 fprintf(stderr, "Unknown input format: %s\n", arg);
855 return AVERROR(EINVAL);
860 static int opt_output_format(void *optctx, const char *opt, const char *arg)
863 if (!strcmp(arg, "json")) {
864 octx.print_header = json_print_header;
865 octx.print_footer = json_print_footer;
866 octx.print_array_header = json_print_array_header;
867 octx.print_array_footer = json_print_array_footer;
868 octx.print_object_header = json_print_object_header;
869 octx.print_object_footer = json_print_object_footer;
871 octx.print_integer = json_print_integer;
872 octx.print_string = json_print_string;
873 } else if (!strcmp(arg, "ini")) {
874 octx.print_header = ini_print_header;
875 octx.print_footer = ini_print_footer;
876 octx.print_array_header = ini_print_array_header;
877 octx.print_array_footer = ini_print_array_footer;
878 octx.print_object_header = ini_print_object_header;
880 octx.print_integer = ini_print_integer;
881 octx.print_string = ini_print_string;
882 } else if (!strcmp(arg, "old")) {
883 octx.print_header = NULL;
884 octx.print_object_header = old_print_object_header;
885 octx.print_object_footer = old_print_object_footer;
887 octx.print_string = old_print_string;
889 av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
890 return AVERROR(EINVAL);
895 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
898 nb_fmt_entries_to_show++;
899 octx.print_header = NULL;
900 octx.print_footer = NULL;
901 octx.print_array_header = NULL;
902 octx.print_array_footer = NULL;
903 octx.print_object_header = NULL;
904 octx.print_object_footer = NULL;
906 octx.print_integer = show_format_entry_integer;
907 octx.print_string = show_format_entry_string;
908 av_dict_set(&fmt_entries_to_show, arg, "", 0);
912 static void opt_input_file(void *optctx, const char *arg)
914 if (input_filename) {
916 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
917 arg, input_filename);
920 if (!strcmp(arg, "-"))
922 input_filename = arg;
925 void show_help_default(const char *opt, const char *arg)
927 av_log_set_callback(log_callback_help);
929 show_help_options(options, "Main options:", 0, 0, 0);
931 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
934 static int opt_pretty(void *optctx, const char *opt, const char *arg)
937 use_value_prefix = 1;
938 use_byte_value_binary_prefix = 1;
939 use_value_sexagesimal_format = 1;
943 static const OptionDef real_options[] = {
944 #include "cmdutils_common_opts.h"
945 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
946 { "of", HAS_ARG, {.func_arg = opt_output_format}, "output the document either as ini or json", "output_format" },
947 { "unit", OPT_BOOL, {&show_value_unit},
948 "show unit of the displayed values" },
949 { "prefix", OPT_BOOL, {&use_value_prefix},
950 "use SI prefixes for the displayed values" },
951 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
952 "use binary prefixes for byte units" },
953 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
954 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
955 { "pretty", 0, {.func_arg = opt_pretty},
956 "prettify the format of displayed values, make it more human readable" },
957 { "show_format", OPT_BOOL, {&do_show_format} , "show format/container info" },
958 { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
959 "show a particular entry from the format/container info", "entry" },
960 { "show_packets", OPT_BOOL, {&do_show_packets}, "show packets info" },
961 { "show_streams", OPT_BOOL, {&do_show_streams}, "show streams info" },
962 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default},
963 "generic catch all option", "" },
967 static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
969 printf("%.*s", buf_size, buf);
973 #define AVP_BUFFSIZE 4096
975 int main(int argc, char **argv)
978 uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
983 register_exit(avprobe_cleanup);
985 options = real_options;
986 parse_loglevel(argc, argv, options);
988 avformat_network_init();
991 avdevice_register_all();
996 octx.print_header = ini_print_header;
997 octx.print_footer = ini_print_footer;
999 octx.print_array_header = ini_print_array_header;
1000 octx.print_array_footer = ini_print_array_footer;
1001 octx.print_object_header = ini_print_object_header;
1003 octx.print_integer = ini_print_integer;
1004 octx.print_string = ini_print_string;
1006 parse_options(NULL, argc, argv, options, opt_input_file);
1008 if (!input_filename) {
1010 fprintf(stderr, "You have to specify one input file.\n");
1012 "Use -h to get full help or, even better, run 'man %s'.\n",
1017 probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
1018 probe_buf_write, NULL);
1023 ret = probe_file(input_filename);
1025 avio_flush(probe_out);
1026 avio_close(probe_out);
1028 avformat_network_deinit();