ffprobe.c 15 KB

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