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.

427 lines
15KB

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