diff --git a/util/parse_chapters.c b/util/parse_chapters.c index cbe570ec32c7d0dc3b689d95dbebb474677a0f39..7d55bcbf2e1b18ba1d59f11b3e76388800e80d3d 100644 --- a/util/parse_chapters.c +++ b/util/parse_chapters.c @@ -10,20 +10,28 @@ void parse_chapters(AVFormatContext *ctx, char *s, int64_t duration) int i; duration = av_rescale_q(duration, AV_TIME_BASE_Q, CHAPTER_TIME_BASE); for (p = jenter(s), i = 0; p; p = jnext(p), i ++); - ctx->chapters = xmalloc(sizeof(AVChapter *)*i); - ctx->nb_chapters = i; - for (p = jenter(s), i = 0; p; p = jnext(p), i ++) + ctx->chapters = xmalloc(sizeof(AVChapter *)*(i+1)); + for (p = jenter(s), i = 0; p; i ++) { ctx->chapters[i] = malloc(sizeof(AVChapter)); memset(ctx->chapters[i], 0, sizeof(AVChapter)); ctx->chapters[i]->id = i; ctx->chapters[i]->time_base = CHAPTER_TIME_BASE; + if (!i && jint(jlookup(p, "time"), -1)) + { + /* Insert dummy chapter starting at time 0 (otherwise ffmpeg glitches) */ + ctx->chapters[i]->start = 0; + av_dict_set(&ctx->chapters[i]->metadata, "title", "Intro", 0); + continue; + } ctx->chapters[i]->start = jint(jlookup(p, "time"), 0); - av_dict_set(&ctx->chapters[i]->metadata, "title", jstr(jlookup(p, "text"), "EMPTY"), 0); if (i) ctx->chapters[i-1]->end = ctx->chapters[i]->start; + av_dict_set(&ctx->chapters[i]->metadata, "title", jstr(jlookup(p, "text"), "EMPTY"), 0); + p = jnext(p); } + ctx->nb_chapters = i; if (ctx->nb_chapters) - ctx->chapters[i-1]->end = duration; + ctx->chapters[ctx->nb_chapters-1]->end = duration; }