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.

895 lines
28KB

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