ffprobe.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * FFprobe : Simple Media Prober based on the FFmpeg libraries
  3. * Copyright (c) 2007-2010 Stefano Sabatini
  4. *
  5. * This file is part of FFmpeg.
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #undef HAVE_AV_CONFIG_H
  22. #include "libavformat/avformat.h"
  23. #include "libavcodec/avcodec.h"
  24. #include "libavcodec/opt.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "cmdutils.h"
  27. const char program_name[] = "FFprobe";
  28. const int program_birth_year = 2007;
  29. static int do_show_format = 0;
  30. static int do_show_streams = 0;
  31. static int show_value_unit = 0;
  32. static int use_value_prefix = 0;
  33. static int use_byte_value_binary_prefix = 0;
  34. static int use_value_sexagesimal_format = 0;
  35. /* globals */
  36. static const OptionDef options[];
  37. /* FFprobe context */
  38. static const char *input_filename;
  39. static AVInputFormat *iformat = NULL;
  40. static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
  41. static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
  42. static const char *unit_second_str = "s" ;
  43. static const char *unit_hertz_str = "Hz" ;
  44. static const char *unit_byte_str = "byte" ;
  45. static const char *unit_bit_per_second_str = "bit/s";
  46. static char *value_string(char *buf, int buf_size, double val, const char *unit)
  47. {
  48. if (unit == unit_second_str && use_value_sexagesimal_format) {
  49. double secs;
  50. int hours, mins;
  51. secs = val;
  52. mins = (int)secs / 60;
  53. secs = secs - mins * 60;
  54. hours = mins / 60;
  55. mins %= 60;
  56. snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
  57. } else if (use_value_prefix) {
  58. const char *prefix_string;
  59. int index;
  60. if (unit == unit_byte_str && use_byte_value_binary_prefix) {
  61. index = (int) (log(val)/log(2)) / 10;
  62. index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
  63. val /= pow(2, index*10);
  64. prefix_string = binary_unit_prefixes[index];
  65. } else {
  66. index = (int) (log10(val)) / 3;
  67. index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
  68. val /= pow(10, index*3);
  69. prefix_string = decimal_unit_prefixes[index];
  70. }
  71. snprintf(buf, buf_size, "%.3f %s%s", val, prefix_string, show_value_unit ? unit : "");
  72. } else {
  73. snprintf(buf, buf_size, "%f %s", val, show_value_unit ? unit : "");
  74. }
  75. return buf;
  76. }
  77. static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base)
  78. {
  79. if (val == AV_NOPTS_VALUE) {
  80. snprintf(buf, buf_size, "N/A");
  81. } else {
  82. value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
  83. }
  84. return buf;
  85. }
  86. static const char *codec_type_string(enum CodecType codec_type)
  87. {
  88. switch (codec_type) {
  89. case CODEC_TYPE_VIDEO: return "video";
  90. case CODEC_TYPE_AUDIO: return "audio";
  91. case CODEC_TYPE_DATA: return "data";
  92. case CODEC_TYPE_SUBTITLE: return "subtitle";
  93. case CODEC_TYPE_ATTACHMENT: return "attachment";
  94. default: return "unknown";
  95. }
  96. }
  97. static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
  98. {
  99. AVStream *stream = fmt_ctx->streams[stream_idx];
  100. AVCodecContext *dec_ctx;
  101. AVCodec *dec;
  102. char val_str[128];
  103. AVMetadataTag *tag;
  104. char a, b, c, d;
  105. printf("[STREAM]\n");
  106. printf("index=%d\n", stream->index);
  107. if ((dec_ctx = stream->codec)) {
  108. if ((dec = dec_ctx->codec)) {
  109. printf("codec_name=%s\n", dec->name);
  110. printf("codec_long_name=%s\n", dec->long_name);
  111. } else {
  112. printf("codec_name=unknown\n");
  113. }
  114. printf("codec_type=%s\n", codec_type_string(dec_ctx->codec_type));
  115. printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den);
  116. /* print AVI/FourCC tag */
  117. a = dec_ctx->codec_tag & 0xff;
  118. b = dec_ctx->codec_tag>>8 & 0xff;
  119. c = dec_ctx->codec_tag>>16 & 0xff;
  120. d = dec_ctx->codec_tag>>24 & 0xff;
  121. printf("codec_tag_string=");
  122. if (isprint(a)) printf("%c", a); else printf("[%d]", a);
  123. if (isprint(b)) printf("%c", b); else printf("[%d]", b);
  124. if (isprint(c)) printf("%c", c); else printf("[%d]", c);
  125. if (isprint(d)) printf("%c", d); else printf("[%d]", d);
  126. printf("\ncodec_tag=0x%04x\n", dec_ctx->codec_tag);
  127. switch (dec_ctx->codec_type) {
  128. case CODEC_TYPE_VIDEO:
  129. printf("width=%d\n", dec_ctx->width);
  130. printf("height=%d\n", dec_ctx->height);
  131. printf("has_b_frames=%d\n", dec_ctx->has_b_frames);
  132. printf("sample_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num,
  133. dec_ctx->sample_aspect_ratio.den);
  134. printf("display_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num,
  135. dec_ctx->sample_aspect_ratio.den);
  136. printf("pix_fmt=%s\n", dec_ctx->pix_fmt != PIX_FMT_NONE ?
  137. av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown");
  138. break;
  139. case CODEC_TYPE_AUDIO:
  140. printf("sample_rate=%s\n", value_string(val_str, sizeof(val_str),
  141. dec_ctx->sample_rate,
  142. unit_hertz_str));
  143. printf("channels=%d\n", dec_ctx->channels);
  144. printf("bits_per_sample=%d\n", av_get_bits_per_sample(dec_ctx->codec_id));
  145. break;
  146. }
  147. } else {
  148. printf("codec_type=unknown\n");
  149. }
  150. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
  151. printf("id=0x%x\n", stream->id);
  152. printf("r_frame_rate=%d/%d\n", stream->r_frame_rate.num, stream->r_frame_rate.den);
  153. printf("avg_frame_rate=%d/%d\n", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
  154. printf("time_base=%d/%d\n", stream->time_base.num, stream->time_base.den);
  155. if (stream->language[0])
  156. printf("language=%s\n", stream->language);
  157. printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), stream->start_time,
  158. &stream->time_base));
  159. printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), stream->duration,
  160. &stream->time_base));
  161. while ((tag = av_metadata_get(stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
  162. printf("%s=%s\n", tag->key, tag->value);
  163. printf("[/STREAM]\n");
  164. }
  165. static void show_format(AVFormatContext *fmt_ctx)
  166. {
  167. AVMetadataTag *tag = NULL;
  168. char val_str[128];
  169. printf("[FORMAT]\n");
  170. printf("filename=%s\n", fmt_ctx->filename);
  171. printf("nb_streams=%d\n", fmt_ctx->nb_streams);
  172. printf("format_name=%s\n", fmt_ctx->iformat->name);
  173. printf("format_long_name=%s\n", fmt_ctx->iformat->long_name);
  174. printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time,
  175. &AV_TIME_BASE_Q));
  176. printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->duration,
  177. &AV_TIME_BASE_Q));
  178. printf("size=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->file_size,
  179. unit_byte_str));
  180. printf("bit_rate=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate,
  181. unit_bit_per_second_str));
  182. while ((tag = av_metadata_get(fmt_ctx->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
  183. printf("TAG:%s=%s\n", tag->key, tag->value);
  184. printf("[/FORMAT]\n");
  185. }
  186. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  187. {
  188. int err, i;
  189. AVFormatContext *fmt_ctx;
  190. fmt_ctx = avformat_alloc_context();
  191. if ((err = av_open_input_file(&fmt_ctx, filename, iformat, 0, NULL)) < 0) {
  192. print_error(filename, err);
  193. return err;
  194. }
  195. /* fill the streams in the format context */
  196. if ((err = av_find_stream_info(fmt_ctx)) < 0) {
  197. print_error(filename, err);
  198. return err;
  199. }
  200. dump_format(fmt_ctx, 0, filename, 0);
  201. /* bind a decoder to each input stream */
  202. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  203. AVStream *stream = fmt_ctx->streams[i];
  204. AVCodec *codec;
  205. if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  206. fprintf(stderr, "Unsupported codec (id=%d) for input stream %d\n",
  207. stream->codec->codec_id, stream->index);
  208. } else if (avcodec_open(stream->codec, codec) < 0) {
  209. fprintf(stderr, "Error while opening codec for input stream %d\n",
  210. stream->index);
  211. }
  212. }
  213. *fmt_ctx_ptr = fmt_ctx;
  214. return 0;
  215. }
  216. static int probe_file(const char *filename)
  217. {
  218. AVFormatContext *fmt_ctx;
  219. int ret, i;
  220. if ((ret = open_input_file(&fmt_ctx, filename)))
  221. return ret;
  222. if (do_show_streams)
  223. for (i = 0; i < fmt_ctx->nb_streams; i++)
  224. show_stream(fmt_ctx, i);
  225. if (do_show_format)
  226. show_format(fmt_ctx);
  227. av_close_input_file(fmt_ctx);
  228. return 0;
  229. }
  230. static void show_usage(void)
  231. {
  232. printf("Simple multimedia streams analyzer\n");
  233. printf("usage: ffprobe [OPTIONS] [INPUT_FILE]\n");
  234. printf("\n");
  235. }
  236. static void opt_format(const char *arg)
  237. {
  238. iformat = av_find_input_format(arg);
  239. if (!iformat) {
  240. fprintf(stderr, "Unknown input format: %s\n", arg);
  241. exit(1);
  242. }
  243. }
  244. static void opt_input_file(const char *filename)
  245. {
  246. if (!strcmp(filename, "-"))
  247. filename = "pipe:";
  248. input_filename = filename;
  249. }
  250. static void show_help(void)
  251. {
  252. show_usage();
  253. show_help_options(options, "Main options:\n", 0, 0);
  254. printf("\n");
  255. }
  256. static void opt_pretty(void)
  257. {
  258. show_value_unit = 1;
  259. use_value_prefix = 1;
  260. use_byte_value_binary_prefix = 1;
  261. use_value_sexagesimal_format = 1;
  262. }
  263. static const OptionDef options[] = {
  264. #include "cmdutils_common_opts.h"
  265. { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
  266. { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
  267. { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
  268. { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
  269. "use binary prefixes for byte units" },
  270. { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
  271. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  272. { "pretty", 0, {(void*)&opt_pretty},
  273. "prettify the format of displayed values, make it more human readable" },
  274. { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
  275. { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
  276. { NULL, },
  277. };
  278. int main(int argc, char **argv)
  279. {
  280. av_register_all();
  281. show_banner();
  282. parse_options(argc, argv, options, opt_input_file);
  283. if (!input_filename) {
  284. show_usage();
  285. fprintf(stderr, "You have to specify one input file.\n");
  286. fprintf(stderr, "Use -h to get full help or, even better, run 'man ffprobe'.\n");
  287. exit(1);
  288. }
  289. return probe_file(input_filename);
  290. }