You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

701 lines
24KB

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