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.

1995 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_section_packet; ///< number of the packet section in case we are in "packets_and_frames" section
  153. unsigned int nb_section_frame; ///< number of the frame section in case we are in "packets_and_frames" section
  154. unsigned int nb_section_packet_frame; ///< nb_section_packet or nb_section_frame according if is_packets_and_frames
  155. unsigned int nb_chapter; ///< number of the chapter, starting at 0
  156. int multiple_sections; ///< tells if the current chapter can contain multiple sections
  157. int is_fmt_chapter; ///< tells if the current chapter is "format", required by the print_format_entry option
  158. int is_packets_and_frames; ///< tells if the current section is "packets_and_frames"
  159. };
  160. static const char *writer_get_name(void *p)
  161. {
  162. WriterContext *wctx = p;
  163. return wctx->writer->name;
  164. }
  165. static const AVClass writer_class = {
  166. "Writer",
  167. writer_get_name,
  168. NULL,
  169. LIBAVUTIL_VERSION_INT,
  170. };
  171. static void writer_close(WriterContext **wctx)
  172. {
  173. if (!*wctx)
  174. return;
  175. if ((*wctx)->writer->uninit)
  176. (*wctx)->writer->uninit(*wctx);
  177. av_freep(&((*wctx)->priv));
  178. av_freep(wctx);
  179. }
  180. static int writer_open(WriterContext **wctx, const Writer *writer,
  181. const char *args, void *opaque)
  182. {
  183. int ret = 0;
  184. if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
  185. ret = AVERROR(ENOMEM);
  186. goto fail;
  187. }
  188. if (!((*wctx)->priv = av_mallocz(writer->priv_size))) {
  189. ret = AVERROR(ENOMEM);
  190. goto fail;
  191. }
  192. (*wctx)->class = &writer_class;
  193. (*wctx)->writer = writer;
  194. if ((*wctx)->writer->init)
  195. ret = (*wctx)->writer->init(*wctx, args, opaque);
  196. if (ret < 0)
  197. goto fail;
  198. return 0;
  199. fail:
  200. writer_close(wctx);
  201. return ret;
  202. }
  203. static inline void writer_print_header(WriterContext *wctx)
  204. {
  205. if (wctx->writer->print_header)
  206. wctx->writer->print_header(wctx);
  207. wctx->nb_chapter = 0;
  208. }
  209. static inline void writer_print_footer(WriterContext *wctx)
  210. {
  211. if (wctx->writer->print_footer)
  212. wctx->writer->print_footer(wctx);
  213. }
  214. static inline void writer_print_chapter_header(WriterContext *wctx,
  215. const char *chapter)
  216. {
  217. wctx->nb_section =
  218. wctx->nb_section_packet = wctx->nb_section_frame =
  219. wctx->nb_section_packet_frame = 0;
  220. wctx->is_packets_and_frames = !strcmp(chapter, "packets_and_frames");
  221. wctx->multiple_sections = !strcmp(chapter, "packets") || !strcmp(chapter, "frames" ) ||
  222. wctx->is_packets_and_frames ||
  223. !strcmp(chapter, "streams") || !strcmp(chapter, "library_versions");
  224. wctx->is_fmt_chapter = !strcmp(chapter, "format");
  225. if (wctx->writer->print_chapter_header)
  226. wctx->writer->print_chapter_header(wctx, chapter);
  227. }
  228. static inline void writer_print_chapter_footer(WriterContext *wctx,
  229. const char *chapter)
  230. {
  231. if (wctx->writer->print_chapter_footer)
  232. wctx->writer->print_chapter_footer(wctx, chapter);
  233. wctx->nb_chapter++;
  234. }
  235. static inline void writer_print_section_header(WriterContext *wctx,
  236. const char *section)
  237. {
  238. if (wctx->is_packets_and_frames)
  239. wctx->nb_section_packet_frame = !strcmp(section, "packet") ? wctx->nb_section_packet
  240. : wctx->nb_section_frame;
  241. if (wctx->writer->print_section_header)
  242. wctx->writer->print_section_header(wctx, section);
  243. wctx->nb_item = 0;
  244. }
  245. static inline void writer_print_section_footer(WriterContext *wctx,
  246. const char *section)
  247. {
  248. if (wctx->writer->print_section_footer)
  249. wctx->writer->print_section_footer(wctx, section);
  250. if (wctx->is_packets_and_frames) {
  251. if (!strcmp(section, "packet")) wctx->nb_section_packet++;
  252. else wctx->nb_section_frame++;
  253. }
  254. wctx->nb_section++;
  255. }
  256. static inline void writer_print_integer(WriterContext *wctx,
  257. const char *key, long long int val)
  258. {
  259. if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  260. wctx->writer->print_integer(wctx, key, val);
  261. wctx->nb_item++;
  262. }
  263. }
  264. static inline void writer_print_string(WriterContext *wctx,
  265. const char *key, const char *val, int opt)
  266. {
  267. if (opt && !(wctx->writer->flags & WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS))
  268. return;
  269. if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  270. wctx->writer->print_string(wctx, key, val);
  271. wctx->nb_item++;
  272. }
  273. }
  274. static void writer_print_time(WriterContext *wctx, const char *key,
  275. int64_t ts, const AVRational *time_base)
  276. {
  277. char buf[128];
  278. if (!wctx->is_fmt_chapter || !fmt_entries_to_show || av_dict_get(fmt_entries_to_show, key, NULL, 0)) {
  279. if (ts == AV_NOPTS_VALUE) {
  280. writer_print_string(wctx, key, "N/A", 1);
  281. } else {
  282. double d = ts * av_q2d(*time_base);
  283. value_string(buf, sizeof(buf), (struct unit_value){.val.d=d, .unit=unit_second_str});
  284. writer_print_string(wctx, key, buf, 0);
  285. }
  286. }
  287. }
  288. static void writer_print_ts(WriterContext *wctx, const char *key, int64_t ts)
  289. {
  290. if (ts == AV_NOPTS_VALUE) {
  291. writer_print_string(wctx, key, "N/A", 1);
  292. } else {
  293. writer_print_integer(wctx, key, ts);
  294. }
  295. }
  296. static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
  297. {
  298. wctx->writer->show_tags(wctx, dict);
  299. }
  300. #define MAX_REGISTERED_WRITERS_NB 64
  301. static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
  302. static int writer_register(const Writer *writer)
  303. {
  304. static int next_registered_writer_idx = 0;
  305. if (next_registered_writer_idx == MAX_REGISTERED_WRITERS_NB)
  306. return AVERROR(ENOMEM);
  307. registered_writers[next_registered_writer_idx++] = writer;
  308. return 0;
  309. }
  310. static const Writer *writer_get_by_name(const char *name)
  311. {
  312. int i;
  313. for (i = 0; registered_writers[i]; i++)
  314. if (!strcmp(registered_writers[i]->name, name))
  315. return registered_writers[i];
  316. return NULL;
  317. }
  318. /* WRITERS */
  319. /* Default output */
  320. typedef struct DefaultContext {
  321. const AVClass *class;
  322. int nokey;
  323. int noprint_wrappers;
  324. } DefaultContext;
  325. #define OFFSET(x) offsetof(DefaultContext, x)
  326. static const AVOption default_options[] = {
  327. { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  328. { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  329. { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  330. { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  331. {NULL},
  332. };
  333. static const char *default_get_name(void *ctx)
  334. {
  335. return "default";
  336. }
  337. static const AVClass default_class = {
  338. "DefaultContext",
  339. default_get_name,
  340. default_options
  341. };
  342. static av_cold int default_init(WriterContext *wctx, const char *args, void *opaque)
  343. {
  344. DefaultContext *def = wctx->priv;
  345. int err;
  346. def->class = &default_class;
  347. av_opt_set_defaults(def);
  348. if (args &&
  349. (err = (av_set_options_string(def, args, "=", ":"))) < 0) {
  350. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  351. return err;
  352. }
  353. return 0;
  354. }
  355. static void default_print_footer(WriterContext *wctx)
  356. {
  357. DefaultContext *def = wctx->priv;
  358. if (!def->noprint_wrappers)
  359. printf("\n");
  360. }
  361. static void default_print_chapter_header(WriterContext *wctx, const char *chapter)
  362. {
  363. DefaultContext *def = wctx->priv;
  364. if (!def->noprint_wrappers && wctx->nb_chapter)
  365. printf("\n");
  366. }
  367. /* lame uppercasing routine, assumes the string is lower case ASCII */
  368. static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
  369. {
  370. int i;
  371. for (i = 0; src[i] && i < dst_size-1; i++)
  372. dst[i] = av_toupper(src[i]);
  373. dst[i] = 0;
  374. return dst;
  375. }
  376. static void default_print_section_header(WriterContext *wctx, const char *section)
  377. {
  378. DefaultContext *def = wctx->priv;
  379. char buf[32];
  380. if (wctx->nb_section)
  381. printf("\n");
  382. if (!def->noprint_wrappers)
  383. printf("[%s]\n", upcase_string(buf, sizeof(buf), section));
  384. }
  385. static void default_print_section_footer(WriterContext *wctx, const char *section)
  386. {
  387. DefaultContext *def = wctx->priv;
  388. char buf[32];
  389. if (!def->noprint_wrappers)
  390. printf("[/%s]", upcase_string(buf, sizeof(buf), section));
  391. }
  392. static void default_print_str(WriterContext *wctx, const char *key, const char *value)
  393. {
  394. DefaultContext *def = wctx->priv;
  395. if (!def->nokey)
  396. printf("%s=", key);
  397. printf("%s\n", value);
  398. }
  399. static void default_print_int(WriterContext *wctx, const char *key, long long int value)
  400. {
  401. DefaultContext *def = wctx->priv;
  402. if (!def->nokey)
  403. printf("%s=", key);
  404. printf("%lld\n", value);
  405. }
  406. static void default_show_tags(WriterContext *wctx, AVDictionary *dict)
  407. {
  408. AVDictionaryEntry *tag = NULL;
  409. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  410. if (!fmt_entries_to_show || (tag->key && av_dict_get(fmt_entries_to_show, tag->key, NULL, 0)))
  411. printf("TAG:");
  412. writer_print_string(wctx, tag->key, tag->value, 0);
  413. }
  414. }
  415. static const Writer default_writer = {
  416. .name = "default",
  417. .priv_size = sizeof(DefaultContext),
  418. .init = default_init,
  419. .print_footer = default_print_footer,
  420. .print_chapter_header = default_print_chapter_header,
  421. .print_section_header = default_print_section_header,
  422. .print_section_footer = default_print_section_footer,
  423. .print_integer = default_print_int,
  424. .print_string = default_print_str,
  425. .show_tags = default_show_tags,
  426. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
  427. };
  428. /* Compact output */
  429. /**
  430. * Apply C-language-like string escaping.
  431. */
  432. static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
  433. {
  434. const char *p;
  435. for (p = src; *p; p++) {
  436. switch (*p) {
  437. case '\b': av_bprintf(dst, "%s", "\\b"); break;
  438. case '\f': av_bprintf(dst, "%s", "\\f"); break;
  439. case '\n': av_bprintf(dst, "%s", "\\n"); break;
  440. case '\r': av_bprintf(dst, "%s", "\\r"); break;
  441. case '\\': av_bprintf(dst, "%s", "\\\\"); break;
  442. default:
  443. if (*p == sep)
  444. av_bprint_chars(dst, '\\', 1);
  445. av_bprint_chars(dst, *p, 1);
  446. }
  447. }
  448. return dst->str;
  449. }
  450. /**
  451. * Quote fields containing special characters, check RFC4180.
  452. */
  453. static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
  454. {
  455. const char *p;
  456. int quote = 0;
  457. /* check if input needs quoting */
  458. for (p = src; *p; p++)
  459. if (*p == '"' || *p == sep || *p == '\n' || *p == '\r')
  460. quote = 1;
  461. if (quote)
  462. av_bprint_chars(dst, '\"', 1);
  463. for (p = src; *p; p++) {
  464. if (*p == '"')
  465. av_bprint_chars(dst, '\"', 1);
  466. av_bprint_chars(dst, *p, 1);
  467. }
  468. if (quote)
  469. av_bprint_chars(dst, '\"', 1);
  470. return dst->str;
  471. }
  472. static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
  473. {
  474. return src;
  475. }
  476. typedef struct CompactContext {
  477. const AVClass *class;
  478. char *item_sep_str;
  479. char item_sep;
  480. int nokey;
  481. char *escape_mode_str;
  482. const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
  483. } CompactContext;
  484. #undef OFFSET
  485. #define OFFSET(x) offsetof(CompactContext, x)
  486. static const AVOption compact_options[]= {
  487. {"item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
  488. {"s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, {.str="|"}, CHAR_MIN, CHAR_MAX },
  489. {"nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  490. {"nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  491. {"escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
  492. {"e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, {.str="c"}, CHAR_MIN, CHAR_MAX },
  493. {NULL},
  494. };
  495. static const char *compact_get_name(void *ctx)
  496. {
  497. return "compact";
  498. }
  499. static const AVClass compact_class = {
  500. "CompactContext",
  501. compact_get_name,
  502. compact_options
  503. };
  504. static av_cold int compact_init(WriterContext *wctx, const char *args, void *opaque)
  505. {
  506. CompactContext *compact = wctx->priv;
  507. int err;
  508. compact->class = &compact_class;
  509. av_opt_set_defaults(compact);
  510. if (args &&
  511. (err = (av_set_options_string(compact, args, "=", ":"))) < 0) {
  512. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  513. return err;
  514. }
  515. if (strlen(compact->item_sep_str) != 1) {
  516. av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
  517. compact->item_sep_str);
  518. return AVERROR(EINVAL);
  519. }
  520. compact->item_sep = compact->item_sep_str[0];
  521. if (!strcmp(compact->escape_mode_str, "none")) compact->escape_str = none_escape_str;
  522. else if (!strcmp(compact->escape_mode_str, "c" )) compact->escape_str = c_escape_str;
  523. else if (!strcmp(compact->escape_mode_str, "csv" )) compact->escape_str = csv_escape_str;
  524. else {
  525. av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
  526. return AVERROR(EINVAL);
  527. }
  528. return 0;
  529. }
  530. static av_cold void compact_uninit(WriterContext *wctx)
  531. {
  532. CompactContext *compact = wctx->priv;
  533. av_freep(&compact->item_sep_str);
  534. av_freep(&compact->escape_mode_str);
  535. }
  536. static void compact_print_section_header(WriterContext *wctx, const char *section)
  537. {
  538. CompactContext *compact = wctx->priv;
  539. printf("%s%c", section, compact->item_sep);
  540. }
  541. static void compact_print_section_footer(WriterContext *wctx, const char *section)
  542. {
  543. printf("\n");
  544. }
  545. static void compact_print_str(WriterContext *wctx, const char *key, const char *value)
  546. {
  547. CompactContext *compact = wctx->priv;
  548. AVBPrint buf;
  549. if (wctx->nb_item) printf("%c", compact->item_sep);
  550. if (!compact->nokey)
  551. printf("%s=", key);
  552. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  553. printf("%s", compact->escape_str(&buf, value, compact->item_sep, wctx));
  554. av_bprint_finalize(&buf, NULL);
  555. }
  556. static void compact_print_int(WriterContext *wctx, const char *key, long long int value)
  557. {
  558. CompactContext *compact = wctx->priv;
  559. if (wctx->nb_item) printf("%c", compact->item_sep);
  560. if (!compact->nokey)
  561. printf("%s=", key);
  562. printf("%lld", value);
  563. }
  564. static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
  565. {
  566. CompactContext *compact = wctx->priv;
  567. AVDictionaryEntry *tag = NULL;
  568. AVBPrint buf;
  569. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  570. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  571. if (wctx->nb_item) printf("%c", compact->item_sep);
  572. if (!compact->nokey) {
  573. av_bprint_clear(&buf);
  574. printf("tag:%s=", compact->escape_str(&buf, tag->key, compact->item_sep, wctx));
  575. }
  576. av_bprint_clear(&buf);
  577. printf("%s", compact->escape_str(&buf, tag->value, compact->item_sep, wctx));
  578. }
  579. av_bprint_finalize(&buf, NULL);
  580. }
  581. static const Writer compact_writer = {
  582. .name = "compact",
  583. .priv_size = sizeof(CompactContext),
  584. .init = compact_init,
  585. .uninit = compact_uninit,
  586. .print_section_header = compact_print_section_header,
  587. .print_section_footer = compact_print_section_footer,
  588. .print_integer = compact_print_int,
  589. .print_string = compact_print_str,
  590. .show_tags = compact_show_tags,
  591. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
  592. };
  593. /* CSV output */
  594. static av_cold int csv_init(WriterContext *wctx, const char *args, void *opaque)
  595. {
  596. return compact_init(wctx, "item_sep=,:nokey=1:escape=csv", opaque);
  597. }
  598. static const Writer csv_writer = {
  599. .name = "csv",
  600. .priv_size = sizeof(CompactContext),
  601. .init = csv_init,
  602. .uninit = compact_uninit,
  603. .print_section_header = compact_print_section_header,
  604. .print_section_footer = compact_print_section_footer,
  605. .print_integer = compact_print_int,
  606. .print_string = compact_print_str,
  607. .show_tags = compact_show_tags,
  608. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS,
  609. };
  610. /* INI format output */
  611. typedef struct {
  612. const AVClass *class;
  613. AVBPrint chapter_name, section_name;
  614. int hierarchical;
  615. } INIContext;
  616. #undef OFFSET
  617. #define OFFSET(x) offsetof(INIContext, x)
  618. static const AVOption ini_options[] = {
  619. {"hierachical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
  620. {"h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
  621. {NULL},
  622. };
  623. static const char *ini_get_name(void *ctx)
  624. {
  625. return "ini";
  626. }
  627. static const AVClass ini_class = {
  628. "INIContext",
  629. ini_get_name,
  630. ini_options
  631. };
  632. static av_cold int ini_init(WriterContext *wctx, const char *args, void *opaque)
  633. {
  634. INIContext *ini = wctx->priv;
  635. int err;
  636. av_bprint_init(&ini->chapter_name, 1, AV_BPRINT_SIZE_UNLIMITED);
  637. av_bprint_init(&ini->section_name, 1, AV_BPRINT_SIZE_UNLIMITED);
  638. ini->class = &ini_class;
  639. av_opt_set_defaults(ini);
  640. if (args && (err = av_set_options_string(ini, args, "=", ":")) < 0) {
  641. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  642. return err;
  643. }
  644. return 0;
  645. }
  646. static av_cold void ini_uninit(WriterContext *wctx)
  647. {
  648. INIContext *ini = wctx->priv;
  649. av_bprint_finalize(&ini->chapter_name, NULL);
  650. av_bprint_finalize(&ini->section_name, NULL);
  651. }
  652. static void ini_print_header(WriterContext *wctx)
  653. {
  654. printf("# ffprobe output\n\n");
  655. }
  656. static char *ini_escape_str(AVBPrint *dst, const char *src)
  657. {
  658. int i = 0;
  659. char c = 0;
  660. while (c = src[i++]) {
  661. switch (c) {
  662. case '\b': av_bprintf(dst, "%s", "\\b"); break;
  663. case '\f': av_bprintf(dst, "%s", "\\f"); break;
  664. case '\n': av_bprintf(dst, "%s", "\\n"); break;
  665. case '\r': av_bprintf(dst, "%s", "\\r"); break;
  666. case '\t': av_bprintf(dst, "%s", "\\t"); break;
  667. case '\\':
  668. case '#' :
  669. case '=' :
  670. case ':' : av_bprint_chars(dst, '\\', 1);
  671. default:
  672. if ((unsigned char)c < 32)
  673. av_bprintf(dst, "\\x00%02x", c & 0xff);
  674. else
  675. av_bprint_chars(dst, c, 1);
  676. break;
  677. }
  678. }
  679. return dst->str;
  680. }
  681. static void ini_print_chapter_header(WriterContext *wctx, const char *chapter)
  682. {
  683. INIContext *ini = wctx->priv;
  684. av_bprint_clear(&ini->chapter_name);
  685. av_bprintf(&ini->chapter_name, "%s", chapter);
  686. if (wctx->nb_chapter)
  687. printf("\n");
  688. }
  689. static void ini_print_section_header(WriterContext *wctx, const char *section)
  690. {
  691. INIContext *ini = wctx->priv;
  692. int n = wctx->is_packets_and_frames ? wctx->nb_section_packet_frame
  693. : wctx->nb_section;
  694. if (wctx->nb_section)
  695. printf("\n");
  696. av_bprint_clear(&ini->section_name);
  697. if (ini->hierarchical && wctx->multiple_sections)
  698. av_bprintf(&ini->section_name, "%s.", ini->chapter_name.str);
  699. av_bprintf(&ini->section_name, "%s", section);
  700. if (wctx->multiple_sections)
  701. av_bprintf(&ini->section_name, ".%d", n);
  702. printf("[%s]\n", ini->section_name.str);
  703. }
  704. static void ini_print_str(WriterContext *wctx, const char *key, const char *value)
  705. {
  706. AVBPrint buf;
  707. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  708. printf("%s=", ini_escape_str(&buf, key));
  709. av_bprint_clear(&buf);
  710. printf("%s\n", ini_escape_str(&buf, value));
  711. av_bprint_finalize(&buf, NULL);
  712. }
  713. static void ini_print_int(WriterContext *wctx, const char *key, long long int value)
  714. {
  715. printf("%s=%lld\n", key, value);
  716. }
  717. static void ini_show_tags(WriterContext *wctx, AVDictionary *dict)
  718. {
  719. INIContext *ini = wctx->priv;
  720. AVDictionaryEntry *tag = NULL;
  721. int is_first = 1;
  722. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  723. if (is_first) {
  724. printf("\n[%s.tags]\n", ini->section_name.str);
  725. is_first = 0;
  726. }
  727. writer_print_string(wctx, tag->key, tag->value, 0);
  728. }
  729. }
  730. static const Writer ini_writer = {
  731. .name = "ini",
  732. .priv_size = sizeof(INIContext),
  733. .init = ini_init,
  734. .uninit = ini_uninit,
  735. .print_header = ini_print_header,
  736. .print_chapter_header = ini_print_chapter_header,
  737. .print_section_header = ini_print_section_header,
  738. .print_integer = ini_print_int,
  739. .print_string = ini_print_str,
  740. .show_tags = ini_show_tags,
  741. .flags = WRITER_FLAG_DISPLAY_OPTIONAL_FIELDS|WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  742. };
  743. /* JSON output */
  744. typedef struct {
  745. const AVClass *class;
  746. int indent_level;
  747. int compact;
  748. const char *item_sep, *item_start_end;
  749. } JSONContext;
  750. #undef OFFSET
  751. #define OFFSET(x) offsetof(JSONContext, x)
  752. static const AVOption json_options[]= {
  753. { "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  754. { "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  755. { NULL }
  756. };
  757. static const char *json_get_name(void *ctx)
  758. {
  759. return "json";
  760. }
  761. static const AVClass json_class = {
  762. "JSONContext",
  763. json_get_name,
  764. json_options
  765. };
  766. static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
  767. {
  768. JSONContext *json = wctx->priv;
  769. int err;
  770. json->class = &json_class;
  771. av_opt_set_defaults(json);
  772. if (args &&
  773. (err = (av_set_options_string(json, args, "=", ":"))) < 0) {
  774. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  775. return err;
  776. }
  777. json->item_sep = json->compact ? ", " : ",\n";
  778. json->item_start_end = json->compact ? " " : "\n";
  779. return 0;
  780. }
  781. static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
  782. {
  783. static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
  784. static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
  785. const char *p;
  786. for (p = src; *p; p++) {
  787. char *s = strchr(json_escape, *p);
  788. if (s) {
  789. av_bprint_chars(dst, '\\', 1);
  790. av_bprint_chars(dst, json_subst[s - json_escape], 1);
  791. } else if ((unsigned char)*p < 32) {
  792. av_bprintf(dst, "\\u00%02x", *p & 0xff);
  793. } else {
  794. av_bprint_chars(dst, *p, 1);
  795. }
  796. }
  797. return dst->str;
  798. }
  799. static void json_print_header(WriterContext *wctx)
  800. {
  801. JSONContext *json = wctx->priv;
  802. printf("{");
  803. json->indent_level++;
  804. }
  805. static void json_print_footer(WriterContext *wctx)
  806. {
  807. JSONContext *json = wctx->priv;
  808. json->indent_level--;
  809. printf("\n}\n");
  810. }
  811. #define JSON_INDENT() printf("%*c", json->indent_level * 4, ' ')
  812. static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
  813. {
  814. JSONContext *json = wctx->priv;
  815. AVBPrint buf;
  816. if (wctx->nb_chapter)
  817. printf(",");
  818. printf("\n");
  819. if (wctx->multiple_sections) {
  820. JSON_INDENT();
  821. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  822. printf("\"%s\": [\n", json_escape_str(&buf, chapter, wctx));
  823. av_bprint_finalize(&buf, NULL);
  824. json->indent_level++;
  825. }
  826. }
  827. static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
  828. {
  829. JSONContext *json = wctx->priv;
  830. if (wctx->multiple_sections) {
  831. printf("\n");
  832. json->indent_level--;
  833. JSON_INDENT();
  834. printf("]");
  835. }
  836. }
  837. static void json_print_section_header(WriterContext *wctx, const char *section)
  838. {
  839. JSONContext *json = wctx->priv;
  840. if (wctx->nb_section)
  841. printf(",\n");
  842. JSON_INDENT();
  843. if (!wctx->multiple_sections)
  844. printf("\"%s\": ", section);
  845. printf("{%s", json->item_start_end);
  846. json->indent_level++;
  847. /* this is required so the parser can distinguish between packets and frames */
  848. if (wctx->is_packets_and_frames) {
  849. if (!json->compact)
  850. JSON_INDENT();
  851. printf("\"type\": \"%s\"%s", section, json->item_sep);
  852. }
  853. }
  854. static void json_print_section_footer(WriterContext *wctx, const char *section)
  855. {
  856. JSONContext *json = wctx->priv;
  857. printf("%s", json->item_start_end);
  858. json->indent_level--;
  859. if (!json->compact)
  860. JSON_INDENT();
  861. printf("}");
  862. }
  863. static inline void json_print_item_str(WriterContext *wctx,
  864. const char *key, const char *value)
  865. {
  866. AVBPrint buf;
  867. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  868. printf("\"%s\":", json_escape_str(&buf, key, wctx));
  869. av_bprint_clear(&buf);
  870. printf(" \"%s\"", json_escape_str(&buf, value, wctx));
  871. av_bprint_finalize(&buf, NULL);
  872. }
  873. static void json_print_str(WriterContext *wctx, const char *key, const char *value)
  874. {
  875. JSONContext *json = wctx->priv;
  876. if (wctx->nb_item) printf("%s", json->item_sep);
  877. if (!json->compact)
  878. JSON_INDENT();
  879. json_print_item_str(wctx, key, value);
  880. }
  881. static void json_print_int(WriterContext *wctx, const char *key, long long int value)
  882. {
  883. JSONContext *json = wctx->priv;
  884. AVBPrint buf;
  885. if (wctx->nb_item) printf("%s", json->item_sep);
  886. if (!json->compact)
  887. JSON_INDENT();
  888. av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
  889. printf("\"%s\": %lld", json_escape_str(&buf, key, wctx), value);
  890. av_bprint_finalize(&buf, NULL);
  891. }
  892. static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
  893. {
  894. JSONContext *json = wctx->priv;
  895. AVDictionaryEntry *tag = NULL;
  896. int is_first = 1;
  897. if (!dict)
  898. return;
  899. printf("%s", json->item_sep);
  900. if (!json->compact)
  901. JSON_INDENT();
  902. printf("\"tags\": {%s", json->item_start_end);
  903. json->indent_level++;
  904. while ((tag = av_dict_get(dict, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  905. if (is_first) is_first = 0;
  906. else printf("%s", json->item_sep);
  907. if (!json->compact)
  908. JSON_INDENT();
  909. json_print_item_str(wctx, tag->key, tag->value);
  910. }
  911. json->indent_level--;
  912. printf("%s", json->item_start_end);
  913. if (!json->compact)
  914. JSON_INDENT();
  915. printf("}");
  916. }
  917. static const Writer json_writer = {
  918. .name = "json",
  919. .priv_size = sizeof(JSONContext),
  920. .init = json_init,
  921. .print_header = json_print_header,
  922. .print_footer = json_print_footer,
  923. .print_chapter_header = json_print_chapter_header,
  924. .print_chapter_footer = json_print_chapter_footer,
  925. .print_section_header = json_print_section_header,
  926. .print_section_footer = json_print_section_footer,
  927. .print_integer = json_print_int,
  928. .print_string = json_print_str,
  929. .show_tags = json_show_tags,
  930. .flags = WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER,
  931. };
  932. /* XML output */
  933. typedef struct {
  934. const AVClass *class;
  935. int within_tag;
  936. int indent_level;
  937. int fully_qualified;
  938. int xsd_strict;
  939. } XMLContext;
  940. #undef OFFSET
  941. #define OFFSET(x) offsetof(XMLContext, x)
  942. static const AVOption xml_options[] = {
  943. {"fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  944. {"q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  945. {"xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  946. {"x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
  947. {NULL},
  948. };
  949. static const char *xml_get_name(void *ctx)
  950. {
  951. return "xml";
  952. }
  953. static const AVClass xml_class = {
  954. "XMLContext",
  955. xml_get_name,
  956. xml_options
  957. };
  958. static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
  959. {
  960. XMLContext *xml = wctx->priv;
  961. int err;
  962. xml->class = &xml_class;
  963. av_opt_set_defaults(xml);
  964. if (args &&
  965. (err = (av_set_options_string(xml, args, "=", ":"))) < 0) {
  966. av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  967. return err;
  968. }
  969. if (xml->xsd_strict) {
  970. xml->fully_qualified = 1;
  971. #define CHECK_COMPLIANCE(opt, opt_name) \
  972. if (opt) { \
  973. av_log(wctx, AV_LOG_ERROR, \
  974. "XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
  975. "You need to disable such option with '-no%s'\n", opt_name, opt_name); \
  976. return AVERROR(EINVAL); \
  977. }
  978. CHECK_COMPLIANCE(show_private_data, "private");
  979. CHECK_COMPLIANCE(show_value_unit, "unit");
  980. CHECK_COMPLIANCE(use_value_prefix, "prefix");
  981. if (do_show_frames && do_show_packets) {
  982. av_log(wctx, AV_LOG_ERROR,
  983. "Interleaved frames and packets are not allowed in XSD. "
  984. "Select only one between the -show_frames and the -show_packets options.\n");
  985. return AVERROR(EINVAL);
  986. }
  987. }
  988. return 0;
  989. }
  990. static const char *xml_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
  991. {
  992. const char *p;
  993. for (p = src; *p; p++) {
  994. switch (*p) {
  995. case '&' : av_bprintf(dst, "%s", "&amp;"); break;
  996. case '<' : av_bprintf(dst, "%s", "&lt;"); break;
  997. case '>' : av_bprintf(dst, "%s", "&gt;"); break;
  998. case '\"': av_bprintf(dst, "%s", "&quot;"); break;
  999. case '\'': av_bprintf(dst, "%s", "&apos;"); break;
  1000. default: av_bprint_chars(dst, *p, 1);
  1001. }
  1002. }
  1003. return dst->str;
  1004. }
  1005. static void xml_print_header(WriterContext *wctx)
  1006. {
  1007. XMLContext *xml = wctx->priv;
  1008. const char *qual = " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
  1009. "xmlns:ffprobe='http://www.ffmpeg.org/schema/ffprobe' "
  1010. "xsi:schemaLocation='http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd'";
  1011. printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  1012. printf("<%sffprobe%s>\n",
  1013. xml->fully_qualified ? "ffprobe:" : "",
  1014. xml->fully_qualified ? qual : "");
  1015. xml->indent_level++;
  1016. }
  1017. static void xml_print_footer(WriterContext *wctx)
  1018. {
  1019. XMLContext *xml = wctx->priv;
  1020. xml->indent_level--;
  1021. printf("</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
  1022. }
  1023. #define XML_INDENT() printf("%*c", xml->indent_level * 4, ' ')
  1024. static void xml_print_chapter_header(WriterContext *wctx, const char *chapter)
  1025. {
  1026. XMLContext *xml = wctx->priv;
  1027. if (wctx->nb_chapter)
  1028. printf("\n");
  1029. if (wctx->multiple_sections) {
  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 (wctx->multiple_sections) {
  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. }