shithub: mcfs

Download patch

ref: b1c20b110bb60ffba60baa27d7c076055438b8e2
parent: 517cd21652ad1f1492b8a74bc16889dddc22f614
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Tue Mar 2 06:38:17 EST 2021

matroska: add ASS subtitles extraction (no timestamps rewriting yet)

--- /dev/null
+++ b/ass.c
@@ -1,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;
+}
--- a/common.h
+++ b/common.h
@@ -16,6 +16,7 @@
 	FmtMp3 = 0x006d7033u,
 	FmtTheora = 0x74687261u,
 	FmtFlac = 0x666c6163u,
+	FmtAss = 0x00617373u, /* ass subtitles */
 };
 
 #define min(a,b) ((a)<=(b)?(a):(b))
--- a/matroska.c
+++ b/matroska.c
@@ -85,6 +85,8 @@
 	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 @@
 		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;
--- a/mkfile
+++ b/mkfile
@@ -6,6 +6,7 @@
 
 OFILES=\
 	aac.$O\
+	ass.$O\
 	crc32.$O\
 	ebml.$O\
 	iso.$O\
--- a/packet.h
+++ b/packet.h
@@ -53,6 +53,7 @@
 };
 
 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);