ffprobe.c 30 KB

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