00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023
00024 #include "libavformat/avformat.h"
00025 #include "libavcodec/avcodec.h"
00026 #include "libavcodec/opt.h"
00027 #include "libavutil/pixdesc.h"
00028 #include "libavdevice/avdevice.h"
00029 #include "cmdutils.h"
00030
00031 const char program_name[] = "FFprobe";
00032 const int program_birth_year = 2007;
00033
00034 static int do_show_format = 0;
00035 static int do_show_streams = 0;
00036
00037 static int convert_tags = 0;
00038 static int show_value_unit = 0;
00039 static int use_value_prefix = 0;
00040 static int use_byte_value_binary_prefix = 0;
00041 static int use_value_sexagesimal_format = 0;
00042
00043
00044 static const OptionDef options[];
00045
00046
00047 static const char *input_filename;
00048 static AVInputFormat *iformat = NULL;
00049
00050 static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
00051 static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
00052
00053 static const char *unit_second_str = "s" ;
00054 static const char *unit_hertz_str = "Hz" ;
00055 static const char *unit_byte_str = "byte" ;
00056 static const char *unit_bit_per_second_str = "bit/s";
00057
00058 static char *value_string(char *buf, int buf_size, double val, const char *unit)
00059 {
00060 if (unit == unit_second_str && use_value_sexagesimal_format) {
00061 double secs;
00062 int hours, mins;
00063 secs = val;
00064 mins = (int)secs / 60;
00065 secs = secs - mins * 60;
00066 hours = mins / 60;
00067 mins %= 60;
00068 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
00069 } else if (use_value_prefix) {
00070 const char *prefix_string;
00071 int index;
00072
00073 if (unit == unit_byte_str && use_byte_value_binary_prefix) {
00074 index = (int) (log(val)/log(2)) / 10;
00075 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
00076 val /= pow(2, index*10);
00077 prefix_string = binary_unit_prefixes[index];
00078 } else {
00079 index = (int) (log10(val)) / 3;
00080 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
00081 val /= pow(10, index*3);
00082 prefix_string = decimal_unit_prefixes[index];
00083 }
00084
00085 snprintf(buf, buf_size, "%.3f %s%s", val, prefix_string, show_value_unit ? unit : "");
00086 } else {
00087 snprintf(buf, buf_size, "%f %s", val, show_value_unit ? unit : "");
00088 }
00089
00090 return buf;
00091 }
00092
00093 static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base)
00094 {
00095 if (val == AV_NOPTS_VALUE) {
00096 snprintf(buf, buf_size, "N/A");
00097 } else {
00098 value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
00099 }
00100
00101 return buf;
00102 }
00103
00104 static const char *media_type_string(enum AVMediaType media_type)
00105 {
00106 switch (media_type) {
00107 case AVMEDIA_TYPE_VIDEO: return "video";
00108 case AVMEDIA_TYPE_AUDIO: return "audio";
00109 case AVMEDIA_TYPE_DATA: return "data";
00110 case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
00111 case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
00112 default: return "unknown";
00113 }
00114 }
00115
00116 static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
00117 {
00118 AVStream *stream = fmt_ctx->streams[stream_idx];
00119 AVCodecContext *dec_ctx;
00120 AVCodec *dec;
00121 char val_str[128];
00122 AVMetadataTag *tag = NULL;
00123 char a, b, c, d;
00124 AVRational display_aspect_ratio;
00125
00126 printf("[STREAM]\n");
00127
00128 printf("index=%d\n", stream->index);
00129
00130 if ((dec_ctx = stream->codec)) {
00131 if ((dec = dec_ctx->codec)) {
00132 printf("codec_name=%s\n", dec->name);
00133 printf("codec_long_name=%s\n", dec->long_name);
00134 } else {
00135 printf("codec_name=unknown\n");
00136 }
00137
00138 printf("codec_type=%s\n", media_type_string(dec_ctx->codec_type));
00139 printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den);
00140
00141
00142 a = dec_ctx->codec_tag & 0xff;
00143 b = dec_ctx->codec_tag>>8 & 0xff;
00144 c = dec_ctx->codec_tag>>16 & 0xff;
00145 d = dec_ctx->codec_tag>>24 & 0xff;
00146 printf("codec_tag_string=");
00147 if (isprint(a)) printf("%c", a); else printf("[%d]", a);
00148 if (isprint(b)) printf("%c", b); else printf("[%d]", b);
00149 if (isprint(c)) printf("%c", c); else printf("[%d]", c);
00150 if (isprint(d)) printf("%c", d); else printf("[%d]", d);
00151 printf("\ncodec_tag=0x%04x\n", dec_ctx->codec_tag);
00152
00153 switch (dec_ctx->codec_type) {
00154 case AVMEDIA_TYPE_VIDEO:
00155 printf("width=%d\n", dec_ctx->width);
00156 printf("height=%d\n", dec_ctx->height);
00157 printf("has_b_frames=%d\n", dec_ctx->has_b_frames);
00158 if (dec_ctx->sample_aspect_ratio.num) {
00159 printf("sample_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num,
00160 dec_ctx->sample_aspect_ratio.den);
00161 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
00162 dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
00163 dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
00164 1024*1024);
00165 printf("display_aspect_ratio=%d:%d\n", display_aspect_ratio.num,
00166 display_aspect_ratio.den);
00167 }
00168 printf("pix_fmt=%s\n", dec_ctx->pix_fmt != PIX_FMT_NONE ?
00169 av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown");
00170 break;
00171
00172 case AVMEDIA_TYPE_AUDIO:
00173 printf("sample_rate=%s\n", value_string(val_str, sizeof(val_str),
00174 dec_ctx->sample_rate,
00175 unit_hertz_str));
00176 printf("channels=%d\n", dec_ctx->channels);
00177 printf("bits_per_sample=%d\n", av_get_bits_per_sample(dec_ctx->codec_id));
00178 break;
00179 }
00180 } else {
00181 printf("codec_type=unknown\n");
00182 }
00183
00184 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
00185 printf("id=0x%x\n", stream->id);
00186 printf("r_frame_rate=%d/%d\n", stream->r_frame_rate.num, stream->r_frame_rate.den);
00187 printf("avg_frame_rate=%d/%d\n", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
00188 printf("time_base=%d/%d\n", stream->time_base.num, stream->time_base.den);
00189 if (stream->language[0])
00190 printf("language=%s\n", stream->language);
00191 printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), stream->start_time,
00192 &stream->time_base));
00193 printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), stream->duration,
00194 &stream->time_base));
00195 if (stream->nb_frames)
00196 printf("nb_frames=%"PRId64"\n", stream->nb_frames);
00197
00198 while ((tag = av_metadata_get(stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
00199 printf("TAG:%s=%s\n", tag->key, tag->value);
00200
00201 printf("[/STREAM]\n");
00202 }
00203
00204 static void show_format(AVFormatContext *fmt_ctx)
00205 {
00206 AVMetadataTag *tag = NULL;
00207 char val_str[128];
00208
00209 printf("[FORMAT]\n");
00210
00211 printf("filename=%s\n", fmt_ctx->filename);
00212 printf("nb_streams=%d\n", fmt_ctx->nb_streams);
00213 printf("format_name=%s\n", fmt_ctx->iformat->name);
00214 printf("format_long_name=%s\n", fmt_ctx->iformat->long_name);
00215 printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time,
00216 &AV_TIME_BASE_Q));
00217 printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->duration,
00218 &AV_TIME_BASE_Q));
00219 printf("size=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->file_size,
00220 unit_byte_str));
00221 printf("bit_rate=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate,
00222 unit_bit_per_second_str));
00223
00224 if (convert_tags)
00225 av_metadata_conv(fmt_ctx, NULL, fmt_ctx->iformat->metadata_conv);
00226 while ((tag = av_metadata_get(fmt_ctx->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
00227 printf("TAG:%s=%s\n", tag->key, tag->value);
00228
00229 printf("[/FORMAT]\n");
00230 }
00231
00232 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
00233 {
00234 int err, i;
00235 AVFormatContext *fmt_ctx;
00236
00237 fmt_ctx = avformat_alloc_context();
00238
00239 if ((err = av_open_input_file(&fmt_ctx, filename, iformat, 0, NULL)) < 0) {
00240 print_error(filename, err);
00241 return err;
00242 }
00243
00244
00245 if ((err = av_find_stream_info(fmt_ctx)) < 0) {
00246 print_error(filename, err);
00247 return err;
00248 }
00249
00250 dump_format(fmt_ctx, 0, filename, 0);
00251
00252
00253 for (i = 0; i < fmt_ctx->nb_streams; i++) {
00254 AVStream *stream = fmt_ctx->streams[i];
00255 AVCodec *codec;
00256
00257 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
00258 fprintf(stderr, "Unsupported codec (id=%d) for input stream %d\n",
00259 stream->codec->codec_id, stream->index);
00260 } else if (avcodec_open(stream->codec, codec) < 0) {
00261 fprintf(stderr, "Error while opening codec for input stream %d\n",
00262 stream->index);
00263 }
00264 }
00265
00266 *fmt_ctx_ptr = fmt_ctx;
00267 return 0;
00268 }
00269
00270 static int probe_file(const char *filename)
00271 {
00272 AVFormatContext *fmt_ctx;
00273 int ret, i;
00274
00275 if ((ret = open_input_file(&fmt_ctx, filename)))
00276 return ret;
00277
00278 if (do_show_streams)
00279 for (i = 0; i < fmt_ctx->nb_streams; i++)
00280 show_stream(fmt_ctx, i);
00281
00282 if (do_show_format)
00283 show_format(fmt_ctx);
00284
00285 av_close_input_file(fmt_ctx);
00286 return 0;
00287 }
00288
00289 static void show_usage(void)
00290 {
00291 printf("Simple multimedia streams analyzer\n");
00292 printf("usage: ffprobe [OPTIONS] [INPUT_FILE]\n");
00293 printf("\n");
00294 }
00295
00296 static void opt_format(const char *arg)
00297 {
00298 iformat = av_find_input_format(arg);
00299 if (!iformat) {
00300 fprintf(stderr, "Unknown input format: %s\n", arg);
00301 exit(1);
00302 }
00303 }
00304
00305 static void opt_input_file(const char *arg)
00306 {
00307 if (input_filename) {
00308 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
00309 arg, input_filename);
00310 exit(1);
00311 }
00312 if (!strcmp(arg, "-"))
00313 arg = "pipe:";
00314 input_filename = arg;
00315 }
00316
00317 static void show_help(void)
00318 {
00319 show_usage();
00320 show_help_options(options, "Main options:\n", 0, 0);
00321 printf("\n");
00322 }
00323
00324 static void opt_pretty(void)
00325 {
00326 show_value_unit = 1;
00327 use_value_prefix = 1;
00328 use_byte_value_binary_prefix = 1;
00329 use_value_sexagesimal_format = 1;
00330 }
00331
00332 static const OptionDef options[] = {
00333 #include "cmdutils_common_opts.h"
00334 { "convert_tags", OPT_BOOL, {(void*)&convert_tags}, "convert tag names to the FFmpeg generic tag names" },
00335 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
00336 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
00337 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
00338 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
00339 "use binary prefixes for byte units" },
00340 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
00341 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
00342 { "pretty", 0, {(void*)&opt_pretty},
00343 "prettify the format of displayed values, make it more human readable" },
00344 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
00345 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
00346 { NULL, },
00347 };
00348
00349 int main(int argc, char **argv)
00350 {
00351 av_register_all();
00352 #if CONFIG_AVDEVICE
00353 avdevice_register_all();
00354 #endif
00355
00356 show_banner();
00357 parse_options(argc, argv, options, opt_input_file);
00358
00359 if (!input_filename) {
00360 show_usage();
00361 fprintf(stderr, "You have to specify one input file.\n");
00362 fprintf(stderr, "Use -h to get full help or, even better, run 'man ffprobe'.\n");
00363 exit(1);
00364 }
00365
00366 return probe_file(input_filename);
00367 }