ffprobe.c 61 KB

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