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.

366 lines
13KB

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