From adb0cf3f64bb16879a8e1ab75b774ebd0f502ea9 Mon Sep 17 00:00:00 2001 From: stabbedbybrick <125766685+stabbedbybrick@users.noreply.github.com> Date: Tue, 30 Jan 2024 13:40:30 +0100 Subject: [PATCH] Implement fork of Subby subtitle converter (https://github.com/vevv/subby) --- utils/utilities.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/utils/utilities.py b/utils/utilities.py index d8eba27..6ddc602 100644 --- a/utils/utilities.py +++ b/utils/utilities.py @@ -15,6 +15,14 @@ import requests from bs4 import BeautifulSoup from pywidevine.device import Device, DeviceTypes from rich.console import Console +from subby import ( + CommonIssuesFixer, + SMPTEConverter, + WebVTTConverter, + ISMTConverter, + WVTTConverter, + SAMIConverter, +) from unidecode import unidecode console = Console() @@ -283,6 +291,25 @@ def add_subtitles(soup: object, subtitle: str, language: str = None) -> object: return soup +def convert_subtitles(tmp: Path, filename: str, sub_type: str) -> Path: + converters = { + "vtt": WebVTTConverter(), + "ttml": SMPTEConverter(), + "mp4": WVTTConverter(), + "dfxp": ISMTConverter(), + "sami": SAMIConverter(), + } + + converter = converters[sub_type] + fixer = CommonIssuesFixer() + + file = Path(tmp / f"{filename}.{sub_type}") + srt, _ = fixer.from_srt(converter.from_file(file)) + output = Path(tmp / f"{filename}.srt") + srt.save(output) + return output + + def from_mpd(mpd_data: str, url: str = None): root = ET.fromstring(mpd_data) items = []