ffprobe.c 60 KB

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