~ft/mcfs

b1c20b110bb60ffba60baa27d7c076055438b8e2 — Sigrid Solveig Haflínudóttir 2 years ago 517cd21
matroska: add ASS subtitles extraction (no timestamps rewriting yet)
5 files changed, 49 insertions(+), 0 deletions(-)

A ass.c
M common.h
M matroska.c
M mkfile
M packet.h
A ass.c => ass.c +41 -0
@@ 0,0 1,41 @@
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "common.h"
#include "packet.h"

static int
sanitize(uchar *s, int sz)
{
	uchar *o, *i;

	for(o = i = s; sz > 0;){
		if(*i == '\r'){
			if(sz <= 1 || i[1] != '\n')
				*o++ = '\n';
			i++;
		}else
			*o++ = *i++;
		sz--;
	}

	return o-s;
}

int
asspacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts, int key)
{
	int i;

	USED(ts, key);
	if(ctx->frid == 0)
		Bwrite(out, ctx->codec.priv.data, sanitize(ctx->codec.priv.data, ctx->codec.priv.sz));
	for(i = 0; i < np; i++, p++){
		Bwrite(out, p->data, sanitize(p->data, p->sz));
		Bwrite(out, "\n", 1);
	}
	ctx->frid++;
	Bflush(out);

	return 0;
}

M common.h => common.h +1 -0
@@ 16,6 16,7 @@ enum {
	FmtMp3 = 0x006d7033u,
	FmtTheora = 0x74687261u,
	FmtFlac = 0x666c6163u,
	FmtAss = 0x00617373u, /* ass subtitles */
};

#define min(a,b) ((a)<=(b)?(a):(b))

M matroska.c => matroska.c +5 -0
@@ 85,6 85,8 @@ format(Ebml *e)
	if(e->tracktype == Etracksubtitles){
		if(strcmp(e->codec.name, "S_TEXT/UTF8") == 0)
			return "srt";
		else if(strcmp(e->codec.name, "S_TEXT/ASS") == 0)
			return "ass";
	}else if(e->tracktype == Etrackaudio){
		if(strcmp(e->codec.name, "A_MPEG/L3") == 0)
			return "mp3";


@@ 162,6 164,9 @@ initctx(Ebml *e, double duration)
		if(strcmp(c, "srt") == 0){
			e->fmt = FmtSrt;
			e->fpacket = srtpacket;
		}else if(strcmp(c, "ass") == 0){
			e->fmt = FmtAss;
			e->fpacket = asspacket;
		}else
			goto err;
		return 0;

M mkfile => mkfile +1 -0
@@ 6,6 6,7 @@ BIN=/$objtype/bin

OFILES=\
	aac.$O\
	ass.$O\
	crc32.$O\
	ebml.$O\
	iso.$O\

M packet.h => packet.h +1 -0
@@ 53,6 53,7 @@ struct Packetctx {
};

int aacpacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts, int key);
int asspacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts, int key);
int ivfpacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts, int key);
int oggpacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts, int key);
int srtpacket(Biobuf *out, Packetctx *ctx, Packet *p, int np, uvlong ts, int key);