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.

881 lines
27KB

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