ffprobe.c 28 KB

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