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.

865 lines
27KB

  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. 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. void exit_program(int ret)
  50. {
  51. exit(ret);
  52. }
  53. static char *value_string(char *buf, int buf_size, double val, const char *unit)
  54. {
  55. if (unit == unit_second_str && use_value_sexagesimal_format) {
  56. double secs;
  57. int hours, mins;
  58. secs = val;
  59. mins = (int)secs / 60;
  60. secs = secs - mins * 60;
  61. hours = mins / 60;
  62. mins %= 60;
  63. snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
  64. } else if (use_value_prefix) {
  65. const char *prefix_string;
  66. int index;
  67. if (unit == unit_byte_str && use_byte_value_binary_prefix) {
  68. index = (int) (log(val)/log(2)) / 10;
  69. index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
  70. val /= pow(2, index*10);
  71. prefix_string = binary_unit_prefixes[index];
  72. } else {
  73. index = (int) (log10(val)) / 3;
  74. index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
  75. val /= pow(10, index*3);
  76. prefix_string = decimal_unit_prefixes[index];
  77. }
  78. snprintf(buf, buf_size, "%.3f%s%s%s", val, prefix_string || show_value_unit ? " " : "",
  79. prefix_string, show_value_unit ? unit : "");
  80. } else {
  81. snprintf(buf, buf_size, "%f%s%s", val, show_value_unit ? " " : "",
  82. show_value_unit ? unit : "");
  83. }
  84. return buf;
  85. }
  86. static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base)
  87. {
  88. if (val == AV_NOPTS_VALUE) {
  89. snprintf(buf, buf_size, "N/A");
  90. } else {
  91. value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
  92. }
  93. return buf;
  94. }
  95. static char *ts_value_string (char *buf, int buf_size, int64_t ts)
  96. {
  97. if (ts == AV_NOPTS_VALUE) {
  98. snprintf(buf, buf_size, "N/A");
  99. } else {
  100. snprintf(buf, buf_size, "%"PRId64, ts);
  101. }
  102. return buf;
  103. }
  104. /* WRITERS API */
  105. typedef struct WriterContext WriterContext;
  106. typedef struct Writer {
  107. int priv_size; ///< private size for the writer context
  108. const char *name;
  109. int (*init) (WriterContext *wctx, const char *args, void *opaque);
  110. void (*uninit)(WriterContext *wctx);
  111. void (*print_header)(WriterContext *ctx);
  112. void (*print_footer)(WriterContext *ctx);
  113. void (*print_chapter_header)(WriterContext *wctx, const char *);
  114. void (*print_chapter_footer)(WriterContext *wctx, const char *);
  115. void (*print_section_header)(WriterContext *wctx, const char *);
  116. void (*print_section_footer)(WriterContext *wctx, const char *);
  117. void (*print_integer) (WriterContext *wctx, const char *, int);
  118. void (*print_string) (WriterContext *wctx, const char *, const char *);
  119. void (*show_tags) (WriterContext *wctx, AVDictionary *dict);
  120. } Writer;
  121. struct WriterContext {
  122. const AVClass *class; ///< class of the writer
  123. const Writer *writer; ///< the Writer of which this is an instance
  124. char *name; ///< name of this writer instance
  125. void *priv; ///< private data for use by the filter
  126. unsigned int nb_item; ///< number of the item printed in the given section, starting at 0
  127. unsigned int nb_section; ///< number of the section printed in the given section sequence, starting at 0
  128. unsigned int nb_chapter; ///< number of the chapter, starting at 0
  129. };
  130. static void writer_close(WriterContext **wctx)
  131. {
  132. if (*wctx && (*wctx)->writer->uninit)
  133. (*wctx)->writer->uninit(*wctx);
  134. av_freep(&((*wctx)->priv));
  135. av_freep(wctx);
  136. }
  137. static int writer_open(WriterContext **wctx, const Writer *writer,
  138. const char *args, void *opaque)
  139. {
  140. int ret = 0;
  141. if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
  142. ret = AVERROR(ENOMEM);
  143. goto fail;
  144. }
  145. if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
  146. ret = AVERROR(ENOMEM);
  147. goto fail;
  148. }
  149. (*wctx)->writer = writer;
  150. if ((*wctx)->writer->init)
  151. ret = (*wctx)->writer->init(*wctx, args, opaque);
  152. if (ret < 0)
  153. goto fail;
  154. return 0;
  155. fail:
  156. writer_close(wctx);
  157. return ret;
  158. }
  159. static inline void writer_print_header(WriterContext *wctx)
  160. {
  161. if (wctx->writer->print_header)
  162. wctx->writer->print_header(wctx);
  163. wctx->nb_chapter = 0;
  164. }
  165. static inline void writer_print_footer(WriterContext *wctx)
  166. {
  167. if (wctx->writer->print_footer)
  168. wctx->writer->print_footer(wctx);
  169. }
  170. static inline void writer_print_chapter_header(WriterContext *wctx,
  171. const char *header)
  172. {
  173. if (wctx->writer->print_chapter_header)
  174. wctx->writer->print_chapter_header(wctx, header);
  175. wctx->nb_section = 0;
  176. }
  177. static inline void writer_print_chapter_footer(WriterContext *wctx,
  178. const char *footer)
  179. {
  180. if (wctx->writer->print_chapter_footer)
  181. wctx->writer->print_chapter_footer(wctx, footer);
  182. wctx->nb_chapter++;
  183. }
  184. static inline void writer_print_section_header(WriterContext *wctx,
  185. const char *header)
  186. {
  187. if (wctx->writer->print_section_header)
  188. wctx->writer->print_section_header(wctx, header);
  189. wctx->nb_item = 0;
  190. }
  191. static inline void writer_print_section_footer(WriterContext *wctx,
  192. const char *footer)
  193. {
  194. if (wctx->writer->print_section_footer)
  195. wctx->writer->print_section_footer(wctx, footer);
  196. wctx->nb_section++;
  197. }
  198. static inline void writer_print_integer(WriterContext *wctx,
  199. const char *key, int val)
  200. {
  201. wctx->writer->print_integer(wctx, key, val);
  202. wctx->nb_item++;
  203. }
  204. static inline void writer_print_string(WriterContext *wctx,
  205. const char *key, const char *val)
  206. {
  207. wctx->writer->print_string(wctx, key, val);
  208. wctx->nb_item++;
  209. }
  210. static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
  211. {
  212. wctx->writer->show_tags(wctx, dict);
  213. }
  214. #define MAX_REGISTERED_WRITERS_NB 64
  215. static Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
  216. static int writer_register(Writer *writer)
  217. {
  218. static int next_registered_writer_idx = 0;
  219. if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
  220. return AVERROR(ENOMEM);
  221. registered_writers[next_registered_writer_idx++] = writer;
  222. return 0;
  223. }
  224. static Writer *writer_get_by_name(const char *name)
  225. {
  226. int i;
  227. for (i = 0; registered_writers[i]; i++)
  228. if (!strcmp(registered_writers[i]->name, name))
  229. return registered_writers[i];
  230. return NULL;
  231. }
  232. /* Print helpers */
  233. struct print_buf {
  234. char *s;
  235. int len;
  236. };
  237. static char *fast_asprintf(struct print_buf *pbuf, const char *fmt, ...)
  238. {
  239. va_list va;
  240. int len;
  241. va_start(va, fmt);
  242. len = vsnprintf(NULL, 0, fmt, va);
  243. va_end(va);
  244. if (len < 0)
  245. goto fail;
  246. if (pbuf->len < len) {
  247. char *p = av_realloc(pbuf->s, len + 1);
  248. if (!p)
  249. goto fail;
  250. pbuf->s = p;
  251. pbuf->len = len;
  252. }
  253. va_start(va, fmt);
  254. len = vsnprintf(pbuf->s, len + 1, fmt, va);
  255. va_end(va);
  256. if (len < 0)
  257. goto fail;
  258. return pbuf->s;
  259. fail:
  260. av_freep(&pbuf->s);
  261. pbuf->len = 0;
  262. return NULL;
  263. }
  264. /* WRITERS */
  265. /* Default output */
  266. static void default_print_footer(WriterContext *wctx)
  267. {
  268. printf("\n");
  269. }
  270. static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
  271. {
  272. if (wctx->nb_chapter)
  273. printf("\n");
  274. }
  275. static void default_print_section_header(WriterContext *wctx, const char *section)
  276. {
  277. if (wctx->nb_section)
  278. printf("\n");
  279. printf("[%s]\n", section);
  280. }
  281. static void default_print_section_footer(WriterContext *wctx, const char *section)
  282. {
  283. printf("[/%s]", section);
  284. }
  285. static void default_print_str(WriterContext *wctx, const char *key, const char *value)
  286. {
  287. printf("%s=%s\n", key, value);
  288. }
  289. static void default_print_int(WriterContext *wctx, const char *key, int value)
  290. {
  291. printf("%s=%d\n", key, value);
  292. }
  293. static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
  294. {
  295. AVDictionaryEntry *tag = NULL;
  296. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  297. printf("TAG:");
  298. writer_print_string(wctx, tag->key, tag->value);
  299. }
  300. }
  301. static Writer default_writer = {
  302. .name = "default",
  303. .print_footer = default_print_footer,
  304. .print_chapter_header = default_print_chapter_header,
  305. .print_section_header = default_print_section_header,
  306. .print_section_footer = default_print_section_footer,
  307. .print_integer = default_print_int,
  308. .print_string = default_print_str,
  309. .show_tags = default_show_tags
  310. };
  311. /* JSON output */
  312. typedef struct {
  313. int multiple_entries; ///< tells if the given chapter requires multiple entries
  314. } JSONContext;
  315. static char *json_escape_str(const char *s)
  316. {
  317. static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
  318. static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
  319. char *ret, *p;
  320. int i, len = 0;
  321. // compute the length of the escaped string
  322. for (i = 0; s[i]; i++) {
  323. if (strchr(json_escape, s[i])) len += 2; // simple escape
  324. else if ((unsigned char)s[i] < 32) len += 6; // handle non-printable chars
  325. else len += 1; // char copy
  326. }
  327. p = ret = av_malloc(len + 1);
  328. if (!p)
  329. return NULL;
  330. for (i = 0; s[i]; i++) {
  331. char *q = strchr(json_escape, s[i]);
  332. if (q) {
  333. *p++ = '\\';
  334. *p++ = json_subst[q - json_escape];
  335. } else if ((unsigned char)s[i] < 32) {
  336. snprintf(p, 7, "\\u00%02x", s[i] & 0xff);
  337. p += 6;
  338. } else {
  339. *p++ = s[i];
  340. }
  341. }
  342. *p = 0;
  343. return ret;
  344. }
  345. static void json_print_header(WriterContext *wctx)
  346. {
  347. printf("{");
  348. }
  349. static void json_print_footer(WriterContext *wctx)
  350. {
  351. printf("\n}\n");
  352. }
  353. static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
  354. {
  355. JSONContext *json = wctx->priv;
  356. char *chapter_esc;
  357. if (wctx->nb_chapter)
  358. printf(",");
  359. json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "streams");
  360. chapter_esc = json_escape_str(chapter);
  361. printf("\n \"%s\":%s", chapter_esc ? chapter_esc : "",
  362. json->multiple_entries ? " [" : " ");
  363. av_free(chapter_esc);
  364. }
  365. static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
  366. {
  367. JSONContext *json = wctx->priv;
  368. if (json->multiple_entries)
  369. printf("]");
  370. }
  371. static void json_print_section_header(WriterContext *wctx, const char *section)
  372. {
  373. if (wctx->nb_section) printf(",");
  374. printf("{\n");
  375. }
  376. static void json_print_section_footer(WriterContext *wctx, const char *section)
  377. {
  378. printf("\n }");
  379. }
  380. static inline void json_print_item_str(WriterContext *wctx,
  381. const char *key, const char *value,
  382. const char *indent)
  383. {
  384. char *key_esc = json_escape_str(key);
  385. char *value_esc = json_escape_str(value);
  386. printf("%s\"%s\": \"%s\"", indent,
  387. key_esc ? key_esc : "",
  388. value_esc ? value_esc : "");
  389. av_free(key_esc);
  390. av_free(value_esc);
  391. }
  392. #define INDENT " "
  393. static void json_print_str(WriterContext *wctx, const char *key, const char *value)
  394. {
  395. if (wctx->nb_item) printf(",\n");
  396. json_print_item_str(wctx, key, value, INDENT);
  397. }
  398. static void json_print_int(WriterContext *wctx, const char *key, int value)
  399. {
  400. char *key_esc = json_escape_str(key);
  401. if (wctx->nb_item) printf(",\n");
  402. printf(INDENT "\"%s\": %d", key_esc ? key_esc : "", value);
  403. av_free(key_esc);
  404. }
  405. static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
  406. {
  407. AVDictionaryEntry *tag = NULL;
  408. int is_first = 1;
  409. if (!dict)
  410. return;
  411. printf(",\n" INDENT "\"tags\": {\n");
  412. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  413. if (is_first) is_first = 0;
  414. else printf(",\n");
  415. json_print_item_str(wctx, tag->key, tag->value, INDENT INDENT);
  416. }
  417. printf("\n }");
  418. }
  419. static Writer json_writer = {
  420. .name = "json",
  421. .priv_size = sizeof(JSONContext),
  422. .print_header = json_print_header,
  423. .print_footer = json_print_footer,
  424. .print_chapter_header = json_print_chapter_header,
  425. .print_chapter_footer = json_print_chapter_footer,
  426. .print_section_header = json_print_section_header,
  427. .print_section_footer = json_print_section_footer,
  428. .print_integer = json_print_int,
  429. .print_string = json_print_str,
  430. .show_tags = json_show_tags,
  431. };
  432. static void writer_register_all(void)
  433. {
  434. static int initialized;
  435. if (initialized)
  436. return;
  437. initialized = 1;
  438. writer_register(&default_writer);
  439. writer_register(&json_writer);
  440. }
  441. #define print_fmt(k, f, ...) do { \
  442. if (fast_asprintf(&pbuf, f, __VA_ARGS__)) \
  443. writer_print_string(w, k, pbuf.s); \
  444. } while (0)
  445. #define print_int(k, v) writer_print_integer(w, k, v)
  446. #define print_str(k, v) writer_print_string(w, k, v)
  447. #define print_section_header(s) writer_print_section_header(w, s)
  448. #define print_section_footer(s) writer_print_section_footer(w, s)
  449. #define show_tags(metadata) writer_show_tags(w, metadata)
  450. static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
  451. {
  452. char val_str[128];
  453. AVStream *st = fmt_ctx->streams[pkt->stream_index];
  454. struct print_buf pbuf = {.s = NULL};
  455. print_section_header("PACKET");
  456. print_str("codec_type", av_x_if_null(av_get_media_type_string(st->codec->codec_type), "unknown"));
  457. print_int("stream_index", pkt->stream_index);
  458. print_str("pts", ts_value_string (val_str, sizeof(val_str), pkt->pts));
  459. print_str("pts_time", time_value_string(val_str, sizeof(val_str), pkt->pts, &st->time_base));
  460. print_str("dts", ts_value_string (val_str, sizeof(val_str), pkt->dts));
  461. print_str("dts_time", time_value_string(val_str, sizeof(val_str), pkt->dts, &st->time_base));
  462. print_str("duration", ts_value_string (val_str, sizeof(val_str), pkt->duration));
  463. print_str("duration_time", time_value_string(val_str, sizeof(val_str), pkt->duration, &st->time_base));
  464. print_str("size", value_string (val_str, sizeof(val_str), pkt->size, unit_byte_str));
  465. print_fmt("pos", "%"PRId64, pkt->pos);
  466. print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
  467. print_section_footer("PACKET");
  468. av_free(pbuf.s);
  469. fflush(stdout);
  470. }
  471. static void show_packets(WriterContext *w, AVFormatContext *fmt_ctx)
  472. {
  473. AVPacket pkt;
  474. int i = 0;
  475. av_init_packet(&pkt);
  476. while (!av_read_frame(fmt_ctx, &pkt))
  477. show_packet(w, fmt_ctx, &pkt, i++);
  478. }
  479. static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
  480. {
  481. AVStream *stream = fmt_ctx->streams[stream_idx];
  482. AVCodecContext *dec_ctx;
  483. AVCodec *dec;
  484. char val_str[128];
  485. AVRational display_aspect_ratio;
  486. struct print_buf pbuf = {.s = NULL};
  487. print_section_header("STREAM");
  488. print_int("index", stream->index);
  489. if ((dec_ctx = stream->codec)) {
  490. if ((dec = dec_ctx->codec)) {
  491. print_str("codec_name", dec->name);
  492. print_str("codec_long_name", dec->long_name);
  493. } else {
  494. print_str("codec_name", "unknown");
  495. }
  496. print_str("codec_type", av_x_if_null(av_get_media_type_string(dec_ctx->codec_type), "unknown"));
  497. print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
  498. /* print AVI/FourCC tag */
  499. av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
  500. print_str("codec_tag_string", val_str);
  501. print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
  502. switch (dec_ctx->codec_type) {
  503. case AVMEDIA_TYPE_VIDEO:
  504. print_int("width", dec_ctx->width);
  505. print_int("height", dec_ctx->height);
  506. print_int("has_b_frames", dec_ctx->has_b_frames);
  507. if (dec_ctx->sample_aspect_ratio.num) {
  508. print_fmt("sample_aspect_ratio", "%d:%d",
  509. dec_ctx->sample_aspect_ratio.num,
  510. dec_ctx->sample_aspect_ratio.den);
  511. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  512. dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
  513. dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
  514. 1024*1024);
  515. print_fmt("display_aspect_ratio", "%d:%d",
  516. display_aspect_ratio.num,
  517. display_aspect_ratio.den);
  518. }
  519. print_str("pix_fmt", av_x_if_null(av_get_pix_fmt_name(dec_ctx->pix_fmt), "unknown"));
  520. print_int("level", dec_ctx->level);
  521. break;
  522. case AVMEDIA_TYPE_AUDIO:
  523. print_str("sample_rate", value_string(val_str, sizeof(val_str), dec_ctx->sample_rate, unit_hertz_str));
  524. print_int("channels", dec_ctx->channels);
  525. print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
  526. break;
  527. }
  528. } else {
  529. print_str("codec_type", "unknown");
  530. }
  531. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
  532. print_fmt("id", "0x%x", stream->id);
  533. print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
  534. print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
  535. print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
  536. print_str("start_time", time_value_string(val_str, sizeof(val_str), stream->start_time, &stream->time_base));
  537. print_str("duration", time_value_string(val_str, sizeof(val_str), stream->duration, &stream->time_base));
  538. if (stream->nb_frames)
  539. print_fmt("nb_frames", "%"PRId64, stream->nb_frames);
  540. show_tags(stream->metadata);
  541. print_section_footer("STREAM");
  542. av_free(pbuf.s);
  543. fflush(stdout);
  544. }
  545. static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
  546. {
  547. int i;
  548. for (i = 0; i < fmt_ctx->nb_streams; i++)
  549. show_stream(w, fmt_ctx, i);
  550. }
  551. static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
  552. {
  553. char val_str[128];
  554. struct print_buf pbuf = {.s = NULL};
  555. print_section_header("FORMAT");
  556. print_str("filename", fmt_ctx->filename);
  557. print_int("nb_streams", fmt_ctx->nb_streams);
  558. print_str("format_name", fmt_ctx->iformat->name);
  559. print_str("format_long_name", fmt_ctx->iformat->long_name);
  560. print_str("start_time", time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time, &AV_TIME_BASE_Q));
  561. print_str("duration", time_value_string(val_str, sizeof(val_str), fmt_ctx->duration, &AV_TIME_BASE_Q));
  562. print_str("size", value_string(val_str, sizeof(val_str), fmt_ctx->file_size, unit_byte_str));
  563. print_str("bit_rate", value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate, unit_bit_per_second_str));
  564. show_tags(fmt_ctx->metadata);
  565. print_section_footer("FORMAT");
  566. av_free(pbuf.s);
  567. fflush(stdout);
  568. }
  569. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  570. {
  571. int err, i;
  572. AVFormatContext *fmt_ctx = NULL;
  573. AVDictionaryEntry *t;
  574. if ((err = avformat_open_input(&fmt_ctx, filename, iformat, &format_opts)) < 0) {
  575. print_error(filename, err);
  576. return err;
  577. }
  578. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  579. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  580. return AVERROR_OPTION_NOT_FOUND;
  581. }
  582. /* fill the streams in the format context */
  583. if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
  584. print_error(filename, err);
  585. return err;
  586. }
  587. av_dump_format(fmt_ctx, 0, filename, 0);
  588. /* bind a decoder to each input stream */
  589. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  590. AVStream *stream = fmt_ctx->streams[i];
  591. AVCodec *codec;
  592. if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  593. fprintf(stderr, "Unsupported codec with id %d for input stream %d\n",
  594. stream->codec->codec_id, stream->index);
  595. } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
  596. fprintf(stderr, "Error while opening codec for input stream %d\n",
  597. stream->index);
  598. }
  599. }
  600. *fmt_ctx_ptr = fmt_ctx;
  601. return 0;
  602. }
  603. #define PRINT_CHAPTER(name) do { \
  604. if (do_show_ ## name) { \
  605. writer_print_chapter_header(wctx, #name); \
  606. show_ ## name (wctx, fmt_ctx); \
  607. writer_print_chapter_footer(wctx, #name); \
  608. } \
  609. } while (0)
  610. static int probe_file(const char *filename)
  611. {
  612. AVFormatContext *fmt_ctx;
  613. int ret;
  614. Writer *w;
  615. WriterContext *wctx;
  616. writer_register_all();
  617. if (!print_format)
  618. print_format = av_strdup("default");
  619. w = writer_get_by_name(print_format);
  620. if (!w) {
  621. fprintf(stderr, "Invalid output format '%s'\n", print_format);
  622. return AVERROR(EINVAL);
  623. }
  624. if ((ret = writer_open(&wctx, w, NULL, NULL)) < 0)
  625. return ret;
  626. if ((ret = open_input_file(&fmt_ctx, filename)))
  627. return ret;
  628. writer_print_header(wctx);
  629. PRINT_CHAPTER(packets);
  630. PRINT_CHAPTER(streams);
  631. PRINT_CHAPTER(format);
  632. writer_print_footer(wctx);
  633. av_close_input_file(fmt_ctx);
  634. writer_close(&wctx);
  635. return 0;
  636. }
  637. static void show_usage(void)
  638. {
  639. printf("Simple multimedia streams analyzer\n");
  640. printf("usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  641. printf("\n");
  642. }
  643. static int opt_format(const char *opt, const char *arg)
  644. {
  645. iformat = av_find_input_format(arg);
  646. if (!iformat) {
  647. fprintf(stderr, "Unknown input format: %s\n", arg);
  648. return AVERROR(EINVAL);
  649. }
  650. return 0;
  651. }
  652. static void opt_input_file(void *optctx, const char *arg)
  653. {
  654. if (input_filename) {
  655. fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  656. arg, input_filename);
  657. exit(1);
  658. }
  659. if (!strcmp(arg, "-"))
  660. arg = "pipe:";
  661. input_filename = arg;
  662. }
  663. static int opt_help(const char *opt, const char *arg)
  664. {
  665. const AVClass *class = avformat_get_class();
  666. av_log_set_callback(log_callback_help);
  667. show_usage();
  668. show_help_options(options, "Main options:\n", 0, 0);
  669. printf("\n");
  670. av_opt_show2(&class, NULL,
  671. AV_OPT_FLAG_DECODING_PARAM, 0);
  672. return 0;
  673. }
  674. static int opt_pretty(const char *opt, const char *arg)
  675. {
  676. show_value_unit = 1;
  677. use_value_prefix = 1;
  678. use_byte_value_binary_prefix = 1;
  679. use_value_sexagesimal_format = 1;
  680. return 0;
  681. }
  682. static const OptionDef options[] = {
  683. #include "cmdutils_common_opts.h"
  684. { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
  685. { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
  686. { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
  687. { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
  688. "use binary prefixes for byte units" },
  689. { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
  690. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  691. { "pretty", 0, {(void*)&opt_pretty},
  692. "prettify the format of displayed values, make it more human readable" },
  693. { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format}, "set the output printing format (available formats are: default, json)", "format" },
  694. { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
  695. { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
  696. { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
  697. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
  698. { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
  699. { NULL, },
  700. };
  701. int main(int argc, char **argv)
  702. {
  703. int ret;
  704. parse_loglevel(argc, argv, options);
  705. av_register_all();
  706. init_opts();
  707. #if CONFIG_AVDEVICE
  708. avdevice_register_all();
  709. #endif
  710. show_banner();
  711. parse_options(NULL, argc, argv, options, opt_input_file);
  712. if (!input_filename) {
  713. show_usage();
  714. fprintf(stderr, "You have to specify one input file.\n");
  715. fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
  716. exit(1);
  717. }
  718. ret = probe_file(input_filename);
  719. return ret;
  720. }