You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
4.0 KiB
114 lines
4.0 KiB
5 years ago
|
import re
|
||
9 months ago
|
import warnings
|
||
6 months ago
|
from typing import Optional, TextIO, Any
|
||
9 months ago
|
|
||
6 months ago
|
from .base import FormatBase
|
||
|
from ..ssaevent import SSAEvent
|
||
|
from ..ssastyle import SSAStyle
|
||
5 years ago
|
from .substation import parse_tags
|
||
6 months ago
|
from ..time import ms_to_times, make_time, TIMESTAMP_SHORT, timestamp_to_ms
|
||
|
from ..ssafile import SSAFile
|
||
|
|
||
5 years ago
|
|
||
|
#: Pattern that matches TMP line
|
||
|
TMP_LINE = re.compile(r"(\d{1,2}:\d{2}:\d{2}):(.+)")
|
||
|
|
||
|
#: Largest timestamp allowed in Tmp, ie. 99:59:59.
|
||
9 months ago
|
MAX_REPRESENTABLE_TIME = make_time(h=99, m=59, s=59)
|
||
5 years ago
|
|
||
3 years ago
|
|
||
5 years ago
|
class TmpFormat(FormatBase):
|
||
3 years ago
|
"""TMP subtitle format implementation"""
|
||
3 years ago
|
|
||
|
@staticmethod
|
||
|
def ms_to_timestamp(ms: int) -> str:
|
||
|
"""Convert ms to 'HH:MM:SS'"""
|
||
9 months ago
|
if ms < 0:
|
||
|
ms = 0
|
||
|
if ms > MAX_REPRESENTABLE_TIME:
|
||
|
warnings.warn("Overflow in TMP timestamp, clamping to MAX_REPRESENTABLE_TIME", RuntimeWarning)
|
||
|
ms = MAX_REPRESENTABLE_TIME
|
||
|
h, m, s, _ = ms_to_times(ms)
|
||
|
return f"{h:02d}:{m:02d}:{s:02d}"
|
||
3 years ago
|
|
||
5 years ago
|
@classmethod
|
||
6 months ago
|
def guess_format(cls, text: str) -> Optional[str]:
|
||
3 years ago
|
"""See :meth:`pysubs2.formats.FormatBase.guess_format()`"""
|
||
5 years ago
|
if "[Script Info]" in text or "[V4+ Styles]" in text:
|
||
|
# disambiguation vs. SSA/ASS
|
||
|
return None
|
||
|
|
||
|
for line in text.splitlines():
|
||
|
if TMP_LINE.match(line) and len(TMP_LINE.findall(line)) == 1:
|
||
|
return "tmp"
|
||
|
|
||
6 months ago
|
return None
|
||
|
|
||
5 years ago
|
@classmethod
|
||
6 months ago
|
def from_file(cls, subs: "SSAFile", fp: TextIO, format_: str, **kwargs: Any) -> None:
|
||
3 years ago
|
"""See :meth:`pysubs2.formats.FormatBase.from_file()`"""
|
||
|
events = []
|
||
|
|
||
6 months ago
|
def prepare_text(text: str) -> str:
|
||
3 years ago
|
text = text.replace("|", r"\N") # convert newlines
|
||
6 months ago
|
text = re.sub(r"< *u *>", r"{\\u1}", text)
|
||
3 years ago
|
text = re.sub(r"< */? *[a-zA-Z][^>]*>", "", text) # strip other HTML tags
|
||
|
return text
|
||
5 years ago
|
|
||
|
for line in fp:
|
||
|
match = TMP_LINE.match(line)
|
||
|
if not match:
|
||
|
continue
|
||
|
|
||
|
start, text = match.groups()
|
||
6 months ago
|
match2 = TIMESTAMP_SHORT.match(start)
|
||
|
assert match2 is not None, "TMP_LINE contains TIMESTAMP_SHORT"
|
||
|
start = timestamp_to_ms(match2.groups())
|
||
5 years ago
|
|
||
3 years ago
|
# 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)
|
||
5 years ago
|
|
||
3 years ago
|
# 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
|
||
5 years ago
|
|
||
|
@classmethod
|
||
6 months ago
|
def to_file(cls, subs: "SSAFile", fp: TextIO, format_: str, apply_styles: bool = True, **kwargs: Any) -> None:
|
||
3 years ago
|
"""
|
||
|
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.
|
||
|
|
||
|
"""
|
||
6 months ago
|
def prepare_text(text: str, style: SSAStyle) -> str:
|
||
5 years ago
|
body = []
|
||
|
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")
|
||
3 years ago
|
if apply_styles:
|
||
6 months ago
|
if sty.italic:
|
||
|
fragment = f"<i>{fragment}</i>"
|
||
|
if sty.underline:
|
||
|
fragment = f"<u>{fragment}</u>"
|
||
|
if sty.strikeout:
|
||
|
fragment = f"<s>{fragment}</s>"
|
||
5 years ago
|
body.append(fragment)
|
||
|
|
||
6 months ago
|
return re.sub("\n+", "\n", "".join(body).strip())
|
||
5 years ago
|
|
||
6 months ago
|
for line in subs.get_text_events():
|
||
3 years ago
|
start = cls.ms_to_timestamp(line.start)
|
||
5 years ago
|
text = prepare_text(line.text, subs.styles.get(line.style, SSAStyle.DEFAULT_STYLE))
|
||
|
|
||
|
print(start + ":" + text, end="\n", file=fp)
|