ismindex.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * Copyright (c) 2012 Martin Storsjo
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*
  21. * To create a simple file for smooth streaming:
  22. * avconv <normal input/transcoding options> -movflags frag_keyframe foo.ismv
  23. * ismindex -n foo foo.ismv
  24. * This step creates foo.ism and foo.ismc that is required by IIS for
  25. * serving it.
  26. *
  27. * By adding -path-prefix path/, the produced foo.ism will refer to the
  28. * files foo.ismv as "path/foo.ismv" - the prefix for the generated ismc
  29. * file can be set with the -ismc-prefix option similarly.
  30. *
  31. * To pre-split files for serving as static files by a web server without
  32. * any extra server support, create the ismv file as above, and split it:
  33. * ismindex -split foo.ismv
  34. * This step creates a file Manifest and directories QualityLevel(...),
  35. * that can be read directly by a smooth streaming player.
  36. *
  37. * The -output dir option can be used to request that output files
  38. * (both .ism/.ismc, or Manifest/QualityLevels* when splitting)
  39. * should be written to this directory instead of in the current directory.
  40. * (The directory itself isn't created if it doesn't already exist.)
  41. */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include "libavformat/avformat.h"
  45. #include "libavformat/os_support.h"
  46. #include "libavutil/intreadwrite.h"
  47. #include "libavutil/mathematics.h"
  48. static int usage(const char *argv0, int ret)
  49. {
  50. fprintf(stderr, "%s [-split] [-n basename] [-path-prefix prefix] "
  51. "[-ismc-prefix prefix] [-output dir] file1 [file2] ...\n", argv0);
  52. return ret;
  53. }
  54. struct MoofOffset {
  55. int64_t time;
  56. int64_t offset;
  57. int64_t duration;
  58. };
  59. struct Track {
  60. const char *name;
  61. int64_t duration;
  62. int bitrate;
  63. int track_id;
  64. int is_audio, is_video;
  65. int width, height;
  66. int chunks;
  67. int sample_rate, channels;
  68. uint8_t *codec_private;
  69. int codec_private_size;
  70. struct MoofOffset *offsets;
  71. int timescale;
  72. const char *fourcc;
  73. int blocksize;
  74. int tag;
  75. };
  76. struct Tracks {
  77. int nb_tracks;
  78. int64_t duration;
  79. struct Track **tracks;
  80. int video_track, audio_track;
  81. int nb_video_tracks, nb_audio_tracks;
  82. };
  83. static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
  84. {
  85. int32_t size, tag;
  86. size = avio_rb32(in);
  87. tag = avio_rb32(in);
  88. avio_wb32(out, size);
  89. avio_wb32(out, tag);
  90. if (tag != tag_name) {
  91. char tag_str[4], tag_name_str[4];
  92. AV_WB32(tag_str, tag);
  93. AV_WB32(tag_name_str, tag_name);
  94. fprintf(stderr, "wanted tag %.4s, got %.4s\n", tag_name_str, tag_str);
  95. return -1;
  96. }
  97. size -= 8;
  98. while (size > 0) {
  99. char buf[1024];
  100. int len = FFMIN(sizeof(buf), size);
  101. int got;
  102. if ((got = avio_read(in, buf, len)) != len) {
  103. fprintf(stderr, "short read, wanted %d, got %d\n", len, got);
  104. break;
  105. }
  106. avio_write(out, buf, len);
  107. size -= len;
  108. }
  109. return 0;
  110. }
  111. static int write_fragment(const char *filename, AVIOContext *in)
  112. {
  113. AVIOContext *out = NULL;
  114. int ret;
  115. if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, NULL, NULL)) < 0) {
  116. char errbuf[100];
  117. av_strerror(ret, errbuf, sizeof(errbuf));
  118. fprintf(stderr, "Unable to open %s: %s\n", filename, errbuf);
  119. return ret;
  120. }
  121. ret = copy_tag(in, out, MKBETAG('m', 'o', 'o', 'f'));
  122. if (!ret)
  123. ret = copy_tag(in, out, MKBETAG('m', 'd', 'a', 't'));
  124. avio_flush(out);
  125. avio_close(out);
  126. return ret;
  127. }
  128. static int write_fragments(struct Tracks *tracks, int start_index,
  129. AVIOContext *in, const char *output_prefix)
  130. {
  131. char dirname[2048], filename[2048];
  132. int i, j;
  133. for (i = start_index; i < tracks->nb_tracks; i++) {
  134. struct Track *track = tracks->tracks[i];
  135. const char *type = track->is_video ? "video" : "audio";
  136. snprintf(dirname, sizeof(dirname), "%sQualityLevels(%d)", output_prefix, track->bitrate);
  137. mkdir(dirname, 0777);
  138. for (j = 0; j < track->chunks; j++) {
  139. snprintf(filename, sizeof(filename), "%s/Fragments(%s=%"PRId64")",
  140. dirname, type, track->offsets[j].time);
  141. avio_seek(in, track->offsets[j].offset, SEEK_SET);
  142. write_fragment(filename, in);
  143. }
  144. }
  145. return 0;
  146. }
  147. static int read_tfra(struct Tracks *tracks, int start_index, AVIOContext *f)
  148. {
  149. int ret = AVERROR_EOF, track_id;
  150. int version, fieldlength, i, j;
  151. int64_t pos = avio_tell(f);
  152. uint32_t size = avio_rb32(f);
  153. struct Track *track = NULL;
  154. if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a'))
  155. goto fail;
  156. version = avio_r8(f);
  157. avio_rb24(f);
  158. track_id = avio_rb32(f); /* track id */
  159. for (i = start_index; i < tracks->nb_tracks && !track; i++)
  160. if (tracks->tracks[i]->track_id == track_id)
  161. track = tracks->tracks[i];
  162. if (!track) {
  163. /* Ok, continue parsing the next atom */
  164. ret = 0;
  165. goto fail;
  166. }
  167. fieldlength = avio_rb32(f);
  168. track->chunks = avio_rb32(f);
  169. track->offsets = av_mallocz(sizeof(*track->offsets) * track->chunks);
  170. if (!track->offsets) {
  171. ret = AVERROR(ENOMEM);
  172. goto fail;
  173. }
  174. for (i = 0; i < track->chunks; i++) {
  175. if (version == 1) {
  176. track->offsets[i].time = avio_rb64(f);
  177. track->offsets[i].offset = avio_rb64(f);
  178. } else {
  179. track->offsets[i].time = avio_rb32(f);
  180. track->offsets[i].offset = avio_rb32(f);
  181. }
  182. for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
  183. avio_r8(f);
  184. for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
  185. avio_r8(f);
  186. for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
  187. avio_r8(f);
  188. if (i > 0)
  189. track->offsets[i - 1].duration = track->offsets[i].time -
  190. track->offsets[i - 1].time;
  191. }
  192. if (track->chunks > 0)
  193. track->offsets[track->chunks - 1].duration = track->duration -
  194. track->offsets[track->chunks - 1].time;
  195. ret = 0;
  196. fail:
  197. avio_seek(f, pos + size, SEEK_SET);
  198. return ret;
  199. }
  200. static int read_mfra(struct Tracks *tracks, int start_index,
  201. const char *file, int split, const char *output_prefix)
  202. {
  203. int err = 0;
  204. const char* err_str = "";
  205. AVIOContext *f = NULL;
  206. int32_t mfra_size;
  207. if ((err = avio_open2(&f, file, AVIO_FLAG_READ, NULL, NULL)) < 0)
  208. goto fail;
  209. avio_seek(f, avio_size(f) - 4, SEEK_SET);
  210. mfra_size = avio_rb32(f);
  211. avio_seek(f, -mfra_size, SEEK_CUR);
  212. if (avio_rb32(f) != mfra_size) {
  213. err = AVERROR_INVALIDDATA;
  214. err_str = "mfra size mismatch";
  215. goto fail;
  216. }
  217. if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
  218. err = AVERROR_INVALIDDATA;
  219. err_str = "mfra tag mismatch";
  220. goto fail;
  221. }
  222. while (!read_tfra(tracks, start_index, f)) {
  223. /* Empty */
  224. }
  225. if (split)
  226. write_fragments(tracks, start_index, f, output_prefix);
  227. fail:
  228. if (f)
  229. avio_close(f);
  230. if (err)
  231. fprintf(stderr, "Unable to read the MFRA atom in %s (%s)\n", file, err_str);
  232. return err;
  233. }
  234. static int get_private_data(struct Track *track, AVCodecContext *codec)
  235. {
  236. track->codec_private_size = codec->extradata_size;
  237. track->codec_private = av_mallocz(codec->extradata_size);
  238. if (!track->codec_private)
  239. return AVERROR(ENOMEM);
  240. memcpy(track->codec_private, codec->extradata, codec->extradata_size);
  241. return 0;
  242. }
  243. static int get_video_private_data(struct Track *track, AVCodecContext *codec)
  244. {
  245. AVIOContext *io = NULL;
  246. uint16_t sps_size, pps_size;
  247. int err;
  248. if (codec->codec_id == AV_CODEC_ID_VC1)
  249. return get_private_data(track, codec);
  250. if ((err = avio_open_dyn_buf(&io)) < 0)
  251. goto fail;
  252. err = AVERROR(EINVAL);
  253. if (codec->extradata_size < 11 || codec->extradata[0] != 1)
  254. goto fail;
  255. sps_size = AV_RB16(&codec->extradata[6]);
  256. if (11 + sps_size > codec->extradata_size)
  257. goto fail;
  258. avio_wb32(io, 0x00000001);
  259. avio_write(io, &codec->extradata[8], sps_size);
  260. pps_size = AV_RB16(&codec->extradata[9 + sps_size]);
  261. if (11 + sps_size + pps_size > codec->extradata_size)
  262. goto fail;
  263. avio_wb32(io, 0x00000001);
  264. avio_write(io, &codec->extradata[11 + sps_size], pps_size);
  265. err = 0;
  266. fail:
  267. track->codec_private_size = avio_close_dyn_buf(io, &track->codec_private);
  268. return err;
  269. }
  270. static int handle_file(struct Tracks *tracks, const char *file, int split,
  271. const char *output_prefix)
  272. {
  273. AVFormatContext *ctx = NULL;
  274. int err = 0, i, orig_tracks = tracks->nb_tracks;
  275. char errbuf[50], *ptr;
  276. struct Track *track;
  277. err = avformat_open_input(&ctx, file, NULL, NULL);
  278. if (err < 0) {
  279. av_strerror(err, errbuf, sizeof(errbuf));
  280. fprintf(stderr, "Unable to open %s: %s\n", file, errbuf);
  281. return 1;
  282. }
  283. err = avformat_find_stream_info(ctx, NULL);
  284. if (err < 0) {
  285. av_strerror(err, errbuf, sizeof(errbuf));
  286. fprintf(stderr, "Unable to identify %s: %s\n", file, errbuf);
  287. goto fail;
  288. }
  289. if (ctx->nb_streams < 1) {
  290. fprintf(stderr, "No streams found in %s\n", file);
  291. goto fail;
  292. }
  293. for (i = 0; i < ctx->nb_streams; i++) {
  294. struct Track **temp;
  295. AVStream *st = ctx->streams[i];
  296. track = av_mallocz(sizeof(*track));
  297. if (!track) {
  298. err = AVERROR(ENOMEM);
  299. goto fail;
  300. }
  301. temp = av_realloc(tracks->tracks,
  302. sizeof(*tracks->tracks) * (tracks->nb_tracks + 1));
  303. if (!temp) {
  304. av_free(track);
  305. err = AVERROR(ENOMEM);
  306. goto fail;
  307. }
  308. tracks->tracks = temp;
  309. tracks->tracks[tracks->nb_tracks] = track;
  310. track->name = file;
  311. if ((ptr = strrchr(file, '/')))
  312. track->name = ptr + 1;
  313. track->bitrate = st->codec->bit_rate;
  314. track->track_id = st->id;
  315. track->timescale = st->time_base.den;
  316. track->duration = st->duration;
  317. track->is_audio = st->codec->codec_type == AVMEDIA_TYPE_AUDIO;
  318. track->is_video = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
  319. if (!track->is_audio && !track->is_video) {
  320. fprintf(stderr,
  321. "Track %d in %s is neither video nor audio, skipping\n",
  322. track->track_id, file);
  323. av_freep(&tracks->tracks[tracks->nb_tracks]);
  324. continue;
  325. }
  326. tracks->duration = FFMAX(tracks->duration,
  327. av_rescale_rnd(track->duration, AV_TIME_BASE,
  328. track->timescale, AV_ROUND_UP));
  329. if (track->is_audio) {
  330. if (tracks->audio_track < 0)
  331. tracks->audio_track = tracks->nb_tracks;
  332. tracks->nb_audio_tracks++;
  333. track->channels = st->codec->channels;
  334. track->sample_rate = st->codec->sample_rate;
  335. if (st->codec->codec_id == AV_CODEC_ID_AAC) {
  336. track->fourcc = "AACL";
  337. track->tag = 255;
  338. track->blocksize = 4;
  339. } else if (st->codec->codec_id == AV_CODEC_ID_WMAPRO) {
  340. track->fourcc = "WMAP";
  341. track->tag = st->codec->codec_tag;
  342. track->blocksize = st->codec->block_align;
  343. }
  344. get_private_data(track, st->codec);
  345. }
  346. if (track->is_video) {
  347. if (tracks->video_track < 0)
  348. tracks->video_track = tracks->nb_tracks;
  349. tracks->nb_video_tracks++;
  350. track->width = st->codec->width;
  351. track->height = st->codec->height;
  352. if (st->codec->codec_id == AV_CODEC_ID_H264)
  353. track->fourcc = "H264";
  354. else if (st->codec->codec_id == AV_CODEC_ID_VC1)
  355. track->fourcc = "WVC1";
  356. get_video_private_data(track, st->codec);
  357. }
  358. tracks->nb_tracks++;
  359. }
  360. avformat_close_input(&ctx);
  361. err = read_mfra(tracks, orig_tracks, file, split, output_prefix);
  362. fail:
  363. if (ctx)
  364. avformat_close_input(&ctx);
  365. return err;
  366. }
  367. static void output_server_manifest(struct Tracks *tracks, const char *basename,
  368. const char *output_prefix,
  369. const char *path_prefix,
  370. const char *ismc_prefix)
  371. {
  372. char filename[1000];
  373. FILE *out;
  374. int i;
  375. snprintf(filename, sizeof(filename), "%s%s.ism", output_prefix, basename);
  376. out = fopen(filename, "w");
  377. if (!out) {
  378. perror(filename);
  379. return;
  380. }
  381. fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  382. fprintf(out, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
  383. fprintf(out, "\t<head>\n");
  384. fprintf(out, "\t\t<meta name=\"clientManifestRelativePath\" "
  385. "content=\"%s%s.ismc\" />\n", ismc_prefix, basename);
  386. fprintf(out, "\t</head>\n");
  387. fprintf(out, "\t<body>\n");
  388. fprintf(out, "\t\t<switch>\n");
  389. for (i = 0; i < tracks->nb_tracks; i++) {
  390. struct Track *track = tracks->tracks[i];
  391. const char *type = track->is_video ? "video" : "audio";
  392. fprintf(out, "\t\t\t<%s src=\"%s%s\" systemBitrate=\"%d\">\n",
  393. type, path_prefix, track->name, track->bitrate);
  394. fprintf(out, "\t\t\t\t<param name=\"trackID\" value=\"%d\" "
  395. "valueType=\"data\" />\n", track->track_id);
  396. fprintf(out, "\t\t\t</%s>\n", type);
  397. }
  398. fprintf(out, "\t\t</switch>\n");
  399. fprintf(out, "\t</body>\n");
  400. fprintf(out, "</smil>\n");
  401. fclose(out);
  402. }
  403. static void print_track_chunks(FILE *out, struct Tracks *tracks, int main,
  404. const char *type)
  405. {
  406. int i, j;
  407. struct Track *track = tracks->tracks[main];
  408. int should_print_time_mismatch = 1;
  409. for (i = 0; i < track->chunks; i++) {
  410. for (j = main + 1; j < tracks->nb_tracks; j++) {
  411. if (tracks->tracks[j]->is_audio == track->is_audio) {
  412. if (track->offsets[i].duration != tracks->tracks[j]->offsets[i].duration) {
  413. fprintf(stderr, "Mismatched duration of %s chunk %d in %s (%d) and %s (%d)\n",
  414. type, i, track->name, main, tracks->tracks[j]->name, j);
  415. should_print_time_mismatch = 1;
  416. }
  417. if (track->offsets[i].time != tracks->tracks[j]->offsets[i].time) {
  418. if (should_print_time_mismatch)
  419. fprintf(stderr, "Mismatched (start) time of %s chunk %d in %s (%d) and %s (%d)\n",
  420. type, i, track->name, main, tracks->tracks[j]->name, j);
  421. should_print_time_mismatch = 0;
  422. }
  423. }
  424. }
  425. fprintf(out, "\t\t<c n=\"%d\" d=\"%"PRId64"\" />\n",
  426. i, track->offsets[i].duration);
  427. }
  428. }
  429. static void output_client_manifest(struct Tracks *tracks, const char *basename,
  430. const char *output_prefix, int split)
  431. {
  432. char filename[1000];
  433. FILE *out;
  434. int i, j;
  435. if (split)
  436. snprintf(filename, sizeof(filename), "%sManifest", output_prefix);
  437. else
  438. snprintf(filename, sizeof(filename), "%s%s.ismc", output_prefix, basename);
  439. out = fopen(filename, "w");
  440. if (!out) {
  441. perror(filename);
  442. return;
  443. }
  444. fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  445. fprintf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" "
  446. "Duration=\"%"PRId64 "\">\n", tracks->duration * 10);
  447. if (tracks->video_track >= 0) {
  448. struct Track *track = tracks->tracks[tracks->video_track];
  449. struct Track *first_track = track;
  450. int index = 0;
  451. fprintf(out,
  452. "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" "
  453. "Chunks=\"%d\" "
  454. "Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n",
  455. tracks->nb_video_tracks, track->chunks);
  456. for (i = 0; i < tracks->nb_tracks; i++) {
  457. track = tracks->tracks[i];
  458. if (!track->is_video)
  459. continue;
  460. fprintf(out,
  461. "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
  462. "FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" "
  463. "CodecPrivateData=\"",
  464. index, track->bitrate, track->fourcc, track->width, track->height);
  465. for (j = 0; j < track->codec_private_size; j++)
  466. fprintf(out, "%02X", track->codec_private[j]);
  467. fprintf(out, "\" />\n");
  468. index++;
  469. if (track->chunks != first_track->chunks)
  470. fprintf(stderr, "Mismatched number of video chunks in %s (id: %d, chunks %d) and %s (id: %d, chunks %d)\n",
  471. track->name, track->track_id, track->chunks, first_track->name, first_track->track_id, first_track->chunks);
  472. }
  473. print_track_chunks(out, tracks, tracks->video_track, "video");
  474. fprintf(out, "\t</StreamIndex>\n");
  475. }
  476. if (tracks->audio_track >= 0) {
  477. struct Track *track = tracks->tracks[tracks->audio_track];
  478. struct Track *first_track = track;
  479. int index = 0;
  480. fprintf(out,
  481. "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" "
  482. "Chunks=\"%d\" "
  483. "Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n",
  484. tracks->nb_audio_tracks, track->chunks);
  485. for (i = 0; i < tracks->nb_tracks; i++) {
  486. track = tracks->tracks[i];
  487. if (!track->is_audio)
  488. continue;
  489. fprintf(out,
  490. "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
  491. "FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" "
  492. "BitsPerSample=\"16\" PacketSize=\"%d\" "
  493. "AudioTag=\"%d\" CodecPrivateData=\"",
  494. index, track->bitrate, track->fourcc, track->sample_rate,
  495. track->channels, track->blocksize, track->tag);
  496. for (j = 0; j < track->codec_private_size; j++)
  497. fprintf(out, "%02X", track->codec_private[j]);
  498. fprintf(out, "\" />\n");
  499. index++;
  500. if (track->chunks != first_track->chunks)
  501. fprintf(stderr, "Mismatched number of audio chunks in %s and %s\n",
  502. track->name, first_track->name);
  503. }
  504. print_track_chunks(out, tracks, tracks->audio_track, "audio");
  505. fprintf(out, "\t</StreamIndex>\n");
  506. }
  507. fprintf(out, "</SmoothStreamingMedia>\n");
  508. fclose(out);
  509. }
  510. static void clean_tracks(struct Tracks *tracks)
  511. {
  512. int i;
  513. for (i = 0; i < tracks->nb_tracks; i++) {
  514. av_freep(&tracks->tracks[i]->codec_private);
  515. av_freep(&tracks->tracks[i]->offsets);
  516. av_freep(&tracks->tracks[i]);
  517. }
  518. av_freep(&tracks->tracks);
  519. tracks->nb_tracks = 0;
  520. }
  521. int main(int argc, char **argv)
  522. {
  523. const char *basename = NULL;
  524. const char *path_prefix = "", *ismc_prefix = "";
  525. const char *output_prefix = "";
  526. char output_prefix_buf[2048];
  527. int split = 0, i;
  528. struct Tracks tracks = { 0, .video_track = -1, .audio_track = -1 };
  529. av_register_all();
  530. for (i = 1; i < argc; i++) {
  531. if (!strcmp(argv[i], "-n")) {
  532. basename = argv[i + 1];
  533. i++;
  534. } else if (!strcmp(argv[i], "-path-prefix")) {
  535. path_prefix = argv[i + 1];
  536. i++;
  537. } else if (!strcmp(argv[i], "-ismc-prefix")) {
  538. ismc_prefix = argv[i + 1];
  539. i++;
  540. } else if (!strcmp(argv[i], "-output")) {
  541. output_prefix = argv[i + 1];
  542. i++;
  543. if (output_prefix[strlen(output_prefix) - 1] != '/') {
  544. snprintf(output_prefix_buf, sizeof(output_prefix_buf),
  545. "%s/", output_prefix);
  546. output_prefix = output_prefix_buf;
  547. }
  548. } else if (!strcmp(argv[i], "-split")) {
  549. split = 1;
  550. } else if (argv[i][0] == '-') {
  551. return usage(argv[0], 1);
  552. } else {
  553. if (handle_file(&tracks, argv[i], split, output_prefix))
  554. return 1;
  555. }
  556. }
  557. if (!tracks.nb_tracks || (!basename && !split))
  558. return usage(argv[0], 1);
  559. if (!split)
  560. output_server_manifest(&tracks, basename, output_prefix,
  561. path_prefix, ismc_prefix);
  562. output_client_manifest(&tracks, basename, output_prefix, split);
  563. clean_tracks(&tracks);
  564. return 0;
  565. }