ffprobe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 "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. static char *print_format;
  39. /* globals */
  40. static const OptionDef options[];
  41. /* FFprobe context */
  42. static const char *input_filename;
  43. static AVInputFormat *iformat = NULL;
  44. static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
  45. static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
  46. static const char *unit_second_str = "s" ;
  47. static const char *unit_hertz_str = "Hz" ;
  48. static const char *unit_byte_str = "byte" ;
  49. static const char *unit_bit_per_second_str = "bit/s";
  50. void exit_program(int ret)
  51. {
  52. exit(ret);
  53. }
  54. static char *value_string(char *buf, int buf_size, double val, const char *unit)
  55. {
  56. if (unit == unit_second_str && use_value_sexagesimal_format) {
  57. double secs;
  58. int hours, mins;
  59. secs = val;
  60. mins = (int)secs / 60;
  61. secs = secs - mins * 60;
  62. hours = mins / 60;
  63. mins %= 60;
  64. snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
  65. } else if (use_value_prefix) {
  66. const char *prefix_string;
  67. int index;
  68. if (unit == unit_byte_str && use_byte_value_binary_prefix) {
  69. index = (int) (log(val)/log(2)) / 10;
  70. index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
  71. val /= pow(2, index*10);
  72. prefix_string = binary_unit_prefixes[index];
  73. } else {
  74. index = (int) (log10(val)) / 3;
  75. index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
  76. val /= pow(10, index*3);
  77. prefix_string = decimal_unit_prefixes[index];
  78. }
  79. snprintf(buf, buf_size, "%.3f%s%s%s", val, prefix_string || show_value_unit ? " " : "",
  80. prefix_string, show_value_unit ? unit : "");
  81. } else {
  82. snprintf(buf, buf_size, "%f%s%s", val, show_value_unit ? " " : "",
  83. show_value_unit ? unit : "");
  84. }
  85. return buf;
  86. }
  87. static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base)
  88. {
  89. if (val == AV_NOPTS_VALUE) {
  90. snprintf(buf, buf_size, "N/A");
  91. } else {
  92. value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
  93. }
  94. return buf;
  95. }
  96. static char *ts_value_string (char *buf, int buf_size, int64_t ts)
  97. {
  98. if (ts == AV_NOPTS_VALUE) {
  99. snprintf(buf, buf_size, "N/A");
  100. } else {
  101. snprintf(buf, buf_size, "%"PRId64, ts);
  102. }
  103. return buf;
  104. }
  105. static const char *media_type_string(enum AVMediaType media_type)
  106. {
  107. const char *s = av_get_media_type_string(media_type);
  108. return s ? s : "unknown";
  109. }
  110. struct writer {
  111. const char *name;
  112. const char *item_sep; ///< separator between key/value couples
  113. const char *items_sep; ///< separator between sets of key/value couples
  114. const char *section_sep; ///< separator between sections (streams, packets, ...)
  115. const char *header, *footer;
  116. void (*print_header)(const char *);
  117. void (*print_footer)(const char *);
  118. void (*print_fmt_f)(const char *, const char *, ...);
  119. void (*print_int_f)(const char *, int);
  120. void (*show_tags)(struct writer *w, AVDictionary *dict);
  121. };
  122. /* Default output */
  123. static void default_print_header(const char *section)
  124. {
  125. printf("[%s]\n", section);
  126. }
  127. static void default_print_fmt(const char *key, const char *fmt, ...)
  128. {
  129. va_list ap;
  130. va_start(ap, fmt);
  131. printf("%s=", key);
  132. vprintf(fmt, ap);
  133. va_end(ap);
  134. }
  135. static void default_print_int(const char *key, int value)
  136. {
  137. printf("%s=%d", key, value);
  138. }
  139. static void default_print_footer(const char *section)
  140. {
  141. printf("\n[/%s]", section);
  142. }
  143. /* Print helpers */
  144. #define print_fmt0(k, f, a...) w->print_fmt_f(k, f, ##a)
  145. #define print_fmt( k, f, a...) do { \
  146. if (w->item_sep) \
  147. printf("%s", w->item_sep); \
  148. w->print_fmt_f(k, f, ##a); \
  149. } while (0)
  150. #define print_int0(k, v) w->print_int_f(k, v)
  151. #define print_int( k, v) do { \
  152. if (w->item_sep) \
  153. printf("%s", w->item_sep); \
  154. print_int0(k, v); \
  155. } while (0)
  156. #define print_str0(k, v) print_fmt0(k, "%s", v)
  157. #define print_str( k, v) print_fmt (k, "%s", v)
  158. static void show_packet(struct writer *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
  159. {
  160. char val_str[128];
  161. AVStream *st = fmt_ctx->streams[pkt->stream_index];
  162. if (packet_idx)
  163. printf("%s", w->items_sep);
  164. w->print_header("PACKET");
  165. print_str0("codec_type", media_type_string(st->codec->codec_type));
  166. print_int("stream_index", pkt->stream_index);
  167. print_str("pts", ts_value_string (val_str, sizeof(val_str), pkt->pts));
  168. print_str("pts_time", time_value_string(val_str, sizeof(val_str), pkt->pts, &st->time_base));
  169. print_str("dts", ts_value_string (val_str, sizeof(val_str), pkt->dts));
  170. print_str("dts_time", time_value_string(val_str, sizeof(val_str), pkt->dts, &st->time_base));
  171. print_str("duration", ts_value_string (val_str, sizeof(val_str), pkt->duration));
  172. print_str("duration_time", time_value_string(val_str, sizeof(val_str), pkt->duration, &st->time_base));
  173. print_str("size", value_string (val_str, sizeof(val_str), pkt->size, unit_byte_str));
  174. print_fmt("pos", "%"PRId64, pkt->pos);
  175. print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
  176. w->print_footer("PACKET");
  177. fflush(stdout);
  178. }
  179. static void show_packets(struct writer *w, AVFormatContext *fmt_ctx)
  180. {
  181. AVPacket pkt;
  182. int i = 0;
  183. av_init_packet(&pkt);
  184. while (!av_read_frame(fmt_ctx, &pkt))
  185. show_packet(w, fmt_ctx, &pkt, i++);
  186. }
  187. static void default_show_tags(struct writer *w, AVDictionary *dict)
  188. {
  189. AVDictionaryEntry *tag = NULL;
  190. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  191. printf("\nTAG:");
  192. print_str0(tag->key, tag->value);
  193. }
  194. }
  195. static void show_stream(struct writer *w, AVFormatContext *fmt_ctx, int stream_idx)
  196. {
  197. AVStream *stream = fmt_ctx->streams[stream_idx];
  198. AVCodecContext *dec_ctx;
  199. AVCodec *dec;
  200. char val_str[128];
  201. AVRational display_aspect_ratio;
  202. if (stream_idx)
  203. printf("%s", w->items_sep);
  204. w->print_header("STREAM");
  205. print_int0("index", stream->index);
  206. if ((dec_ctx = stream->codec)) {
  207. if ((dec = dec_ctx->codec)) {
  208. print_str("codec_name", dec->name);
  209. print_str("codec_long_name", dec->long_name);
  210. } else {
  211. print_str("codec_name", "unknown");
  212. }
  213. print_str("codec_type", media_type_string(dec_ctx->codec_type));
  214. print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
  215. /* print AVI/FourCC tag */
  216. av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
  217. print_str("codec_tag_string", val_str);
  218. print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
  219. switch (dec_ctx->codec_type) {
  220. case AVMEDIA_TYPE_VIDEO:
  221. print_int("width", dec_ctx->width);
  222. print_int("height", dec_ctx->height);
  223. print_int("has_b_frames", dec_ctx->has_b_frames);
  224. if (dec_ctx->sample_aspect_ratio.num) {
  225. print_fmt("sample_aspect_ratio", "%d:%d",
  226. dec_ctx->sample_aspect_ratio.num,
  227. dec_ctx->sample_aspect_ratio.den);
  228. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  229. dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
  230. dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
  231. 1024*1024);
  232. print_fmt("display_aspect_ratio", "%d:%d",
  233. display_aspect_ratio.num,
  234. display_aspect_ratio.den);
  235. }
  236. print_str("pix_fmt", dec_ctx->pix_fmt != PIX_FMT_NONE ? av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown");
  237. print_int("level", dec_ctx->level);
  238. break;
  239. case AVMEDIA_TYPE_AUDIO:
  240. print_str("sample_rate", value_string(val_str, sizeof(val_str), dec_ctx->sample_rate, unit_hertz_str));
  241. print_int("channels", dec_ctx->channels);
  242. print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
  243. break;
  244. }
  245. } else {
  246. print_fmt("codec_type", "unknown");
  247. }
  248. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
  249. print_fmt("id=", "0x%x", stream->id);
  250. print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
  251. print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
  252. print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
  253. print_str("start_time", time_value_string(val_str, sizeof(val_str), stream->start_time, &stream->time_base));
  254. print_str("duration", time_value_string(val_str, sizeof(val_str), stream->duration, &stream->time_base));
  255. if (stream->nb_frames)
  256. print_fmt("nb_frames", "%"PRId64, stream->nb_frames);
  257. w->show_tags(w, stream->metadata);
  258. w->print_footer("STREAM");
  259. fflush(stdout);
  260. }
  261. static void show_streams(struct writer *w, AVFormatContext *fmt_ctx)
  262. {
  263. int i;
  264. for (i = 0; i < fmt_ctx->nb_streams; i++)
  265. show_stream(w, fmt_ctx, i);
  266. }
  267. static void show_format(struct writer *w, AVFormatContext *fmt_ctx)
  268. {
  269. char val_str[128];
  270. w->print_header("FORMAT");
  271. print_str0("filename", fmt_ctx->filename);
  272. print_int("nb_streams", fmt_ctx->nb_streams);
  273. print_str("format_name", fmt_ctx->iformat->name);
  274. print_str("format_long_name", fmt_ctx->iformat->long_name);
  275. print_str("start_time", time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time, &AV_TIME_BASE_Q));
  276. print_str("duration", time_value_string(val_str, sizeof(val_str), fmt_ctx->duration, &AV_TIME_BASE_Q));
  277. print_str("size", value_string(val_str, sizeof(val_str), fmt_ctx->file_size, unit_byte_str));
  278. print_str("bit_rate", value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate, unit_bit_per_second_str));
  279. w->show_tags(w, fmt_ctx->metadata);
  280. w->print_footer("FORMAT");
  281. fflush(stdout);
  282. }
  283. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  284. {
  285. int err, i;
  286. AVFormatContext *fmt_ctx = NULL;
  287. AVDictionaryEntry *t;
  288. if ((err = avformat_open_input(&fmt_ctx, filename, iformat, &format_opts)) < 0) {
  289. print_error(filename, err);
  290. return err;
  291. }
  292. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  293. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  294. return AVERROR_OPTION_NOT_FOUND;
  295. }
  296. /* fill the streams in the format context */
  297. if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
  298. print_error(filename, err);
  299. return err;
  300. }
  301. av_dump_format(fmt_ctx, 0, filename, 0);
  302. /* bind a decoder to each input stream */
  303. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  304. AVStream *stream = fmt_ctx->streams[i];
  305. AVCodec *codec;
  306. if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  307. fprintf(stderr, "Unsupported codec with id %d for input stream %d\n",
  308. stream->codec->codec_id, stream->index);
  309. } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
  310. fprintf(stderr, "Error while opening codec for input stream %d\n",
  311. stream->index);
  312. }
  313. }
  314. *fmt_ctx_ptr = fmt_ctx;
  315. return 0;
  316. }
  317. #define WRITER_FUNC(func) \
  318. .print_header = func ## _print_header, \
  319. .print_footer = func ## _print_footer, \
  320. .print_fmt_f = func ## _print_fmt, \
  321. .print_int_f = func ## _print_int, \
  322. .show_tags = func ## _show_tags
  323. static struct writer writers[] = {{
  324. .name = "default",
  325. .item_sep = "\n",
  326. .items_sep = "\n",
  327. .section_sep = "\n",
  328. .footer = "\n",
  329. WRITER_FUNC(default),
  330. }
  331. };
  332. static int get_writer(const char *name)
  333. {
  334. int i;
  335. if (!name)
  336. return 0;
  337. for (i = 0; i < FF_ARRAY_ELEMS(writers); i++)
  338. if (!strcmp(writers[i].name, name))
  339. return i;
  340. return -1;
  341. }
  342. #define SECTION_PRINT(name, left) do { \
  343. if (do_show_ ## name) { \
  344. show_ ## name (w, fmt_ctx); \
  345. if (left) \
  346. printf("%s", w->section_sep); \
  347. } \
  348. } while (0)
  349. static int probe_file(const char *filename)
  350. {
  351. AVFormatContext *fmt_ctx;
  352. int ret, writer_id;
  353. struct writer *w;
  354. writer_id = get_writer(print_format);
  355. if (writer_id < 0) {
  356. fprintf(stderr, "Invalid output format '%s'\n", print_format);
  357. return AVERROR(EINVAL);
  358. }
  359. w = &writers[writer_id];
  360. if ((ret = open_input_file(&fmt_ctx, filename)))
  361. return ret;
  362. if (w->header)
  363. printf("%s", w->header);
  364. SECTION_PRINT(packets, do_show_streams || do_show_format);
  365. SECTION_PRINT(streams, do_show_format);
  366. SECTION_PRINT(format, 0);
  367. if (w->footer)
  368. printf("%s", w->footer);
  369. av_close_input_file(fmt_ctx);
  370. return 0;
  371. }
  372. static void show_usage(void)
  373. {
  374. printf("Simple multimedia streams analyzer\n");
  375. printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  376. printf("\n");
  377. }
  378. static int opt_format(const char *opt, const char *arg)
  379. {
  380. iformat = av_find_input_format(arg);
  381. if (!iformat) {
  382. fprintf(stderr, "Unknown input format: %s\n", arg);
  383. return AVERROR(EINVAL);
  384. }
  385. return 0;
  386. }
  387. static void opt_input_file(void *optctx, const char *arg)
  388. {
  389. if (input_filename) {
  390. fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  391. arg, input_filename);
  392. exit(1);
  393. }
  394. if (!strcmp(arg, "-"))
  395. arg = "pipe:";
  396. input_filename = arg;
  397. }
  398. static int opt_help(const char *opt, const char *arg)
  399. {
  400. const AVClass *class = avformat_get_class();
  401. av_log_set_callback(log_callback_help);
  402. show_usage();
  403. show_help_options(options, "Main options:\n", 0, 0);
  404. printf("\n");
  405. av_opt_show2(&class, NULL,
  406. AV_OPT_FLAG_DECODING_PARAM, 0);
  407. return 0;
  408. }
  409. static int opt_pretty(const char *opt, const char *arg)
  410. {
  411. show_value_unit = 1;
  412. use_value_prefix = 1;
  413. use_byte_value_binary_prefix = 1;
  414. use_value_sexagesimal_format = 1;
  415. return 0;
  416. }
  417. static const OptionDef options[] = {
  418. #include "cmdutils_common_opts.h"
  419. { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
  420. { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
  421. { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
  422. { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
  423. "use binary prefixes for byte units" },
  424. { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
  425. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  426. { "pretty", 0, {(void*)&opt_pretty},
  427. "prettify the format of displayed values, make it more human readable" },
  428. { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
  429. { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
  430. { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
  431. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
  432. { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
  433. { NULL, },
  434. };
  435. int main(int argc, char **argv)
  436. {
  437. int ret;
  438. av_register_all();
  439. init_opts();
  440. #if CONFIG_AVDEVICE
  441. avdevice_register_all();
  442. #endif
  443. show_banner();
  444. parse_options(NULL, argc, argv, options, opt_input_file);
  445. if (!input_filename) {
  446. show_usage();
  447. fprintf(stderr, "You have to specify one input file.\n");
  448. fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
  449. exit(1);
  450. }
  451. ret = probe_file(input_filename);
  452. return ret;
  453. }