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/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/dict.h"
29 #include "libavdevice/avdevice.h"
32 const char program_name[] = "avprobe";
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;
45 static const OptionDef options[];
48 static const char *input_filename;
49 static AVInputFormat *iformat = NULL;
51 static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
52 static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
54 static const char *unit_second_str = "s" ;
55 static const char *unit_hertz_str = "Hz" ;
56 static const char *unit_byte_str = "byte" ;
57 static const char *unit_bit_per_second_str = "bit/s";
59 void exit_program(int ret)
64 static char *value_string(char *buf, int buf_size, double val, const char *unit)
66 if (unit == unit_second_str && use_value_sexagesimal_format) {
70 mins = (int)secs / 60;
71 secs = secs - mins * 60;
74 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
75 } else if (use_value_prefix) {
76 const char *prefix_string;
79 if (unit == unit_byte_str && use_byte_value_binary_prefix) {
80 index = (int) (log(val)/log(2)) / 10;
81 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
82 val /= pow(2, index * 10);
83 prefix_string = binary_unit_prefixes[index];
85 index = (int) (log10(val)) / 3;
86 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
87 val /= pow(10, index * 3);
88 prefix_string = decimal_unit_prefixes[index];
91 snprintf(buf, buf_size, "%.3f %s%s", val, prefix_string,
92 show_value_unit ? unit : "");
94 snprintf(buf, buf_size, "%f %s", val, show_value_unit ? unit : "");
100 static char *time_value_string(char *buf, int buf_size, int64_t val,
101 const AVRational *time_base)
103 if (val == AV_NOPTS_VALUE) {
104 snprintf(buf, buf_size, "N/A");
106 value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
112 static char *ts_value_string (char *buf, int buf_size, int64_t ts)
114 if (ts == AV_NOPTS_VALUE) {
115 snprintf(buf, buf_size, "N/A");
117 snprintf(buf, buf_size, "%"PRId64, ts);
123 static const char *media_type_string(enum AVMediaType media_type)
125 switch (media_type) {
126 case AVMEDIA_TYPE_VIDEO: return "video";
127 case AVMEDIA_TYPE_AUDIO: return "audio";
128 case AVMEDIA_TYPE_DATA: return "data";
129 case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
130 case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
131 default: return "unknown";
135 static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
138 AVStream *st = fmt_ctx->streams[pkt->stream_index];
140 printf("[PACKET]\n");
141 printf("codec_type=%s\n", media_type_string(st->codec->codec_type));
142 printf("stream_index=%d\n", pkt->stream_index);
143 printf("pts=%s\n", ts_value_string(val_str, sizeof(val_str), pkt->pts));
144 printf("pts_time=%s\n", time_value_string(val_str, sizeof(val_str),
145 pkt->pts, &st->time_base));
146 printf("dts=%s\n", ts_value_string(val_str, sizeof(val_str), pkt->dts));
147 printf("dts_time=%s\n", time_value_string(val_str, sizeof(val_str),
148 pkt->dts, &st->time_base));
149 printf("duration=%s\n", ts_value_string(val_str, sizeof(val_str),
151 printf("duration_time=%s\n", time_value_string(val_str, sizeof(val_str),
154 printf("size=%s\n", value_string(val_str, sizeof(val_str),
155 pkt->size, unit_byte_str));
156 printf("pos=%"PRId64"\n", pkt->pos);
157 printf("flags=%c\n", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
158 printf("[/PACKET]\n");
161 static void show_packets(AVFormatContext *fmt_ctx)
165 av_init_packet(&pkt);
167 while (!av_read_frame(fmt_ctx, &pkt))
168 show_packet(fmt_ctx, &pkt);
171 static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
173 AVStream *stream = fmt_ctx->streams[stream_idx];
174 AVCodecContext *dec_ctx;
177 AVDictionaryEntry *tag = NULL;
178 AVRational display_aspect_ratio;
180 printf("[STREAM]\n");
182 printf("index=%d\n", stream->index);
184 if ((dec_ctx = stream->codec)) {
185 if ((dec = dec_ctx->codec)) {
186 printf("codec_name=%s\n", dec->name);
187 printf("codec_long_name=%s\n", dec->long_name);
189 printf("codec_name=unknown\n");
192 printf("codec_type=%s\n", media_type_string(dec_ctx->codec_type));
193 printf("codec_time_base=%d/%d\n",
194 dec_ctx->time_base.num, dec_ctx->time_base.den);
196 /* print AVI/FourCC tag */
197 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
198 printf("codec_tag_string=%s\n", val_str);
199 printf("codec_tag=0x%04x\n", dec_ctx->codec_tag);
201 switch (dec_ctx->codec_type) {
202 case AVMEDIA_TYPE_VIDEO:
203 printf("width=%d\n", dec_ctx->width);
204 printf("height=%d\n", dec_ctx->height);
205 printf("has_b_frames=%d\n", dec_ctx->has_b_frames);
206 if (dec_ctx->sample_aspect_ratio.num) {
207 printf("sample_aspect_ratio=%d:%d\n",
208 dec_ctx->sample_aspect_ratio.num,
209 dec_ctx->sample_aspect_ratio.den);
210 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
211 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
212 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
214 printf("display_aspect_ratio=%d:%d\n",
215 display_aspect_ratio.num, display_aspect_ratio.den);
217 printf("pix_fmt=%s\n",
218 dec_ctx->pix_fmt != PIX_FMT_NONE ? av_pix_fmt_descriptors[dec_ctx->pix_fmt].name
220 printf("level=%d\n", dec_ctx->level);
223 case AVMEDIA_TYPE_AUDIO:
224 printf("sample_rate=%s\n", value_string(val_str, sizeof(val_str),
225 dec_ctx->sample_rate,
227 printf("channels=%d\n", dec_ctx->channels);
228 printf("bits_per_sample=%d\n",
229 av_get_bits_per_sample(dec_ctx->codec_id));
233 printf("codec_type=unknown\n");
236 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
237 printf("id=0x%x\n", stream->id);
238 printf("r_frame_rate=%d/%d\n",
239 stream->r_frame_rate.num, stream->r_frame_rate.den);
240 printf("avg_frame_rate=%d/%d\n",
241 stream->avg_frame_rate.num, stream->avg_frame_rate.den);
242 printf("time_base=%d/%d\n",
243 stream->time_base.num, stream->time_base.den);
244 printf("start_time=%s\n",
245 time_value_string(val_str, sizeof(val_str),
246 stream->start_time, &stream->time_base));
247 printf("duration=%s\n",
248 time_value_string(val_str, sizeof(val_str),
249 stream->duration, &stream->time_base));
250 if (stream->nb_frames)
251 printf("nb_frames=%"PRId64"\n", stream->nb_frames);
253 while ((tag = av_dict_get(stream->metadata, "", tag,
254 AV_DICT_IGNORE_SUFFIX)))
255 printf("TAG:%s=%s\n", tag->key, tag->value);
257 printf("[/STREAM]\n");
260 static void show_format(AVFormatContext *fmt_ctx)
262 AVDictionaryEntry *tag = NULL;
264 int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
266 printf("[FORMAT]\n");
268 printf("filename=%s\n", fmt_ctx->filename);
269 printf("nb_streams=%d\n", fmt_ctx->nb_streams);
270 printf("format_name=%s\n", fmt_ctx->iformat->name);
271 printf("format_long_name=%s\n", fmt_ctx->iformat->long_name);
272 printf("start_time=%s\n",
273 time_value_string(val_str, sizeof(val_str),
274 fmt_ctx->start_time, &AV_TIME_BASE_Q));
275 printf("duration=%s\n",
276 time_value_string(val_str, sizeof(val_str),
277 fmt_ctx->duration, &AV_TIME_BASE_Q));
278 printf("size=%s\n", size >= 0 ? value_string(val_str, sizeof(val_str),
281 printf("bit_rate=%s\n",
282 value_string(val_str, sizeof(val_str),
283 fmt_ctx->bit_rate, unit_bit_per_second_str));
285 while ((tag = av_dict_get(fmt_ctx->metadata, "", tag,
286 AV_DICT_IGNORE_SUFFIX)))
287 printf("TAG:%s=%s\n", tag->key, tag->value);
289 printf("[/FORMAT]\n");
292 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
295 AVFormatContext *fmt_ctx = NULL;
296 AVDictionaryEntry *t;
298 if ((err = avformat_open_input(&fmt_ctx, filename,
299 iformat, &format_opts)) < 0) {
300 print_error(filename, err);
303 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
304 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
305 return AVERROR_OPTION_NOT_FOUND;
309 /* fill the streams in the format context */
310 if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
311 print_error(filename, err);
315 av_dump_format(fmt_ctx, 0, filename, 0);
317 /* bind a decoder to each input stream */
318 for (i = 0; i < fmt_ctx->nb_streams; i++) {
319 AVStream *stream = fmt_ctx->streams[i];
322 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
324 "Unsupported codec with id %d for input stream %d\n",
325 stream->codec->codec_id, stream->index);
326 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
327 fprintf(stderr, "Error while opening codec for input stream %d\n",
332 *fmt_ctx_ptr = fmt_ctx;
336 static int probe_file(const char *filename)
338 AVFormatContext *fmt_ctx;
341 if ((ret = open_input_file(&fmt_ctx, filename)))
345 show_packets(fmt_ctx);
348 for (i = 0; i < fmt_ctx->nb_streams; i++)
349 show_stream(fmt_ctx, i);
352 show_format(fmt_ctx);
354 avformat_close_input(&fmt_ctx);
358 static void show_usage(void)
360 printf("Simple multimedia streams analyzer\n");
361 printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
365 static int opt_format(const char *opt, const char *arg)
367 iformat = av_find_input_format(arg);
369 fprintf(stderr, "Unknown input format: %s\n", arg);
370 return AVERROR(EINVAL);
375 static void opt_input_file(void *optctx, const char *arg)
377 if (input_filename) {
379 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
380 arg, input_filename);
383 if (!strcmp(arg, "-"))
385 input_filename = arg;
388 static void show_help(void)
390 av_log_set_callback(log_callback_help);
392 show_help_options(options, "Main options:\n", 0, 0);
394 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
397 static void opt_pretty(void)
400 use_value_prefix = 1;
401 use_byte_value_binary_prefix = 1;
402 use_value_sexagesimal_format = 1;
405 static const OptionDef options[] = {
406 #include "cmdutils_common_opts.h"
407 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
408 { "unit", OPT_BOOL, {(void*)&show_value_unit},
409 "show unit of the displayed values" },
410 { "prefix", OPT_BOOL, {(void*)&use_value_prefix},
411 "use SI prefixes for the displayed values" },
412 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
413 "use binary prefixes for byte units" },
414 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
415 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
416 { "pretty", 0, {(void*)&opt_pretty},
417 "prettify the format of displayed values, make it more human readable" },
418 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
419 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
420 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
421 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default},
422 "generic catch all option", "" },
426 int main(int argc, char **argv)
430 parse_loglevel(argc, argv, options);
432 avformat_network_init();
435 avdevice_register_all();
439 parse_options(NULL, argc, argv, options, opt_input_file);
441 if (!input_filename) {
443 fprintf(stderr, "You have to specify one input file.\n");
445 "Use -h to get full help or, even better, run 'man %s'.\n",
450 ret = probe_file(input_filename);
452 avformat_network_deinit();