Libav 0.7.1
|
00001 /* 00002 * ffprobe : Simple Media Prober based on the Libav libraries 00003 * Copyright (c) 2007-2010 Stefano Sabatini 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "config.h" 00023 00024 #include "libavformat/avformat.h" 00025 #include "libavcodec/avcodec.h" 00026 #include "libavutil/opt.h" 00027 #include "libavutil/pixdesc.h" 00028 #include "libavutil/dict.h" 00029 #include "libavdevice/avdevice.h" 00030 #include "cmdutils.h" 00031 00032 const char program_name[] = "ffprobe"; 00033 const int program_birth_year = 2007; 00034 00035 static int do_show_format = 0; 00036 static int do_show_packets = 0; 00037 static int do_show_streams = 0; 00038 00039 static int show_value_unit = 0; 00040 static int use_value_prefix = 0; 00041 static int use_byte_value_binary_prefix = 0; 00042 static int use_value_sexagesimal_format = 0; 00043 00044 /* globals */ 00045 static const OptionDef options[]; 00046 00047 /* FFprobe context */ 00048 static const char *input_filename; 00049 static AVInputFormat *iformat = NULL; 00050 00051 static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" }; 00052 static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" }; 00053 00054 static const char *unit_second_str = "s" ; 00055 static const char *unit_hertz_str = "Hz" ; 00056 static const char *unit_byte_str = "byte" ; 00057 static const char *unit_bit_per_second_str = "bit/s"; 00058 00059 static char *value_string(char *buf, int buf_size, double val, const char *unit) 00060 { 00061 if (unit == unit_second_str && use_value_sexagesimal_format) { 00062 double secs; 00063 int hours, mins; 00064 secs = val; 00065 mins = (int)secs / 60; 00066 secs = secs - mins * 60; 00067 hours = mins / 60; 00068 mins %= 60; 00069 snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs); 00070 } else if (use_value_prefix) { 00071 const char *prefix_string; 00072 int index; 00073 00074 if (unit == unit_byte_str && use_byte_value_binary_prefix) { 00075 index = (int) (log(val)/log(2)) / 10; 00076 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1); 00077 val /= pow(2, index*10); 00078 prefix_string = binary_unit_prefixes[index]; 00079 } else { 00080 index = (int) (log10(val)) / 3; 00081 index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1); 00082 val /= pow(10, index*3); 00083 prefix_string = decimal_unit_prefixes[index]; 00084 } 00085 00086 snprintf(buf, buf_size, "%.3f %s%s", val, prefix_string, show_value_unit ? unit : ""); 00087 } else { 00088 snprintf(buf, buf_size, "%f %s", val, show_value_unit ? unit : ""); 00089 } 00090 00091 return buf; 00092 } 00093 00094 static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base) 00095 { 00096 if (val == AV_NOPTS_VALUE) { 00097 snprintf(buf, buf_size, "N/A"); 00098 } else { 00099 value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str); 00100 } 00101 00102 return buf; 00103 } 00104 00105 static char *ts_value_string (char *buf, int buf_size, int64_t ts) 00106 { 00107 if (ts == AV_NOPTS_VALUE) { 00108 snprintf(buf, buf_size, "N/A"); 00109 } else { 00110 snprintf(buf, buf_size, "%"PRId64, ts); 00111 } 00112 00113 return buf; 00114 } 00115 00116 static const char *media_type_string(enum AVMediaType media_type) 00117 { 00118 switch (media_type) { 00119 case AVMEDIA_TYPE_VIDEO: return "video"; 00120 case AVMEDIA_TYPE_AUDIO: return "audio"; 00121 case AVMEDIA_TYPE_DATA: return "data"; 00122 case AVMEDIA_TYPE_SUBTITLE: return "subtitle"; 00123 case AVMEDIA_TYPE_ATTACHMENT: return "attachment"; 00124 default: return "unknown"; 00125 } 00126 } 00127 00128 static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt) 00129 { 00130 char val_str[128]; 00131 AVStream *st = fmt_ctx->streams[pkt->stream_index]; 00132 00133 printf("[PACKET]\n"); 00134 printf("codec_type=%s\n" , media_type_string(st->codec->codec_type)); 00135 printf("stream_index=%d\n" , pkt->stream_index); 00136 printf("pts=%s\n" , ts_value_string (val_str, sizeof(val_str), pkt->pts)); 00137 printf("pts_time=%s\n" , time_value_string(val_str, sizeof(val_str), pkt->pts, &st->time_base)); 00138 printf("dts=%s\n" , ts_value_string (val_str, sizeof(val_str), pkt->dts)); 00139 printf("dts_time=%s\n" , time_value_string(val_str, sizeof(val_str), pkt->dts, &st->time_base)); 00140 printf("duration=%s\n" , ts_value_string (val_str, sizeof(val_str), pkt->duration)); 00141 printf("duration_time=%s\n", time_value_string(val_str, sizeof(val_str), pkt->duration, &st->time_base)); 00142 printf("size=%s\n" , value_string (val_str, sizeof(val_str), pkt->size, unit_byte_str)); 00143 printf("pos=%"PRId64"\n" , pkt->pos); 00144 printf("flags=%c\n" , pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_'); 00145 printf("[/PACKET]\n"); 00146 } 00147 00148 static void show_packets(AVFormatContext *fmt_ctx) 00149 { 00150 AVPacket pkt; 00151 00152 av_init_packet(&pkt); 00153 00154 while (!av_read_frame(fmt_ctx, &pkt)) 00155 show_packet(fmt_ctx, &pkt); 00156 } 00157 00158 static void show_stream(AVFormatContext *fmt_ctx, int stream_idx) 00159 { 00160 AVStream *stream = fmt_ctx->streams[stream_idx]; 00161 AVCodecContext *dec_ctx; 00162 AVCodec *dec; 00163 char val_str[128]; 00164 AVDictionaryEntry *tag = NULL; 00165 AVRational display_aspect_ratio; 00166 00167 printf("[STREAM]\n"); 00168 00169 printf("index=%d\n", stream->index); 00170 00171 if ((dec_ctx = stream->codec)) { 00172 if ((dec = dec_ctx->codec)) { 00173 printf("codec_name=%s\n", dec->name); 00174 printf("codec_long_name=%s\n", dec->long_name); 00175 } else { 00176 printf("codec_name=unknown\n"); 00177 } 00178 00179 printf("codec_type=%s\n", media_type_string(dec_ctx->codec_type)); 00180 printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den); 00181 00182 /* print AVI/FourCC tag */ 00183 av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag); 00184 printf("codec_tag_string=%s\n", val_str); 00185 printf("codec_tag=0x%04x\n", dec_ctx->codec_tag); 00186 00187 switch (dec_ctx->codec_type) { 00188 case AVMEDIA_TYPE_VIDEO: 00189 printf("width=%d\n", dec_ctx->width); 00190 printf("height=%d\n", dec_ctx->height); 00191 printf("has_b_frames=%d\n", dec_ctx->has_b_frames); 00192 if (dec_ctx->sample_aspect_ratio.num) { 00193 printf("sample_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num, 00194 dec_ctx->sample_aspect_ratio.den); 00195 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, 00196 dec_ctx->width * dec_ctx->sample_aspect_ratio.num, 00197 dec_ctx->height * dec_ctx->sample_aspect_ratio.den, 00198 1024*1024); 00199 printf("display_aspect_ratio=%d:%d\n", display_aspect_ratio.num, 00200 display_aspect_ratio.den); 00201 } 00202 printf("pix_fmt=%s\n", dec_ctx->pix_fmt != PIX_FMT_NONE ? 00203 av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown"); 00204 break; 00205 00206 case AVMEDIA_TYPE_AUDIO: 00207 printf("sample_rate=%s\n", value_string(val_str, sizeof(val_str), 00208 dec_ctx->sample_rate, 00209 unit_hertz_str)); 00210 printf("channels=%d\n", dec_ctx->channels); 00211 printf("bits_per_sample=%d\n", av_get_bits_per_sample(dec_ctx->codec_id)); 00212 break; 00213 } 00214 } else { 00215 printf("codec_type=unknown\n"); 00216 } 00217 00218 if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) 00219 printf("id=0x%x\n", stream->id); 00220 printf("r_frame_rate=%d/%d\n", stream->r_frame_rate.num, stream->r_frame_rate.den); 00221 printf("avg_frame_rate=%d/%d\n", stream->avg_frame_rate.num, stream->avg_frame_rate.den); 00222 printf("time_base=%d/%d\n", stream->time_base.num, stream->time_base.den); 00223 printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), stream->start_time, 00224 &stream->time_base)); 00225 printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), stream->duration, 00226 &stream->time_base)); 00227 if (stream->nb_frames) 00228 printf("nb_frames=%"PRId64"\n", stream->nb_frames); 00229 00230 while ((tag = av_dict_get(stream->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) 00231 printf("TAG:%s=%s\n", tag->key, tag->value); 00232 00233 printf("[/STREAM]\n"); 00234 } 00235 00236 static void show_format(AVFormatContext *fmt_ctx) 00237 { 00238 AVDictionaryEntry *tag = NULL; 00239 char val_str[128]; 00240 00241 printf("[FORMAT]\n"); 00242 00243 printf("filename=%s\n", fmt_ctx->filename); 00244 printf("nb_streams=%d\n", fmt_ctx->nb_streams); 00245 printf("format_name=%s\n", fmt_ctx->iformat->name); 00246 printf("format_long_name=%s\n", fmt_ctx->iformat->long_name); 00247 printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time, 00248 &AV_TIME_BASE_Q)); 00249 printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->duration, 00250 &AV_TIME_BASE_Q)); 00251 printf("size=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->file_size, 00252 unit_byte_str)); 00253 printf("bit_rate=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate, 00254 unit_bit_per_second_str)); 00255 00256 while ((tag = av_dict_get(fmt_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) 00257 printf("TAG:%s=%s\n", tag->key, tag->value); 00258 00259 printf("[/FORMAT]\n"); 00260 } 00261 00262 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename) 00263 { 00264 int err, i; 00265 AVFormatContext *fmt_ctx = NULL; 00266 AVDictionaryEntry *t; 00267 00268 if ((err = avformat_open_input(&fmt_ctx, filename, iformat, &format_opts)) < 0) { 00269 print_error(filename, err); 00270 return err; 00271 } 00272 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) { 00273 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key); 00274 return AVERROR_OPTION_NOT_FOUND; 00275 } 00276 00277 00278 /* fill the streams in the format context */ 00279 if ((err = av_find_stream_info(fmt_ctx)) < 0) { 00280 print_error(filename, err); 00281 return err; 00282 } 00283 00284 av_dump_format(fmt_ctx, 0, filename, 0); 00285 00286 /* bind a decoder to each input stream */ 00287 for (i = 0; i < fmt_ctx->nb_streams; i++) { 00288 AVStream *stream = fmt_ctx->streams[i]; 00289 AVCodec *codec; 00290 00291 if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) { 00292 fprintf(stderr, "Unsupported codec with id %d for input stream %d\n", 00293 stream->codec->codec_id, stream->index); 00294 } else if (avcodec_open2(stream->codec, codec, NULL) < 0) { 00295 fprintf(stderr, "Error while opening codec for input stream %d\n", 00296 stream->index); 00297 } 00298 } 00299 00300 *fmt_ctx_ptr = fmt_ctx; 00301 return 0; 00302 } 00303 00304 static int probe_file(const char *filename) 00305 { 00306 AVFormatContext *fmt_ctx; 00307 int ret, i; 00308 00309 if ((ret = open_input_file(&fmt_ctx, filename))) 00310 return ret; 00311 00312 if (do_show_packets) 00313 show_packets(fmt_ctx); 00314 00315 if (do_show_streams) 00316 for (i = 0; i < fmt_ctx->nb_streams; i++) 00317 show_stream(fmt_ctx, i); 00318 00319 if (do_show_format) 00320 show_format(fmt_ctx); 00321 00322 av_close_input_file(fmt_ctx); 00323 return 0; 00324 } 00325 00326 static void show_usage(void) 00327 { 00328 printf("Simple multimedia streams analyzer\n"); 00329 printf("usage: ffprobe [OPTIONS] [INPUT_FILE]\n"); 00330 printf("\n"); 00331 } 00332 00333 static int opt_format(const char *opt, const char *arg) 00334 { 00335 iformat = av_find_input_format(arg); 00336 if (!iformat) { 00337 fprintf(stderr, "Unknown input format: %s\n", arg); 00338 return AVERROR(EINVAL); 00339 } 00340 return 0; 00341 } 00342 00343 static void opt_input_file(const char *arg) 00344 { 00345 if (input_filename) { 00346 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n", 00347 arg, input_filename); 00348 exit(1); 00349 } 00350 if (!strcmp(arg, "-")) 00351 arg = "pipe:"; 00352 input_filename = arg; 00353 } 00354 00355 static void show_help(void) 00356 { 00357 av_log_set_callback(log_callback_help); 00358 show_usage(); 00359 show_help_options(options, "Main options:\n", 0, 0); 00360 printf("\n"); 00361 av_opt_show2(avformat_opts, NULL, 00362 AV_OPT_FLAG_DECODING_PARAM, 0); 00363 } 00364 00365 static void opt_pretty(void) 00366 { 00367 show_value_unit = 1; 00368 use_value_prefix = 1; 00369 use_byte_value_binary_prefix = 1; 00370 use_value_sexagesimal_format = 1; 00371 } 00372 00373 static const OptionDef options[] = { 00374 #include "cmdutils_common_opts.h" 00375 { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" }, 00376 { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" }, 00377 { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" }, 00378 { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix}, 00379 "use binary prefixes for byte units" }, 00380 { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format}, 00381 "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" }, 00382 { "pretty", 0, {(void*)&opt_pretty}, 00383 "prettify the format of displayed values, make it more human readable" }, 00384 { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" }, 00385 { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" }, 00386 { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" }, 00387 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" }, 00388 { NULL, }, 00389 }; 00390 00391 int main(int argc, char **argv) 00392 { 00393 int ret; 00394 00395 av_register_all(); 00396 init_opts(); 00397 #if CONFIG_AVDEVICE 00398 avdevice_register_all(); 00399 #endif 00400 00401 avformat_opts = avformat_alloc_context(); 00402 00403 show_banner(); 00404 parse_options(argc, argv, options, opt_input_file); 00405 00406 if (!input_filename) { 00407 show_usage(); 00408 fprintf(stderr, "You have to specify one input file.\n"); 00409 fprintf(stderr, "Use -h to get full help or, even better, run 'man ffprobe'.\n"); 00410 exit(1); 00411 } 00412 00413 ret = probe_file(input_filename); 00414 00415 av_free(avformat_opts); 00416 00417 return ret; 00418 }