You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

536 lines
18KB

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