ffprobe.c 13 KB

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