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/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/dict.h"
29 #include "libavdevice/avdevice.h"
32 const char program_name[] = "ffprobe";
33 const int program_birth_year = 2007;
35 static int do_show_format = 0;
36 static int do_show_packets = 0;
37 static int do_show_streams = 0;
39 static int show_value_unit = 0;
40 static int use_value_prefix = 0;
41 static int use_byte_value_binary_prefix = 0;
42 static int use_value_sexagesimal_format = 0;
44 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 static char *value_string(char *buf, int buf_size, double val, const char *unit)
63 if (unit == unit_second_str && use_value_sexagesimal_format) {
67 mins = (int)secs / 60;
68 secs = secs - mins * 60;
71 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
72 } else if (use_value_prefix) {
73 const char *prefix_string;
76 if (unit == unit_byte_str && use_byte_value_binary_prefix) {
77 index = (int) (log(val)/log(2)) / 10;
78 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
79 val /= pow(2, index*10);
80 prefix_string = binary_unit_prefixes[index];
82 index = (int) (log10(val)) / 3;
83 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
84 val /= pow(10, index*3);
85 prefix_string = decimal_unit_prefixes[index];
88 snprintf(buf, buf_size, "%.3f%s%s%s", val, prefix_string || show_value_unit ? " " : "",
89 prefix_string, show_value_unit ? unit : "");
91 snprintf(buf, buf_size, "%f%s%s", val, show_value_unit ? " " : "",
92 show_value_unit ? unit : "");
98 static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base)
100 if (val == AV_NOPTS_VALUE) {
101 snprintf(buf, buf_size, "N/A");
103 value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
109 static char *ts_value_string (char *buf, int buf_size, int64_t ts)
111 if (ts == AV_NOPTS_VALUE) {
112 snprintf(buf, buf_size, "N/A");
114 snprintf(buf, buf_size, "%"PRId64, ts);
120 static const char *media_type_string(enum AVMediaType media_type)
122 const char *s = av_get_media_type_string(media_type);
123 return s ? s : "unknown";
129 const char *item_sep; ///< separator between key/value couples
130 const char *items_sep; ///< separator between sets of key/value couples
131 const char *section_sep; ///< separator between sections (streams, packets, ...)
132 const char *header, *footer;
133 void (*print_header)(const char *);
134 void (*print_footer)(const char *);
135 void (*print_fmt_f)(const char *, const char *, va_list ap);
136 void (*print_int_f)(const char *, int);
137 void (*show_tags)(struct writer *w, AVDictionary *dict);
143 static void default_print_header(const char *section)
145 printf("[%s]\n", section);
148 static void default_print_fmt(const char *key, const char *fmt, va_list ap)
154 static void default_print_int(const char *key, int value)
156 printf("%s=%d", key, value);
159 static void default_print_footer(const char *section)
161 printf("\n[/%s]", section);
167 static void print_vfmt0(struct writer *w, const char *key, const char *fmt, ...)
171 w->print_fmt_f(key, fmt, ap);
175 #define print_fmt0(k, f, a...) print_vfmt0(w, k, f, ##a)
176 #define print_fmt( k, f, a...) do { \
178 printf("%s", w->item_sep); \
179 print_vfmt0(w, k, f, ##a); \
182 #define print_int0(k, v) w->print_int_f(k, v)
183 #define print_int( k, v) do { \
185 printf("%s", w->item_sep); \
189 #define print_str0(k, v) print_fmt0(k, "%s", v)
190 #define print_str( k, v) print_fmt (k, "%s", v)
193 static void show_packet(struct writer *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
196 AVStream *st = fmt_ctx->streams[pkt->stream_index];
199 printf("%s", w->items_sep);
200 w->print_header("PACKET");
201 print_str0("codec_type", media_type_string(st->codec->codec_type));
202 print_int("stream_index", pkt->stream_index);
203 print_str("pts", ts_value_string (val_str, sizeof(val_str), pkt->pts));
204 print_str("pts_time", time_value_string(val_str, sizeof(val_str), pkt->pts, &st->time_base));
205 print_str("dts", ts_value_string (val_str, sizeof(val_str), pkt->dts));
206 print_str("dts_time", time_value_string(val_str, sizeof(val_str), pkt->dts, &st->time_base));
207 print_str("duration", ts_value_string (val_str, sizeof(val_str), pkt->duration));
208 print_str("duration_time", time_value_string(val_str, sizeof(val_str), pkt->duration, &st->time_base));
209 print_str("size", value_string (val_str, sizeof(val_str), pkt->size, unit_byte_str));
210 print_fmt("pos", "%"PRId64, pkt->pos);
211 print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
212 w->print_footer("PACKET");
216 static void show_packets(struct writer *w, AVFormatContext *fmt_ctx)
221 av_init_packet(&pkt);
223 while (!av_read_frame(fmt_ctx, &pkt))
224 show_packet(w, fmt_ctx, &pkt, i++);
227 static void default_show_tags(struct writer *w, AVDictionary *dict)
229 AVDictionaryEntry *tag = NULL;
230 while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
232 print_str0(tag->key, tag->value);
236 static void show_stream(struct writer *w, AVFormatContext *fmt_ctx, int stream_idx)
238 AVStream *stream = fmt_ctx->streams[stream_idx];
239 AVCodecContext *dec_ctx;
242 AVRational display_aspect_ratio;
245 printf("%s", w->items_sep);
246 w->print_header("STREAM");
248 print_int0("index", stream->index);
250 if ((dec_ctx = stream->codec)) {
251 if ((dec = dec_ctx->codec)) {
252 print_str("codec_name", dec->name);
253 print_str("codec_long_name", dec->long_name);
255 print_str("codec_name", "unknown");
258 print_str("codec_type", media_type_string(dec_ctx->codec_type));
259 print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
261 /* print AVI/FourCC tag */
262 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
263 print_str("codec_tag_string", val_str);
264 print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
266 switch (dec_ctx->codec_type) {
267 case AVMEDIA_TYPE_VIDEO:
268 print_int("width", dec_ctx->width);
269 print_int("height", dec_ctx->height);
270 print_int("has_b_frames", dec_ctx->has_b_frames);
271 if (dec_ctx->sample_aspect_ratio.num) {
272 print_fmt("sample_aspect_ratio", "%d:%d",
273 dec_ctx->sample_aspect_ratio.num,
274 dec_ctx->sample_aspect_ratio.den);
275 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
276 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
277 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
279 print_fmt("display_aspect_ratio", "%d:%d",
280 display_aspect_ratio.num,
281 display_aspect_ratio.den);
283 print_str("pix_fmt", dec_ctx->pix_fmt != PIX_FMT_NONE ? av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown");
284 print_int("level", dec_ctx->level);
287 case AVMEDIA_TYPE_AUDIO:
288 print_str("sample_rate", value_string(val_str, sizeof(val_str), dec_ctx->sample_rate, unit_hertz_str));
289 print_int("channels", dec_ctx->channels);
290 print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
294 print_fmt("codec_type", "unknown");
297 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
298 print_fmt("id=", "0x%x", stream->id);
299 print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
300 print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
301 print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
302 print_str("start_time", time_value_string(val_str, sizeof(val_str), stream->start_time, &stream->time_base));
303 print_str("duration", time_value_string(val_str, sizeof(val_str), stream->duration, &stream->time_base));
304 if (stream->nb_frames)
305 print_fmt("nb_frames", "%"PRId64, stream->nb_frames);
307 w->show_tags(w, stream->metadata);
309 w->print_footer("STREAM");
313 static void show_streams(struct writer *w, AVFormatContext *fmt_ctx)
316 for (i = 0; i < fmt_ctx->nb_streams; i++)
317 show_stream(w, fmt_ctx, i);
320 static void show_format(struct writer *w, AVFormatContext *fmt_ctx)
324 w->print_header("FORMAT");
325 print_str0("filename", fmt_ctx->filename);
326 print_int("nb_streams", fmt_ctx->nb_streams);
327 print_str("format_name", fmt_ctx->iformat->name);
328 print_str("format_long_name", fmt_ctx->iformat->long_name);
329 print_str("start_time", time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time, &AV_TIME_BASE_Q));
330 print_str("duration", time_value_string(val_str, sizeof(val_str), fmt_ctx->duration, &AV_TIME_BASE_Q));
331 print_str("size", value_string(val_str, sizeof(val_str), fmt_ctx->file_size, unit_byte_str));
332 print_str("bit_rate", value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate, unit_bit_per_second_str));
333 w->show_tags(w, fmt_ctx->metadata);
334 w->print_footer("FORMAT");
338 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
341 AVFormatContext *fmt_ctx = NULL;
342 AVDictionaryEntry *t;
344 if ((err = avformat_open_input(&fmt_ctx, filename, iformat, &format_opts)) < 0) {
345 print_error(filename, err);
348 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
349 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
350 return AVERROR_OPTION_NOT_FOUND;
354 /* fill the streams in the format context */
355 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
356 print_error(filename, err);
360 av_dump_format(fmt_ctx, 0, filename, 0);
362 /* bind a decoder to each input stream */
363 for (i = 0; i < fmt_ctx->nb_streams; i++) {
364 AVStream *stream = fmt_ctx->streams[i];
367 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
368 fprintf(stderr, "Unsupported codec with id %d for input stream %d\n",
369 stream->codec->codec_id, stream->index);
370 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
371 fprintf(stderr, "Error while opening codec for input stream %d\n",
376 *fmt_ctx_ptr = fmt_ctx;
380 #define WRITER_FUNC(func) \
381 .print_header = func ## _print_header, \
382 .print_footer = func ## _print_footer, \
383 .print_fmt_f = func ## _print_fmt, \
384 .print_int_f = func ## _print_int, \
385 .show_tags = func ## _show_tags
387 static struct writer writers[] = {{
393 WRITER_FUNC(default),
397 static int get_writer(const char *name)
402 for (i = 0; i < FF_ARRAY_ELEMS(writers); i++)
403 if (!strcmp(writers[i].name, name))
408 #define SECTION_PRINT(name, left) do { \
409 if (do_show_ ## name) { \
410 show_ ## name (w, fmt_ctx); \
412 printf("%s", w->section_sep); \
416 static int probe_file(const char *filename)
418 AVFormatContext *fmt_ctx;
422 writer_id = get_writer(print_format);
424 fprintf(stderr, "Invalid output format '%s'\n", print_format);
425 return AVERROR(EINVAL);
427 w = &writers[writer_id];
429 if ((ret = open_input_file(&fmt_ctx, filename)))
433 printf("%s", w->header);
435 SECTION_PRINT(packets, do_show_streams || do_show_format);
436 SECTION_PRINT(streams, do_show_format);
437 SECTION_PRINT(format, 0);
440 printf("%s", w->footer);
442 av_close_input_file(fmt_ctx);
446 static void show_usage(void)
448 printf("Simple multimedia streams analyzer\n");
449 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
453 static int opt_format(const char *opt, const char *arg)
455 iformat = av_find_input_format(arg);
457 fprintf(stderr, "Unknown input format: %s\n", arg);
458 return AVERROR(EINVAL);
463 static int opt_input_file(const char *opt, const char *arg)
465 if (input_filename) {
466 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
467 arg, input_filename);
470 if (!strcmp(arg, "-"))
472 input_filename = arg;
476 static int opt_help(const char *opt, const char *arg)
478 av_log_set_callback(log_callback_help);
480 show_help_options(options, "Main options:\n", 0, 0);
482 av_opt_show2(avformat_opts, NULL,
483 AV_OPT_FLAG_DECODING_PARAM, 0);
487 static int opt_pretty(const char *opt, const char *arg)
490 use_value_prefix = 1;
491 use_byte_value_binary_prefix = 1;
492 use_value_sexagesimal_format = 1;
496 static const OptionDef options[] = {
497 #include "cmdutils_common_opts.h"
498 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
499 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
500 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
501 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
502 "use binary prefixes for byte units" },
503 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
504 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
505 { "pretty", 0, {(void*)&opt_pretty},
506 "prettify the format of displayed values, make it more human readable" },
507 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
508 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
509 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
510 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
511 { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
515 int main(int argc, char **argv)
522 avdevice_register_all();
526 parse_options(argc, argv, options, opt_input_file);
528 if (!input_filename) {
530 fprintf(stderr, "You have to specify one input file.\n");
531 fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
535 ret = probe_file(input_filename);
537 av_free(avformat_opts);