ffprobe.c 15 KB

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