From f4a9d6c0b105c9767b0e07d0c67f55d27200d96d Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Fri, 17 Mar 2023 19:28:55 +0000 Subject: [PATCH] Replace negative size values in TTML text with 0 Negative size values are not allowed by the spec basically anywhere in the document. Some services seem to accidentally specify a negative value which puts pycaption on a fritz. --- devine/core/tracks/subtitle.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/devine/core/tracks/subtitle.py b/devine/core/tracks/subtitle.py index 6310d3a..7d8d590 100644 --- a/devine/core/tracks/subtitle.py +++ b/devine/core/tracks/subtitle.py @@ -1,5 +1,6 @@ from __future__ import annotations +import re import subprocess from collections import defaultdict from enum import Enum @@ -161,6 +162,8 @@ class Subtitle(Track): return captions if codec == Subtitle.Codec.TimedTextMarkupLang: text = data.decode("utf8").replace("tt:", "") + # negative size values aren't allowed in TTML/DFXP spec, replace with 0 + text = re.sub(r'"(-\d+(\.\d+)?(px|em|%|c|pt))"', '"0"', text) return pycaption.DFXPReader().read(text) if codec == Subtitle.Codec.fVTT: caption_lists: dict[str, pycaption.CaptionList] = defaultdict(pycaption.CaptionList)