2 * ffprobe : Simple Media Prober based on the FFmpeg libraries
3 * Copyright (c) 2007-2010 Stefano Sabatini
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/dict.h"
30 #include "libavdevice/avdevice.h"
33 const char program_name[] = "ffprobe";
34 const int program_birth_year = 2007;
36 static int do_show_format = 0;
37 static int do_show_packets = 0;
38 static int do_show_streams = 0;
40 static int show_value_unit = 0;
41 static int use_value_prefix = 0;
42 static int use_byte_value_binary_prefix = 0;
43 static int use_value_sexagesimal_format = 0;
45 static char *print_format;
47 static const OptionDef options[];
50 static const char *input_filename;
51 static AVInputFormat *iformat = NULL;
53 static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
54 static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
56 static const char *unit_second_str = "s" ;
57 static const char *unit_hertz_str = "Hz" ;
58 static const char *unit_byte_str = "byte" ;
59 static const char *unit_bit_per_second_str = "bit/s";
61 void exit_program(int ret)
67 union { double d; int i; } val;
71 static char *value_string(char *buf, int buf_size, struct unit_value uv)
76 if (uv.unit == unit_second_str) {
83 if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
87 mins = (int)secs / 60;
88 secs = secs - mins * 60;
91 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
92 } else if (use_value_prefix) {
93 const char *prefix_string;
96 if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
97 index = (int) (log(vald)/log(2)) / 10;
98 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
99 vald /= pow(2, index*10);
100 prefix_string = binary_unit_prefixes[index];
102 index = (int) (log10(vald)) / 3;
103 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
104 vald /= pow(10, index*3);
105 prefix_string = decimal_unit_prefixes[index];
108 if (show_float || vald != (int)vald) l = snprintf(buf, buf_size, "%.3f", vald);
109 else l = snprintf(buf, buf_size, "%d", (int)vald);
110 snprintf(buf+l, buf_size-l, "%s%s%s", prefix_string || show_value_unit ? " " : "",
111 prefix_string, show_value_unit ? uv.unit : "");
115 if (show_float) l = snprintf(buf, buf_size, "%.3f", vald);
116 else l = snprintf(buf, buf_size, "%d", (int)vald);
117 snprintf(buf+l, buf_size-l, "%s%s", show_value_unit ? " " : "",
118 show_value_unit ? uv.unit : "");
124 static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base)
126 if (val == AV_NOPTS_VALUE) {
127 snprintf(buf, buf_size, "N/A");
129 double d = val * av_q2d(*time_base);
130 value_string(buf, buf_size, (struct unit_value){.val.d=d, .unit=unit_second_str});
136 static char *ts_value_string (char *buf, int buf_size, int64_t ts)
138 if (ts == AV_NOPTS_VALUE) {
139 snprintf(buf, buf_size, "N/A");
141 snprintf(buf, buf_size, "%"PRId64, ts);
149 typedef struct WriterContext WriterContext;
151 typedef struct Writer {
152 int priv_size; ///< private size for the writer context
155 int (*init) (WriterContext *wctx, const char *args, void *opaque);
156 void (*uninit)(WriterContext *wctx);
158 void (*print_header)(WriterContext *ctx);
159 void (*print_footer)(WriterContext *ctx);
161 void (*print_chapter_header)(WriterContext *wctx, const char *);
162 void (*print_chapter_footer)(WriterContext *wctx, const char *);
163 void (*print_section_header)(WriterContext *wctx, const char *);
164 void (*print_section_footer)(WriterContext *wctx, const char *);
165 void (*print_integer) (WriterContext *wctx, const char *, int);
166 void (*print_string) (WriterContext *wctx, const char *, const char *);
167 void (*show_tags) (WriterContext *wctx, AVDictionary *dict);
170 struct WriterContext {
171 const AVClass *class; ///< class of the writer
172 const Writer *writer; ///< the Writer of which this is an instance
173 char *name; ///< name of this writer instance
174 void *priv; ///< private data for use by the filter
175 unsigned int nb_item; ///< number of the item printed in the given section, starting at 0
176 unsigned int nb_section; ///< number of the section printed in the given section sequence, starting at 0
177 unsigned int nb_chapter; ///< number of the chapter, starting at 0
180 static const char *writer_get_name(void *p)
182 WriterContext *wctx = p;
183 return wctx->writer->name;
186 static const AVClass writer_class = {
190 LIBAVUTIL_VERSION_INT,
193 static void writer_close(WriterContext **wctx)
195 if (*wctx && (*wctx)->writer->uninit)
196 (*wctx)->writer->uninit(*wctx);
198 av_freep(&((*wctx)->priv));
202 static int writer_open(WriterContext **wctx, const Writer *writer,
203 const char *args, void *opaque)
207 if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
208 ret = AVERROR(ENOMEM);
212 if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
213 ret = AVERROR(ENOMEM);
217 (*wctx)->class = &writer_class;
218 (*wctx)->writer = writer;
219 if ((*wctx)->writer->init)
220 ret = (*wctx)->writer->init(*wctx, args, opaque);
231 static inline void writer_print_header(WriterContext *wctx)
233 if (wctx->writer->print_header)
234 wctx->writer->print_header(wctx);
235 wctx->nb_chapter = 0;
238 static inline void writer_print_footer(WriterContext *wctx)
240 if (wctx->writer->print_footer)
241 wctx->writer->print_footer(wctx);
244 static inline void writer_print_chapter_header(WriterContext *wctx,
247 if (wctx->writer->print_chapter_header)
248 wctx->writer->print_chapter_header(wctx, header);
249 wctx->nb_section = 0;
252 static inline void writer_print_chapter_footer(WriterContext *wctx,
255 if (wctx->writer->print_chapter_footer)
256 wctx->writer->print_chapter_footer(wctx, footer);
260 static inline void writer_print_section_header(WriterContext *wctx,
263 if (wctx->writer->print_section_header)
264 wctx->writer->print_section_header(wctx, header);
268 static inline void writer_print_section_footer(WriterContext *wctx,
271 if (wctx->writer->print_section_footer)
272 wctx->writer->print_section_footer(wctx, footer);
276 static inline void writer_print_integer(WriterContext *wctx,
277 const char *key, int val)
279 wctx->writer->print_integer(wctx, key, val);
283 static inline void writer_print_string(WriterContext *wctx,
284 const char *key, const char *val)
286 wctx->writer->print_string(wctx, key, val);
290 static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
292 wctx->writer->show_tags(wctx, dict);
295 #define MAX_REGISTERED_WRITERS_NB 64
297 static Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
299 static int writer_register(Writer *writer)
301 static int next_registered_writer_idx = 0;
303 if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
304 return AVERROR(ENOMEM);
306 registered_writers[next_registered_writer_idx++] = writer;
310 static Writer *writer_get_by_name(const char *name)
314 for (i = 0; registered_writers[i]; i++)
315 if (!strcmp(registered_writers[i]->name, name))
316 return registered_writers[i];
328 static char *fast_asprintf(struct print_buf *pbuf, const char *fmt, ...)
334 len = vsnprintf(NULL, 0, fmt, va);
339 if (pbuf->len < len) {
340 char *p = av_realloc(pbuf->s, len + 1);
348 len = vsnprintf(pbuf->s, len + 1, fmt, va);
360 #define ESCAPE_INIT_BUF_SIZE 256
362 #define ESCAPE_CHECK_SIZE(src, size, max_size) \
363 if (size > max_size) { \
365 snprintf(buf, sizeof(buf), "%s", src); \
366 av_log(log_ctx, AV_LOG_WARNING, \
367 "String '%s...' with is too big\n", buf); \
368 return "FFPROBE_TOO_BIG_STRING"; \
371 #define ESCAPE_REALLOC_BUF(dst_size_p, dst_p, src, size) \
372 if (*dst_size_p < size) { \
373 char *q = av_realloc(*dst_p, size); \
376 snprintf(buf, sizeof(buf), "%s", src); \
377 av_log(log_ctx, AV_LOG_WARNING, \
378 "String '%s...' could not be escaped\n", buf); \
379 return "FFPROBE_THIS_STRING_COULD_NOT_BE_ESCAPED"; \
381 *dst_size_p = size; \
389 static void default_print_footer(WriterContext *wctx)
394 static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
396 if (wctx->nb_chapter)
400 /* lame uppercasing routine, assumes the string is lower case ASCII */
401 static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
404 for (i = 0; src[i] && i < dst_size-1; i++)
410 static void default_print_section_header(WriterContext *wctx, const char *section)
414 if (wctx->nb_section)
416 printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
419 static void default_print_section_footer(WriterContext *wctx, const char *section)
423 printf("[/%s]", upcase_string(buf, sizeof(buf), section));
426 static void default_print_str(WriterContext *wctx, const char *key, const char *value)
428 printf("%s=%s\n", key, value);
431 static void default_print_int(WriterContext *wctx, const char *key, int value)
433 printf("%s=%d\n", key, value);
436 static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
438 AVDictionaryEntry *tag = NULL;
439 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
441 writer_print_string(wctx, tag->key, tag->value);
445 static Writer default_writer = {
447 .print_footer = default_print_footer,
448 .print_chapter_header = default_print_chapter_header,
449 .print_section_header = default_print_section_header,
450 .print_section_footer = default_print_section_footer,
451 .print_integer = default_print_int,
452 .print_string = default_print_str,
453 .show_tags = default_show_tags
459 int multiple_entries; ///< tells if the given chapter requires multiple entries
464 static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
466 JSONContext *json = wctx->priv;
468 json->buf_size = ESCAPE_INIT_BUF_SIZE;
469 if (!(json->buf = av_malloc(json->buf_size)))
470 return AVERROR(ENOMEM);
475 static av_cold void json_uninit(WriterContext *wctx)
477 JSONContext *json = wctx->priv;
478 av_freep(&json->buf);
481 static const char *json_escape_str(char **dst, size_t *dst_size, const char *src,
484 static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
485 static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
490 // compute the length of the escaped string
491 for (p = src; *p; p++) {
492 ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-6);
493 if (strchr(json_escape, *p)) size += 2; // simple escape
494 else if ((unsigned char)*p < 32) size += 6; // handle non-printable chars
495 else size += 1; // char copy
497 ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
500 for (p = src; *p; p++) {
501 char *s = strchr(json_escape, *p);
504 *q++ = json_subst[s - json_escape];
505 } else if ((unsigned char)*p < 32) {
506 snprintf(q, 7, "\\u00%02x", *p & 0xff);
516 static void json_print_header(WriterContext *wctx)
521 static void json_print_footer(WriterContext *wctx)
526 static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
528 JSONContext *json = wctx->priv;
530 if (wctx->nb_chapter)
532 json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "streams");
533 printf("\n \"%s\":%s", json_escape_str(&json->buf, &json->buf_size, chapter, wctx),
534 json->multiple_entries ? " [" : " ");
537 static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
539 JSONContext *json = wctx->priv;
541 if (json->multiple_entries)
545 static void json_print_section_header(WriterContext *wctx, const char *section)
547 if (wctx->nb_section) printf(",");
551 static void json_print_section_footer(WriterContext *wctx, const char *section)
556 static inline void json_print_item_str(WriterContext *wctx,
557 const char *key, const char *value,
560 JSONContext *json = wctx->priv;
562 printf("%s\"%s\":", indent, json_escape_str(&json->buf, &json->buf_size, key, wctx));
563 printf(" \"%s\"", json_escape_str(&json->buf, &json->buf_size, value, wctx));
568 static void json_print_str(WriterContext *wctx, const char *key, const char *value)
570 if (wctx->nb_item) printf(",\n");
571 json_print_item_str(wctx, key, value, INDENT);
574 static void json_print_int(WriterContext *wctx, const char *key, int value)
576 JSONContext *json = wctx->priv;
578 if (wctx->nb_item) printf(",\n");
579 printf(INDENT "\"%s\": %d",
580 json_escape_str(&json->buf, &json->buf_size, key, wctx), value);
583 static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
585 AVDictionaryEntry *tag = NULL;
589 printf(",\n" INDENT "\"tags\": {\n");
590 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
591 if (is_first) is_first = 0;
593 json_print_item_str(wctx, tag->key, tag->value, INDENT INDENT);
598 static Writer json_writer = {
600 .priv_size = sizeof(JSONContext),
603 .uninit = json_uninit,
604 .print_header = json_print_header,
605 .print_footer = json_print_footer,
606 .print_chapter_header = json_print_chapter_header,
607 .print_chapter_footer = json_print_chapter_footer,
608 .print_section_header = json_print_section_header,
609 .print_section_footer = json_print_section_footer,
610 .print_integer = json_print_int,
611 .print_string = json_print_str,
612 .show_tags = json_show_tags,
615 static void writer_register_all(void)
617 static int initialized;
623 writer_register(&default_writer);
624 writer_register(&json_writer);
627 #define print_fmt(k, f, ...) do { \
628 if (fast_asprintf(&pbuf, f, __VA_ARGS__)) \
629 writer_print_string(w, k, pbuf.s); \
632 #define print_int(k, v) writer_print_integer(w, k, v)
633 #define print_str(k, v) writer_print_string(w, k, v)
634 #define print_ts(k, v) writer_print_string(w, k, ts_value_string (val_str, sizeof(val_str), v))
635 #define print_time(k, v, tb) writer_print_string(w, k, time_value_string(val_str, sizeof(val_str), v, tb))
636 #define print_val(k, v, u) writer_print_string(w, k, value_string (val_str, sizeof(val_str), \
637 (struct unit_value){.val.i = v, .unit=u}))
638 #define print_section_header(s) writer_print_section_header(w, s)
639 #define print_section_footer(s) writer_print_section_footer(w, s)
640 #define show_tags(metadata) writer_show_tags(w, metadata)
642 static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
645 AVStream *st = fmt_ctx->streams[pkt->stream_index];
646 struct print_buf pbuf = {.s = NULL};
648 print_section_header("packet");
649 print_str("codec_type", av_x_if_null(av_get_media_type_string(st->codec->codec_type), "unknown"));
650 print_int("stream_index", pkt->stream_index);
651 print_ts ("pts", pkt->pts);
652 print_time("pts_time", pkt->pts, &st->time_base);
653 print_ts ("dts", pkt->dts);
654 print_time("dts_time", pkt->dts, &st->time_base);
655 print_ts ("duration", pkt->duration);
656 print_time("duration_time", pkt->duration, &st->time_base);
657 print_val("size", pkt->size, unit_byte_str);
658 print_fmt("pos", "%"PRId64, pkt->pos);
659 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
660 print_section_footer("packet");
666 static void show_packets(WriterContext *w, AVFormatContext *fmt_ctx)
671 av_init_packet(&pkt);
673 while (!av_read_frame(fmt_ctx, &pkt))
674 show_packet(w, fmt_ctx, &pkt, i++);
677 static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
679 AVStream *stream = fmt_ctx->streams[stream_idx];
680 AVCodecContext *dec_ctx;
683 AVRational display_aspect_ratio;
684 struct print_buf pbuf = {.s = NULL};
686 print_section_header("stream");
688 print_int("index", stream->index);
690 if ((dec_ctx = stream->codec)) {
691 if ((dec = dec_ctx->codec)) {
692 print_str("codec_name", dec->name);
693 print_str("codec_long_name", dec->long_name);
695 print_str("codec_name", "unknown");
698 print_str("codec_type", av_x_if_null(av_get_media_type_string(dec_ctx->codec_type), "unknown"));
699 print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
701 /* print AVI/FourCC tag */
702 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
703 print_str("codec_tag_string", val_str);
704 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
706 switch (dec_ctx->codec_type) {
707 case AVMEDIA_TYPE_VIDEO:
708 print_int("width", dec_ctx->width);
709 print_int("height", dec_ctx->height);
710 print_int("has_b_frames", dec_ctx->has_b_frames);
711 if (dec_ctx->sample_aspect_ratio.num) {
712 print_fmt("sample_aspect_ratio", "%d:%d",
713 dec_ctx->sample_aspect_ratio.num,
714 dec_ctx->sample_aspect_ratio.den);
715 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
716 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
717 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
719 print_fmt("display_aspect_ratio", "%d:%d",
720 display_aspect_ratio.num,
721 display_aspect_ratio.den);
723 print_str("pix_fmt", av_x_if_null(av_get_pix_fmt_name(dec_ctx->pix_fmt), "unknown"));
724 print_int("level", dec_ctx->level);
727 case AVMEDIA_TYPE_AUDIO:
728 print_str("sample_fmt",
729 av_x_if_null(av_get_sample_fmt_name(dec_ctx->sample_fmt), "unknown"));
730 print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
731 print_int("channels", dec_ctx->channels);
732 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
736 print_str("codec_type", "unknown");
738 if (dec_ctx->codec && dec_ctx->codec->priv_class) {
739 const AVOption *opt = NULL;
740 while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
742 if (opt->flags) continue;
743 if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
744 print_str(opt->name, str);
750 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
751 print_fmt("id", "0x%x", stream->id);
752 print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
753 print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
754 print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
755 print_time("start_time", stream->start_time, &stream->time_base);
756 print_time("duration", stream->duration, &stream->time_base);
757 if (stream->nb_frames)
758 print_fmt("nb_frames", "%"PRId64, stream->nb_frames);
760 show_tags(stream->metadata);
762 print_section_footer("stream");
767 static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
770 for (i = 0; i < fmt_ctx->nb_streams; i++)
771 show_stream(w, fmt_ctx, i);
774 static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
777 int64_t size = avio_size(fmt_ctx->pb);
778 struct print_buf pbuf = {.s = NULL};
780 print_section_header("format");
781 print_str("filename", fmt_ctx->filename);
782 print_int("nb_streams", fmt_ctx->nb_streams);
783 print_str("format_name", fmt_ctx->iformat->name);
784 print_str("format_long_name", fmt_ctx->iformat->long_name);
785 print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
786 print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
788 print_val("size", size, unit_byte_str);
789 print_val("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
790 show_tags(fmt_ctx->metadata);
791 print_section_footer("format");
796 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
799 AVFormatContext *fmt_ctx = NULL;
800 AVDictionaryEntry *t;
802 if ((err = avformat_open_input(&fmt_ctx, filename, iformat, &format_opts)) < 0) {
803 print_error(filename, err);
806 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
807 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
808 return AVERROR_OPTION_NOT_FOUND;
812 /* fill the streams in the format context */
813 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
814 print_error(filename, err);
818 av_dump_format(fmt_ctx, 0, filename, 0);
820 /* bind a decoder to each input stream */
821 for (i = 0; i < fmt_ctx->nb_streams; i++) {
822 AVStream *stream = fmt_ctx->streams[i];
825 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
826 fprintf(stderr, "Unsupported codec with id %d for input stream %d\n",
827 stream->codec->codec_id, stream->index);
828 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
829 fprintf(stderr, "Error while opening codec for input stream %d\n",
834 *fmt_ctx_ptr = fmt_ctx;
838 #define PRINT_CHAPTER(name) do { \
839 if (do_show_ ## name) { \
840 writer_print_chapter_header(wctx, #name); \
841 show_ ## name (wctx, fmt_ctx); \
842 writer_print_chapter_footer(wctx, #name); \
846 static int probe_file(const char *filename)
848 AVFormatContext *fmt_ctx;
852 char *w_name = NULL, *w_args = NULL;
855 writer_register_all();
858 print_format = av_strdup("default");
859 w_name = av_strtok(print_format, "=", &buf);
862 w = writer_get_by_name(w_name);
864 av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
865 ret = AVERROR(EINVAL);
869 if ((ret = writer_open(&wctx, w, w_args, NULL)) < 0)
871 if ((ret = open_input_file(&fmt_ctx, filename)))
874 writer_print_header(wctx);
875 PRINT_CHAPTER(packets);
876 PRINT_CHAPTER(streams);
877 PRINT_CHAPTER(format);
878 writer_print_footer(wctx);
880 av_close_input_file(fmt_ctx);
884 av_freep(&print_format);
889 static void show_usage(void)
891 printf("Simple multimedia streams analyzer\n");
892 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
896 static int opt_format(const char *opt, const char *arg)
898 iformat = av_find_input_format(arg);
900 fprintf(stderr, "Unknown input format: %s\n", arg);
901 return AVERROR(EINVAL);
906 static void opt_input_file(void *optctx, const char *arg)
908 if (input_filename) {
909 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
910 arg, input_filename);
913 if (!strcmp(arg, "-"))
915 input_filename = arg;
918 static int opt_help(const char *opt, const char *arg)
920 av_log_set_callback(log_callback_help);
922 show_help_options(options, "Main options:\n", 0, 0);
925 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
930 static int opt_pretty(const char *opt, const char *arg)
933 use_value_prefix = 1;
934 use_byte_value_binary_prefix = 1;
935 use_value_sexagesimal_format = 1;
939 static const OptionDef options[] = {
940 #include "cmdutils_common_opts.h"
941 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
942 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
943 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
944 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
945 "use binary prefixes for byte units" },
946 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
947 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
948 { "pretty", 0, {(void*)&opt_pretty},
949 "prettify the format of displayed values, make it more human readable" },
950 { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format}, "set the output printing format (available formats are: default, json)", "format" },
951 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
952 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
953 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
954 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
955 { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
959 int main(int argc, char **argv)
963 parse_loglevel(argc, argv, options);
965 avformat_network_init();
968 avdevice_register_all();
972 parse_options(NULL, argc, argv, options, opt_input_file);
974 if (!input_filename) {
976 fprintf(stderr, "You have to specify one input file.\n");
977 fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
981 ret = probe_file(input_filename);
983 avformat_network_deinit();