diff options
Diffstat (limited to 'libs/pysubs2/tmp.py')
-rw-r--r-- | libs/pysubs2/tmp.py | 67 |
1 files changed, 43 insertions, 24 deletions
diff --git a/libs/pysubs2/tmp.py b/libs/pysubs2/tmp.py index aae55202c..392c8615f 100644 --- a/libs/pysubs2/tmp.py +++ b/libs/pysubs2/tmp.py @@ -1,5 +1,3 @@ -from __future__ import print_function, unicode_literals - import re from .formatbase import FormatBase from .ssaevent import SSAEvent @@ -15,6 +13,7 @@ TMP_LINE = re.compile(r"(\d{1,2}:\d{2}:\d{2}):(.+)") #: Largest timestamp allowed in Tmp, ie. 99:59:59. MAX_REPRESENTABLE_TIME = make_time(h=100) - 1 + def ms_to_timestamp(ms): """Convert ms to 'HH:MM:SS'""" # XXX throw on overflow/underflow? @@ -25,8 +24,10 @@ def ms_to_timestamp(ms): class TmpFormat(FormatBase): + """TMP subtitle format implementation""" @classmethod def guess_format(cls, text): + """See :meth:`pysubs2.formats.FormatBase.guess_format()`""" if "[Script Info]" in text or "[V4+ Styles]" in text: # disambiguation vs. SSA/ASS return None @@ -37,8 +38,14 @@ class TmpFormat(FormatBase): @classmethod def from_file(cls, subs, fp, format_, **kwargs): - timestamps = [] # (start) - lines = [] # contains lists of lines following each timestamp + """See :meth:`pysubs2.formats.FormatBase.from_file()`""" + events = [] + + def prepare_text(text): + text = text.replace("|", r"\N") # convert newlines + text = re.sub(r"< *u *>", "{\\\\u1}", text) # not r" for Python 2.7 compat, triggers unicodeescape + text = re.sub(r"< */? *[a-zA-Z][^>]*>", "", text) # strip other HTML tags + return text for line in fp: match = TMP_LINE.match(line) @@ -47,42 +54,54 @@ class TmpFormat(FormatBase): start, text = match.groups() start = tmptimestamp_to_ms(TMPTIMESTAMP.match(start).groups()) - #calculate endtime from starttime + 500 miliseconds + 67 miliseconds per each character (15 chars per second) - end = start + 500 + (len(line) * 67) - timestamps.append((start, end)) - lines.append(text) - def prepare_text(lines): - lines = lines.replace("|", r"\N") # convert newlines - lines = re.sub(r"< *u *>", "{\\\\u1}", lines) # not r" for Python 2.7 compat, triggers unicodeescape - lines = re.sub(r"< */? *[a-zA-Z][^>]*>", "", lines) # strip other HTML tags - return lines + # Unfortunately, end timestamp is not given; try to estimate something reasonable: + # start + 500 ms + 67 ms/character (15 chars per second) + end_guess = start + 500 + (len(line) * 67) + + event = SSAEvent(start=start, end=end_guess, text=prepare_text(text)) + events.append(event) - subs.events = [SSAEvent(start=start, end=end, text=prepare_text(lines)) - for (start, end), lines in zip(timestamps, lines)] + # correct any overlapping subtitles created by end_guess + for i in range(len(events) - 1): + events[i].end = min(events[i].end, events[i+1].start) + + subs.events = events @classmethod - def to_file(cls, subs, fp, format_, **kwargs): + def to_file(cls, subs, fp, format_, apply_styles=True, **kwargs): + """ + See :meth:`pysubs2.formats.FormatBase.to_file()` + + Italic, underline and strikeout styling is supported. + + Keyword args: + apply_styles: If False, do not write any styling. + + """ def prepare_text(text, style): body = [] + skip = False for fragment, sty in parse_tags(text, style, subs.styles): fragment = fragment.replace(r"\h", " ") fragment = fragment.replace(r"\n", "\n") fragment = fragment.replace(r"\N", "\n") - if sty.italic: fragment = "<i>%s</i>" % fragment - if sty.underline: fragment = "<u>%s</u>" % fragment - if sty.strikeout: fragment = "<s>%s</s>" % fragment + if apply_styles: + if sty.italic: fragment = "<i>%s</i>" % fragment + if sty.underline: fragment = "<u>%s</u>" % fragment + if sty.strikeout: fragment = "<s>%s</s>" % fragment + if sty.drawing: skip = True body.append(fragment) - return re.sub("\n+", "\n", "".join(body).strip()) + if skip: + return "" + else: + return re.sub("\n+", "\n", "".join(body).strip()) visible_lines = (line for line in subs if not line.is_comment) - for i, line in enumerate(visible_lines, 1): + for line in visible_lines: start = ms_to_timestamp(line.start) - #end = ms_to_timestamp(line.end) text = prepare_text(line.text, subs.styles.get(line.style, SSAStyle.DEFAULT_STYLE)) - #print("%d" % i, file=fp) # Python 2.7 compat print(start + ":" + text, end="\n", file=fp) - #print(text, end="\n\n", file=fp) |