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.

1996 lines
65KB

  1. /*
  2. * Copyright (c) 2007-2010 Stefano Sabatini
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * simple media prober based on the FFmpeg libraries
  23. */
  24. #include "config.h"
  25. #include "version.h"
  26. #include "libavformat/avformat.h"
  27. #include "libavcodec/avcodec.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/bprint.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/dict.h"
  33. #include "libavutil/timecode.h"
  34. #include "libavdevice/avdevice.h"
  35. #include "libswscale/swscale.h"
  36. #include "libswresample/swresample.h"
  37. #include "libpostproc/postprocess.h"
  38. #include "cmdutils.h"
  39. const char program_name[] = "ffprobe";
  40. const int program_birth_year = 2007;
  41. static int do_count_frames = 0;
  42. static int do_count_packets = 0;
  43. static int do_read_frames = 0;
  44. static int do_read_packets = 0;
  45. static int do_show_error = 0;
  46. static int do_show_format = 0;
  47. static int do_show_frames = 0;
  48. static AVDictionary *fmt_entries_to_show = NULL;
  49. static int do_show_packets = 0;
  50. static int do_show_streams = 0;
  51. static int do_show_program_version = 0;
  52. static int do_show_library_versions = 0;
  53. static int show_value_unit = 0;
  54. static int use_value_prefix = 0;
  55. static int use_byte_value_binary_prefix = 0;
  56. static int use_value_sexagesimal_format = 0;
  57. static int show_private_data = 1;
  58. static char *print_format;
  59. static const OptionDef options[];
  60. /* FFprobe context */
  61. static const char *input_filename;
  62. static AVInputFormat *iformat = NULL;
  63. static const char *const binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
  64. static const char *const decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" };
  65. static const char unit_second_str[] = "s" ;
  66. static const char unit_hertz_str[] = "Hz" ;
  67. static const char unit_byte_str[] = "byte" ;
  68. static const char unit_bit_per_second_str[] = "bit/s";
  69. static uint64_t *nb_streams_packets;
  70. static uint64_t *nb_streams_frames;
  71. void av_noreturn exit_program(int ret)
  72. {
  73. av_dict_free(&fmt_entries_to_show);
  74. exit(ret);
  75. }
  76. struct unit_value {
  77. union { double d; long long int i; } val;
  78. const char *unit;
  79. };
  80. static char *value_string(char *buf, int buf_size, struct unit_value uv)
  81. {
  82. double vald;
  83. int show_float = 0;
  84. if (uv.unit == unit_second_str) {
  85. vald = uv.val.d;
  86. show_float = 1;
  87. } else {
  88. vald = uv.val.i;
  89. }
  90. if (uv.unit == unit_second_str && use_value_sexagesimal_format) {
  91. double secs;
  92. int hours, mins;
  93. secs = vald;
  94. mins = (int)secs / 60;
  95. secs = secs - mins * 60;
  96. hours = mins / 60;
  97. mins %= 60;
  98. snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
  99. } else {
  100. const char *prefix_string = "";
  101. int l;
  102. if (use_value_prefix && vald > 1) {
  103. long long int index;
  104. if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
  105. index = (long long int) (log(vald)/log(2)) / 10;
  106. index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
  107. vald /= pow(2, index * 10);
  108. prefix_string = binary_unit_prefixes[index];
  109. } else {
  110. index = (long long int) (log10(vald)) / 3;
  111. index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) - 1);
  112. vald /= pow(10, index * 3);
  113. prefix_string = decimal_unit_prefixes[index];
  114. }
  115. }
  116. if (show_float || (use_value_prefix && vald != (long long int)vald))
  117. l = snprintf(buf, buf_size, "%f", vald);
  118. else
  119. l = snprintf(buf, buf_size, "%lld", (long long int)vald);
  120. snprintf(buf+l, buf_size-l, "%s%s%s", *prefix_string || show_value_unit ? " " : "",
  121. prefix_string, show_value_unit ? uv.unit : "");
  122. }
  123. return buf;
  124. }
  125. /* WRITERS API */
  126. typedef struct WriterContext WriterContext;
  127. #define WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS 1
  128. #define WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER 2
  129. typedef struct Writer {
  130. int priv_size; ///< private size for the writer context
  131. const char *name;
  132. int (*init) (WriterContext *wctx, const char *args, void *opaque);
  133. void (*uninit)(WriterContext *wctx);
  134. void (*print_header)(WriterContext *ctx);
  135. void (*print_footer)(WriterContext *ctx);
  136. void (*print_chapter_header)(WriterContext *wctx, const char *);
  137. void (*print_chapter_footer)(WriterContext *wctx, const char *);
  138. void (*print_section_header)(WriterContext *wctx, const char *);
  139. void (*print_section_footer)(WriterContext *wctx, const char *);
  140. void (*print_integer) (WriterContext *wctx, const char *, long long int);
  141. void (*print_string) (WriterContext *wctx, const char *, const char *);
  142. void (*show_tags) (WriterContext *wctx, AVDictionary *dict);
  143. int flags; ///< a combination or WRITER_FLAG_*
  144. } Writer;
  145. struct WriterContext {
  146. const AVClass *class; ///< class of the writer
  147. const Writer *writer; ///< the Writer of which this is an instance
  148. char *name; ///< name of this writer instance
  149. void *priv; ///< private data for use by the filter
  150. unsigned int nb_item; ///< number of the item printed in the given section, starting at 0
  151. unsigned int nb_section; ///< number of the section printed in the given section sequence, starting at 0
  152. unsigned int nb_chapter; ///< number of the chapter, starting at 0
  153. int multiple_sections; ///< tells if the current chapter can contain multiple sections
  154. int is_fmt_chapter; ///< tells if the current chapter is "format", required by the print_format_entry option
  155. };
  156. static const char *writer_get_name(void *p)
  157. {
  158. WriterContext *wctx = p;
  159. return wctx->writer->name;
  160. }
  161. static const AVClass writer_class = {
  162. "Writer",
  163. writer_get_name,
  164. NULL,
  165. LIBAVUTIL_VERSION_INT,
  166. };
  167. static void writer_close(WriterContext **wctx)
  168. {
  169. if (!*wctx)
  170. return;
  171. if ((*wctx)->writer->uninit)
  172. (*wctx)->writer->uninit(*wctx);
  173. av_freep(&((*wctx)->priv));
  174. av_freep(wctx);
  175. }
  176. static int writer_open(WriterContext **wctx, const Writer *writer,
  177. const char *args, void *opaque)
  178. {
  179. int ret = 0;
  180. if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
  181. ret = AVERROR(ENOMEM);
  182. goto fail;
  183. }
  184. if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
  185. ret = AVERROR(ENOMEM);
  186. goto fail;
  187. }
  188. (*wctx)->class = &writer_class;
  189. (*wctx)->writer = writer;
  190. if ((*wctx)->writer->init)
  191. ret = (*wctx)->writer->init(*wctx, args, opaque);
  192. if (ret < 0)
  193. goto fail;
  194. return 0;
  195. fail:
  196. writer_close(wctx);
  197. return ret;
  198. }
  199. static inline void writer_print_header(WriterContext *wctx)
  200. {
  201. if (wctx->writer->print_header)
  202. wctx->writer->print_header(wctx);
  203. wctx->nb_chapter = 0;
  204. }
  205. static inline void writer_print_footer(WriterContext *wctx)
  206. {
  207. if (wctx->writer->print_footer)
  208. wctx->writer->print_footer(wctx);
  209. }
  210. static inline void writer_print_chapter_header(WriterContext *wctx,
  211. const char *chapter)
  212. {
  213. wctx->nb_section = 0;
  214. wctx->multiple_sections = !strcmp(chapter, "packets") || !strcmp(chapter, "frames" ) ||
  215. !strcmp(chapter, "packets_and_frames") ||
  216. !strcmp(chapter, "streams") || !strcmp(chapter, "library_versions");
  217. wctx->is_fmt_chapter = !strcmp(chapter, "format");
  218. if (wctx->writer->print_chapter_header)
  219. wctx->writer->print_chapter_header(wctx, chapter);
  220. }
  221. static inline void writer_print_chapter_footer(WriterContext *wctx,
  222. const char *chapter)
  223. {
  224. if (wctx->writer->print_chapter_footer)
  225. wctx->writer->print_chapter_footer(wctx, chapter);
  226. wctx->nb_chapter++;
  227. }
  228. static inline void writer_print_section_header(WriterContext *wctx,
  229. const char *section)
  230. {
  231. if (wctx->writer->print_section_header)
  232. wctx->writer->print_section_header(wctx, section);
  233. wctx->nb_item = 0;
  234. }
  235. static inline void writer_print_section_footer(WriterContext *wctx,
  236. const char *section)
  237. {
  238. if (wctx->writer->print_section_footer)
  239. wctx->writer->print_section_footer(wctx, section);
  240. wctx->nb_section++;
  241. }
  242. static inline void writer_print_integer(WriterContext *wctx,
  243. const char *key, long long int val)
  244. {
  245. if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  246. wctx->writer->print_integer(wctx, key, val);
  247. wctx->nb_item++;
  248. }
  249. }
  250. static inline void writer_print_string(WriterContext *wctx,
  251. const char *key, const char *val, int opt)
  252. {
  253. if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
  254. return;
  255. if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  256. wctx->writer->print_string(wctx, key, val);
  257. wctx->nb_item++;
  258. }
  259. }
  260. static void writer_print_time(WriterContext *wctx, const char *key,
  261. int64_t ts, const AVRational *time_base)
  262. {
  263. char buf[128];
  264. if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  265. if (ts == AV_NOPTS_VALUE) {
  266. writer_print_string(wctx, key, "N/A", 1);
  267. } else {
  268. double d = ts * av_q2d(*time_base);
  269. value_string(buf, sizeof(buf), (struct unit_value){.val.d=d, .unit=unit_second_str});
  270. writer_print_string(wctx, key, buf, 0);
  271. }
  272. }
  273. }
  274. static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts)
  275. {
  276. if (ts == AV_NOPTS_VALUE) {
  277. writer_print_string(wctx, key, "N/A", 1);
  278. } else {
  279. writer_print_integer(wctx, key, ts);
  280. }
  281. }
  282. static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
  283. {
  284. wctx->writer->show_tags(wctx, dict);
  285. }
  286. #define MAX_REGISTERED_WRITERS_NB 64
  287. static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
  288. static int writer_register(const Writer *writer)
  289. {
  290. static int next_registered_writer_idx = 0;
  291. if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
  292. return AVERROR(ENOMEM);
  293. registered_writers[next_registered_writer_idx++] = writer;
  294. return 0;
  295. }
  296. static const Writer *writer_get_by_name(const char *name)
  297. {
  298. int i;
  299. for (i = 0; registered_writers[i]; i++)
  300. if (!strcmp(registered_writers[i]->name, name))
  301. return registered_writers[i];
  302. return NULL;
  303. }
  304. /* WRITERS */
  305. /* Default output */
  306. typedef struct DefaultContext {
  307. const AVClass *class;
  308. int nokey;
  309. int noprint_wrappers;
  310. } DefaultContext;
  311. #define OFFSET(x) offsetof(DefaultContext, x)
  312. static const AVOption default_options[] = {
  313. { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  314. { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  315. { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  316. { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  317. {NULL},
  318. };
  319. static const char *default_get_name(void *ctx)
  320. {
  321. return "default";
  322. }
  323. static const AVClass default_class = {
  324. "DefaultContext",
  325. default_get_name,
  326. default_options
  327. };
  328. static av_cold int default_init(WriterContext *wctx, const char *args, void *opaque)
  329. {
  330. DefaultContext *def = wctx->priv;
  331. int err;
  332. def->class = &default_class;
  333. av_opt_set_defaults(def);
  334. if (args &&
  335. (err = (av_set_options_string(def, args, "=", ":"))) < 0) {
  336. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  337. return err;
  338. }
  339. return 0;
  340. }
  341. static void default_print_footer(WriterContext *wctx)
  342. {
  343. DefaultContext *def = wctx->priv;
  344. if (!def->noprint_wrappers)
  345. printf("\n");
  346. }
  347. static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
  348. {
  349. DefaultContext *def = wctx->priv;
  350. if (!def->noprint_wrappers && wctx->nb_chapter)
  351. printf("\n");
  352. }
  353. /* lame uppercasing routine, assumes the string is lower case ASCII */
  354. static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
  355. {
  356. int i;
  357. for (i = 0; src[i] && i < dst_size-1; i++)
  358. dst[i] = av_toupper(src[i]);
  359. dst[i] = 0;
  360. return dst;
  361. }
  362. static void default_print_section_header(WriterContext *wctx, const char *section)
  363. {
  364. DefaultContext *def = wctx->priv;
  365. char buf[32];
  366. if (wctx->nb_section)
  367. printf("\n");
  368. if (!def->noprint_wrappers)
  369. printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
  370. }
  371. static void default_print_section_footer(WriterContext *wctx, const char *section)
  372. {
  373. DefaultContext *def = wctx->priv;
  374. char buf[32];
  375. if (!def->noprint_wrappers)
  376. printf("[/%s]", upcase_string(buf, sizeof(buf), section));
  377. }
  378. static void default_print_str(WriterContext *wctx, const char *key, const char *value)
  379. {
  380. DefaultContext *def = wctx->priv;
  381. if (!def->nokey)
  382. printf("%s=", key);
  383. printf("%s\n", value);
  384. }
  385. static void default_print_int(WriterContext *wctx, const char *key, long long int value)
  386. {
  387. DefaultContext *def = wctx->priv;
  388. if (!def->nokey)
  389. printf("%s=", key);
  390. printf("%lld\n", value);
  391. }
  392. static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
  393. {
  394. AVDictionaryEntry *tag = NULL;
  395. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  396. if (!fmt_entries_to_show || (tag->key && av_dict_get(fmt_entries_to_show, tag->key, NULL, 0)))
  397. printf("TAG:");
  398. writer_print_string(wctx, tag->key, tag->value, 0);
  399. }
  400. }
  401. static const Writer default_writer = {
  402. .name = "default",
  403. .priv_size = sizeof(DefaultContext),
  404. .init = default_init,
  405. .print_footer = default_print_footer,
  406. .print_chapter_header = default_print_chapter_header,
  407. .print_section_header = default_print_section_header,
  408. .print_section_footer = default_print_section_footer,
  409. .print_integer = default_print_int,
  410. .print_string = default_print_str,
  411. .show_tags = default_show_tags,
  412. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
  413. };
  414. /* Compact output */
  415. /**
  416. * Apply C-language-like string escaping.
  417. */
  418. static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
  419. {
  420. const char *p;
  421. for (p = src; *p; p++) {
  422. switch (*p) {
  423. case '\b': av_bprintf(dst, "%s", "\\b"); break;
  424. case '\f': av_bprintf(dst, "%s", "\\f"); break;
  425. case '\n': av_bprintf(dst, "%s", "\\n"); break;
  426. case '\r': av_bprintf(dst, "%s", "\\r"); break;
  427. case '\\': av_bprintf(dst, "%s", "\\\\"); break;
  428. default:
  429. if (*p == sep)
  430. av_bprint_chars(dst, '\\', 1);
  431. av_bprint_chars(dst, *p, 1);
  432. }
  433. }
  434. return dst->str;
  435. }
  436. /**
  437. * Quote fields containing special characters, check RFC4180.
  438. */
  439. static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
  440. {
  441. const char *p;
  442. int quote = 0;
  443. /* check if input needs quoting */
  444. for (p = src; *p; p++)
  445. if (*p == '"' || *p == sep || *p == '\n' || *p == '\r')
  446. quote = 1;
  447. if (quote)
  448. av_bprint_chars(dst, '\"', 1);
  449. for (p = src; *p; p++) {
  450. if (*p == '"')
  451. av_bprint_chars(dst, '\"', 1);
  452. av_bprint_chars(dst, *p, 1);
  453. }
  454. if (quote)
  455. av_bprint_chars(dst, '\"', 1);
  456. return dst->str;
  457. }
  458. static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
  459. {
  460. return src;
  461. }
  462. typedef struct CompactContext {
  463. const AVClass *class;
  464. char *item_sep_str;
  465. char item_sep;
  466. int nokey;
  467. char *escape_mode_str;
  468. const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
  469. } CompactContext;
  470. #undef OFFSET
  471. #define OFFSET(x) offsetof(CompactContext, x)
  472. static const AVOption compact_options[]= {
  473. {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
  474. {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
  475. {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  476. {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  477. {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
  478. {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
  479. {NULL},
  480. };
  481. static const char *compact_get_name(void *ctx)
  482. {
  483. return "compact";
  484. }
  485. static const AVClass compact_class = {
  486. "CompactContext",
  487. compact_get_name,
  488. compact_options
  489. };
  490. static av_cold int compact_init(WriterContext *wctx, const char *args, void *opaque)
  491. {
  492. CompactContext *compact = wctx->priv;
  493. int err;
  494. compact->class = &compact_class;
  495. av_opt_set_defaults(compact);
  496. if (args &&
  497. (err = (av_set_options_string(compact, args, "=", ":"))) < 0) {
  498. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  499. return err;
  500. }
  501. if (strlen(compact->item_sep_str) != 1) {
  502. av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
  503. compact->item_sep_str);
  504. return AVERROR(EINVAL);
  505. }
  506. compact->item_sep = compact->item_sep_str[0];
  507. if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
  508. else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
  509. else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
  510. else {
  511. av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
  512. return AVERROR(EINVAL);
  513. }
  514. return 0;
  515. }
  516. static av_cold void compact_uninit(WriterContext *wctx)
  517. {
  518. CompactContext *compact = wctx->priv;
  519. av_freep(&compact->item_sep_str);
  520. av_freep(&compact->escape_mode_str);
  521. }
  522. static void compact_print_section_header(WriterContext *wctx, const char *section)
  523. {
  524. CompactContext *compact = wctx->priv;
  525. printf("%s%c", section, compact->item_sep);
  526. }
  527. static void compact_print_section_footer(WriterContext *wctx, const char *section)
  528. {
  529. printf("\n");
  530. }
  531. static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
  532. {
  533. CompactContext *compact = wctx->priv;
  534. AVBPrint buf;
  535. if (wctx->nb_item) printf("%c", compact->item_sep);
  536. if (!compact->nokey)
  537. printf("%s=", key);
  538. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  539. printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
  540. av_bprint_finalize(&buf, NULL);
  541. }
  542. static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
  543. {
  544. CompactContext *compact = wctx->priv;
  545. if (wctx->nb_item) printf("%c", compact->item_sep);
  546. if (!compact->nokey)
  547. printf("%s=", key);
  548. printf("%lld", value);
  549. }
  550. static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
  551. {
  552. CompactContext *compact = wctx->priv;
  553. AVDictionaryEntry *tag = NULL;
  554. AVBPrint buf;
  555. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  556. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  557. if (wctx->nb_item) printf("%c", compact->item_sep);
  558. if (!compact->nokey) {
  559. av_bprint_clear(&buf);
  560. printf("tag:%s=", compact->escape_str(&buf, tag->key, compact->item_sep, wctx));
  561. }
  562. av_bprint_clear(&buf);
  563. printf("%s", compact->escape_str(&buf, tag->value, compact->item_sep, wctx));
  564. }
  565. av_bprint_finalize(&buf, NULL);
  566. }
  567. static const Writer compact_writer = {
  568. .name = "compact",
  569. .priv_size = sizeof(CompactContext),
  570. .init = compact_init,
  571. .uninit = compact_uninit,
  572. .print_section_header = compact_print_section_header,
  573. .print_section_footer = compact_print_section_footer,
  574. .print_integer = compact_print_int,
  575. .print_string = compact_print_str,
  576. .show_tags = compact_show_tags,
  577. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
  578. };
  579. /* CSV output */
  580. static av_cold int csv_init(WriterContext *wctx, const char *args, void *opaque)
  581. {
  582. return compact_init(wctx, "item_sep=,:nokey=1:escape=csv", opaque);
  583. }
  584. static const Writer csv_writer = {
  585. .name = "csv",
  586. .priv_size = sizeof(CompactContext),
  587. .init = csv_init,
  588. .uninit = compact_uninit,
  589. .print_section_header = compact_print_section_header,
  590. .print_section_footer = compact_print_section_footer,
  591. .print_integer = compact_print_int,
  592. .print_string = compact_print_str,
  593. .show_tags = compact_show_tags,
  594. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
  595. };
  596. /* INI format output */
  597. typedef struct {
  598. const AVClass *class;
  599. AVBPrint chapter_name, section_name;
  600. int print_packets_and_frames;
  601. int nb_frame;
  602. int nb_packet;
  603. int hierarchical;
  604. } INIContext;
  605. #undef OFFSET
  606. #define OFFSET(x) offsetof(INIContext, x)
  607. static const AVOption ini_options[] = {
  608. {"hierachical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
  609. {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
  610. {NULL},
  611. };
  612. static const char *ini_get_name(void *ctx)
  613. {
  614. return "ini";
  615. }
  616. static const AVClass ini_class = {
  617. "INIContext",
  618. ini_get_name,
  619. ini_options
  620. };
  621. static av_cold int ini_init(WriterContext *wctx, const char *args, void *opaque)
  622. {
  623. INIContext *ini = wctx->priv;
  624. int err;
  625. av_bprint_init(&ini->chapter_name, 1, AV_BPRINT_SIZE_UNLIMITED);
  626. av_bprint_init(&ini->section_name, 1, AV_BPRINT_SIZE_UNLIMITED);
  627. ini->nb_frame = ini->nb_packet = 0;
  628. ini->class = &ini_class;
  629. av_opt_set_defaults(ini);
  630. if (args && (err = av_set_options_string(ini, args, "=", ":")) < 0) {
  631. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  632. return err;
  633. }
  634. return 0;
  635. }
  636. static av_cold void ini_uninit(WriterContext *wctx)
  637. {
  638. INIContext *ini = wctx->priv;
  639. av_bprint_finalize(&ini->chapter_name, NULL);
  640. av_bprint_finalize(&ini->section_name, NULL);
  641. }
  642. static void ini_print_header(WriterContext *wctx)
  643. {
  644. printf("# ffprobe output\n\n");
  645. }
  646. static char *ini_escape_str(AVBPrint *dst, const char *src)
  647. {
  648. int i = 0;
  649. char c = 0;
  650. while (c = src[i++]) {
  651. switch (c) {
  652. case '\b': av_bprintf(dst, "%s", "\\b"); break;
  653. case '\f': av_bprintf(dst, "%s", "\\f"); break;
  654. case '\n': av_bprintf(dst, "%s", "\\n"); break;
  655. case '\r': av_bprintf(dst, "%s", "\\r"); break;
  656. case '\t': av_bprintf(dst, "%s", "\\t"); break;
  657. case '\\':
  658. case '#' :
  659. case '=' :
  660. case ':' : av_bprint_chars(dst, '\\', 1);
  661. default:
  662. if ((unsigned char)c < 32)
  663. av_bprintf(dst, "\\x00%02x", c & 0xff);
  664. else
  665. av_bprint_chars(dst, c, 1);
  666. break;
  667. }
  668. }
  669. return dst->str;
  670. }
  671. static void ini_print_chapter_header(WriterContext *wctx, const char *chapter)
  672. {
  673. INIContext *ini = wctx->priv;
  674. av_bprint_clear(&ini->chapter_name);
  675. av_bprintf(&ini->chapter_name, "%s", chapter);
  676. if (wctx->nb_chapter)
  677. printf("\n");
  678. ini->print_packets_and_frames = !strcmp("packets_and_frames", chapter);
  679. }
  680. static void ini_print_section_header(WriterContext *wctx, const char *section)
  681. {
  682. INIContext *ini = wctx->priv;
  683. int n;
  684. if (wctx->nb_section)
  685. printf("\n");
  686. av_bprint_clear(&ini->section_name);
  687. if (ini->hierarchical && wctx->multiple_sections)
  688. av_bprintf(&ini->section_name, "%s.", ini->chapter_name.str);
  689. av_bprintf(&ini->section_name, "%s", section);
  690. if (ini->print_packets_and_frames)
  691. n = !strcmp(section, "packet") ? ini->nb_packet++ : ini->nb_frame++;
  692. else
  693. n = wctx->nb_section;
  694. if (wctx->multiple_sections)
  695. av_bprintf(&ini->section_name, ".%d", n);
  696. printf("[%s]\n", ini->section_name.str);
  697. }
  698. static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
  699. {
  700. AVBPrint buf;
  701. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  702. printf("%s=", ini_escape_str(&buf, key));
  703. av_bprint_clear(&buf);
  704. printf("%s\n", ini_escape_str(&buf, value));
  705. av_bprint_finalize(&buf, NULL);
  706. }
  707. static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
  708. {
  709. printf("%s=%lld\n", key, value);
  710. }
  711. static void ini_show_tags(WriterContext *wctx, AVDictionary *dict)
  712. {
  713. INIContext *ini = wctx->priv;
  714. AVDictionaryEntry *tag = NULL;
  715. int is_first = 1;
  716. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  717. if (is_first) {
  718. printf("\n[%s.tags]\n", ini->section_name.str);
  719. is_first = 0;
  720. }
  721. writer_print_string(wctx, tag->key, tag->value, 0);
  722. }
  723. }
  724. static const Writer ini_writer = {
  725. .name = "ini",
  726. .priv_size = sizeof(INIContext),
  727. .init = ini_init,
  728. .uninit = ini_uninit,
  729. .print_header = ini_print_header,
  730. .print_chapter_header = ini_print_chapter_header,
  731. .print_section_header = ini_print_section_header,
  732. .print_integer = ini_print_int,
  733. .print_string = ini_print_str,
  734. .show_tags = ini_show_tags,
  735. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  736. };
  737. /* JSON output */
  738. typedef struct {
  739. const AVClass *class;
  740. int print_packets_and_frames;
  741. int indent_level;
  742. int compact;
  743. const char *item_sep, *item_start_end;
  744. } JSONContext;
  745. #undef OFFSET
  746. #define OFFSET(x) offsetof(JSONContext, x)
  747. static const AVOption json_options[]= {
  748. { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  749. { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  750. { NULL }
  751. };
  752. static const char *json_get_name(void *ctx)
  753. {
  754. return "json";
  755. }
  756. static const AVClass json_class = {
  757. "JSONContext",
  758. json_get_name,
  759. json_options
  760. };
  761. static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
  762. {
  763. JSONContext *json = wctx->priv;
  764. int err;
  765. json->class = &json_class;
  766. av_opt_set_defaults(json);
  767. if (args &&
  768. (err = (av_set_options_string(json, args, "=", ":"))) < 0) {
  769. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  770. return err;
  771. }
  772. json->item_sep = json->compact ? ", " : ",\n";
  773. json->item_start_end = json->compact ? " " : "\n";
  774. return 0;
  775. }
  776. static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
  777. {
  778. static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
  779. static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
  780. const char *p;
  781. for (p = src; *p; p++) {
  782. char *s = strchr(json_escape, *p);
  783. if (s) {
  784. av_bprint_chars(dst, '\\', 1);
  785. av_bprint_chars(dst, json_subst[s - json_escape], 1);
  786. } else if ((unsigned char)*p < 32) {
  787. av_bprintf(dst, "\\u00%02x", *p & 0xff);
  788. } else {
  789. av_bprint_chars(dst, *p, 1);
  790. }
  791. }
  792. return dst->str;
  793. }
  794. static void json_print_header(WriterContext *wctx)
  795. {
  796. JSONContext *json = wctx->priv;
  797. printf("{");
  798. json->indent_level++;
  799. }
  800. static void json_print_footer(WriterContext *wctx)
  801. {
  802. JSONContext *json = wctx->priv;
  803. json->indent_level--;
  804. printf("\n}\n");
  805. }
  806. #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
  807. static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
  808. {
  809. JSONContext *json = wctx->priv;
  810. AVBPrint buf;
  811. if (wctx->nb_chapter)
  812. printf(",");
  813. printf("\n");
  814. if (wctx->multiple_sections) {
  815. JSON_INDENT();
  816. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  817. printf("\"%s\": [\n", json_escape_str(&buf, chapter, wctx));
  818. av_bprint_finalize(&buf, NULL);
  819. json->print_packets_and_frames = !strcmp(chapter, "packets_and_frames");
  820. json->indent_level++;
  821. }
  822. }
  823. static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
  824. {
  825. JSONContext *json = wctx->priv;
  826. if (wctx->multiple_sections) {
  827. printf("\n");
  828. json->indent_level--;
  829. JSON_INDENT();
  830. printf("]");
  831. }
  832. }
  833. static void json_print_section_header(WriterContext *wctx, const char *section)
  834. {
  835. JSONContext *json = wctx->priv;
  836. if (wctx->nb_section)
  837. printf(",\n");
  838. JSON_INDENT();
  839. if (!wctx->multiple_sections)
  840. printf("\"%s\": ", section);
  841. printf("{%s", json->item_start_end);
  842. json->indent_level++;
  843. /* this is required so the parser can distinguish between packets and frames */
  844. if (json->print_packets_and_frames) {
  845. if (!json->compact)
  846. JSON_INDENT();
  847. printf("\"type\": \"%s\"%s", section, json->item_sep);
  848. }
  849. }
  850. static void json_print_section_footer(WriterContext *wctx, const char *section)
  851. {
  852. JSONContext *json = wctx->priv;
  853. printf("%s", json->item_start_end);
  854. json->indent_level--;
  855. if (!json->compact)
  856. JSON_INDENT();
  857. printf("}");
  858. }
  859. static inline void json_print_item_str(WriterContext *wctx,
  860. const char *key, const char *value)
  861. {
  862. AVBPrint buf;
  863. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  864. printf("\"%s\":", json_escape_str(&buf, key, wctx));
  865. av_bprint_clear(&buf);
  866. printf(" \"%s\"", json_escape_str(&buf, value, wctx));
  867. av_bprint_finalize(&buf, NULL);
  868. }
  869. static void json_print_str(WriterContext *wctx, const char *key, const char *value)
  870. {
  871. JSONContext *json = wctx->priv;
  872. if (wctx->nb_item) printf("%s", json->item_sep);
  873. if (!json->compact)
  874. JSON_INDENT();
  875. json_print_item_str(wctx, key, value);
  876. }
  877. static void json_print_int(WriterContext *wctx, const char *key, long long int value)
  878. {
  879. JSONContext *json = wctx->priv;
  880. AVBPrint buf;
  881. if (wctx->nb_item) printf("%s", json->item_sep);
  882. if (!json->compact)
  883. JSON_INDENT();
  884. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  885. printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
  886. av_bprint_finalize(&buf, NULL);
  887. }
  888. static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
  889. {
  890. JSONContext *json = wctx->priv;
  891. AVDictionaryEntry *tag = NULL;
  892. int is_first = 1;
  893. if (!dict)
  894. return;
  895. printf("%s", json->item_sep);
  896. if (!json->compact)
  897. JSON_INDENT();
  898. printf("\"tags\": {%s", json->item_start_end);
  899. json->indent_level++;
  900. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  901. if (is_first) is_first = 0;
  902. else printf("%s", json->item_sep);
  903. if (!json->compact)
  904. JSON_INDENT();
  905. json_print_item_str(wctx, tag->key, tag->value);
  906. }
  907. json->indent_level--;
  908. printf("%s", json->item_start_end);
  909. if (!json->compact)
  910. JSON_INDENT();
  911. printf("}");
  912. }
  913. static const Writer json_writer = {
  914. .name = "json",
  915. .priv_size = sizeof(JSONContext),
  916. .init = json_init,
  917. .print_header = json_print_header,
  918. .print_footer = json_print_footer,
  919. .print_chapter_header = json_print_chapter_header,
  920. .print_chapter_footer = json_print_chapter_footer,
  921. .print_section_header = json_print_section_header,
  922. .print_section_footer = json_print_section_footer,
  923. .print_integer = json_print_int,
  924. .print_string = json_print_str,
  925. .show_tags = json_show_tags,
  926. .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  927. };
  928. /* XML output */
  929. typedef struct {
  930. const AVClass *class;
  931. int within_tag;
  932. int multiple_entries; ///< tells if the given chapter requires multiple entries
  933. int indent_level;
  934. int fully_qualified;
  935. int xsd_strict;
  936. } XMLContext;
  937. #undef OFFSET
  938. #define OFFSET(x) offsetof(XMLContext, x)
  939. static const AVOption xml_options[] = {
  940. {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  941. {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  942. {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  943. {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  944. {NULL},
  945. };
  946. static const char *xml_get_name(void *ctx)
  947. {
  948. return "xml";
  949. }
  950. static const AVClass xml_class = {
  951. "XMLContext",
  952. xml_get_name,
  953. xml_options
  954. };
  955. static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
  956. {
  957. XMLContext *xml = wctx->priv;
  958. int err;
  959. xml->class = &xml_class;
  960. av_opt_set_defaults(xml);
  961. if (args &&
  962. (err = (av_set_options_string(xml, args, "=", ":"))) < 0) {
  963. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  964. return err;
  965. }
  966. if (xml->xsd_strict) {
  967. xml->fully_qualified = 1;
  968. #define CHECK_COMPLIANCE(opt, opt_name) \
  969. if (opt) { \
  970. av_log(wctx, AV_LOG_ERROR, \
  971. "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
  972. "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
  973. return AVERROR(EINVAL); \
  974. }
  975. CHECK_COMPLIANCE(show_private_data, "private");
  976. CHECK_COMPLIANCE(show_value_unit, "unit");
  977. CHECK_COMPLIANCE(use_value_prefix, "prefix");
  978. if (do_show_frames && do_show_packets) {
  979. av_log(wctx, AV_LOG_ERROR,
  980. "Interleaved frames and packets are not allowed in XSD. "
  981. "Select only one between the -show_frames and the -show_packets options.\n");
  982. return AVERROR(EINVAL);
  983. }
  984. }
  985. return 0;
  986. }
  987. static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
  988. {
  989. const char *p;
  990. for (p = src; *p; p++) {
  991. switch (*p) {
  992. case '&' : av_bprintf(dst, "%s", "&amp;"); break;
  993. case '<' : av_bprintf(dst, "%s", "&lt;"); break;
  994. case '>' : av_bprintf(dst, "%s", "&gt;"); break;
  995. case '\"': av_bprintf(dst, "%s", "&quot;"); break;
  996. case '\'': av_bprintf(dst, "%s", "&apos;"); break;
  997. default: av_bprint_chars(dst, *p, 1);
  998. }
  999. }
  1000. return dst->str;
  1001. }
  1002. static void xml_print_header(WriterContext *wctx)
  1003. {
  1004. XMLContext *xml = wctx->priv;
  1005. const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
  1006. "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
  1007. "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
  1008. printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  1009. printf("<%sffprobe%s>\n",
  1010. xml->fully_qualified ? "ffprobe:" : "",
  1011. xml->fully_qualified ? qual : "");
  1012. xml->indent_level++;
  1013. }
  1014. static void xml_print_footer(WriterContext *wctx)
  1015. {
  1016. XMLContext *xml = wctx->priv;
  1017. xml->indent_level--;
  1018. printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
  1019. }
  1020. #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
  1021. static void xml_print_chapter_header(WriterContext *wctx, const char *chapter)
  1022. {
  1023. XMLContext *xml = wctx->priv;
  1024. if (wctx->nb_chapter)
  1025. printf("\n");
  1026. xml->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "frames") ||
  1027. !strcmp(chapter, "packets_and_frames") ||
  1028. !strcmp(chapter, "streams") || !strcmp(chapter, "library_versions");
  1029. if (xml->multiple_entries) {
  1030. XML_INDENT(); printf("<%s>\n", chapter);
  1031. xml->indent_level++;
  1032. }
  1033. }
  1034. static void xml_print_chapter_footer(WriterContext *wctx, const char *chapter)
  1035. {
  1036. XMLContext *xml = wctx->priv;
  1037. if (xml->multiple_entries) {
  1038. xml->indent_level--;
  1039. XML_INDENT(); printf("</%s>\n", chapter);
  1040. }
  1041. }
  1042. static void xml_print_section_header(WriterContext *wctx, const char *section)
  1043. {
  1044. XMLContext *xml = wctx->priv;
  1045. XML_INDENT(); printf("<%s ", section);
  1046. xml->within_tag = 1;
  1047. }
  1048. static void xml_print_section_footer(WriterContext *wctx, const char *section)
  1049. {
  1050. XMLContext *xml = wctx->priv;
  1051. if (xml->within_tag)
  1052. printf("/>\n");
  1053. else {
  1054. XML_INDENT(); printf("</%s>\n", section);
  1055. }
  1056. }
  1057. static void xml_print_str(WriterContext *wctx, const char *key, const char *value)
  1058. {
  1059. AVBPrint buf;
  1060. if (wctx->nb_item)
  1061. printf(" ");
  1062. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1063. printf("%s=\"%s\"", key, xml_escape_str(&buf, value, wctx));
  1064. av_bprint_finalize(&buf, NULL);
  1065. }
  1066. static void xml_print_int(WriterContext *wctx, const char *key, long long int value)
  1067. {
  1068. if (wctx->nb_item)
  1069. printf(" ");
  1070. printf("%s=\"%lld\"", key, value);
  1071. }
  1072. static void xml_show_tags(WriterContext *wctx, AVDictionary *dict)
  1073. {
  1074. XMLContext *xml = wctx->priv;
  1075. AVDictionaryEntry *tag = NULL;
  1076. int is_first = 1;
  1077. AVBPrint buf;
  1078. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1079. xml->indent_level++;
  1080. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  1081. if (is_first) {
  1082. /* close section tag */
  1083. printf(">\n");
  1084. xml->within_tag = 0;
  1085. is_first = 0;
  1086. }
  1087. XML_INDENT();
  1088. av_bprint_clear(&buf);
  1089. printf("<tag key=\"%s\"", xml_escape_str(&buf, tag->key, wctx));
  1090. av_bprint_clear(&buf);
  1091. printf(" value=\"%s\"/>\n", xml_escape_str(&buf, tag->value, wctx));
  1092. }
  1093. av_bprint_finalize(&buf, NULL);
  1094. xml->indent_level--;
  1095. }
  1096. static Writer xml_writer = {
  1097. .name = "xml",
  1098. .priv_size = sizeof(XMLContext),
  1099. .init = xml_init,
  1100. .print_header = xml_print_header,
  1101. .print_footer = xml_print_footer,
  1102. .print_chapter_header = xml_print_chapter_header,
  1103. .print_chapter_footer = xml_print_chapter_footer,
  1104. .print_section_header = xml_print_section_header,
  1105. .print_section_footer = xml_print_section_footer,
  1106. .print_integer = xml_print_int,
  1107. .print_string = xml_print_str,
  1108. .show_tags = xml_show_tags,
  1109. .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  1110. };
  1111. static void writer_register_all(void)
  1112. {
  1113. static int initialized;
  1114. if (initialized)
  1115. return;
  1116. initialized = 1;
  1117. writer_register(&default_writer);
  1118. writer_register(&compact_writer);
  1119. writer_register(&csv_writer);
  1120. writer_register(&ini_writer);
  1121. writer_register(&json_writer);
  1122. writer_register(&xml_writer);
  1123. }
  1124. #define print_fmt(k, f, ...) do { \
  1125. av_bprint_clear(&pbuf); \
  1126. av_bprintf(&pbuf, f, __VA_ARGS__); \
  1127. writer_print_string(w, k, pbuf.str, 0); \
  1128. } while (0)
  1129. #define print_int(k, v) writer_print_integer(w, k, v)
  1130. #define print_str(k, v) writer_print_string(w, k, v, 0)
  1131. #define print_str_opt(k, v) writer_print_string(w, k, v, 1)
  1132. #define print_time(k, v, tb) writer_print_time(w, k, v, tb)
  1133. #define print_ts(k, v) writer_print_ts(w, k, v)
  1134. #define print_val(k, v, u) writer_print_string(w, k, \
  1135. value_string(val_str, sizeof(val_str), (struct unit_value){.val.i = v, .unit=u}), 0)
  1136. #define print_section_header(s) writer_print_section_header(w, s)
  1137. #define print_section_footer(s) writer_print_section_footer(w, s)
  1138. #define show_tags(metadata) writer_show_tags(w, metadata)
  1139. static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pkt, int packet_idx)
  1140. {
  1141. char val_str[128];
  1142. AVStream *st = fmt_ctx->streams[pkt->stream_index];
  1143. AVBPrint pbuf;
  1144. const char *s;
  1145. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1146. print_section_header("packet");
  1147. s = av_get_media_type_string(st->codec->codec_type);
  1148. if (s) print_str ("codec_type", s);
  1149. else print_str_opt("codec_type", "unknown");
  1150. print_int("stream_index", pkt->stream_index);
  1151. print_ts ("pts", pkt->pts);
  1152. print_time("pts_time", pkt->pts, &st->time_base);
  1153. print_ts ("dts", pkt->dts);
  1154. print_time("dts_time", pkt->dts, &st->time_base);
  1155. print_ts ("duration", pkt->duration);
  1156. print_time("duration_time", pkt->duration, &st->time_base);
  1157. print_val("size", pkt->size, unit_byte_str);
  1158. if (pkt->pos != -1) print_fmt ("pos", "%"PRId64, pkt->pos);
  1159. else print_str_opt("pos", "N/A");
  1160. print_fmt("flags", "%c", pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
  1161. print_section_footer("packet");
  1162. av_bprint_finalize(&pbuf, NULL);
  1163. fflush(stdout);
  1164. }
  1165. static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream)
  1166. {
  1167. AVBPrint pbuf;
  1168. const char *s;
  1169. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1170. print_section_header("frame");
  1171. s = av_get_media_type_string(stream->codec->codec_type);
  1172. if (s) print_str ("media_type", s);
  1173. else print_str_opt("media_type", "unknown");
  1174. print_int("key_frame", frame->key_frame);
  1175. print_ts ("pkt_pts", frame->pkt_pts);
  1176. print_time("pkt_pts_time", frame->pkt_pts, &stream->time_base);
  1177. print_ts ("pkt_dts", frame->pkt_dts);
  1178. print_time("pkt_dts_time", frame->pkt_dts, &stream->time_base);
  1179. if (frame->pkt_pos != -1) print_fmt ("pkt_pos", "%"PRId64, frame->pkt_pos);
  1180. else print_str_opt("pkt_pos", "N/A");
  1181. switch (stream->codec->codec_type) {
  1182. case AVMEDIA_TYPE_VIDEO:
  1183. print_int("width", frame->width);
  1184. print_int("height", frame->height);
  1185. s = av_get_pix_fmt_name(frame->format);
  1186. if (s) print_str ("pix_fmt", s);
  1187. else print_str_opt("pix_fmt", "unknown");
  1188. if (frame->sample_aspect_ratio.num) {
  1189. print_fmt("sample_aspect_ratio", "%d:%d",
  1190. frame->sample_aspect_ratio.num,
  1191. frame->sample_aspect_ratio.den);
  1192. } else {
  1193. print_str_opt("sample_aspect_ratio", "N/A");
  1194. }
  1195. print_fmt("pict_type", "%c", av_get_picture_type_char(frame->pict_type));
  1196. print_int("coded_picture_number", frame->coded_picture_number);
  1197. print_int("display_picture_number", frame->display_picture_number);
  1198. print_int("interlaced_frame", frame->interlaced_frame);
  1199. print_int("top_field_first", frame->top_field_first);
  1200. print_int("repeat_pict", frame->repeat_pict);
  1201. print_int("reference", frame->reference);
  1202. break;
  1203. case AVMEDIA_TYPE_AUDIO:
  1204. s = av_get_sample_fmt_name(frame->format);
  1205. if (s) print_str ("sample_fmt", s);
  1206. else print_str_opt("sample_fmt", "unknown");
  1207. print_int("nb_samples", frame->nb_samples);
  1208. break;
  1209. }
  1210. print_section_footer("frame");
  1211. av_bprint_finalize(&pbuf, NULL);
  1212. fflush(stdout);
  1213. }
  1214. static av_always_inline int get_decoded_frame(AVFormatContext *fmt_ctx,
  1215. AVFrame *frame, int *got_frame,
  1216. AVPacket *pkt)
  1217. {
  1218. AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
  1219. int ret = 0;
  1220. *got_frame = 0;
  1221. switch (dec_ctx->codec_type) {
  1222. case AVMEDIA_TYPE_VIDEO:
  1223. ret = avcodec_decode_video2(dec_ctx, frame, got_frame, pkt);
  1224. break;
  1225. case AVMEDIA_TYPE_AUDIO:
  1226. ret = avcodec_decode_audio4(dec_ctx, frame, got_frame, pkt);
  1227. break;
  1228. }
  1229. return ret;
  1230. }
  1231. static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
  1232. {
  1233. AVPacket pkt, pkt1;
  1234. AVFrame frame;
  1235. int i = 0, ret, got_frame;
  1236. av_init_packet(&pkt);
  1237. while (!av_read_frame(fmt_ctx, &pkt)) {
  1238. if (do_read_packets) {
  1239. if (do_show_packets)
  1240. show_packet(w, fmt_ctx, &pkt, i++);
  1241. nb_streams_packets[pkt.stream_index]++;
  1242. }
  1243. if (do_read_frames) {
  1244. pkt1 = pkt;
  1245. while (pkt1.size) {
  1246. avcodec_get_frame_defaults(&frame);
  1247. ret = get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt1);
  1248. if (ret < 0 || !got_frame)
  1249. break;
  1250. if (do_show_frames)
  1251. show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
  1252. pkt1.data += ret;
  1253. pkt1.size -= ret;
  1254. nb_streams_frames[pkt.stream_index]++;
  1255. }
  1256. }
  1257. av_free_packet(&pkt);
  1258. }
  1259. av_init_packet(&pkt);
  1260. pkt.data = NULL;
  1261. pkt.size = 0;
  1262. //Flush remaining frames that are cached in the decoder
  1263. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  1264. pkt.stream_index = i;
  1265. while (get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt) >= 0 && got_frame) {
  1266. if (do_read_frames) {
  1267. if (do_show_frames)
  1268. show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
  1269. nb_streams_frames[pkt.stream_index]++;
  1270. }
  1271. }
  1272. }
  1273. }
  1274. static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_idx)
  1275. {
  1276. AVStream *stream = fmt_ctx->streams[stream_idx];
  1277. AVCodecContext *dec_ctx;
  1278. AVCodec *dec;
  1279. char val_str[128];
  1280. const char *s;
  1281. AVRational display_aspect_ratio;
  1282. AVBPrint pbuf;
  1283. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1284. print_section_header("stream");
  1285. print_int("index", stream->index);
  1286. if ((dec_ctx = stream->codec)) {
  1287. const char *profile = NULL;
  1288. if ((dec = dec_ctx->codec)) {
  1289. print_str("codec_name", dec->name);
  1290. print_str("codec_long_name", dec->long_name);
  1291. } else {
  1292. print_str_opt("codec_name", "unknown");
  1293. print_str_opt("codec_long_name", "unknown");
  1294. }
  1295. if (dec && (profile = av_get_profile_name(dec, dec_ctx->profile)))
  1296. print_str("profile", profile);
  1297. else
  1298. print_str_opt("profile", "unknown");
  1299. s = av_get_media_type_string(dec_ctx->codec_type);
  1300. if (s) print_str ("codec_type", s);
  1301. else print_str_opt("codec_type", "unknown");
  1302. print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
  1303. /* print AVI/FourCC tag */
  1304. av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
  1305. print_str("codec_tag_string", val_str);
  1306. print_fmt("codec_tag", "0x%04x", dec_ctx->codec_tag);
  1307. switch (dec_ctx->codec_type) {
  1308. case AVMEDIA_TYPE_VIDEO:
  1309. print_int("width", dec_ctx->width);
  1310. print_int("height", dec_ctx->height);
  1311. print_int("has_b_frames", dec_ctx->has_b_frames);
  1312. if (dec_ctx->sample_aspect_ratio.num) {
  1313. print_fmt("sample_aspect_ratio", "%d:%d",
  1314. dec_ctx->sample_aspect_ratio.num,
  1315. dec_ctx->sample_aspect_ratio.den);
  1316. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1317. dec_ctx->width * dec_ctx->sample_aspect_ratio.num,
  1318. dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
  1319. 1024*1024);
  1320. print_fmt("display_aspect_ratio", "%d:%d",
  1321. display_aspect_ratio.num,
  1322. display_aspect_ratio.den);
  1323. } else {
  1324. print_str_opt("sample_aspect_ratio", "N/A");
  1325. print_str_opt("display_aspect_ratio", "N/A");
  1326. }
  1327. s = av_get_pix_fmt_name(dec_ctx->pix_fmt);
  1328. if (s) print_str ("pix_fmt", s);
  1329. else print_str_opt("pix_fmt", "unknown");
  1330. print_int("level", dec_ctx->level);
  1331. if (dec_ctx->timecode_frame_start >= 0) {
  1332. char tcbuf[AV_TIMECODE_STR_SIZE];
  1333. av_timecode_make_mpeg_tc_string(tcbuf, dec_ctx->timecode_frame_start);
  1334. print_str("timecode", tcbuf);
  1335. } else {
  1336. print_str_opt("timecode", "N/A");
  1337. }
  1338. break;
  1339. case AVMEDIA_TYPE_AUDIO:
  1340. s = av_get_sample_fmt_name(dec_ctx->sample_fmt);
  1341. if (s) print_str ("sample_fmt", s);
  1342. else print_str_opt("sample_fmt", "unknown");
  1343. print_val("sample_rate", dec_ctx->sample_rate, unit_hertz_str);
  1344. print_int("channels", dec_ctx->channels);
  1345. print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
  1346. break;
  1347. }
  1348. } else {
  1349. print_str_opt("codec_type", "unknown");
  1350. }
  1351. if (dec_ctx->codec && dec_ctx->codec->priv_class && show_private_data) {
  1352. const AVOption *opt = NULL;
  1353. while (opt = av_opt_next(dec_ctx->priv_data,opt)) {
  1354. uint8_t *str;
  1355. if (opt->flags) continue;
  1356. if (av_opt_get(dec_ctx->priv_data, opt->name, 0, &str) >= 0) {
  1357. print_str(opt->name, str);
  1358. av_free(str);
  1359. }
  1360. }
  1361. }
  1362. if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt ("id", "0x%x", stream->id);
  1363. else print_str_opt("id", "N/A");
  1364. print_fmt("r_frame_rate", "%d/%d", stream->r_frame_rate.num, stream->r_frame_rate.den);
  1365. print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
  1366. print_fmt("time_base", "%d/%d", stream->time_base.num, stream->time_base.den);
  1367. print_time("start_time", stream->start_time, &stream->time_base);
  1368. print_time("duration", stream->duration, &stream->time_base);
  1369. if (dec_ctx->bit_rate > 0) print_val ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
  1370. else print_str_opt("bit_rate", "N/A");
  1371. if (stream->nb_frames) print_fmt ("nb_frames", "%"PRId64, stream->nb_frames);
  1372. else print_str_opt("nb_frames", "N/A");
  1373. if (nb_streams_frames[stream_idx]) print_fmt ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
  1374. else print_str_opt("nb_read_frames", "N/A");
  1375. if (nb_streams_packets[stream_idx]) print_fmt ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
  1376. else print_str_opt("nb_read_packets", "N/A");
  1377. show_tags(stream->metadata);
  1378. print_section_footer("stream");
  1379. av_bprint_finalize(&pbuf, NULL);
  1380. fflush(stdout);
  1381. }
  1382. static void show_streams(WriterContext *w, AVFormatContext *fmt_ctx)
  1383. {
  1384. int i;
  1385. for (i = 0; i < fmt_ctx->nb_streams; i++)
  1386. show_stream(w, fmt_ctx, i);
  1387. }
  1388. static void show_format(WriterContext *w, AVFormatContext *fmt_ctx)
  1389. {
  1390. char val_str[128];
  1391. int64_t size = fmt_ctx->pb ? avio_size(fmt_ctx->pb) : -1;
  1392. print_section_header("format");
  1393. print_str("filename", fmt_ctx->filename);
  1394. print_int("nb_streams", fmt_ctx->nb_streams);
  1395. print_str("format_name", fmt_ctx->iformat->name);
  1396. print_str("format_long_name", fmt_ctx->iformat->long_name);
  1397. print_time("start_time", fmt_ctx->start_time, &AV_TIME_BASE_Q);
  1398. print_time("duration", fmt_ctx->duration, &AV_TIME_BASE_Q);
  1399. if (size >= 0) print_val ("size", size, unit_byte_str);
  1400. else print_str_opt("size", "N/A");
  1401. if (fmt_ctx->bit_rate > 0) print_val ("bit_rate", fmt_ctx->bit_rate, unit_bit_per_second_str);
  1402. else print_str_opt("bit_rate", "N/A");
  1403. show_tags(fmt_ctx->metadata);
  1404. print_section_footer("format");
  1405. fflush(stdout);
  1406. }
  1407. static void show_error(WriterContext *w, int err)
  1408. {
  1409. char errbuf[128];
  1410. const char *errbuf_ptr = errbuf;
  1411. if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
  1412. errbuf_ptr = strerror(AVUNERROR(err));
  1413. writer_print_chapter_header(w, "error");
  1414. print_section_header("error");
  1415. print_int("code", err);
  1416. print_str("string", errbuf_ptr);
  1417. print_section_footer("error");
  1418. writer_print_chapter_footer(w, "error");
  1419. }
  1420. static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
  1421. {
  1422. int err, i;
  1423. AVFormatContext *fmt_ctx = NULL;
  1424. AVDictionaryEntry *t;
  1425. if ((err = avformat_open_input(&fmt_ctx, filename,
  1426. iformat, &format_opts)) < 0) {
  1427. print_error(filename, err);
  1428. return err;
  1429. }
  1430. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  1431. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  1432. return AVERROR_OPTION_NOT_FOUND;
  1433. }
  1434. /* fill the streams in the format context */
  1435. if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
  1436. print_error(filename, err);
  1437. return err;
  1438. }
  1439. av_dump_format(fmt_ctx, 0, filename, 0);
  1440. /* bind a decoder to each input stream */
  1441. for (i = 0; i < fmt_ctx->nb_streams; i++) {
  1442. AVStream *stream = fmt_ctx->streams[i];
  1443. AVCodec *codec;
  1444. if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
  1445. av_log(NULL, AV_LOG_ERROR,
  1446. "Unsupported codec with id %d for input stream %d\n",
  1447. stream->codec->codec_id, stream->index);
  1448. } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
  1449. av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
  1450. stream->index);
  1451. }
  1452. }
  1453. *fmt_ctx_ptr = fmt_ctx;
  1454. return 0;
  1455. }
  1456. static void close_input_file(AVFormatContext **ctx_ptr)
  1457. {
  1458. int i;
  1459. AVFormatContext *fmt_ctx = *ctx_ptr;
  1460. /* close decoder for each stream */
  1461. for (i = 0; i < fmt_ctx->nb_streams; i++)
  1462. if (fmt_ctx->streams[i]->codec->codec_id != CODEC_ID_NONE)
  1463. avcodec_close(fmt_ctx->streams[i]->codec);
  1464. avformat_close_input(ctx_ptr);
  1465. }
  1466. #define PRINT_CHAPTER(name) do { \
  1467. if (do_show_ ## name) { \
  1468. writer_print_chapter_header(wctx, #name); \
  1469. show_ ## name (wctx, fmt_ctx); \
  1470. writer_print_chapter_footer(wctx, #name); \
  1471. } \
  1472. } while (0)
  1473. static int probe_file(WriterContext *wctx, const char *filename)
  1474. {
  1475. AVFormatContext *fmt_ctx;
  1476. int ret;
  1477. do_read_frames = do_show_frames || do_count_frames;
  1478. do_read_packets = do_show_packets || do_count_packets;
  1479. ret = open_input_file(&fmt_ctx, filename);
  1480. if (ret >= 0) {
  1481. nb_streams_frames = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
  1482. nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
  1483. if (do_read_frames || do_read_packets) {
  1484. const char *chapter;
  1485. if (do_show_frames && do_show_packets &&
  1486. wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
  1487. chapter = "packets_and_frames";
  1488. else if (do_show_packets && !do_show_frames)
  1489. chapter = "packets";
  1490. else // (!do_show_packets && do_show_frames)
  1491. chapter = "frames";
  1492. if (do_show_frames || do_show_packets)
  1493. writer_print_chapter_header(wctx, chapter);
  1494. read_packets(wctx, fmt_ctx);
  1495. if (do_show_frames || do_show_packets)
  1496. writer_print_chapter_footer(wctx, chapter);
  1497. }
  1498. PRINT_CHAPTER(streams);
  1499. PRINT_CHAPTER(format);
  1500. close_input_file(&fmt_ctx);
  1501. av_freep(&nb_streams_frames);
  1502. av_freep(&nb_streams_packets);
  1503. }
  1504. return ret;
  1505. }
  1506. static void show_usage(void)
  1507. {
  1508. av_log(NULL, AV_LOG_INFO, "Simple multimedia streams analyzer\n");
  1509. av_log(NULL, AV_LOG_INFO, "usage: %s [OPTIONS] [INPUT_FILE]\n", program_name);
  1510. av_log(NULL, AV_LOG_INFO, "\n");
  1511. }
  1512. static void ffprobe_show_program_version(WriterContext *w)
  1513. {
  1514. AVBPrint pbuf;
  1515. av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  1516. writer_print_chapter_header(w, "program_version");
  1517. print_section_header("program_version");
  1518. print_str("version", FFMPEG_VERSION);
  1519. print_fmt("copyright", "Copyright (c) %d-%d the FFmpeg developers",
  1520. program_birth_year, this_year);
  1521. print_str("build_date", __DATE__);
  1522. print_str("build_time", __TIME__);
  1523. print_str("compiler_type", CC_TYPE);
  1524. print_str("compiler_version", CC_VERSION);
  1525. print_str("configuration", FFMPEG_CONFIGURATION);
  1526. print_section_footer("program_version");
  1527. writer_print_chapter_footer(w, "program_version");
  1528. av_bprint_finalize(&pbuf, NULL);
  1529. }
  1530. #define SHOW_LIB_VERSION(libname, LIBNAME) \
  1531. do { \
  1532. if (CONFIG_##LIBNAME) { \
  1533. unsigned int version = libname##_version(); \
  1534. print_section_header("library_version"); \
  1535. print_str("name", "lib" #libname); \
  1536. print_int("major", LIB##LIBNAME##_VERSION_MAJOR); \
  1537. print_int("minor", LIB##LIBNAME##_VERSION_MINOR); \
  1538. print_int("micro", LIB##LIBNAME##_VERSION_MICRO); \
  1539. print_int("version", version); \
  1540. print_section_footer("library_version"); \
  1541. } \
  1542. } while (0)
  1543. static void ffprobe_show_library_versions(WriterContext *w)
  1544. {
  1545. writer_print_chapter_header(w, "library_versions");
  1546. SHOW_LIB_VERSION(avutil, AVUTIL);
  1547. SHOW_LIB_VERSION(avcodec, AVCODEC);
  1548. SHOW_LIB_VERSION(avformat, AVFORMAT);
  1549. SHOW_LIB_VERSION(avdevice, AVDEVICE);
  1550. SHOW_LIB_VERSION(avfilter, AVFILTER);
  1551. SHOW_LIB_VERSION(swscale, SWSCALE);
  1552. SHOW_LIB_VERSION(swresample, SWRESAMPLE);
  1553. SHOW_LIB_VERSION(postproc, POSTPROC);
  1554. writer_print_chapter_footer(w, "library_versions");
  1555. }
  1556. static int opt_format(const char *opt, const char *arg)
  1557. {
  1558. iformat = av_find_input_format(arg);
  1559. if (!iformat) {
  1560. av_log(NULL, AV_LOG_ERROR, "Unknown input format: %s\n", arg);
  1561. return AVERROR(EINVAL);
  1562. }
  1563. return 0;
  1564. }
  1565. static int opt_show_format_entry(const char *opt, const char *arg)
  1566. {
  1567. do_show_format = 1;
  1568. av_dict_set(&fmt_entries_to_show, arg, "", 0);
  1569. return 0;
  1570. }
  1571. static void opt_input_file(void *optctx, const char *arg)
  1572. {
  1573. if (input_filename) {
  1574. av_log(NULL, AV_LOG_ERROR,
  1575. "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  1576. arg, input_filename);
  1577. exit(1);
  1578. }
  1579. if (!strcmp(arg, "-"))
  1580. arg = "pipe:";
  1581. input_filename = arg;
  1582. }
  1583. static int opt_help(const char *opt, const char *arg)
  1584. {
  1585. av_log_set_callback(log_callback_help);
  1586. show_usage();
  1587. show_help_options(options, "Main options:\n", 0, 0);
  1588. printf("\n");
  1589. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  1590. return 0;
  1591. }
  1592. static int opt_pretty(const char *opt, const char *arg)
  1593. {
  1594. show_value_unit = 1;
  1595. use_value_prefix = 1;
  1596. use_byte_value_binary_prefix = 1;
  1597. use_value_sexagesimal_format = 1;
  1598. return 0;
  1599. }
  1600. static int opt_show_versions(const char *opt, const char *arg)
  1601. {
  1602. do_show_program_version = 1;
  1603. do_show_library_versions = 1;
  1604. return 0;
  1605. }
  1606. static const OptionDef options[] = {
  1607. #include "cmdutils_common_opts.h"
  1608. { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
  1609. { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
  1610. { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
  1611. { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
  1612. "use binary prefixes for byte units" },
  1613. { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format},
  1614. "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
  1615. { "pretty", 0, {(void*)&opt_pretty},
  1616. "prettify the format of displayed values, make it more human readable" },
  1617. { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
  1618. "set the output printing format (available formats are: default, compact, csv, ini, json, xml)", "format" },
  1619. { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
  1620. { "show_error", OPT_BOOL, {(void*)&do_show_error} , "show probing error" },
  1621. { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
  1622. { "show_frames", OPT_BOOL, {(void*)&do_show_frames} , "show frames info" },
  1623. { "show_format_entry", HAS_ARG, {(void*)opt_show_format_entry},
  1624. "show a particular entry from the format/container info", "entry" },
  1625. { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
  1626. { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
  1627. { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
  1628. { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
  1629. { "show_program_version", OPT_BOOL, {(void*)&do_show_program_version}, "show ffprobe version" },
  1630. { "show_library_versions", OPT_BOOL, {(void*)&do_show_library_versions}, "show library versions" },
  1631. { "show_versions", 0, {(void*)&opt_show_versions}, "show program and library versions" },
  1632. { "show_private_data", OPT_BOOL, {(void*)&show_private_data}, "show private data" },
  1633. { "private", OPT_BOOL, {(void*)&show_private_data}, "same as show_private_data" },
  1634. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
  1635. { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
  1636. { NULL, },
  1637. };
  1638. int main(int argc, char **argv)
  1639. {
  1640. const Writer *w;
  1641. WriterContext *wctx;
  1642. char *buf;
  1643. char *w_name = NULL, *w_args = NULL;
  1644. int ret;
  1645. av_log_set_flags(AV_LOG_SKIP_REPEATED);
  1646. parse_loglevel(argc, argv, options);
  1647. av_register_all();
  1648. avformat_network_init();
  1649. init_opts();
  1650. #if CONFIG_AVDEVICE
  1651. avdevice_register_all();
  1652. #endif
  1653. show_banner(argc, argv, options);
  1654. parse_options(NULL, argc, argv, options, opt_input_file);
  1655. writer_register_all();
  1656. if (!print_format)
  1657. print_format = av_strdup("default");
  1658. w_name = av_strtok(print_format, "=", &buf);
  1659. w_args = buf;
  1660. w = writer_get_by_name(w_name);
  1661. if (!w) {
  1662. av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
  1663. ret = AVERROR(EINVAL);
  1664. goto end;
  1665. }
  1666. if ((ret = writer_open(&wctx, w, w_args, NULL)) >= 0) {
  1667. writer_print_header(wctx);
  1668. if (do_show_program_version)
  1669. ffprobe_show_program_version(wctx);
  1670. if (do_show_library_versions)
  1671. ffprobe_show_library_versions(wctx);
  1672. if (!input_filename &&
  1673. ((do_show_format || do_show_streams || do_show_packets || do_show_error) ||
  1674. (!do_show_program_version && !do_show_library_versions))) {
  1675. show_usage();
  1676. av_log(NULL, AV_LOG_ERROR, "You have to specify one input file.\n");
  1677. av_log(NULL, AV_LOG_ERROR, "Use -h to get full help or, even better, run 'man %s'.\n", program_name);
  1678. ret = AVERROR(EINVAL);
  1679. } else if (input_filename) {
  1680. ret = probe_file(wctx, input_filename);
  1681. if (ret < 0 && do_show_error)
  1682. show_error(wctx, ret);
  1683. }
  1684. writer_print_footer(wctx);
  1685. writer_close(&wctx);
  1686. }
  1687. end:
  1688. av_freep(&print_format);
  1689. uninit_opts();
  1690. av_dict_free(&fmt_entries_to_show);
  1691. avformat_network_deinit();
  1692. return ret;
  1693. }