ffprobe.c 12 KB

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