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.
176 lines
7.3 KiB
176 lines
7.3 KiB
6 years ago
|
import re
|
||
9 months ago
|
import warnings
|
||
6 months ago
|
from typing import List, Sequence, Optional, TextIO, Any, Tuple
|
||
9 months ago
|
|
||
6 months ago
|
from .base import FormatBase
|
||
|
from ..ssaevent import SSAEvent
|
||
|
from ..ssastyle import SSAStyle
|
||
6 years ago
|
from .substation import parse_tags
|
||
6 months ago
|
from ..time import ms_to_times, make_time, TIMESTAMP, timestamp_to_ms
|
||
|
from ..ssafile import SSAFile
|
||
|
|
||
6 years ago
|
|
||
|
#: Largest timestamp allowed in SubRip, ie. 99:59:59,999.
|
||
|
MAX_REPRESENTABLE_TIME = make_time(h=100) - 1
|
||
|
|
||
|
|
||
|
class SubripFormat(FormatBase):
|
||
3 years ago
|
"""SubRip Text (SRT) subtitle format implementation"""
|
||
|
TIMESTAMP = TIMESTAMP
|
||
|
|
||
3 years ago
|
@staticmethod
|
||
|
def ms_to_timestamp(ms: int) -> str:
|
||
|
"""Convert ms to 'HH:MM:SS,mmm'"""
|
||
9 months ago
|
if ms < 0:
|
||
|
ms = 0
|
||
|
if ms > MAX_REPRESENTABLE_TIME:
|
||
|
warnings.warn("Overflow in SubRip timestamp, clamping to MAX_REPRESENTABLE_TIME", RuntimeWarning)
|
||
|
ms = MAX_REPRESENTABLE_TIME
|
||
3 years ago
|
h, m, s, ms = ms_to_times(ms)
|
||
9 months ago
|
return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"
|
||
3 years ago
|
|
||
3 years ago
|
@staticmethod
|
||
6 months ago
|
def timestamp_to_ms(groups: Sequence[str]) -> int:
|
||
3 years ago
|
return timestamp_to_ms(groups)
|
||
|
|
||
6 years ago
|
@classmethod
|
||
6 months ago
|
def guess_format(cls, text: str) -> Optional[str]:
|
||
3 years ago
|
"""See :meth:`pysubs2.formats.FormatBase.guess_format()`"""
|
||
6 years ago
|
if "[Script Info]" in text or "[V4+ Styles]" in text:
|
||
|
# disambiguation vs. SSA/ASS
|
||
|
return None
|
||
|
|
||
3 years ago
|
if text.lstrip().startswith("WEBVTT"):
|
||
|
# disambiguation vs. WebVTT
|
||
|
return None
|
||
|
|
||
6 years ago
|
for line in text.splitlines():
|
||
3 years ago
|
if len(cls.TIMESTAMP.findall(line)) == 2:
|
||
6 years ago
|
return "srt"
|
||
|
|
||
6 months ago
|
return None
|
||
|
|
||
6 years ago
|
@classmethod
|
||
6 months ago
|
def from_file(cls, subs: "SSAFile", fp: TextIO, format_: str, keep_html_tags: bool = False,
|
||
|
keep_unknown_html_tags: bool = False, **kwargs: Any) -> None:
|
||
3 years ago
|
"""
|
||
|
See :meth:`pysubs2.formats.FormatBase.from_file()`
|
||
|
|
||
|
Supported tags:
|
||
|
|
||
|
- ``<i>``
|
||
|
- ``<u>``
|
||
|
- ``<s>``
|
||
2 years ago
|
- ``<b>``
|
||
3 years ago
|
|
||
|
Keyword args:
|
||
2 years ago
|
keep_html_tags: If True, all HTML tags will be kept as-is instead of being
|
||
|
converted to SubStation tags (eg. you will get ``<i>example</i>`` instead of ``{\\i1}example{\\i0}``).
|
||
|
Setting this to True overrides the ``keep_unknown_html_tags`` option.
|
||
|
keep_unknown_html_tags: If True, supported HTML tags will be converted
|
||
|
to SubStation tags and any other HTML tags will be kept as-is
|
||
|
(eg. you would get ``<blink>example {\\i1}text{\\i0}</blink>``).
|
||
|
If False, these other HTML tags will be stripped from output
|
||
|
(in the previous example, you would get only ``example {\\i1}text{\\i0}``).
|
||
3 years ago
|
"""
|
||
6 months ago
|
timestamps: List[Tuple[int, int]] = [] # (start, end)
|
||
|
following_lines: List[List[str]] = [] # contains lists of lines following each timestamp
|
||
6 years ago
|
|
||
|
for line in fp:
|
||
3 years ago
|
stamps = cls.TIMESTAMP.findall(line)
|
||
6 years ago
|
if len(stamps) == 2: # timestamp line
|
||
3 years ago
|
start, end = map(cls.timestamp_to_ms, stamps)
|
||
6 years ago
|
timestamps.append((start, end))
|
||
|
following_lines.append([])
|
||
|
else:
|
||
|
if timestamps:
|
||
|
following_lines[-1].append(line)
|
||
|
|
||
6 months ago
|
def prepare_text(lines: List[str]) -> str:
|
||
6 years ago
|
# Handle the "happy" empty subtitle case, which is timestamp line followed by blank line(s)
|
||
|
# followed by number line and timestamp line of the next subtitle. Fixes issue #11.
|
||
|
if (len(lines) >= 2
|
||
3 years ago
|
and all(re.match(r"\s*$", line) for line in lines[:-1])
|
||
|
and re.match(r"\s*\d+\s*$", lines[-1])):
|
||
6 years ago
|
return ""
|
||
|
|
||
|
# Handle the general case.
|
||
6 years ago
|
s = "".join(lines).strip()
|
||
6 years ago
|
s = re.sub(r"\n+ *\d+ *$", "", s) # strip number of next subtitle
|
||
2 years ago
|
if not keep_html_tags:
|
||
|
s = re.sub(r"< *i *>", r"{\\i1}", s)
|
||
|
s = re.sub(r"< */ *i *>", r"{\\i0}", s)
|
||
|
s = re.sub(r"< *s *>", r"{\\s1}", s)
|
||
|
s = re.sub(r"< */ *s *>", r"{\\s0}", s)
|
||
|
s = re.sub(r"< *u *>", r"{\\u1}", s)
|
||
|
s = re.sub(r"< */ *u *>", r"{\\u0}", s)
|
||
|
s = re.sub(r"< *b *>", r"{\\b1}", s)
|
||
|
s = re.sub(r"< */ *b *>", r"{\\b0}", s)
|
||
|
if not (keep_html_tags or keep_unknown_html_tags):
|
||
5 years ago
|
s = re.sub(r"< */? *[a-zA-Z][^>]*>", "", s) # strip other HTML tags
|
||
|
s = re.sub(r"\n", r"\\N", s) # convert newlines
|
||
6 years ago
|
return s
|
||
|
|
||
6 months ago
|
for (start, end), lines in zip(timestamps, following_lines):
|
||
|
e = SSAEvent(start=start, end=end, text=prepare_text(lines))
|
||
|
subs.append(e)
|
||
6 years ago
|
|
||
|
@classmethod
|
||
6 months ago
|
def to_file(cls, subs: "SSAFile", fp: TextIO, format_: str, apply_styles: bool = True,
|
||
|
keep_ssa_tags: bool = False, **kwargs: Any) -> None:
|
||
3 years ago
|
"""
|
||
|
See :meth:`pysubs2.formats.FormatBase.to_file()`
|
||
|
|
||
|
Italic, underline and strikeout styling is supported.
|
||
|
|
||
|
Keyword args:
|
||
2 years ago
|
apply_styles: If False, do not write any styling (ignore line style
|
||
|
and override tags).
|
||
|
keep_ssa_tags: If True, instead of trying to convert inline override
|
||
|
tags to HTML (as supported by SRT), any inline tags will be passed
|
||
|
to output (eg. ``{\\an7}``, which would be otherwise stripped;
|
||
|
or ``{\\b1}`` instead of ``<b>``). Whitespace tags ``\\h``, ``\\n``
|
||
|
and ``\\N`` will always be converted to whitespace regardless of
|
||
|
this option. In the current implementation, enabling this option
|
||
|
disables processing of line styles - you will get inline tags but
|
||
|
if for example line's style is italic you will not get ``{\\i1}``
|
||
|
at the beginning of the line. (Since this option is mostly useful
|
||
|
for dealing with non-standard SRT files, ie. both input and output
|
||
|
is SRT which doesn't use line styles - this shouldn't be much
|
||
|
of an issue in practice.)
|
||
3 years ago
|
"""
|
||
6 months ago
|
def prepare_text(text: str, style: SSAStyle) -> str:
|
||
2 years ago
|
text = text.replace(r"\h", " ")
|
||
|
text = text.replace(r"\n", "\n")
|
||
|
text = text.replace(r"\N", "\n")
|
||
|
|
||
6 years ago
|
body = []
|
||
2 years ago
|
if keep_ssa_tags:
|
||
|
body.append(text)
|
||
|
else:
|
||
|
for fragment, sty in parse_tags(text, style, subs.styles):
|
||
|
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>"
|
||
2 years ago
|
body.append(fragment)
|
||
6 years ago
|
|
||
|
return re.sub("\n+", "\n", "".join(body).strip())
|
||
|
|
||
6 months ago
|
for lineno, line in enumerate(cls._get_visible_lines(subs), 1):
|
||
3 years ago
|
start = cls.ms_to_timestamp(line.start)
|
||
|
end = cls.ms_to_timestamp(line.end)
|
||
6 months ago
|
text = prepare_text(line.text, subs.styles.get(line.style, SSAStyle.DEFAULT_STYLE))
|
||
6 years ago
|
|
||
9 months ago
|
print(lineno, file=fp)
|
||
6 years ago
|
print(start, "-->", end, file=fp)
|
||
|
print(text, end="\n\n", file=fp)
|
||
3 years ago
|
lineno += 1
|
||
9 months ago
|
|
||
|
@classmethod
|
||
6 months ago
|
def _get_visible_lines(cls, subs: "SSAFile") -> List[SSAEvent]:
|
||
|
return subs.get_text_events()
|