parse_chapters.c 1.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include <libavformat/avformat.h>

#include "../util.h"

#define CHAPTER_TIME_BASE (AVRational){1, 1}

void parse_chapters(AVFormatContext *ctx, char *s, int64_t duration)
{
	char *p;
	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 ++);
13 14
	ctx->chapters = xmalloc(sizeof(AVChapter *)*(i+1));
	for (p = jenter(s), i = 0; p; i ++)
15 16 17 18 19
	{
		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;
20 21 22 23 24 25 26
		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;
		}
27 28 29
		ctx->chapters[i]->start = jint(jlookup(p, "time"), 0);
		if (i)
			ctx->chapters[i-1]->end = ctx->chapters[i]->start;
30 31
		av_dict_set(&ctx->chapters[i]->metadata, "title", jstr(jlookup(p, "text"), "EMPTY"), 0);
		p = jnext(p);
32
	}
33
	ctx->nb_chapters = i;
34
	if (ctx->nb_chapters)
35
		ctx->chapters[ctx->nb_chapters-1]->end = duration;
36 37
}