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 printf("sample_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num,
00159 dec_ctx->sample_aspect_ratio.den);
00160 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
00161 dec_ctx->width*dec_ctx->sample_aspect_ratio.num,
00162 dec_ctx->height*dec_ctx->sample_aspect_ratio.den,
00163 1024*1024);
00164 printf("display_aspect_ratio=%d:%d\n", display_aspect_ratio.num,
00165 display_aspect_ratio.den);
00166 printf("pix_fmt=%s\n", dec_ctx->pix_fmt != PIX_FMT_NONE ?
00167 av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown");
00168 break;
00169
00170 case AVMEDIA_TYPE_AUDIO:
00171 printf("sample_rate=%s\n", value_string(val_str, sizeof(val_str),
00172 dec_ctx->sample_rate,
00173 unit_hertz_str));
00174 printf("channels=%d\n", dec_ctx->channels);
00175 printf("bits_per_sample=%d\n", av_get_bits_per_sample(dec_ctx->codec_id));
00176 break;
00177 }
00178 } else {
00179 printf("codec_type=unknown\n");
00180 }
00181
00182 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
00183 printf("id=0x%x\n", stream->id);
00184 printf("r_frame_rate=%d/%d\n", stream->r_frame_rate.num, stream->r_frame_rate.den);
00185 printf("avg_frame_rate=%d/%d\n", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
00186 printf("time_base=%d/%d\n", stream->time_base.num, stream->time_base.den);
00187 if (stream->language[0])
00188 printf("language=%s\n", stream->language);
00189 printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), stream->start_time,
00190 &stream->time_base));
00191 printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), stream->duration,
00192 &stream->time_base));
00193
00194 while ((tag = av_metadata_get(stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
00195 printf("TAG:%s=%s\n", tag->key, tag->value);
00196
00197 printf("[/STREAM]\n");
00198 }
00199
00200 static void show_format(AVFormatContext *fmt_ctx)
00201 {
00202 AVMetadataTag *tag = NULL;
00203 char val_str[128];
00204
00205 printf("[FORMAT]\n");
00206
00207 printf("filename=%s\n", fmt_ctx->filename);
00208 printf("nb_streams=%d\n", fmt_ctx->nb_streams);
00209 printf("format_name=%s\n", fmt_ctx->iformat->name);
00210 printf("format_long_name=%s\n", fmt_ctx->iformat->long_name);
00211 printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time,
00212 &AV_TIME_BASE_Q));
00213 printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->duration,
00214 &AV_TIME_BASE_Q));
00215 printf("size=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->file_size,
00216 unit_byte_str));
00217 printf("bit_rate=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate,
00218 unit_bit_per_second_str));
00219
00220 if (convert_tags)
00221 av_metadata_conv(fmt_ctx, NULL, fmt_ctx->iformat->metadata_conv);
00222 while ((tag = av_metadata_get(fmt_ctx->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
00223 printf("TAG:%s=%s\n", tag->key, tag->value);
00224
00225 printf("[/FORMAT]\n");
00226 }
00227
00228 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
00229 {
00230 int err, i;
00231 AVFormatContext *fmt_ctx;
00232
00233 fmt_ctx = avformat_alloc_context();
00234
00235 if ((err = av_open_input_file(&fmt_ctx, filename, iformat, 0, NULL)) < 0) {
00236 print_error(filename, err);
00237 return err;
00238 }
00239
00240
00241 if ((err = av_find_stream_info(fmt_ctx)) < 0) {
00242 print_error(filename, err);
00243 return err;
00244 }
00245
00246 dump_format(fmt_ctx, 0, filename, 0);
00247
00248
00249 for (i = 0; i < fmt_ctx->nb_streams; i++) {
00250 AVStream *stream = fmt_ctx->streams[i];
00251 AVCodec *codec;
00252
00253 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
00254 fprintf(stderr, "Unsupported codec (id=%d) for input stream %d\n",
00255 stream->codec->codec_id, stream->index);
00256 } else if (avcodec_open(stream->codec, codec) < 0) {
00257 fprintf(stderr, "Error while opening codec for input stream %d\n",
00258 stream->index);
00259 }
00260 }
00261
00262 *fmt_ctx_ptr = fmt_ctx;
00263 return 0;
00264 }
00265
00266 static int probe_file(const char *filename)
00267 {
00268 AVFormatContext *fmt_ctx;
00269 int ret, i;
00270
00271 if ((ret = open_input_file(&fmt_ctx, filename)))
00272 return ret;
00273
00274 if (do_show_streams)
00275 for (i = 0; i < fmt_ctx->nb_streams; i++)
00276 show_stream(fmt_ctx, i);
00277
00278 if (do_show_format)
00279 show_format(fmt_ctx);
00280
00281 av_close_input_file(fmt_ctx);
00282 return 0;
00283 }
00284
00285 static void show_usage(void)
00286 {
00287 printf("Simple multimedia streams analyzer\n");
00288 printf("usage: ffprobe [OPTIONS] [INPUT_FILE]\n");
00289 printf("\n");
00290 }
00291
00292 static void opt_format(const char *arg)
00293 {
00294 iformat = av_find_input_format(arg);
00295 if (!iformat) {
00296 fprintf(stderr, "Unknown input format: %s\n", arg);
00297 exit(1);
00298 }
00299 }
00300
00301 static void opt_input_file(const char *arg)
00302 {
00303 if (input_filename) {
00304 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
00305 arg, input_filename);
00306 exit(1);
00307 }
00308 if (!strcmp(arg, "-"))
00309 arg = "pipe:";
00310 input_filename = arg;
00311 }
00312
00313 static void show_help(void)
00314 {
00315 show_usage();
00316 show_help_options(options, "Main options:\n", 0, 0);
00317 printf("\n");
00318 }
00319
00320 static void opt_pretty(void)
00321 {
00322 show_value_unit = 1;
00323 use_value_prefix = 1;
00324 use_byte_value_binary_prefix = 1;
00325 use_value_sexagesimal_format = 1;
00326 }
00327
00328 static const OptionDef options[] = {
00329 #include "cmdutils_common_opts.h"
00330 { "convert_tags", OPT_BOOL, {(void*)&convert_tags}, "convert tag names to the FFmpeg generic tag names" },
00331 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
00332 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
00333 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
00334 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
00335 "use binary prefixes for byte units" },
00336 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
00337 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
00338 { "pretty", 0, {(void*)&opt_pretty},
00339 "prettify the format of displayed values, make it more human readable" },
00340 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
00341 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
00342 { NULL, },
00343 };
00344
00345 int main(int argc, char **argv)
00346 {
00347 av_register_all();
00348 #if CONFIG_AVDEVICE
00349 avdevice_register_all();
00350 #endif
00351
00352 show_banner();
00353 parse_options(argc, argv, options, opt_input_file);
00354
00355 if (!input_filename) {
00356 show_usage();
00357 fprintf(stderr, "You have to specify one input file.\n");
00358 fprintf(stderr, "Use -h to get full help or, even better, run 'man ffprobe'.\n");
00359 exit(1);
00360 }
00361
00362 return probe_file(input_filename);
00363 }