2018-10-31 16:08:29 +00:00
|
|
|
import re
|
2024-03-03 17:15:23 +00:00
|
|
|
import warnings
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
import pysubs2
|
2018-10-31 16:08:29 +00:00
|
|
|
from .formatbase import FormatBase
|
|
|
|
from .ssaevent import SSAEvent
|
|
|
|
from .ssastyle import SSAStyle
|
|
|
|
from .substation import parse_tags
|
2020-03-08 04:49:53 +00:00
|
|
|
from .exceptions import ContentNotUsable
|
2018-10-31 16:08:29 +00:00
|
|
|
from .time import ms_to_times, make_time, TIMESTAMP, timestamp_to_ms
|
|
|
|
|
|
|
|
#: Largest timestamp allowed in SubRip, ie. 99:59:59,999.
|
|
|
|
MAX_REPRESENTABLE_TIME = make_time(h=100) - 1
|
|
|
|
|
|
|
|
|
|
|
|
class SubripFormat(FormatBase):
|
2021-07-14 23:13:28 +00:00
|
|
|
"""SubRip Text (SRT) subtitle format implementation"""
|
|
|
|
TIMESTAMP = TIMESTAMP
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
@staticmethod
|
|
|
|
def ms_to_timestamp(ms: int) -> str:
|
|
|
|
"""Convert ms to 'HH:MM:SS,mmm'"""
|
2024-03-03 17:15:23 +00:00
|
|
|
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
|
2022-01-24 04:07:52 +00:00
|
|
|
h, m, s, ms = ms_to_times(ms)
|
2024-03-03 17:15:23 +00:00
|
|
|
return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2021-07-14 23:13:28 +00:00
|
|
|
@staticmethod
|
|
|
|
def timestamp_to_ms(groups):
|
|
|
|
return timestamp_to_ms(groups)
|
|
|
|
|
2018-10-31 16:08:29 +00:00
|
|
|
@classmethod
|
|
|
|
def guess_format(cls, text):
|
2021-07-14 23:13:28 +00:00
|
|
|
"""See :meth:`pysubs2.formats.FormatBase.guess_format()`"""
|
2018-10-31 16:08:29 +00:00
|
|
|
if "[Script Info]" in text or "[V4+ Styles]" in text:
|
|
|
|
# disambiguation vs. SSA/ASS
|
|
|
|
return None
|
|
|
|
|
2021-07-14 23:13:28 +00:00
|
|
|
if text.lstrip().startswith("WEBVTT"):
|
|
|
|
# disambiguation vs. WebVTT
|
|
|
|
return None
|
|
|
|
|
2018-10-31 16:08:29 +00:00
|
|
|
for line in text.splitlines():
|
2021-07-14 23:13:28 +00:00
|
|
|
if len(cls.TIMESTAMP.findall(line)) == 2:
|
2018-10-31 16:08:29 +00:00
|
|
|
return "srt"
|
|
|
|
|
|
|
|
@classmethod
|
2022-11-07 18:06:49 +00:00
|
|
|
def from_file(cls, subs, fp, format_, keep_html_tags=False, keep_unknown_html_tags=False, **kwargs):
|
2021-07-14 23:13:28 +00:00
|
|
|
"""
|
|
|
|
See :meth:`pysubs2.formats.FormatBase.from_file()`
|
|
|
|
|
|
|
|
Supported tags:
|
|
|
|
|
|
|
|
- ``<i>``
|
|
|
|
- ``<u>``
|
|
|
|
- ``<s>``
|
2022-11-07 18:06:49 +00:00
|
|
|
- ``<b>``
|
2021-07-14 23:13:28 +00:00
|
|
|
|
|
|
|
Keyword args:
|
2022-11-07 18:06:49 +00:00
|
|
|
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}``).
|
2021-07-14 23:13:28 +00:00
|
|
|
"""
|
2018-10-31 16:08:29 +00:00
|
|
|
timestamps = [] # (start, end)
|
|
|
|
following_lines = [] # contains lists of lines following each timestamp
|
|
|
|
|
|
|
|
for line in fp:
|
2021-07-14 23:13:28 +00:00
|
|
|
stamps = cls.TIMESTAMP.findall(line)
|
2018-10-31 16:08:29 +00:00
|
|
|
if len(stamps) == 2: # timestamp line
|
2021-07-14 23:13:28 +00:00
|
|
|
start, end = map(cls.timestamp_to_ms, stamps)
|
2018-10-31 16:08:29 +00:00
|
|
|
timestamps.append((start, end))
|
|
|
|
following_lines.append([])
|
|
|
|
else:
|
|
|
|
if timestamps:
|
|
|
|
following_lines[-1].append(line)
|
|
|
|
|
|
|
|
def prepare_text(lines):
|
2019-05-12 04:23:46 +00:00
|
|
|
# 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
|
2021-12-01 20:47:00 +00:00
|
|
|
and all(re.match(r"\s*$", line) for line in lines[:-1])
|
|
|
|
and re.match(r"\s*\d+\s*$", lines[-1])):
|
2019-05-12 04:23:46 +00:00
|
|
|
return ""
|
|
|
|
|
|
|
|
# Handle the general case.
|
2018-10-31 16:08:29 +00:00
|
|
|
s = "".join(lines).strip()
|
2019-05-12 04:23:46 +00:00
|
|
|
s = re.sub(r"\n+ *\d+ *$", "", s) # strip number of next subtitle
|
2022-11-07 18:06:49 +00:00
|
|
|
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):
|
2019-09-20 21:56:33 +00:00
|
|
|
s = re.sub(r"< */? *[a-zA-Z][^>]*>", "", s) # strip other HTML tags
|
|
|
|
s = re.sub(r"\n", r"\\N", s) # convert newlines
|
2018-10-31 16:08:29 +00:00
|
|
|
return s
|
|
|
|
|
|
|
|
subs.events = [SSAEvent(start=start, end=end, text=prepare_text(lines))
|
|
|
|
for (start, end), lines in zip(timestamps, following_lines)]
|
|
|
|
|
|
|
|
@classmethod
|
2022-11-07 18:06:49 +00:00
|
|
|
def to_file(cls, subs, fp, format_, apply_styles=True, keep_ssa_tags=False, **kwargs):
|
2021-07-14 23:13:28 +00:00
|
|
|
"""
|
|
|
|
See :meth:`pysubs2.formats.FormatBase.to_file()`
|
|
|
|
|
|
|
|
Italic, underline and strikeout styling is supported.
|
|
|
|
|
|
|
|
Keyword args:
|
2022-11-07 18:06:49 +00:00
|
|
|
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.)
|
2021-07-14 23:13:28 +00:00
|
|
|
"""
|
2022-11-07 18:06:49 +00:00
|
|
|
def prepare_text(text: str, style: SSAStyle):
|
|
|
|
text = text.replace(r"\h", " ")
|
|
|
|
text = text.replace(r"\n", "\n")
|
|
|
|
text = text.replace(r"\N", "\n")
|
|
|
|
|
2018-10-31 16:08:29 +00:00
|
|
|
body = []
|
2022-11-07 18:06:49 +00:00
|
|
|
if keep_ssa_tags:
|
|
|
|
body.append(text)
|
|
|
|
else:
|
|
|
|
for fragment, sty in parse_tags(text, style, subs.styles):
|
|
|
|
if apply_styles:
|
2024-03-03 17:15:23 +00:00
|
|
|
if sty.italic: fragment = f"<i>{fragment}</i>"
|
|
|
|
if sty.underline: fragment = f"<u>{fragment}</u>"
|
|
|
|
if sty.strikeout: fragment = f"<s>{fragment}</s>"
|
2022-11-07 18:06:49 +00:00
|
|
|
if sty.drawing: raise ContentNotUsable
|
|
|
|
body.append(fragment)
|
2018-10-31 16:08:29 +00:00
|
|
|
|
|
|
|
return re.sub("\n+", "\n", "".join(body).strip())
|
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
visible_lines = cls._get_visible_lines(subs)
|
2018-10-31 16:08:29 +00:00
|
|
|
|
2021-07-14 23:13:28 +00:00
|
|
|
lineno = 1
|
|
|
|
for line in visible_lines:
|
2022-01-24 04:07:52 +00:00
|
|
|
start = cls.ms_to_timestamp(line.start)
|
|
|
|
end = cls.ms_to_timestamp(line.end)
|
2020-03-08 04:49:53 +00:00
|
|
|
try:
|
|
|
|
text = prepare_text(line.text, subs.styles.get(line.style, SSAStyle.DEFAULT_STYLE))
|
|
|
|
except ContentNotUsable:
|
|
|
|
continue
|
2018-10-31 16:08:29 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
print(lineno, file=fp)
|
2018-10-31 16:08:29 +00:00
|
|
|
print(start, "-->", end, file=fp)
|
|
|
|
print(text, end="\n\n", file=fp)
|
2021-07-14 23:13:28 +00:00
|
|
|
lineno += 1
|
2024-03-03 17:15:23 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _get_visible_lines(cls, subs: "pysubs2.SSAFile") -> List["pysubs2.SSAEvent"]:
|
|
|
|
visible_lines = [line for line in subs if not line.is_comment]
|
|
|
|
return visible_lines
|