/*
* Copyright (c) 2012 Martin Storsjo
*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
*
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* To create a simple file for smooth streaming:
- * avconv <normal input/transcoding options> -movflags frag_keyframe foo.ismv
+ * ffmpeg <normal input/transcoding options> -movflags frag_keyframe foo.ismv
* ismindex -n foo foo.ismv
* This step creates foo.ism and foo.ismc that is required by IIS for
* serving it.
*
+ * With -ismf, it also creates foo.ismf, which maps fragment names to
+ * start-end offsets in the ismv, for use in your own streaming server.
+ *
* By adding -path-prefix path/, the produced foo.ism will refer to the
* files foo.ismv as "path/foo.ismv" - the prefix for the generated ismc
* file can be set with the -ismc-prefix option similarly.
#include <stdio.h>
#include <string.h>
+#include "cmdutils.h"
+
#include "libavformat/avformat.h"
#include "libavformat/os_support.h"
#include "libavutil/intreadwrite.h"
static int usage(const char *argv0, int ret)
{
- fprintf(stderr, "%s [-split] [-n basename] [-path-prefix prefix] "
+ fprintf(stderr, "%s [-split] [-ismf] [-n basename] [-path-prefix prefix] "
"[-ismc-prefix prefix] [-output dir] file1 [file2] ...\n", argv0);
return ret;
}
int nb_video_tracks, nb_audio_tracks;
};
+ static int expect_tag(int32_t got_tag, int32_t expected_tag) {
+ if (got_tag != expected_tag) {
+ char got_tag_str[4], expected_tag_str[4];
+ AV_WB32(got_tag_str, got_tag);
+ AV_WB32(expected_tag_str, expected_tag);
+ fprintf(stderr, "wanted tag %.4s, got %.4s\n", expected_tag_str,
+ got_tag_str);
+ return -1;
+ }
+ return 0;
+ }
+
static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
{
int32_t size, tag;
tag = avio_rb32(in);
avio_wb32(out, size);
avio_wb32(out, tag);
- if (tag != tag_name) {
- char tag_str[4], tag_name_str[4];
- AV_WB32(tag_str, tag);
- AV_WB32(tag_name_str, tag_name);
- fprintf(stderr, "wanted tag %.4s, got %.4s\n", tag_name_str, tag_str);
+ if (expect_tag(tag, tag_name) != 0)
return -1;
- }
size -= 8;
while (size > 0) {
char buf[1024];
return 0;
}
+ static int skip_tag(AVIOContext *in, int32_t tag_name)
+ {
+ int64_t pos = avio_tell(in);
+ int32_t size, tag;
+
+ size = avio_rb32(in);
+ tag = avio_rb32(in);
+ if (expect_tag(tag, tag_name) != 0)
+ return -1;
+ avio_seek(in, pos + size, SEEK_SET);
+ return 0;
+ }
+
static int write_fragment(const char *filename, AVIOContext *in)
{
AVIOContext *out = NULL;
return ret;
}
- static int write_fragments(struct Tracks *tracks, int start_index,
- AVIOContext *in, const char *output_prefix)
+ static int skip_fragment(AVIOContext *in)
{
- char dirname[2048], filename[2048];
- int i, j;
+ int ret;
+ ret = skip_tag(in, MKBETAG('m', 'o', 'o', 'f'));
+ if (!ret)
+ ret = skip_tag(in, MKBETAG('m', 'd', 'a', 't'));
+ return ret;
+ }
+ static int write_fragments(struct Tracks *tracks, int start_index,
+ AVIOContext *in, const char *basename,
+ int split, int ismf, const char* output_prefix)
+ {
+ char dirname[2048], filename[2048], idxname[2048];
+ int i, j, ret = 0, fragment_ret;
+ FILE* out = NULL;
+
+ if (ismf) {
+ snprintf(idxname, sizeof(idxname), "%s%s.ismf", output_prefix, basename);
+ out = fopen(idxname, "w");
+ if (!out) {
+ ret = AVERROR(errno);
+ perror(idxname);
+ goto fail;
+ }
+ }
for (i = start_index; i < tracks->nb_tracks; i++) {
struct Track *track = tracks->tracks[i];
const char *type = track->is_video ? "video" : "audio";
snprintf(dirname, sizeof(dirname), "%sQualityLevels(%d)", output_prefix, track->bitrate);
- if (mkdir(dirname, 0777) == -1)
- return AVERROR(errno);
+ if (split) {
+ if (mkdir(dirname, 0777) == -1 && errno != EEXIST) {
+ ret = AVERROR(errno);
+ perror(dirname);
+ goto fail;
+ }
+ }
for (j = 0; j < track->chunks; j++) {
snprintf(filename, sizeof(filename), "%s/Fragments(%s=%"PRId64")",
dirname, type, track->offsets[j].time);
avio_seek(in, track->offsets[j].offset, SEEK_SET);
- write_fragment(filename, in);
+ if (ismf)
+ fprintf(out, "%s %"PRId64, filename, avio_tell(in));
+ if (split)
+ fragment_ret = write_fragment(filename, in);
+ else
+ fragment_ret = skip_fragment(in);
+ if (ismf)
+ fprintf(out, " %"PRId64"\n", avio_tell(in));
+ if (fragment_ret != 0) {
+ fprintf(stderr, "failed fragment %d in track %d (%s)\n", j,
+ track->track_id, track->name);
+ ret = fragment_ret;
+ }
}
}
- return 0;
+ fail:
+ if (out)
+ fclose(out);
+ return ret;
}
static int read_tfra(struct Tracks *tracks, int start_index, AVIOContext *f)
}
fieldlength = avio_rb32(f);
track->chunks = avio_rb32(f);
- track->offsets = av_mallocz(sizeof(*track->offsets) * track->chunks);
+ track->offsets = av_mallocz_array(track->chunks, sizeof(*track->offsets));
if (!track->offsets) {
ret = AVERROR(ENOMEM);
goto fail;
}
static int read_mfra(struct Tracks *tracks, int start_index,
- const char *file, int split, const char *output_prefix)
+ const char *file, int split, int ismf,
+ const char *basename, const char* output_prefix)
{
int err = 0;
const char* err_str = "";
/* Empty */
}
- if (split)
- err = write_fragments(tracks, start_index, f, output_prefix);
+ if (split || ismf)
+ err = write_fragments(tracks, start_index, f, basename, split, ismf,
+ output_prefix);
+ err_str = "error in write_fragments";
fail:
if (f)
}
static int handle_file(struct Tracks *tracks, const char *file, int split,
- const char *output_prefix)
+ int ismf, const char *basename,
+ const char* output_prefix)
{
AVFormatContext *ctx = NULL;
int err = 0, i, orig_tracks = tracks->nb_tracks;
AVStream *st = ctx->streams[i];
if (st->codec->bit_rate == 0) {
- fprintf(stderr, "Skipping track %d in %s as it has zero bitrate\n", i, file);
+ fprintf(stderr, "Skipping track %d in %s as it has zero bitrate\n",
+ st->id, file);
continue;
}
avformat_close_input(&ctx);
- err = read_mfra(tracks, orig_tracks, file, split, output_prefix);
+ err = read_mfra(tracks, orig_tracks, file, split, ismf, basename,
+ output_prefix);
fail:
if (ctx)
const char *path_prefix = "", *ismc_prefix = "";
const char *output_prefix = "";
char output_prefix_buf[2048];
- int split = 0, i;
+ int split = 0, ismf = 0, i;
struct Tracks tracks = { 0, .video_track = -1, .audio_track = -1 };
av_register_all();
}
} else if (!strcmp(argv[i], "-split")) {
split = 1;
+ } else if (!strcmp(argv[i], "-ismf")) {
+ ismf = 1;
} else if (argv[i][0] == '-') {
return usage(argv[0], 1);
} else {
- if (handle_file(&tracks, argv[i], split, output_prefix))
+ if (handle_file(&tracks, argv[i], split, ismf,
+ basename, output_prefix))
return 1;
}
}