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 typedef struct InputStream {
38 AVCodecContext *dec_ctx;
41 typedef struct InputFile {
42 AVFormatContext *fmt_ctx;
48 const char program_name[] = "avprobe";
49 const int program_birth_year = 2007;
51 static int do_show_format = 0;
52 static AVDictionary *fmt_entries_to_show = NULL;
53 static int nb_fmt_entries_to_show;
54 static int do_show_packets = 0;
55 static int do_show_streams = 0;
57 static int show_value_unit = 0;
58 static int use_value_prefix = 0;
59 static int use_byte_value_binary_prefix = 0;
60 static int use_value_sexagesimal_format = 0;
63 static const OptionDef *options;
66 static const char *input_filename;
67 static AVInputFormat *iformat = NULL;
69 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
70 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
72 static const char unit_second_str[] = "s" ;
73 static const char unit_hertz_str[] = "Hz" ;
74 static const char unit_byte_str[] = "byte" ;
75 static const char unit_bit_per_second_str[] = "bit/s";
77 static void avprobe_cleanup(int ret)
79 av_dict_free(&fmt_entries_to_show);
83 * The output is structured in array and objects that might contain items
84 * Array could require the objects within to not be named.
85 * Object could require the items within to be named.
87 * For flat representation the name of each section is saved on prefix so it
88 * can be rendered in order to represent nested structures (e.g. array of
89 * objects for the packets list).
91 * Within an array each element can need an unique identifier or an index.
93 * Nesting level is accounted separately.
101 typedef struct PrintElement {
103 PrintElementType type;
108 typedef struct PrintContext {
109 PrintElement *prefix;
111 void (*print_header)(void);
112 void (*print_footer)(void);
114 void (*print_array_header) (const char *name, int plain_values);
115 void (*print_array_footer) (const char *name, int plain_values);
116 void (*print_object_header)(const char *name);
117 void (*print_object_footer)(const char *name);
119 void (*print_integer) (const char *key, int64_t value);
120 void (*print_string) (const char *key, const char *value);
123 static AVIOContext *probe_out = NULL;
124 static PrintContext octx;
125 #define AVP_INDENT() avio_printf(probe_out, "%*c", octx.level * 2, ' ')
128 * Default format, INI
130 * - all key and values are utf8
131 * - '.' is the subgroup separator
132 * - newlines and the following characters are escaped
133 * - '\' is the escape character
134 * - '#' is the comment
135 * - '=' is the key/value separators
136 * - ':' is not used but usually parsed as key/value separator
139 static void ini_print_header(void)
141 avio_printf(probe_out, "# avprobe output\n\n");
143 static void ini_print_footer(void)
145 avio_w8(probe_out, '\n');
148 static void ini_escape_print(const char *s)
155 case '\r': avio_printf(probe_out, "%s", "\\r"); break;
156 case '\n': avio_printf(probe_out, "%s", "\\n"); break;
157 case '\f': avio_printf(probe_out, "%s", "\\f"); break;
158 case '\b': avio_printf(probe_out, "%s", "\\b"); break;
159 case '\t': avio_printf(probe_out, "%s", "\\t"); break;
163 case ':' : avio_w8(probe_out, '\\');
165 if ((unsigned char)c < 32)
166 avio_printf(probe_out, "\\x00%02x", c & 0xff);
168 avio_w8(probe_out, c);
174 static void ini_print_array_header(const char *name, int plain_values)
177 /* Add a new line if we create a new full group */
178 if (octx.prefix[octx.level -1].nb_elems)
179 avio_printf(probe_out, "\n");
181 ini_escape_print(name);
182 avio_w8(probe_out, '=');
186 static void ini_print_array_footer(const char *name, int plain_values)
189 avio_printf(probe_out, "\n");
192 static void ini_print_object_header(const char *name)
195 PrintElement *el = octx.prefix + octx.level -1;
198 avio_printf(probe_out, "\n");
200 avio_printf(probe_out, "[");
202 for (i = 1; i < octx.level; i++) {
203 el = octx.prefix + i;
204 avio_printf(probe_out, "%s.", el->name);
206 avio_printf(probe_out, "%"PRId64".", el->index);
209 avio_printf(probe_out, "%s", name);
210 if (el->type == ARRAY)
211 avio_printf(probe_out, ".%"PRId64"", el->nb_elems);
212 avio_printf(probe_out, "]\n");
215 static void ini_print_integer(const char *key, int64_t value)
218 ini_escape_print(key);
219 avio_printf(probe_out, "=%"PRId64"\n", value);
221 if (octx.prefix[octx.level -1].nb_elems)
222 avio_printf(probe_out, ",");
223 avio_printf(probe_out, "%"PRId64, value);
228 static void ini_print_string(const char *key, const char *value)
230 ini_escape_print(key);
231 avio_printf(probe_out, "=");
232 ini_escape_print(value);
233 avio_w8(probe_out, '\n');
237 * Alternate format, JSON
240 static void json_print_header(void)
242 avio_printf(probe_out, "{");
244 static void json_print_footer(void)
246 avio_printf(probe_out, "}\n");
249 static void json_print_array_header(const char *name, int plain_values)
251 if (octx.prefix[octx.level -1].nb_elems)
252 avio_printf(probe_out, ",\n");
254 avio_printf(probe_out, "\"%s\" : ", name);
255 avio_printf(probe_out, "[\n");
258 static void json_print_array_footer(const char *name, int plain_values)
260 avio_printf(probe_out, "\n");
262 avio_printf(probe_out, "]");
265 static void json_print_object_header(const char *name)
267 if (octx.prefix[octx.level -1].nb_elems)
268 avio_printf(probe_out, ",\n");
270 if (octx.prefix[octx.level -1].type == OBJECT)
271 avio_printf(probe_out, "\"%s\" : ", name);
272 avio_printf(probe_out, "{\n");
275 static void json_print_object_footer(const char *name)
277 avio_printf(probe_out, "\n");
279 avio_printf(probe_out, "}");
282 static void json_print_integer(const char *key, int64_t value)
285 if (octx.prefix[octx.level -1].nb_elems)
286 avio_printf(probe_out, ",\n");
288 avio_printf(probe_out, "\"%s\" : ", key);
290 if (octx.prefix[octx.level -1].nb_elems)
291 avio_printf(probe_out, ", ");
295 avio_printf(probe_out, "%"PRId64, value);
298 static void json_escape_print(const char *s)
305 case '\r': avio_printf(probe_out, "%s", "\\r"); break;
306 case '\n': avio_printf(probe_out, "%s", "\\n"); break;
307 case '\f': avio_printf(probe_out, "%s", "\\f"); break;
308 case '\b': avio_printf(probe_out, "%s", "\\b"); break;
309 case '\t': avio_printf(probe_out, "%s", "\\t"); break;
311 case '"' : avio_w8(probe_out, '\\');
313 if ((unsigned char)c < 32)
314 avio_printf(probe_out, "\\u00%02x", c & 0xff);
316 avio_w8(probe_out, c);
322 static void json_print_string(const char *key, const char *value)
324 if (octx.prefix[octx.level -1].nb_elems)
325 avio_printf(probe_out, ",\n");
327 avio_w8(probe_out, '\"');
328 json_escape_print(key);
329 avio_printf(probe_out, "\" : \"");
330 json_escape_print(value);
331 avio_w8(probe_out, '\"');
335 * old-style pseudo-INI
337 static void old_print_object_header(const char *name)
341 if (!strcmp(name, "tags"))
344 str = p = av_strdup(name);
352 avio_printf(probe_out, "[%s]\n", str);
356 static void old_print_object_footer(const char *name)
360 if (!strcmp(name, "tags"))
363 str = p = av_strdup(name);
371 avio_printf(probe_out, "[/%s]\n", str);
375 static void old_print_string(const char *key, const char *value)
377 if (!strcmp(octx.prefix[octx.level - 1].name, "tags"))
378 avio_printf(probe_out, "TAG:");
379 ini_print_string(key, value);
383 * Simple Formatter for single entries.
386 static void show_format_entry_integer(const char *key, int64_t value)
388 if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
389 if (nb_fmt_entries_to_show > 1)
390 avio_printf(probe_out, "%s=", key);
391 avio_printf(probe_out, "%"PRId64"\n", value);
395 static void show_format_entry_string(const char *key, const char *value)
397 if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
398 if (nb_fmt_entries_to_show > 1)
399 avio_printf(probe_out, "%s=", key);
400 avio_printf(probe_out, "%s\n", value);
404 static void probe_group_enter(const char *name, int type)
409 av_realloc(octx.prefix, sizeof(PrintElement) * (octx.level + 1));
411 if (!octx.prefix || !name) {
412 fprintf(stderr, "Out of memory\n");
417 PrintElement *parent = octx.prefix + octx.level -1;
418 if (parent->type == ARRAY)
419 count = parent->nb_elems;
423 octx.prefix[octx.level++] = (PrintElement){name, type, count, 0};
426 static void probe_group_leave(void)
431 static void probe_header(void)
433 if (octx.print_header)
435 probe_group_enter("root", OBJECT);
438 static void probe_footer(void)
440 if (octx.print_footer)
446 static void probe_array_header(const char *name, int plain_values)
448 if (octx.print_array_header)
449 octx.print_array_header(name, plain_values);
451 probe_group_enter(name, ARRAY);
454 static void probe_array_footer(const char *name, int plain_values)
457 if (octx.print_array_footer)
458 octx.print_array_footer(name, plain_values);
461 static void probe_object_header(const char *name)
463 if (octx.print_object_header)
464 octx.print_object_header(name);
466 probe_group_enter(name, OBJECT);
469 static void probe_object_footer(const char *name)
472 if (octx.print_object_footer)
473 octx.print_object_footer(name);
476 static void probe_int(const char *key, int64_t value)
478 octx.print_integer(key, value);
479 octx.prefix[octx.level -1].nb_elems++;
482 static void probe_str(const char *key, const char *value)
484 octx.print_string(key, value);
485 octx.prefix[octx.level -1].nb_elems++;
488 static void probe_dict(AVDictionary *dict, const char *name)
490 AVDictionaryEntry *entry = NULL;
493 probe_object_header(name);
494 while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
495 probe_str(entry->key, entry->value);
497 probe_object_footer(name);
500 static char *value_string(char *buf, int buf_size, double val, const char *unit)
502 if (unit == unit_second_str && use_value_sexagesimal_format) {
506 mins = (int)secs / 60;
507 secs = secs - mins * 60;
510 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
511 } else if (use_value_prefix) {
512 const char *prefix_string;
515 if (unit == unit_byte_str && use_byte_value_binary_prefix) {
516 index = (int) log2(val) / 10;
517 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
518 val /= pow(2, index * 10);
519 prefix_string = binary_unit_prefixes[index];
521 index = (int) (log10(val)) / 3;
522 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
523 val /= pow(10, index * 3);
524 prefix_string = decimal_unit_prefixes[index];
526 snprintf(buf, buf_size, "%.*f%s%s",
529 show_value_unit ? unit : "");
531 snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
537 static char *time_value_string(char *buf, int buf_size, int64_t val,
538 const AVRational *time_base)
540 if (val == AV_NOPTS_VALUE) {
541 snprintf(buf, buf_size, "N/A");
543 value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
549 static char *ts_value_string(char *buf, int buf_size, int64_t ts)
551 if (ts == AV_NOPTS_VALUE) {
552 snprintf(buf, buf_size, "N/A");
554 snprintf(buf, buf_size, "%"PRId64, ts);
560 static char *rational_string(char *buf, int buf_size, const char *sep,
561 const AVRational *rat)
563 snprintf(buf, buf_size, "%d%s%d", rat->num, sep, rat->den);
567 static char *tag_string(char *buf, int buf_size, int tag)
569 snprintf(buf, buf_size, "0x%04x", tag);
573 static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
576 AVStream *st = fmt_ctx->streams[pkt->stream_index];
578 probe_object_header("packet");
579 probe_str("codec_type", media_type_string(st->codecpar->codec_type));
580 probe_int("stream_index", pkt->stream_index);
581 probe_str("pts", ts_value_string(val_str, sizeof(val_str), pkt->pts));
582 probe_str("pts_time", time_value_string(val_str, sizeof(val_str),
583 pkt->pts, &st->time_base));
584 probe_str("dts", ts_value_string(val_str, sizeof(val_str), pkt->dts));
585 probe_str("dts_time", time_value_string(val_str, sizeof(val_str),
586 pkt->dts, &st->time_base));
587 probe_str("duration", ts_value_string(val_str, sizeof(val_str),
589 probe_str("duration_time", time_value_string(val_str, sizeof(val_str),
592 probe_str("size", value_string(val_str, sizeof(val_str),
593 pkt->size, unit_byte_str));
594 probe_int("pos", pkt->pos);
595 probe_str("flags", pkt->flags & AV_PKT_FLAG_KEY ? "K" : "_");
596 probe_object_footer("packet");
599 static void show_packets(InputFile *ifile)
601 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
604 av_init_packet(&pkt);
605 probe_array_header("packets", 0);
606 while (!av_read_frame(fmt_ctx, &pkt)) {
607 show_packet(fmt_ctx, &pkt);
608 av_packet_unref(&pkt);
610 probe_array_footer("packets", 0);
613 static void show_stream(InputFile *ifile, InputStream *ist)
615 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
616 AVStream *stream = ist->st;
617 AVCodecParameters *par;
618 AVCodecContext *dec_ctx;
619 const AVCodecDescriptor *codec_desc;
622 AVRational display_aspect_ratio, *sar = NULL;
623 const AVPixFmtDescriptor *desc;
625 probe_object_header("stream");
627 probe_int("index", stream->index);
629 par = stream->codecpar;
630 dec_ctx = ist->dec_ctx;
631 codec_desc = avcodec_descriptor_get(par->codec_id);
633 probe_str("codec_name", codec_desc->name);
634 probe_str("codec_long_name", codec_desc->long_name);
636 probe_str("codec_name", "unknown");
639 probe_str("codec_type", media_type_string(par->codec_type));
641 /* print AVI/FourCC tag */
642 av_get_codec_tag_string(val_str, sizeof(val_str), par->codec_tag);
643 probe_str("codec_tag_string", val_str);
644 probe_str("codec_tag", tag_string(val_str, sizeof(val_str),
647 /* print profile, if there is one */
648 profile = avcodec_profile_name(par->codec_id, par->profile);
650 probe_str("profile", profile);
652 switch (par->codec_type) {
653 case AVMEDIA_TYPE_VIDEO:
654 probe_int("width", par->width);
655 probe_int("height", par->height);
657 probe_int("coded_width", dec_ctx->coded_width);
658 probe_int("coded_height", dec_ctx->coded_height);
659 probe_int("has_b_frames", dec_ctx->has_b_frames);
661 if (dec_ctx && dec_ctx->sample_aspect_ratio.num)
662 sar = &dec_ctx->sample_aspect_ratio;
663 else if (par->sample_aspect_ratio.num)
664 sar = &par->sample_aspect_ratio;
665 else if (stream->sample_aspect_ratio.num)
666 sar = &stream->sample_aspect_ratio;
669 probe_str("sample_aspect_ratio",
670 rational_string(val_str, sizeof(val_str), ":", sar));
671 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
672 par->width * sar->num, par->height * sar->den,
674 probe_str("display_aspect_ratio",
675 rational_string(val_str, sizeof(val_str), ":",
676 &display_aspect_ratio));
678 desc = av_pix_fmt_desc_get(par->format);
679 probe_str("pix_fmt", desc ? desc->name : "unknown");
680 probe_int("level", par->level);
682 probe_str("color_range", av_color_range_name (par->color_range));
683 probe_str("color_space", av_color_space_name (par->color_space));
684 probe_str("color_trc", av_color_transfer_name (par->color_trc));
685 probe_str("color_pri", av_color_primaries_name(par->color_primaries));
686 probe_str("chroma_loc", av_chroma_location_name (par->chroma_location));
689 case AVMEDIA_TYPE_AUDIO:
690 probe_str("sample_rate",
691 value_string(val_str, sizeof(val_str),
694 probe_int("channels", par->channels);
695 probe_int("bits_per_sample",
696 av_get_bits_per_sample(par->codec_id));
700 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
701 probe_int("id", stream->id);
702 probe_str("avg_frame_rate",
703 rational_string(val_str, sizeof(val_str), "/",
704 &stream->avg_frame_rate));
707 probe_str("bit_rate",
708 value_string(val_str, sizeof(val_str),
709 par->bit_rate, unit_bit_per_second_str));
710 probe_str("time_base",
711 rational_string(val_str, sizeof(val_str), "/",
712 &stream->time_base));
713 probe_str("start_time",
714 time_value_string(val_str, sizeof(val_str),
715 stream->start_time, &stream->time_base));
716 probe_str("duration",
717 time_value_string(val_str, sizeof(val_str),
718 stream->duration, &stream->time_base));
719 if (stream->nb_frames)
720 probe_int("nb_frames", stream->nb_frames);
722 probe_dict(stream->metadata, "tags");
724 if (stream->nb_side_data) {
726 probe_object_header("sidedata");
727 for (i = 0; i < stream->nb_side_data; i++) {
728 const AVPacketSideData* sd = &stream->side_data[i];
730 case AV_PKT_DATA_DISPLAYMATRIX:
731 probe_object_header("displaymatrix");
732 probe_array_header("matrix", 1);
733 for (j = 0; j < 9; j++)
734 probe_int(NULL, ((int32_t *)sd->data)[j]);
735 probe_array_footer("matrix", 1);
736 probe_int("rotation",
737 av_display_rotation_get((int32_t *)sd->data));
738 probe_object_footer("displaymatrix");
742 probe_object_footer("sidedata");
745 probe_object_footer("stream");
748 static void show_format(InputFile *ifile)
750 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
752 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
754 probe_object_header("format");
755 probe_str("filename", fmt_ctx->filename);
756 probe_int("nb_streams", fmt_ctx->nb_streams);
757 probe_str("format_name", fmt_ctx->iformat->name);
758 probe_str("format_long_name", fmt_ctx->iformat->long_name);
759 probe_str("start_time",
760 time_value_string(val_str, sizeof(val_str),
761 fmt_ctx->start_time, &AV_TIME_BASE_Q));
762 probe_str("duration",
763 time_value_string(val_str, sizeof(val_str),
764 fmt_ctx->duration, &AV_TIME_BASE_Q));
766 size >= 0 ? value_string(val_str, sizeof(val_str),
769 probe_str("bit_rate",
770 value_string(val_str, sizeof(val_str),
771 fmt_ctx->bit_rate, unit_bit_per_second_str));
773 probe_dict(fmt_ctx->metadata, "tags");
775 probe_object_footer("format");
778 static int open_input_file(InputFile *ifile, const char *filename)
781 AVFormatContext *fmt_ctx = NULL;
782 AVDictionaryEntry *t;
784 if ((err = avformat_open_input(&fmt_ctx, filename,
785 iformat, &format_opts)) < 0) {
786 print_error(filename, err);
789 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
790 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
791 return AVERROR_OPTION_NOT_FOUND;
795 /* fill the streams in the format context */
796 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
797 print_error(filename, err);
801 av_dump_format(fmt_ctx, 0, filename, 0);
803 ifile->streams = av_mallocz_array(fmt_ctx->nb_streams,
804 sizeof(*ifile->streams));
807 ifile->nb_streams = fmt_ctx->nb_streams;
809 /* bind a decoder to each input stream */
810 for (i = 0; i < fmt_ctx->nb_streams; i++) {
811 InputStream *ist = &ifile->streams[i];
812 AVStream *stream = fmt_ctx->streams[i];
817 if (stream->codecpar->codec_id == AV_CODEC_ID_PROBE) {
818 fprintf(stderr, "Failed to probe codec for input stream %d\n",
823 codec = avcodec_find_decoder(stream->codecpar->codec_id);
826 "Unsupported codec with id %d for input stream %d\n",
827 stream->codecpar->codec_id, stream->index);
831 ist->dec_ctx = avcodec_alloc_context3(codec);
835 err = avcodec_parameters_to_context(ist->dec_ctx, stream->codecpar);
839 err = avcodec_open2(ist->dec_ctx, NULL, NULL);
841 fprintf(stderr, "Error while opening codec for input stream %d\n",
848 ifile->fmt_ctx = fmt_ctx;
852 static void close_input_file(InputFile *ifile)
856 /* close decoder for each stream */
857 for (i = 0; i < ifile->nb_streams; i++) {
858 InputStream *ist = &ifile->streams[i];
860 avcodec_free_context(&ist->dec_ctx);
863 av_freep(&ifile->streams);
864 ifile->nb_streams = 0;
866 avformat_close_input(&ifile->fmt_ctx);
869 static int probe_file(const char *filename)
874 ret = open_input_file(&ifile, filename);
881 if (do_show_streams) {
882 probe_array_header("streams", 0);
883 for (i = 0; i < ifile.nb_streams; i++)
884 show_stream(&ifile, &ifile.streams[i]);
885 probe_array_footer("streams", 0);
889 show_packets(&ifile);
891 close_input_file(&ifile);
895 static void show_usage(void)
897 printf("Simple multimedia streams analyzer\n");
898 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
902 static int opt_format(void *optctx, const char *opt, const char *arg)
904 iformat = av_find_input_format(arg);
906 fprintf(stderr, "Unknown input format: %s\n", arg);
907 return AVERROR(EINVAL);
912 static int opt_output_format(void *optctx, const char *opt, const char *arg)
915 if (!strcmp(arg, "json")) {
916 octx.print_header = json_print_header;
917 octx.print_footer = json_print_footer;
918 octx.print_array_header = json_print_array_header;
919 octx.print_array_footer = json_print_array_footer;
920 octx.print_object_header = json_print_object_header;
921 octx.print_object_footer = json_print_object_footer;
923 octx.print_integer = json_print_integer;
924 octx.print_string = json_print_string;
925 } else if (!strcmp(arg, "ini")) {
926 octx.print_header = ini_print_header;
927 octx.print_footer = ini_print_footer;
928 octx.print_array_header = ini_print_array_header;
929 octx.print_array_footer = ini_print_array_footer;
930 octx.print_object_header = ini_print_object_header;
932 octx.print_integer = ini_print_integer;
933 octx.print_string = ini_print_string;
934 } else if (!strcmp(arg, "old")) {
935 octx.print_header = NULL;
936 octx.print_object_header = old_print_object_header;
937 octx.print_object_footer = old_print_object_footer;
939 octx.print_string = old_print_string;
941 av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
942 return AVERROR(EINVAL);
947 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
950 nb_fmt_entries_to_show++;
951 octx.print_header = NULL;
952 octx.print_footer = NULL;
953 octx.print_array_header = NULL;
954 octx.print_array_footer = NULL;
955 octx.print_object_header = NULL;
956 octx.print_object_footer = NULL;
958 octx.print_integer = show_format_entry_integer;
959 octx.print_string = show_format_entry_string;
960 av_dict_set(&fmt_entries_to_show, arg, "", 0);
964 static void opt_input_file(void *optctx, const char *arg)
966 if (input_filename) {
968 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
969 arg, input_filename);
972 if (!strcmp(arg, "-"))
974 input_filename = arg;
977 void show_help_default(const char *opt, const char *arg)
979 av_log_set_callback(log_callback_help);
981 show_help_options(options, "Main options:", 0, 0, 0);
983 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
986 static int opt_pretty(void *optctx, const char *opt, const char *arg)
989 use_value_prefix = 1;
990 use_byte_value_binary_prefix = 1;
991 use_value_sexagesimal_format = 1;
995 static const OptionDef real_options[] = {
996 #include "cmdutils_common_opts.h"
997 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
998 { "of", HAS_ARG, {.func_arg = opt_output_format}, "output the document either as ini or json", "output_format" },
999 { "unit", OPT_BOOL, {&show_value_unit},
1000 "show unit of the displayed values" },
1001 { "prefix", OPT_BOOL, {&use_value_prefix},
1002 "use SI prefixes for the displayed values" },
1003 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
1004 "use binary prefixes for byte units" },
1005 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
1006 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
1007 { "pretty", 0, {.func_arg = opt_pretty},
1008 "prettify the format of displayed values, make it more human readable" },
1009 { "show_format", OPT_BOOL, {&do_show_format} , "show format/container info" },
1010 { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
1011 "show a particular entry from the format/container info", "entry" },
1012 { "show_packets", OPT_BOOL, {&do_show_packets}, "show packets info" },
1013 { "show_streams", OPT_BOOL, {&do_show_streams}, "show streams info" },
1014 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default},
1015 "generic catch all option", "" },
1019 static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
1021 printf("%.*s", buf_size, buf);
1025 #define AVP_BUFFSIZE 4096
1027 int main(int argc, char **argv)
1030 uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
1035 register_exit(avprobe_cleanup);
1037 options = real_options;
1038 parse_loglevel(argc, argv, options);
1040 avformat_network_init();
1043 avdevice_register_all();
1048 octx.print_header = ini_print_header;
1049 octx.print_footer = ini_print_footer;
1051 octx.print_array_header = ini_print_array_header;
1052 octx.print_array_footer = ini_print_array_footer;
1053 octx.print_object_header = ini_print_object_header;
1055 octx.print_integer = ini_print_integer;
1056 octx.print_string = ini_print_string;
1058 parse_options(NULL, argc, argv, options, opt_input_file);
1060 if (!input_filename) {
1062 fprintf(stderr, "You have to specify one input file.\n");
1064 "Use -h to get full help or, even better, run 'man %s'.\n",
1069 probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
1070 probe_buf_write, NULL);
1075 ret = probe_file(input_filename);
1077 avio_flush(probe_out);
1078 av_freep(&probe_out);
1081 avformat_network_deinit();