diff --git a/pyproject.toml b/pyproject.toml
index 8ac4a0b70..1a1ddc4c3 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -117,7 +117,6 @@ filterwarnings = [
"ignore::aiohttp.web_exceptions.NotAppKeyWarning",
"ignore::DeprecationWarning:pycaption.*",
"ignore::DeprecationWarning:pytest_asyncio.*",
- "ignore::bs4.XMLParsedAsHTMLWarning",
]
addopts = "-ra --strict-markers"
# dumps every thread's stack for a test that hangs; the CI job timeout alone leaves no trace
diff --git a/tests/tracks/test_subtitle_convert.py b/tests/tracks/test_subtitle_convert.py
index 3e5e0df4e..d5373c069 100644
--- a/tests/tracks/test_subtitle_convert.py
+++ b/tests/tracks/test_subtitle_convert.py
@@ -15,9 +15,11 @@ from __future__ import annotations
import pathlib
import re
import struct
+import warnings
import pycaption
import pytest
+from bs4 import XMLParsedAsHTMLWarning
from unshackle.core import binaries
from unshackle.core.tracks import subtitle_convert as sc
@@ -655,3 +657,43 @@ def test_fttml_with_viewport_units_converts(tmp_path, monkeypatch):
out = sub.convert(Codec.SubRip, forced=True)
assert cue_count(out) == 3
assert "Speed 10vw" in out.read_text("utf8")
+
+
+TTML_DECLARATION = ''
+TTML_CUE = ttml_fragment(("00:00:01.000", "00:00:02.000"))
+
+
+@pytest.mark.parametrize(
+ ("data", "codec", "cues"),
+ [
+ pytest.param((TTML_DECLARATION + TTML_CUE).encode(), Codec.TimedTextMarkupLang, 1, id="plain"),
+ pytest.param((TTML_DECLARATION + TTML_CUE).encode("utf-8-sig"), Codec.TimedTextMarkupLang, 1, id="bom"),
+ pytest.param(
+ (TTML_DECLARATION + TTML_CUE).encode("utf-8-sig") + b"\xff", Codec.TimedTextMarkupLang, 1, id="bom-bad-byte"
+ ),
+ pytest.param((TTML_DECLARATION.upper() + TTML_CUE).encode(), Codec.TimedTextMarkupLang, 1, id="uppercase"),
+ pytest.param(f"\r\n {TTML_DECLARATION}{TTML_CUE}".encode(), Codec.TimedTextMarkupLang, 1, id="whitespace"),
+ pytest.param(
+ f'{TTML_DECLARATION}'.encode(),
+ Codec.TimedTextMarkupLang,
+ 0,
+ id="self-closing-root",
+ ),
+ pytest.param(
+ build_stpp_mp4(TTML_DECLARATION + TTML_CUE, f"{TTML_DECLARATION}"), Codec.fTTML, 1, id="fttml"
+ ),
+ ],
+)
+def test_ttml_parse_raises_no_bs4_warning(data, codec, cues):
+ with warnings.catch_warnings():
+ warnings.simplefilter("error", XMLParsedAsHTMLWarning)
+ assert caption_total(Subtitle.parse(data, codec)) == cues
+
+
+def test_ttml_parse_keeps_a_declaration_inside_the_cue_text():
+ cue = ttml_fragment(("00:00:01.000", "00:00:02.000", " there]]>"))
+ expected = pycaption.DFXPReader().read(cue)
+
+ caption_set = Subtitle.parse((TTML_DECLARATION + cue).encode(), Codec.TimedTextMarkupLang)
+
+ assert pycaption.DFXPWriter().write(caption_set) == pycaption.DFXPWriter().write(expected)
diff --git a/unshackle/core/tracks/subtitle.py b/unshackle/core/tracks/subtitle.py
index d513c6e27..978e9254f 100644
--- a/unshackle/core/tracks/subtitle.py
+++ b/unshackle/core/tracks/subtitle.py
@@ -824,6 +824,7 @@ class Subtitle(Track):
@staticmethod
def parse(data: bytes, codec: Subtitle.Codec) -> pycaption.CaptionSet:
+ """Parse a subtitle, without the XML declaration of a TTML file, which makes bs4 warn when pycaption reads it."""
if not isinstance(data, bytes):
raise ValueError(f"Subtitle data must be parsed as bytes data, not {type(data).__name__}")
@@ -843,6 +844,7 @@ class Subtitle(Track):
caption_set = pycaption.CaptionSet(caption_lists)
elif codec == Subtitle.Codec.TimedTextMarkupLang:
text = try_ensure_utf8(data).decode("utf8")
+ text = re.sub(r"(?i)^[^<]*<\?xml\s[^>]*>", "", text)
text = text.replace("tt:", "")
text = re.sub(
r"tts:(?:extent|origin|padding)=(\"[^\"]*\"|'[^']*')",