ffprobe.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "libavutil/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_packets = 0;
  32. static int do_show_streams = 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 char *ts_value_string (char *buf, int buf_size, int64_t ts)
  89. {
  90. if (ts == AV_NOPTS_VALUE) {
  91. snprintf(buf, buf_size, "N/A");
  92. } else {
  93. snprintf(buf, buf_size, "%"PRId64, ts);
  94. }
  95. return buf;
  96. }
  97. static const char *media_type_string(enum AVMediaType media_type)
  98. {
  99. switch (media_type) {
  100. case AVMEDIA_TYPE_VIDEO: return "video";
  101. case AVMEDIA_TYPE_AUDIO: return "audio";
  102. case AVMEDIA_TYPE_DATA: return "data";
  103. case AVMEDIA_TYPE_SUBTITLE: return "subtitle";
  104. case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
  105. default: return "unknown";
  106. }
  107. }
  108. static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
  109. {
  110. char val_str[128];
  111. AVStream *st = fmt_ctx->streams[pkt->stream_index];
  112. printf("[PACKET]\n");
  113. printf("codec_type=%s\n" , media_type_string(st->codec->codec_type));
  114. printf("stream_index=%d\n" , pkt->stream_index);
  115. printf("pts=%s\n" , ts_value_string (val_str, sizeof(val_str), pkt->pts));
  116. printf("pts_time=%s\n" , time_value_string(val_str, sizeof(val_str), pkt->pts, &st->time_base));
  117. printf("dts=%s\n" , ts_value_string (val_str, sizeof(val_str), pkt->dts));
  118. printf("dts_time=%s\n" , time_value_string(val_str, sizeof(val_str), pkt->dts, &st->time_base));
  119. printf("duration=%s\n" , ts_value_string (val_str, sizeof(val_str), pkt->duration));
  120. printf("duration_time=%s\n", time_value_string(val_str, sizeof(val_str), pkt->duration, &st->time_base));
  121. printf("size=%s\n" , value_string (val_str, sizeof(val_str), pkt->size, unit_byte_str));
  122. printf("pos=%"PRId64"\n" , pkt->pos);
  123. printf("flags=%c\n" , pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
  124. printf("[/PACKET]\n");
  125. }
  126. static void show_packets(AVFormatContext *fmt_ctx)
  127. {
  128. AVPacket pkt;
  129. av_init_packet(&pkt);
  130. while (!av_read_frame(fmt_ctx, &pkt))
  131. show_packet(fmt_ctx, &pkt);
  132. }
  133. static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
  134. {
  135. AVStream *stream = fmt_ctx->streams[stream_idx];
  136. AVCodecContext *dec_ctx;
  137. AVCodec *dec;
  138. char val_str[128];
  139. AVMetadataTag *tag = NULL;
  140. AVRational display_aspect_ratio;
  141. printf("[STREAM]\n");
  142. printf("index=%d\n", stream->index);
  143. if ((dec_ctx = stream->codec)) {
  144. if ((dec = dec_ctx->codec)) {
  145. printf("codec_name=%s\n", dec->name);
  146. printf("codec_long_name=%s\n", dec->long_name);
  147. } else {
  148. printf("codec_name=unknown\n");
  149. }
  150. printf("codec_type=%s\n", media_type_string(dec_ctx->codec_type));
  151. printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den);
  152. /* print AVI/FourCC tag */
  153. av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
  154. printf("codec_tag_string=%s\n", val_str);
  155. printf("codec_tag=0x%04x\n", dec_ctx->codec_tag);
  156. switch (dec_ctx->codec_type) {
  157. case AVMEDIA_TYPE_VIDEO:
  158. printf("width=%d\n", dec_ctx->width);
  159. printf("height=%d\n", dec_ctx->height);
  160. printf("has_b_frames=%d\n", dec_ctx->has_b_frames);
  161. if (dec_ctx->sample_aspect_ratio.num) {
  162. printf("sample_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num,
  163. dec_ctx->sample_aspect_ratio.den);
  164. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  165. dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
  166. dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
  167. 1024*1024);
  168. printf("display_aspect_ratio=%d:%d\n", display_aspect_ratio.num,
  169. display_aspect_ratio.den);
  170. }
  171. printf("pix_fmt=%s\n", dec_ctx->pix_fmt != PIX_FMT_NONE ?
  172. av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown");
  173. break;
  174. case AVMEDIA_TYPE_AUDIO:
  175. printf("sample_rate=%s\n", value_string(val_str, sizeof(val_str),
  176. dec_ctx->sample_rate,
  177. unit_hertz_str));
  178. printf("channels=%d\n", dec_ctx->channels);
  179. printf("bits_per_sample=%d\n", av_get_bits_per_sample(dec_ctx->codec_id));
  180. break;
  181. }
  182. } else {
  183. printf("codec_type=unknown\n");
  184. }
  185. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
  186. printf("id=0x%x\n", stream->id);
  187. printf("r_frame_rate=%d/%d\n", stream->r_frame_rate.num, stream->r_frame_rate.den);
  188. printf("avg_frame_rate=%d/%d\n", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
  189. printf("time_base=%d/%d\n", stream->time_base.num, stream->time_base.den);
  190. printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), stream->start_time,
  191. &stream->time_base));
  192. printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), stream->duration,
  193. &stream->time_base));
  194. if (stream->nb_frames)
  195. printf("nb_frames=%"PRId64"\n", stream->nb_frames);
  196. while ((tag = av_metadata_get(stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
  197. printf("TAG:%s=%s\n", tag->key, tag->value);
  198. printf("[/STREAM]\n");
  199. }
  200. static void show_format(AVFormatContext *fmt_ctx)
  201. {
  202. AVMetadataTag *tag = NULL;
  203. char val_str[128];
  204. printf("[FORMAT]\n");
  205. printf("filename=%s\n", fmt_ctx->filename);
  206. printf("nb_streams=%d\n", fmt_ctx->nb_streams);
  207. printf("format_name=%s\n", fmt_ctx->iformat->name);
  208. printf("format_long_name=%s\n", fmt_ctx->iformat->long_name);
  209. printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time,
  210. &AV_TIME_BASE_Q));
  211. printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->duration,
  212. &AV_TIME_BASE_Q));
  213. printf("size=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->file_size,
  214. unit_byte_str));
  215. printf("bit_rate=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate,
  216. unit_bit_per_second_str));
  217. while ((tag = av_metadata_get(fmt_ctx->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
  218. printf("TAG:%s=%s\n", tag->key, tag->value);
  219. printf("[/FORMAT]\n");
  220. }
  221. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  222. {
  223. int err, i;
  224. AVFormatParameters fmt_params;
  225. AVFormatContext *fmt_ctx;
  226. memset(&fmt_params, 0, sizeof(fmt_params));
  227. fmt_params.prealloced_context = 1;
  228. fmt_ctx = avformat_alloc_context();
  229. set_context_opts(fmt_ctx, avformat_opts, AV_OPT_FLAG_DECODING_PARAM, NULL);
  230. if ((err = av_open_input_file(&fmt_ctx, filename, iformat, 0, &fmt_params)) < 0) {
  231. print_error(filename, err);
  232. return err;
  233. }
  234. /* fill the streams in the format context */
  235. if ((err = av_find_stream_info(fmt_ctx)) < 0) {
  236. print_error(filename, err);
  237. return err;
  238. }
  239. av_dump_format(fmt_ctx, 0, filename, 0);
  240. /* bind a decoder to each input stream */
  241. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  242. AVStream *stream = fmt_ctx->streams[i];
  243. AVCodec *codec;
  244. if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  245. fprintf(stderr, "Unsupported codec with id %d for input stream %d\n",
  246. stream->codec->codec_id, stream->index);
  247. } else if (avcodec_open(stream->codec, codec) < 0) {
  248. fprintf(stderr, "Error while opening codec for input stream %d\n",
  249. stream->index);
  250. }
  251. }
  252. *fmt_ctx_ptr = fmt_ctx;
  253. return 0;
  254. }
  255. static int probe_file(const char *filename)
  256. {
  257. AVFormatContext *fmt_ctx;
  258. int ret, i;
  259. if ((ret = open_input_file(&fmt_ctx, filename)))
  260. return ret;
  261. if (do_show_packets)
  262. show_packets(fmt_ctx);
  263. if (do_show_streams)
  264. for (i = 0; i < fmt_ctx->nb_streams; i++)
  265. show_stream(fmt_ctx, i);
  266. if (do_show_format)
  267. show_format(fmt_ctx);
  268. av_close_input_file(fmt_ctx);
  269. return 0;
  270. }
  271. static void show_usage(void)
  272. {
  273. printf("Simple multimedia streams analyzer\n");
  274. printf("usage: ffprobe [OPTIONS] [INPUT_FILE]\n");
  275. printf("\n");
  276. }
  277. static int opt_format(const char *opt, const char *arg)
  278. {
  279. iformat = av_find_input_format(arg);
  280. if (!iformat) {
  281. fprintf(stderr, "Unknown input format: %s\n", arg);
  282. return AVERROR(EINVAL);
  283. }
  284. return 0;
  285. }
  286. static void opt_input_file(const char *arg)
  287. {
  288. if (input_filename) {
  289. fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  290. arg, input_filename);
  291. exit(1);
  292. }
  293. if (!strcmp(arg, "-"))
  294. arg = "pipe:";
  295. input_filename = arg;
  296. }
  297. static void show_help(void)
  298. {
  299. av_log_set_callback(log_callback_help);
  300. show_usage();
  301. show_help_options(options, "Main options:\n", 0, 0);
  302. printf("\n");
  303. av_opt_show2(avformat_opts, NULL,
  304. AV_OPT_FLAG_DECODING_PARAM, 0);
  305. }
  306. static void opt_pretty(void)
  307. {
  308. show_value_unit = 1;
  309. use_value_prefix = 1;
  310. use_byte_value_binary_prefix = 1;
  311. use_value_sexagesimal_format = 1;
  312. }
  313. static const OptionDef options[] = {
  314. #include "cmdutils_common_opts.h"
  315. { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
  316. { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
  317. { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
  318. { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
  319. "use binary prefixes for byte units" },
  320. { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
  321. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  322. { "pretty", 0, {(void*)&opt_pretty},
  323. "prettify the format of displayed values, make it more human readable" },
  324. { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
  325. { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
  326. { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
  327. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
  328. { NULL, },
  329. };
  330. int main(int argc, char **argv)
  331. {
  332. int ret;
  333. av_register_all();
  334. #if CONFIG_AVDEVICE
  335. avdevice_register_all();
  336. #endif
  337. avformat_opts = avformat_alloc_context();
  338. show_banner();
  339. parse_options(argc, argv, options, opt_input_file);
  340. if (!input_filename) {
  341. show_usage();
  342. fprintf(stderr, "You have to specify one input file.\n");
  343. fprintf(stderr, "Use -h to get full help or, even better, run 'man ffprobe'.\n");
  344. exit(1);
  345. }
  346. ret = probe_file(input_filename);
  347. av_free(avformat_opts);
  348. return ret;
  349. }