[MPlayer-dev-eng] [PATCH] Style tag support for srt and microdvd subtitles

ubitux ubitux at gmail.com
Wed May 12 21:02:20 CEST 2010


On Wed, May 12, 2010 at 08:08:52PM +0200, ubitux wrote:
> On Tue, May 11, 2010 at 11:27:29PM +0300, Uoti Urpala wrote:
> > On Tue, 2010-05-11 at 19:03 +0000, Kazuo Teramoto wrote:
> > > Jonatan Nilsson <jh.nilsson <at> gmail.com> writes:
> > > > This patch adds support for font styles in external srt and microdvd
> > > > (.sub) subtitles. Srt-subtitles uses html tags to define tags such as
> > 
> > > Any opinion on this patch?
> > > 
> > > I'm using it and its works great. I use it with subrip (srt) subtitles that
> > > mainly use italic and bold.
> > > 
> > > I think this is a great addition.
> > 
> > I talked about this patch on irc with ubitux some days ago. My main
> > issue with it is the amount of complexity and abstraction. Compared to
> > an alternative much simpler implementation that would just add a
> > self-contained "convert SRT markup to ASS markup" function and call that
> > before feeding a line to libass, does the complexity of this patch
> > really give any significant extra benefit?
> > 
> > I haven't studied this enough to say for sure that that kind of
> > alternative implementation would be the best choice, but at least I
> > currently know of no reason why it would be impractical. I'm reluctant
> > to consider applying the patch unless it's either simplified or there's
> > an explanation why a simpler version would have significant problems.
> > 
> 
> Yeah, I'm working on it, give me a few days (maybe less).
> 

Okay, it was very easy in fact. Does this patch look good to you? It's
just a test using the libass rendering, so it works if you play the video
with -ass. Also, it's actually just for subrip files (.srt). If you like
it I can easily do the same for MicroSub.

It may be not very optimized but it's just a "proof of concept" following
what Uoti was saying.

Regards,

-- 
ubitux
-------------- next part --------------
Index: subreader.c
===================================================================
--- subreader.c	(revision 31163)
+++ subreader.c	(working copy)
@@ -36,6 +36,8 @@
 #include "libavutil/common.h"
 #include "libavutil/avstring.h"
 
+#include "libass/ass_mp.h"
+
 #ifdef CONFIG_ENCA
 #include <enca.h>
 #endif
@@ -358,6 +360,42 @@
     return current;
 }
 
+static const struct {
+    char *from;
+    char *to;
+} subviewer_to_ass[] = {
+    {"<i>", "{\\i1}"}, {"</i>", "{\\i0}"},
+    {"<b>", "{\\b1}"}, {"</b>", "{\\b0}"},
+    {"<u>", "{\\u1}"}, {"</u>", "{\\u0}"},
+    {"<s>", "{\\s1}"}, {"</s>", "{\\s0}"}
+};
+
+static void sub_subviewer_line_to_ass(char *line)
+{
+    char new_line[LINE_LEN + 1];
+    int i = 0, j = 0;
+
+    while (line[i]) {
+        int n, match = 0;
+
+        for (n = 0; n < (int)(sizeof(subviewer_to_ass) / sizeof(*subviewer_to_ass)); n++) {
+            int from_len = strlen(subviewer_to_ass[n].from);
+
+            if (strncmp(&line[i], subviewer_to_ass[n].from, from_len) == 0) {
+                strcpy(&new_line[j], subviewer_to_ass[n].to);
+                i += from_len;
+                j += strlen(subviewer_to_ass[n].to);
+                match = 1;
+                break;
+            }
+        }
+        if (!match)
+            new_line[j++] = line[i++];
+    }
+    new_line[j] = 0;
+    strcpy(line, new_line);
+}
+
 static subtitle *sub_read_line_subviewer(stream_t *st,subtitle *current, int utf16) {
     char line[LINE_LEN+1];
     int a1,a2,a3,a4,b1,b2,b3,b4;
@@ -373,6 +411,10 @@
 	for (i=0; i<SUB_MAX_TEXT;) {
 	    int blank = 1;
 	    if (!stream_read_line (st, line, LINE_LEN, utf16)) break;
+
+	    if (ass_enabled)
+		sub_subviewer_line_to_ass(line);
+
 	    len=0;
 	    for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++)
 		if (*p != ' ' && *p != '\t')
@@ -384,17 +426,19 @@
 		//strncpy (current->text[i], line, len); current->text[i][len]='\0';
                 for(; j<len; j++) {
 		    /* let's filter html tags ::atmos */
-		    if(line[j]=='>') {
-			skip=0;
-			continue;
+		    if (!ass_enabled) {
+			if(line[j]=='>') {
+			    skip=0;
+			    continue;
+			}
+			if(line[j]=='<') {
+			    skip=1;
+			    continue;
+			}
+			if(skip) {
+			    continue;
+			}
 		    }
-		    if(line[j]=='<') {
-			skip=1;
-			continue;
-		    }
-		    if(skip) {
-			continue;
-		    }
 		    *curptr=line[j];
 		    curptr++;
 		}


More information about the MPlayer-dev-eng mailing list