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/stereo3d.h"
31 #include "libavutil/dict.h"
32 #include "libavutil/libm.h"
33 #include "libavdevice/avdevice.h"
36 typedef struct InputStream {
39 AVCodecContext *dec_ctx;
42 typedef struct InputFile {
43 AVFormatContext *fmt_ctx;
49 const char program_name[] = "avprobe";
50 const int program_birth_year = 2007;
52 static int do_show_format = 0;
53 static AVDictionary *fmt_entries_to_show = NULL;
54 static int nb_fmt_entries_to_show;
55 static int do_show_packets = 0;
56 static int do_show_streams = 0;
58 static int show_value_unit = 0;
59 static int use_value_prefix = 0;
60 static int use_byte_value_binary_prefix = 0;
61 static int use_value_sexagesimal_format = 0;
64 static const OptionDef *options;
67 static const char *input_filename;
68 static AVInputFormat *iformat = NULL;
70 static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
71 static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
73 static const char unit_second_str[] = "s" ;
74 static const char unit_hertz_str[] = "Hz" ;
75 static const char unit_byte_str[] = "byte" ;
76 static const char unit_bit_per_second_str[] = "bit/s";
78 static void avprobe_cleanup(int ret)
80 av_dict_free(&fmt_entries_to_show);
84 * The output is structured in array and objects that might contain items
85 * Array could require the objects within to not be named.
86 * Object could require the items within to be named.
88 * For flat representation the name of each section is saved on prefix so it
89 * can be rendered in order to represent nested structures (e.g. array of
90 * objects for the packets list).
92 * Within an array each element can need an unique identifier or an index.
94 * Nesting level is accounted separately.
102 typedef struct PrintElement {
104 PrintElementType type;
109 typedef struct PrintContext {
110 PrintElement *prefix;
112 void (*print_header)(void);
113 void (*print_footer)(void);
115 void (*print_array_header) (const char *name, int plain_values);
116 void (*print_array_footer) (const char *name, int plain_values);
117 void (*print_object_header)(const char *name);
118 void (*print_object_footer)(const char *name);
120 void (*print_integer) (const char *key, int64_t value);
121 void (*print_string) (const char *key, const char *value);
124 static AVIOContext *probe_out = NULL;
125 static PrintContext octx;
126 #define AVP_INDENT() avio_printf(probe_out, "%*c", octx.level * 2, ' ')
129 * Default format, INI
131 * - all key and values are utf8
132 * - '.' is the subgroup separator
133 * - newlines and the following characters are escaped
134 * - '\' is the escape character
135 * - '#' is the comment
136 * - '=' is the key/value separators
137 * - ':' is not used but usually parsed as key/value separator
140 static void ini_print_header(void)
142 avio_printf(probe_out, "# avprobe output\n\n");
144 static void ini_print_footer(void)
146 avio_w8(probe_out, '\n');
149 static void ini_escape_print(const char *s)
156 case '\r': avio_printf(probe_out, "%s", "\\r"); break;
157 case '\n': avio_printf(probe_out, "%s", "\\n"); break;
158 case '\f': avio_printf(probe_out, "%s", "\\f"); break;
159 case '\b': avio_printf(probe_out, "%s", "\\b"); break;
160 case '\t': avio_printf(probe_out, "%s", "\\t"); break;
164 case ':' : avio_w8(probe_out, '\\');
166 if ((unsigned char)c < 32)
167 avio_printf(probe_out, "\\x00%02x", c & 0xff);
169 avio_w8(probe_out, c);
175 static void ini_print_array_header(const char *name, int plain_values)
178 /* Add a new line if we create a new full group */
179 if (octx.prefix[octx.level -1].nb_elems)
180 avio_printf(probe_out, "\n");
182 ini_escape_print(name);
183 avio_w8(probe_out, '=');
187 static void ini_print_array_footer(const char *name, int plain_values)
190 avio_printf(probe_out, "\n");
193 static void ini_print_object_header(const char *name)
196 PrintElement *el = octx.prefix + octx.level -1;
199 avio_printf(probe_out, "\n");
201 avio_printf(probe_out, "[");
203 for (i = 1; i < octx.level; i++) {
204 el = octx.prefix + i;
205 avio_printf(probe_out, "%s.", el->name);
207 avio_printf(probe_out, "%"PRId64".", el->index);
210 avio_printf(probe_out, "%s", name);
211 if (el->type == ARRAY)
212 avio_printf(probe_out, ".%"PRId64"", el->nb_elems);
213 avio_printf(probe_out, "]\n");
216 static void ini_print_integer(const char *key, int64_t value)
219 ini_escape_print(key);
220 avio_printf(probe_out, "=%"PRId64"\n", value);
222 if (octx.prefix[octx.level -1].nb_elems)
223 avio_printf(probe_out, ",");
224 avio_printf(probe_out, "%"PRId64, value);
229 static void ini_print_string(const char *key, const char *value)
231 ini_escape_print(key);
232 avio_printf(probe_out, "=");
233 ini_escape_print(value);
234 avio_w8(probe_out, '\n');
238 * Alternate format, JSON
241 static void json_print_header(void)
243 avio_printf(probe_out, "{");
245 static void json_print_footer(void)
247 avio_printf(probe_out, "}\n");
250 static void json_print_array_header(const char *name, int plain_values)
252 if (octx.prefix[octx.level -1].nb_elems)
253 avio_printf(probe_out, ",\n");
255 avio_printf(probe_out, "\"%s\" : ", name);
256 avio_printf(probe_out, "[\n");
259 static void json_print_array_footer(const char *name, int plain_values)
261 avio_printf(probe_out, "\n");
263 avio_printf(probe_out, "]");
266 static void json_print_object_header(const char *name)
268 if (octx.prefix[octx.level -1].nb_elems)
269 avio_printf(probe_out, ",\n");
271 if (octx.prefix[octx.level -1].type == OBJECT)
272 avio_printf(probe_out, "\"%s\" : ", name);
273 avio_printf(probe_out, "{\n");
276 static void json_print_object_footer(const char *name)
278 avio_printf(probe_out, "\n");
280 avio_printf(probe_out, "}");
283 static void json_print_integer(const char *key, int64_t value)
286 if (octx.prefix[octx.level -1].nb_elems)
287 avio_printf(probe_out, ",\n");
289 avio_printf(probe_out, "\"%s\" : ", key);
291 if (octx.prefix[octx.level -1].nb_elems)
292 avio_printf(probe_out, ", ");
296 avio_printf(probe_out, "%"PRId64, value);
299 static void json_escape_print(const char *s)
306 case '\r': avio_printf(probe_out, "%s", "\\r"); break;
307 case '\n': avio_printf(probe_out, "%s", "\\n"); break;
308 case '\f': avio_printf(probe_out, "%s", "\\f"); break;
309 case '\b': avio_printf(probe_out, "%s", "\\b"); break;
310 case '\t': avio_printf(probe_out, "%s", "\\t"); break;
312 case '"' : avio_w8(probe_out, '\\');
314 if ((unsigned char)c < 32)
315 avio_printf(probe_out, "\\u00%02x", c & 0xff);
317 avio_w8(probe_out, c);
323 static void json_print_string(const char *key, const char *value)
325 if (octx.prefix[octx.level -1].nb_elems)
326 avio_printf(probe_out, ",\n");
328 avio_w8(probe_out, '\"');
329 json_escape_print(key);
330 avio_printf(probe_out, "\" : \"");
331 json_escape_print(value);
332 avio_w8(probe_out, '\"');
336 * old-style pseudo-INI
338 static void old_print_object_header(const char *name)
342 if (!strcmp(name, "tags"))
345 str = p = av_strdup(name);
353 avio_printf(probe_out, "[%s]\n", str);
357 static void old_print_object_footer(const char *name)
361 if (!strcmp(name, "tags"))
364 str = p = av_strdup(name);
372 avio_printf(probe_out, "[/%s]\n", str);
376 static void old_print_string(const char *key, const char *value)
378 if (!strcmp(octx.prefix[octx.level - 1].name, "tags"))
379 avio_printf(probe_out, "TAG:");
380 ini_print_string(key, value);
384 * Simple Formatter for single entries.
387 static void show_format_entry_integer(const char *key, int64_t value)
389 if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
390 if (nb_fmt_entries_to_show > 1)
391 avio_printf(probe_out, "%s=", key);
392 avio_printf(probe_out, "%"PRId64"\n", value);
396 static void show_format_entry_string(const char *key, const char *value)
398 if (key && av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
399 if (nb_fmt_entries_to_show > 1)
400 avio_printf(probe_out, "%s=", key);
401 avio_printf(probe_out, "%s\n", value);
405 static void probe_group_enter(const char *name, int type)
410 av_realloc(octx.prefix, sizeof(PrintElement) * (octx.level + 1));
412 if (!octx.prefix || !name) {
413 fprintf(stderr, "Out of memory\n");
418 PrintElement *parent = octx.prefix + octx.level -1;
419 if (parent->type == ARRAY)
420 count = parent->nb_elems;
424 octx.prefix[octx.level++] = (PrintElement){name, type, count, 0};
427 static void probe_group_leave(void)
432 static void probe_header(void)
434 if (octx.print_header)
436 probe_group_enter("root", OBJECT);
439 static void probe_footer(void)
441 if (octx.print_footer)
447 static void probe_array_header(const char *name, int plain_values)
449 if (octx.print_array_header)
450 octx.print_array_header(name, plain_values);
452 probe_group_enter(name, ARRAY);
455 static void probe_array_footer(const char *name, int plain_values)
458 if (octx.print_array_footer)
459 octx.print_array_footer(name, plain_values);
462 static void probe_object_header(const char *name)
464 if (octx.print_object_header)
465 octx.print_object_header(name);
467 probe_group_enter(name, OBJECT);
470 static void probe_object_footer(const char *name)
473 if (octx.print_object_footer)
474 octx.print_object_footer(name);
477 static void probe_int(const char *key, int64_t value)
479 octx.print_integer(key, value);
480 octx.prefix[octx.level -1].nb_elems++;
483 static void probe_str(const char *key, const char *value)
485 octx.print_string(key, value);
486 octx.prefix[octx.level -1].nb_elems++;
489 static void probe_dict(AVDictionary *dict, const char *name)
491 AVDictionaryEntry *entry = NULL;
494 probe_object_header(name);
495 while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
496 probe_str(entry->key, entry->value);
498 probe_object_footer(name);
501 static char *value_string(char *buf, int buf_size, double val, const char *unit)
503 if (unit == unit_second_str && use_value_sexagesimal_format) {
507 mins = (int)secs / 60;
508 secs = secs - mins * 60;
511 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
512 } else if (use_value_prefix) {
513 const char *prefix_string;
516 if (unit == unit_byte_str && use_byte_value_binary_prefix) {
517 index = (int) log2(val) / 10;
518 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
519 val /= pow(2, index * 10);
520 prefix_string = binary_unit_prefixes[index];
522 index = (int) (log10(val)) / 3;
523 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
524 val /= pow(10, index * 3);
525 prefix_string = decimal_unit_prefixes[index];
527 snprintf(buf, buf_size, "%.*f%s%s",
530 show_value_unit ? unit : "");
532 snprintf(buf, buf_size, "%f%s", val, show_value_unit ? unit : "");
538 static char *time_value_string(char *buf, int buf_size, int64_t val,
539 const AVRational *time_base)
541 if (val == AV_NOPTS_VALUE) {
542 snprintf(buf, buf_size, "N/A");
544 value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
550 static char *ts_value_string(char *buf, int buf_size, int64_t ts)
552 if (ts == AV_NOPTS_VALUE) {
553 snprintf(buf, buf_size, "N/A");
555 snprintf(buf, buf_size, "%"PRId64, ts);
561 static char *rational_string(char *buf, int buf_size, const char *sep,
562 const AVRational *rat)
564 snprintf(buf, buf_size, "%d%s%d", rat->num, sep, rat->den);
568 static char *tag_string(char *buf, int buf_size, int tag)
570 snprintf(buf, buf_size, "0x%04x", tag);
574 static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
577 AVStream *st = fmt_ctx->streams[pkt->stream_index];
579 probe_object_header("packet");
580 probe_str("codec_type", media_type_string(st->codecpar->codec_type));
581 probe_int("stream_index", pkt->stream_index);
582 probe_str("pts", ts_value_string(val_str, sizeof(val_str), pkt->pts));
583 probe_str("pts_time", time_value_string(val_str, sizeof(val_str),
584 pkt->pts, &st->time_base));
585 probe_str("dts", ts_value_string(val_str, sizeof(val_str), pkt->dts));
586 probe_str("dts_time", time_value_string(val_str, sizeof(val_str),
587 pkt->dts, &st->time_base));
588 probe_str("duration", ts_value_string(val_str, sizeof(val_str),
590 probe_str("duration_time", time_value_string(val_str, sizeof(val_str),
593 probe_str("size", value_string(val_str, sizeof(val_str),
594 pkt->size, unit_byte_str));
595 probe_int("pos", pkt->pos);
596 probe_str("flags", pkt->flags & AV_PKT_FLAG_KEY ? "K" : "_");
597 probe_object_footer("packet");
600 static void show_packets(InputFile *ifile)
602 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
605 av_init_packet(&pkt);
606 probe_array_header("packets", 0);
607 while (!av_read_frame(fmt_ctx, &pkt)) {
608 show_packet(fmt_ctx, &pkt);
609 av_packet_unref(&pkt);
611 probe_array_footer("packets", 0);
614 static void show_stream(InputFile *ifile, InputStream *ist)
616 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
617 AVStream *stream = ist->st;
618 AVCodecParameters *par;
619 AVCodecContext *dec_ctx;
620 const AVCodecDescriptor *codec_desc;
623 AVRational display_aspect_ratio, *sar = NULL;
624 const AVPixFmtDescriptor *desc;
626 probe_object_header("stream");
628 probe_int("index", stream->index);
630 par = stream->codecpar;
631 dec_ctx = ist->dec_ctx;
632 codec_desc = avcodec_descriptor_get(par->codec_id);
634 probe_str("codec_name", codec_desc->name);
635 probe_str("codec_long_name", codec_desc->long_name);
637 probe_str("codec_name", "unknown");
640 probe_str("codec_type", media_type_string(par->codec_type));
642 /* print AVI/FourCC tag */
643 av_get_codec_tag_string(val_str, sizeof(val_str), par->codec_tag);
644 probe_str("codec_tag_string", val_str);
645 probe_str("codec_tag", tag_string(val_str, sizeof(val_str),
648 /* print profile, if there is one */
649 profile = avcodec_profile_name(par->codec_id, par->profile);
651 probe_str("profile", profile);
653 switch (par->codec_type) {
654 case AVMEDIA_TYPE_VIDEO:
655 probe_int("width", par->width);
656 probe_int("height", par->height);
658 probe_int("coded_width", dec_ctx->coded_width);
659 probe_int("coded_height", dec_ctx->coded_height);
660 probe_int("has_b_frames", dec_ctx->has_b_frames);
662 if (dec_ctx && dec_ctx->sample_aspect_ratio.num)
663 sar = &dec_ctx->sample_aspect_ratio;
664 else if (par->sample_aspect_ratio.num)
665 sar = &par->sample_aspect_ratio;
666 else if (stream->sample_aspect_ratio.num)
667 sar = &stream->sample_aspect_ratio;
670 probe_str("sample_aspect_ratio",
671 rational_string(val_str, sizeof(val_str), ":", sar));
672 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
673 par->width * sar->num, par->height * sar->den,
675 probe_str("display_aspect_ratio",
676 rational_string(val_str, sizeof(val_str), ":",
677 &display_aspect_ratio));
679 desc = av_pix_fmt_desc_get(par->format);
680 probe_str("pix_fmt", desc ? desc->name : "unknown");
681 probe_int("level", par->level);
683 probe_str("color_range", av_color_range_name (par->color_range));
684 probe_str("color_space", av_color_space_name (par->color_space));
685 probe_str("color_trc", av_color_transfer_name (par->color_trc));
686 probe_str("color_pri", av_color_primaries_name(par->color_primaries));
687 probe_str("chroma_loc", av_chroma_location_name (par->chroma_location));
690 case AVMEDIA_TYPE_AUDIO:
691 probe_str("sample_rate",
692 value_string(val_str, sizeof(val_str),
695 probe_int("channels", par->channels);
696 probe_int("bits_per_sample",
697 av_get_bits_per_sample(par->codec_id));
701 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
702 probe_int("id", stream->id);
703 probe_str("avg_frame_rate",
704 rational_string(val_str, sizeof(val_str), "/",
705 &stream->avg_frame_rate));
708 probe_str("bit_rate",
709 value_string(val_str, sizeof(val_str),
710 par->bit_rate, unit_bit_per_second_str));
711 probe_str("time_base",
712 rational_string(val_str, sizeof(val_str), "/",
713 &stream->time_base));
714 probe_str("start_time",
715 time_value_string(val_str, sizeof(val_str),
716 stream->start_time, &stream->time_base));
717 probe_str("duration",
718 time_value_string(val_str, sizeof(val_str),
719 stream->duration, &stream->time_base));
720 if (stream->nb_frames)
721 probe_int("nb_frames", stream->nb_frames);
723 probe_dict(stream->metadata, "tags");
725 if (stream->nb_side_data) {
728 probe_object_header("sidedata");
729 for (i = 0; i < stream->nb_side_data; i++) {
730 const AVPacketSideData* sd = &stream->side_data[i];
734 case AV_PKT_DATA_DISPLAYMATRIX:
735 probe_object_header("displaymatrix");
736 probe_array_header("matrix", 1);
737 for (j = 0; j < 9; j++)
738 probe_int(NULL, ((int32_t *)sd->data)[j]);
739 probe_array_footer("matrix", 1);
740 probe_int("rotation",
741 av_display_rotation_get((int32_t *)sd->data));
742 probe_object_footer("displaymatrix");
744 case AV_PKT_DATA_STEREO3D:
745 stereo = (AVStereo3D *)sd->data;
746 probe_object_header("stereo3d");
747 probe_str("type", av_stereo3d_type_name(stereo->type));
748 probe_int("inverted",
749 !!(stereo->flags & AV_STEREO3D_FLAG_INVERT));
750 probe_object_footer("stereo3d");
754 probe_object_footer("sidedata");
757 probe_object_footer("stream");
760 static void show_format(InputFile *ifile)
762 AVFormatContext *fmt_ctx = ifile->fmt_ctx;
764 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
766 probe_object_header("format");
767 probe_str("filename", fmt_ctx->filename);
768 probe_int("nb_streams", fmt_ctx->nb_streams);
769 probe_str("format_name", fmt_ctx->iformat->name);
770 probe_str("format_long_name", fmt_ctx->iformat->long_name);
771 probe_str("start_time",
772 time_value_string(val_str, sizeof(val_str),
773 fmt_ctx->start_time, &AV_TIME_BASE_Q));
774 probe_str("duration",
775 time_value_string(val_str, sizeof(val_str),
776 fmt_ctx->duration, &AV_TIME_BASE_Q));
778 size >= 0 ? value_string(val_str, sizeof(val_str),
781 probe_str("bit_rate",
782 value_string(val_str, sizeof(val_str),
783 fmt_ctx->bit_rate, unit_bit_per_second_str));
785 probe_dict(fmt_ctx->metadata, "tags");
787 probe_object_footer("format");
790 static int open_input_file(InputFile *ifile, const char *filename)
793 AVFormatContext *fmt_ctx = NULL;
794 AVDictionaryEntry *t;
796 if ((err = avformat_open_input(&fmt_ctx, filename,
797 iformat, &format_opts)) < 0) {
798 print_error(filename, err);
801 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
802 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
803 return AVERROR_OPTION_NOT_FOUND;
807 /* fill the streams in the format context */
808 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
809 print_error(filename, err);
813 av_dump_format(fmt_ctx, 0, filename, 0);
815 ifile->streams = av_mallocz_array(fmt_ctx->nb_streams,
816 sizeof(*ifile->streams));
819 ifile->nb_streams = fmt_ctx->nb_streams;
821 /* bind a decoder to each input stream */
822 for (i = 0; i < fmt_ctx->nb_streams; i++) {
823 InputStream *ist = &ifile->streams[i];
824 AVStream *stream = fmt_ctx->streams[i];
829 if (stream->codecpar->codec_id == AV_CODEC_ID_PROBE) {
830 fprintf(stderr, "Failed to probe codec for input stream %d\n",
835 codec = avcodec_find_decoder(stream->codecpar->codec_id);
838 "Unsupported codec with id %d for input stream %d\n",
839 stream->codecpar->codec_id, stream->index);
843 ist->dec_ctx = avcodec_alloc_context3(codec);
847 err = avcodec_parameters_to_context(ist->dec_ctx, stream->codecpar);
851 err = avcodec_open2(ist->dec_ctx, NULL, NULL);
853 fprintf(stderr, "Error while opening codec for input stream %d\n",
860 ifile->fmt_ctx = fmt_ctx;
864 static void close_input_file(InputFile *ifile)
868 /* close decoder for each stream */
869 for (i = 0; i < ifile->nb_streams; i++) {
870 InputStream *ist = &ifile->streams[i];
872 avcodec_free_context(&ist->dec_ctx);
875 av_freep(&ifile->streams);
876 ifile->nb_streams = 0;
878 avformat_close_input(&ifile->fmt_ctx);
881 static int probe_file(const char *filename)
886 ret = open_input_file(&ifile, filename);
893 if (do_show_streams) {
894 probe_array_header("streams", 0);
895 for (i = 0; i < ifile.nb_streams; i++)
896 show_stream(&ifile, &ifile.streams[i]);
897 probe_array_footer("streams", 0);
901 show_packets(&ifile);
903 close_input_file(&ifile);
907 static void show_usage(void)
909 printf("Simple multimedia streams analyzer\n");
910 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
914 static int opt_format(void *optctx, const char *opt, const char *arg)
916 iformat = av_find_input_format(arg);
918 fprintf(stderr, "Unknown input format: %s\n", arg);
919 return AVERROR(EINVAL);
924 static int opt_output_format(void *optctx, const char *opt, const char *arg)
927 if (!strcmp(arg, "json")) {
928 octx.print_header = json_print_header;
929 octx.print_footer = json_print_footer;
930 octx.print_array_header = json_print_array_header;
931 octx.print_array_footer = json_print_array_footer;
932 octx.print_object_header = json_print_object_header;
933 octx.print_object_footer = json_print_object_footer;
935 octx.print_integer = json_print_integer;
936 octx.print_string = json_print_string;
937 } else if (!strcmp(arg, "ini")) {
938 octx.print_header = ini_print_header;
939 octx.print_footer = ini_print_footer;
940 octx.print_array_header = ini_print_array_header;
941 octx.print_array_footer = ini_print_array_footer;
942 octx.print_object_header = ini_print_object_header;
944 octx.print_integer = ini_print_integer;
945 octx.print_string = ini_print_string;
946 } else if (!strcmp(arg, "old")) {
947 octx.print_header = NULL;
948 octx.print_object_header = old_print_object_header;
949 octx.print_object_footer = old_print_object_footer;
951 octx.print_string = old_print_string;
953 av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
954 return AVERROR(EINVAL);
959 static int opt_show_format_entry(void *optctx, const char *opt, const char *arg)
962 nb_fmt_entries_to_show++;
963 octx.print_header = NULL;
964 octx.print_footer = NULL;
965 octx.print_array_header = NULL;
966 octx.print_array_footer = NULL;
967 octx.print_object_header = NULL;
968 octx.print_object_footer = NULL;
970 octx.print_integer = show_format_entry_integer;
971 octx.print_string = show_format_entry_string;
972 av_dict_set(&fmt_entries_to_show, arg, "", 0);
976 static void opt_input_file(void *optctx, const char *arg)
978 if (input_filename) {
980 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
981 arg, input_filename);
984 if (!strcmp(arg, "-"))
986 input_filename = arg;
989 void show_help_default(const char *opt, const char *arg)
991 av_log_set_callback(log_callback_help);
993 show_help_options(options, "Main options:", 0, 0, 0);
995 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
998 static int opt_pretty(void *optctx, const char *opt, const char *arg)
1000 show_value_unit = 1;
1001 use_value_prefix = 1;
1002 use_byte_value_binary_prefix = 1;
1003 use_value_sexagesimal_format = 1;
1007 static const OptionDef real_options[] = {
1008 #include "cmdutils_common_opts.h"
1009 { "f", HAS_ARG, {.func_arg = opt_format}, "force format", "format" },
1010 { "of", HAS_ARG, {.func_arg = opt_output_format}, "output the document either as ini or json", "output_format" },
1011 { "unit", OPT_BOOL, {&show_value_unit},
1012 "show unit of the displayed values" },
1013 { "prefix", OPT_BOOL, {&use_value_prefix},
1014 "use SI prefixes for the displayed values" },
1015 { "byte_binary_prefix", OPT_BOOL, {&use_byte_value_binary_prefix},
1016 "use binary prefixes for byte units" },
1017 { "sexagesimal", OPT_BOOL, {&use_value_sexagesimal_format},
1018 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
1019 { "pretty", 0, {.func_arg = opt_pretty},
1020 "prettify the format of displayed values, make it more human readable" },
1021 { "show_format", OPT_BOOL, {&do_show_format} , "show format/container info" },
1022 { "show_format_entry", HAS_ARG, {.func_arg = opt_show_format_entry},
1023 "show a particular entry from the format/container info", "entry" },
1024 { "show_packets", OPT_BOOL, {&do_show_packets}, "show packets info" },
1025 { "show_streams", OPT_BOOL, {&do_show_streams}, "show streams info" },
1026 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {.func_arg = opt_default},
1027 "generic catch all option", "" },
1031 static int probe_buf_write(void *opaque, uint8_t *buf, int buf_size)
1033 printf("%.*s", buf_size, buf);
1037 #define AVP_BUFFSIZE 4096
1039 int main(int argc, char **argv)
1042 uint8_t *buffer = av_malloc(AVP_BUFFSIZE);
1047 register_exit(avprobe_cleanup);
1049 options = real_options;
1050 parse_loglevel(argc, argv, options);
1052 avformat_network_init();
1055 avdevice_register_all();
1060 octx.print_header = ini_print_header;
1061 octx.print_footer = ini_print_footer;
1063 octx.print_array_header = ini_print_array_header;
1064 octx.print_array_footer = ini_print_array_footer;
1065 octx.print_object_header = ini_print_object_header;
1067 octx.print_integer = ini_print_integer;
1068 octx.print_string = ini_print_string;
1070 parse_options(NULL, argc, argv, options, opt_input_file);
1072 if (!input_filename) {
1074 fprintf(stderr, "You have to specify one input file.\n");
1076 "Use -h to get full help or, even better, run 'man %s'.\n",
1081 probe_out = avio_alloc_context(buffer, AVP_BUFFSIZE, 1, NULL, NULL,
1082 probe_buf_write, NULL);
1087 ret = probe_file(input_filename);
1089 avio_flush(probe_out);
1090 av_freep(&probe_out);
1093 avformat_network_deinit();